fix(medusa): Default sales channel on product create (#3249)
What: Assign the default sales channel if none is provided while creating a new product. FIXES: CORE-1114 Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
co-authored by
Oliver Windall Juhl
parent
10ff72c30a
commit
80452332d8
@@ -3,6 +3,7 @@ import { request } from "../../../../../helpers/test-request"
|
||||
import { ProductServiceMock } from "../../../../../services/__mocks__/product"
|
||||
import { ProductVariantServiceMock } from "../../../../../services/__mocks__/product-variant"
|
||||
import { ShippingProfileServiceMock } from "../../../../../services/__mocks__/shipping-profile"
|
||||
import { SalesChannelServiceMock } from "../../../../../services/__mocks__/sales-channel"
|
||||
|
||||
describe("POST /admin/products", () => {
|
||||
describe("successful creation with variants", () => {
|
||||
@@ -46,6 +47,7 @@ describe("POST /admin/products", () => {
|
||||
})
|
||||
|
||||
it("returns 200", () => {
|
||||
expect(SalesChannelServiceMock.retrieveDefault).toHaveBeenCalledTimes(1)
|
||||
expect(subject.status).toEqual(200)
|
||||
})
|
||||
|
||||
@@ -114,6 +116,13 @@ describe("POST /admin/products", () => {
|
||||
is_giftcard: false,
|
||||
options: [{ title: "Denominations" }],
|
||||
profile_id: IdMap.getId("default_shipping_profile"),
|
||||
sales_channels: [
|
||||
{
|
||||
description: "sales channel 1 description",
|
||||
is_disabled: false,
|
||||
name: "sales channel 1 name",
|
||||
},
|
||||
],
|
||||
})
|
||||
})
|
||||
|
||||
@@ -173,6 +182,13 @@ describe("POST /admin/products", () => {
|
||||
is_giftcard: true,
|
||||
status: "draft",
|
||||
profile_id: IdMap.getId("giftCardProfile"),
|
||||
sales_channels: [
|
||||
{
|
||||
description: "sales channel 1 description",
|
||||
is_disabled: false,
|
||||
name: "sales channel 1 name",
|
||||
},
|
||||
],
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
ProductService,
|
||||
ProductVariantInventoryService,
|
||||
ProductVariantService,
|
||||
SalesChannelService,
|
||||
ShippingProfileService,
|
||||
} from "../../../../services"
|
||||
import {
|
||||
@@ -121,6 +122,10 @@ export default async (req, res) => {
|
||||
const inventoryService: IInventoryService | undefined =
|
||||
req.scope.resolve("inventoryService")
|
||||
|
||||
const salesChannelService: SalesChannelService = req.scope.resolve(
|
||||
"salesChannelService"
|
||||
)
|
||||
|
||||
const entityManager: EntityManager = req.scope.resolve("manager")
|
||||
|
||||
const newProduct = await entityManager.transaction(async (manager) => {
|
||||
@@ -143,6 +148,14 @@ export default async (req, res) => {
|
||||
.retrieveDefault()
|
||||
}
|
||||
|
||||
// If no sales channel available, set the default one
|
||||
if (!validated?.sales_channels?.length) {
|
||||
const defaultSalesChannel = await salesChannelService
|
||||
.withTransaction(manager)
|
||||
.retrieveDefault()
|
||||
validated.sales_channels = [defaultSalesChannel]
|
||||
}
|
||||
|
||||
const newProduct = await productService
|
||||
.withTransaction(manager)
|
||||
.create({ ...validated, profile_id: shippingProfile.id })
|
||||
|
||||
Reference in New Issue
Block a user