-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add support for setting sales channel when creating a product #6986
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -35,7 +35,7 @@ export const removeRemoteLinkStep = createStep( | |||||
) | ||||||
await link.delete(grouped) | ||||||
|
||||||
return new StepResponse(void 0, grouped) | ||||||
return new StepResponse(grouped, grouped) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Suggested change
|
||||||
}, | ||||||
async (removedLinks, { container }) => { | ||||||
if (!removedLinks) { | ||||||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,13 @@ import { | |
} from "@medusajs/workflows-sdk" | ||
import { createProductsStep, createVariantPricingLinkStep } from "../steps" | ||
import { createPriceSetsStep } from "../../pricing" | ||
import { associateProductsWithSalesChannelsStep } from "../../sales-channel" | ||
|
||
// TODO: We should have separate types here as input, not the module DTO. Eg. the HTTP request that we are handling | ||
// has different data than the DTO, so that needs to be represented differently. | ||
type WorkflowInput = { | ||
products: (Omit<ProductTypes.CreateProductDTO, "variants"> & { | ||
sales_channels?: { id: string }[] | ||
variants?: (ProductTypes.CreateProductVariantDTO & { | ||
prices?: PricingTypes.CreateMoneyAmountDTO[] | ||
})[] | ||
|
@@ -24,17 +26,34 @@ export const createProductsWorkflow = createWorkflow( | |
input: WorkflowData<WorkflowInput> | ||
): WorkflowData<ProductTypes.ProductDTO[]> => { | ||
// Passing prices to the product module will fail, we want to keep them for after the product is created. | ||
const productWithoutPrices = transform({ input }, (data) => | ||
const productWithoutExternalRelations = transform({ input }, (data) => | ||
data.input.products.map((p) => ({ | ||
...p, | ||
sales_channels: undefined, | ||
variants: p.variants?.map((v) => ({ | ||
...v, | ||
prices: undefined, | ||
})), | ||
})) | ||
) | ||
|
||
const createdProducts = createProductsStep(productWithoutPrices) | ||
const createdProducts = createProductsStep(productWithoutExternalRelations) | ||
|
||
const salesChannelLinks = transform({ input, createdProducts }, (data) => { | ||
return data.createdProducts | ||
.map((createdProduct, i) => { | ||
const inputProduct = data.input.products[i] | ||
return ( | ||
inputProduct.sales_channels?.map((salesChannel) => ({ | ||
sales_channel_id: salesChannel.id, | ||
product_id: createdProduct.id, | ||
})) ?? [] | ||
) | ||
}) | ||
.flat() | ||
}) | ||
|
||
associateProductsWithSalesChannelsStep({ links: salesChannelLinks }) | ||
|
||
// Note: We rely on the same order of input and output when creating products here, ensure this always holds true | ||
const variantsWithAssociatedPrices = transform( | ||
|
@@ -83,23 +102,6 @@ export const createProductsWorkflow = createWorkflow( | |
|
||
createVariantPricingLinkStep(variantAndPriceSetLinks) | ||
|
||
// TODO: Should we just refetch the products here? | ||
return transform( | ||
{ | ||
createdProducts, | ||
variantAndPriceSets, | ||
}, | ||
(data) => { | ||
return data.createdProducts.map((product) => ({ | ||
...product, | ||
variants: product.variants?.map((variant) => ({ | ||
...variant, | ||
price_set: data.variantAndPriceSets.find( | ||
(v) => v.variant.id === variant.id | ||
)?.price_set, | ||
})), | ||
})) | ||
} | ||
) | ||
return createdProducts | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we refetch after a workflow, I think it's better to not bother and reconstruct the data for now |
||
} | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk" | ||
import { | ||
deleteProductVariantsStep, | ||
removeVariantPricingLinkStep, | ||
} from "../steps" | ||
import { Modules } from "@medusajs/modules-sdk" | ||
import { deleteProductVariantsStep } from "../steps" | ||
import { removeRemoteLinkStep } from "../../common" | ||
|
||
type WorkflowInput = { ids: string[] } | ||
|
||
export const deleteProductVariantsWorkflowId = "delete-product-variants" | ||
export const deleteProductVariantsWorkflow = createWorkflow( | ||
deleteProductVariantsWorkflowId, | ||
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => { | ||
removeVariantPricingLinkStep({ variant_ids: input.ids }) | ||
removeRemoteLinkStep({ | ||
[Modules.PRODUCT]: { variant_id: input.ids }, | ||
}).config({ name: "remove-variant-link-step" }) | ||
|
||
return deleteProductVariantsStep(input.ids) | ||
} | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a typing issue where if you return
void 0
you cannot do.config
on the step to change the name