fix(medusa): Creating Product with Sales Channels disabled (#3357)
This commit is contained in:
1 parent
4e59129a31
commit
c432481319
4 files changed
+87
-4
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix(medusa): fix error on creating product without FF sales channel
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
const path = require("path")
|
||||||
|
const setupServer = require("../../../../helpers/setup-server")
|
||||||
|
const { useApi } = require("../../../../helpers/use-api")
|
||||||
|
const { initDb, useDb } = require("../../../../helpers/use-db")
|
||||||
|
const adminSeeder = require("../../../helpers/admin-seeder")
|
||||||
|
|
||||||
|
const adminHeaders = {
|
||||||
|
headers: {
|
||||||
|
Authorization: "Bearer test_token",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("/admin/products", () => {
|
||||||
|
let medusaProcess
|
||||||
|
let dataSource
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
|
||||||
|
|
||||||
|
dataSource = await initDb({ cwd })
|
||||||
|
medusaProcess = await setupServer({
|
||||||
|
cwd,
|
||||||
|
env: {
|
||||||
|
MEDUSA_FF_SALES_CHANNELS: false,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
const db = useDb()
|
||||||
|
await db.shutdown()
|
||||||
|
|
||||||
|
medusaProcess.kill()
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("POST /admin/products", () => {
|
||||||
|
beforeEach(async () => {
|
||||||
|
await adminSeeder(dataSource)
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
const db = useDb()
|
||||||
|
await db.teardown()
|
||||||
|
})
|
||||||
|
|
||||||
|
it("creates a product successfully", async () => {
|
||||||
|
const api = useApi()
|
||||||
|
|
||||||
|
const payload = {
|
||||||
|
title: "Test",
|
||||||
|
description: "test-product-description",
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await api
|
||||||
|
.post("/admin/products", payload, adminHeaders)
|
||||||
|
|
||||||
|
expect(response.status).toEqual(200)
|
||||||
|
expect(response.data.product).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
id: expect.stringMatching(/^prod_*/),
|
||||||
|
title: "Test",
|
||||||
|
description: "test-product-description",
|
||||||
|
handle: "test",
|
||||||
|
status: "draft",
|
||||||
|
created_at: expect.any(String),
|
||||||
|
updated_at: expect.any(String),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
const Scrypt = require("scrypt-kdf")
|
const Scrypt = require("scrypt-kdf")
|
||||||
const { User } = require("@medusajs/medusa")
|
const { User } = require("@medusajs/medusa")
|
||||||
|
|
||||||
module.exports = async (connection, data = {}) => {
|
module.exports = async (dataSource, data = {}) => {
|
||||||
const manager = connection.manager
|
const manager = dataSource.manager
|
||||||
|
|
||||||
const buf = await Scrypt.kdf("secret_password", { logN: 1, r: 1, p: 1 })
|
const buf = await Scrypt.kdf("secret_password", { logN: 1, r: 1, p: 1 })
|
||||||
const password_hash = buf.toString("base64")
|
const password_hash = buf.toString("base64")
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ import {
|
|||||||
} from "./transaction/create-product-variant"
|
} from "./transaction/create-product-variant"
|
||||||
import { DistributedTransaction } from "../../../../utils/transaction"
|
import { DistributedTransaction } from "../../../../utils/transaction"
|
||||||
import { Logger } from "../../../../types/global"
|
import { Logger } from "../../../../types/global"
|
||||||
|
import { FlagRouter } from "../../../../utils/flag-router"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @oas [post] /products
|
* @oas [post] /products
|
||||||
@@ -117,6 +118,8 @@ export default async (req, res) => {
|
|||||||
const shippingProfileService: ShippingProfileService = req.scope.resolve(
|
const shippingProfileService: ShippingProfileService = req.scope.resolve(
|
||||||
"shippingProfileService"
|
"shippingProfileService"
|
||||||
)
|
)
|
||||||
|
const featureFlagRouter: FlagRouter = req.scope.resolve("featureFlagRouter")
|
||||||
|
|
||||||
const productVariantInventoryService: ProductVariantInventoryService =
|
const productVariantInventoryService: ProductVariantInventoryService =
|
||||||
req.scope.resolve("productVariantInventoryService")
|
req.scope.resolve("productVariantInventoryService")
|
||||||
const inventoryService: IInventoryService | undefined =
|
const inventoryService: IInventoryService | undefined =
|
||||||
@@ -148,8 +151,12 @@ export default async (req, res) => {
|
|||||||
.retrieveDefault()
|
.retrieveDefault()
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no sales channel available, set the default one
|
// Provided that the feature flag is enabled and
|
||||||
if (!validated?.sales_channels?.length) {
|
// no sales channels are available, set the default one
|
||||||
|
if (
|
||||||
|
featureFlagRouter.isFeatureEnabled(SalesChannelFeatureFlag.key) &&
|
||||||
|
!validated?.sales_channels?.length
|
||||||
|
) {
|
||||||
const defaultSalesChannel = await salesChannelService
|
const defaultSalesChannel = await salesChannelService
|
||||||
.withTransaction(manager)
|
.withTransaction(manager)
|
||||||
.retrieveDefault()
|
.retrieveDefault()
|
||||||
|
|||||||
Reference in new issue
Block a user