Skip to content

Commit e4bfa6c

Browse files
authored
fix(medusa): throw proper error when product doesn't exist w/ key header (medusajs#5745)
**What** - ensure that an error is raised properly if the requested product doesn't exist and a publishable-key header is set **Why** - previously endpoints would hang if the product didn't exist and requests would time out closes medusajs#5724
1 parent 870d686 commit e4bfa6c

File tree

4 files changed

+92
-24
lines changed

4 files changed

+92
-24
lines changed

.changeset/tough-wombats-tan.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/medusa": patch
3+
---
4+
5+
fix(medusa): raise error properly in api-key middleware if product is not found

integration-tests/api/__tests__/admin/publishable-api-key.js

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -385,13 +385,11 @@ describe("Publishable API keys", () => {
385385
})
386386

387387
await dbConnection.manager.query(
388-
`INSERT INTO
389-
publishable_api_key_sales_channel
390-
(publishable_key_id, sales_channel_id)
391-
VALUES
392-
('${pubKeyId}', '${salesChannel1.id}'),
393-
('${pubKeyId}', '${salesChannel2.id}'),
394-
('${pubKeyId}', '${salesChannel3.id}');`
388+
`INSERT INTO publishable_api_key_sales_channel
389+
(publishable_key_id, sales_channel_id)
390+
VALUES ('${pubKeyId}', '${salesChannel1.id}'),
391+
('${pubKeyId}', '${salesChannel2.id}'),
392+
('${pubKeyId}', '${salesChannel3.id}');`
395393
)
396394
})
397395

@@ -468,12 +466,10 @@ describe("Publishable API keys", () => {
468466
})
469467

470468
await dbConnection.manager.query(
471-
`INSERT INTO
472-
publishable_api_key_sales_channel
469+
`INSERT INTO publishable_api_key_sales_channel
473470
(publishable_key_id, sales_channel_id)
474-
VALUES
475-
('${pubKeyId}', '${salesChannel1.id}'),
476-
('${pubKeyId}', '${salesChannel2.id}');`
471+
VALUES ('${pubKeyId}', '${salesChannel1.id}'),
472+
('${pubKeyId}', '${salesChannel2.id}');`
477473
)
478474
})
479475

@@ -856,6 +852,62 @@ describe("Publishable API keys", () => {
856852
expect(response.status).toEqual(400)
857853
})
858854

855+
it("should return 404 when the requested variant doesn't exist", async () => {
856+
const api = useApi()
857+
858+
await api.post(
859+
`/admin/publishable-api-keys/${pubKeyId}/sales-channels/batch`,
860+
{
861+
sales_channel_ids: [{ id: salesChannel1.id }],
862+
},
863+
adminHeaders
864+
)
865+
866+
const response = await api
867+
.get(`/store/variants/does-not-exist`, {
868+
headers: {
869+
"x-medusa-access-token": "test_token",
870+
"x-publishable-api-key": pubKeyId,
871+
},
872+
})
873+
.catch((err) => {
874+
return err.response
875+
})
876+
877+
expect(response.status).toEqual(404)
878+
expect(response.data.message).toEqual(
879+
"Variant with id: does-not-exist was not found"
880+
)
881+
})
882+
883+
it("should return 404 when the requested product doesn't exist", async () => {
884+
const api = useApi()
885+
886+
await api.post(
887+
`/admin/publishable-api-keys/${pubKeyId}/sales-channels/batch`,
888+
{
889+
sales_channel_ids: [{ id: salesChannel1.id }],
890+
},
891+
adminHeaders
892+
)
893+
894+
const response = await api
895+
.get(`/store/products/does-not-exist`, {
896+
headers: {
897+
"x-medusa-access-token": "test_token",
898+
"x-publishable-api-key": pubKeyId,
899+
},
900+
})
901+
.catch((err) => {
902+
return err.response
903+
})
904+
905+
expect(response.status).toEqual(404)
906+
expect(response.data.message).toEqual(
907+
"Product with id: does-not-exist was not found"
908+
)
909+
})
910+
859911
it("correctly returns a product if passed PK has no associated SCs", async () => {
860912
const api = useApi()
861913

packages/medusa/src/api/middlewares/publishable-api-key/validate-product-sales-channel-association.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,18 @@ async function validateProductSalesChannelAssociation(
2626
const { sales_channel_ids: salesChannelIds } =
2727
await publishableKeyService.getResourceScopes(pubKey)
2828

29-
if (
30-
salesChannelIds.length &&
31-
!(await productService.isProductInSalesChannels(
29+
let isProductInSalesChannel = false
30+
31+
try {
32+
isProductInSalesChannel = await productService.isProductInSalesChannels(
3233
req.params.id,
3334
salesChannelIds
34-
))
35-
) {
35+
)
36+
} catch (error) {
37+
next(error)
38+
}
39+
40+
if (salesChannelIds.length && !isProductInSalesChannel) {
3641
req.errors = req.errors ?? []
3742
req.errors.push(
3843
`Product with id: ${req.params.id} is not associated with sales channels defined by the Publishable API Key passed in the header of the request.`

packages/medusa/src/api/middlewares/publishable-api-key/validate-variant-sales-channel-association.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,19 @@ async function validateProductVariantSalesChannelAssociation(
2828
const { sales_channel_ids: salesChannelIds } =
2929
await publishableKeyService.getResourceScopes(pubKey)
3030

31-
if (
32-
salesChannelIds.length &&
33-
!(await productVariantService.isVariantInSalesChannels(
34-
req.params.id,
35-
salesChannelIds
36-
))
37-
) {
31+
let isVariantInSalesChannel = false
32+
33+
try {
34+
isVariantInSalesChannel =
35+
await productVariantService.isVariantInSalesChannels(
36+
req.params.id,
37+
salesChannelIds
38+
)
39+
} catch (error) {
40+
next(error)
41+
}
42+
43+
if (salesChannelIds.length && !isVariantInSalesChannel) {
3844
req.errors = req.errors ?? []
3945
req.errors.push(
4046
`Variant with id: ${req.params.id} is not associated with sales channels defined by the Publishable API Key passed in the header of the request.`

0 commit comments

Comments
 (0)