Skip to content

Commit 1eeb65d

Browse files
committed
pr comments
1 parent 75662f4 commit 1eeb65d

File tree

5 files changed

+157
-23
lines changed

5 files changed

+157
-23
lines changed

integration-tests/http/__tests__/product/admin/product.spec.ts

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2815,6 +2815,145 @@ medusaIntegrationTestRunner({
28152815
})
28162816
})
28172817

2818+
describe("POST /admin/products/:id/options", () => {
2819+
let colorOption
2820+
let sizeOption
2821+
2822+
beforeEach(async () => {
2823+
colorOption = (
2824+
await api.post(
2825+
"/admin/product-options",
2826+
{ title: "Color", values: ["Red", "Blue"] },
2827+
adminHeaders
2828+
)
2829+
).data.product_option
2830+
2831+
sizeOption = (
2832+
await api.post(
2833+
"/admin/product-options",
2834+
{ title: "Size", values: ["L", "M"] },
2835+
adminHeaders
2836+
)
2837+
).data.product_option
2838+
})
2839+
2840+
it("should link existing options to product", async () => {
2841+
const payload = {
2842+
add: [colorOption.id, sizeOption.id],
2843+
}
2844+
2845+
const response = await api.post(
2846+
`/admin/products/${baseProduct.id}/options`,
2847+
payload,
2848+
adminHeaders
2849+
)
2850+
2851+
expect(response.status).toEqual(200)
2852+
expect(response.data.product.options.length).toEqual(4) // 2 new ones and 2 it already had
2853+
expect(response.data.product.options).toEqual(
2854+
expect.arrayContaining([
2855+
expect.objectContaining({
2856+
id: baseProduct.options[0].id,
2857+
}),
2858+
expect.objectContaining({
2859+
id: baseProduct.options[1].id,
2860+
}),
2861+
expect.objectContaining({
2862+
id: colorOption.id,
2863+
}),
2864+
expect.objectContaining({
2865+
id: sizeOption.id,
2866+
}),
2867+
])
2868+
)
2869+
})
2870+
2871+
it("should unlink existing options from product", async () => {
2872+
let response = await api.post(
2873+
`/admin/products/${baseProduct.id}/options`,
2874+
{
2875+
add: [colorOption.id, sizeOption.id],
2876+
},
2877+
adminHeaders
2878+
)
2879+
2880+
expect(response.status).toEqual(200)
2881+
expect(response.data.product.options.length).toEqual(4) // 2 new ones and 2 it already had
2882+
2883+
const payload = {
2884+
remove: [colorOption.id, sizeOption.id],
2885+
}
2886+
2887+
response = await api.post(
2888+
`/admin/products/${baseProduct.id}/options`,
2889+
payload,
2890+
adminHeaders
2891+
)
2892+
2893+
expect(response.status).toEqual(200)
2894+
expect(response.data.product.options.length).toEqual(2)
2895+
})
2896+
2897+
it("should link and unlink existing options to/from product", async () => {
2898+
const payload = {
2899+
add: [colorOption.id, sizeOption.id],
2900+
remove: [baseProduct.options[0].id, baseProduct.options[1].id],
2901+
}
2902+
2903+
const response = await api.post(
2904+
`/admin/products/${baseProduct.id}/options`,
2905+
payload,
2906+
adminHeaders
2907+
)
2908+
2909+
expect(response.status).toEqual(200)
2910+
expect(response.data.product.options.length).toEqual(2)
2911+
expect(response.data.product.options).toEqual(
2912+
expect.arrayContaining([
2913+
expect.objectContaining({
2914+
id: colorOption.id,
2915+
}),
2916+
expect.objectContaining({
2917+
id: sizeOption.id,
2918+
}),
2919+
])
2920+
)
2921+
})
2922+
2923+
it("should link a new options to product", async () => {
2924+
const payload = {
2925+
add: [
2926+
colorOption.id,
2927+
sizeOption.id,
2928+
{ title: "new", values: ["A", "B"] },
2929+
],
2930+
remove: [baseProduct.options[0].id, baseProduct.options[1].id],
2931+
}
2932+
2933+
const response = await api.post(
2934+
`/admin/products/${baseProduct.id}/options`,
2935+
payload,
2936+
adminHeaders
2937+
)
2938+
2939+
expect(response.status).toEqual(200)
2940+
expect(response.data.product.options.length).toEqual(3)
2941+
expect(response.data.product.options).toEqual(
2942+
expect.arrayContaining([
2943+
expect.objectContaining({
2944+
id: colorOption.id,
2945+
}),
2946+
expect.objectContaining({
2947+
id: sizeOption.id,
2948+
}),
2949+
expect.objectContaining({
2950+
title: "new",
2951+
}),
2952+
])
2953+
)
2954+
})
2955+
})
2956+
28182957
describe("testing for soft-deletion + uniqueness on handles, collection and variant properties", () => {
28192958
it("successfully deletes a product", async () => {
28202959
const response = await api

packages/core/core-flows/src/product/workflows/link-product-options-to-product.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,30 @@ export const linkProductOptionsToProductWorkflowId =
6161
export const linkProductOptionsToProductWorkflow = createWorkflow(
6262
linkProductOptionsToProductWorkflowId,
6363
(input: WorkflowData<LinkProductOptionsToProductWorkflowInput>) => {
64-
const optionsToCreate = transform({ input }, ({ input }) => {
65-
return (input.add ?? []).filter((option) => !isString(option))
66-
}) as ProductTypes.CreateProductOptionDTO[]
64+
const { toCreate, toAdd } = transform({ input }, ({ input }) => {
65+
const toCreate: ProductTypes.CreateProductOptionDTO[] = []
66+
const toAdd: string[] = []
67+
for (const option of input.add ?? []) {
68+
isString(option) ? toAdd.push(option) : toCreate.push(option)
69+
}
70+
71+
return { toCreate, toAdd }
72+
})
6773

6874
const createdIds = when(
6975
"creating-product-options",
70-
{ optionsToCreate },
71-
({ optionsToCreate }) => optionsToCreate.length > 0
76+
{ toCreate },
77+
({ toCreate }) => toCreate.length > 0
7278
).then(() => {
73-
const createdOptions = createProductOptionsStep(optionsToCreate)
74-
75-
return transform({ createdOptions }, ({ createdOptions }) => {
76-
return createdOptions.map((option) => option.id)
77-
})
79+
const createdOptions = createProductOptionsStep(toCreate)
80+
return transform({ createdOptions }, ({ createdOptions }) =>
81+
createdOptions.map((option) => option.id)
82+
)
7883
})
7984

8085
const toAddProductOptionIds = transform(
81-
{ input, createdIds },
82-
({ input, createdIds }) => {
83-
return (input.add ?? [])
84-
.filter((option) => isString(option))
85-
.concat(createdIds ? createdIds : [])
86-
}
86+
{ toAdd, createdIds },
87+
({ toAdd, createdIds }) => toAdd.concat(createdIds ?? [])
8788
)
8889

8990
const productOptions = linkProductOptionsToProductStep({

packages/medusa/src/api/admin/product-categories/middlewares.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ export const adminProductCategoryRoutesMiddlewares: MiddlewareRoute[] = [
5858
{
5959
method: ["DELETE"],
6060
matcher: "/admin/product-categories/:id",
61-
middlewares: [],
6261
},
6362
{
6463
method: ["POST"],

packages/medusa/src/api/admin/product-options/middlewares.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,5 @@ export const adminProductOptionRoutesMiddlewares: MiddlewareRoute[] = [
5757
{
5858
method: ["DELETE"],
5959
matcher: "/admin/product-options/:id",
60-
middlewares: [],
6160
},
6261
]

packages/medusa/src/api/admin/product-tags/middlewares.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import * as QueryConfig from "./query-config"
22
import { MiddlewareRoute } from "@medusajs/framework/http"
3-
import {
4-
validateAndTransformBody,
5-
validateAndTransformQuery,
6-
} from "@medusajs/framework"
3+
import { validateAndTransformBody, validateAndTransformQuery, } from "@medusajs/framework"
74
import {
85
AdminCreateProductTag,
96
AdminGetProductTagParams,
@@ -58,6 +55,5 @@ export const adminProductTagRoutesMiddlewares: MiddlewareRoute[] = [
5855
{
5956
method: ["DELETE"],
6057
matcher: "/admin/product-tags/:id",
61-
middlewares: [],
6258
},
6359
]

0 commit comments

Comments
 (0)