fix(core-flows): variant pricing batch update (#7677)
* fix: variant pricing batch update * fix: batch endpoint throwing when `create` array isn't passed * fix: batch variants workflow
This commit is contained in:
@@ -166,7 +166,10 @@ export const useUpdateProductVariantsBatch = (
|
||||
return useMutation({
|
||||
mutationFn: (
|
||||
payload: HttpTypes.AdminBatchProductVariantRequest["update"]
|
||||
) => sdk.admin.product.batchVariants(productId, { update: payload }),
|
||||
) =>
|
||||
sdk.admin.product.batchVariants(productId, {
|
||||
update: payload,
|
||||
}),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({ queryKey: variantsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({
|
||||
|
||||
@@ -48,17 +48,22 @@ export const PricingEdit = ({
|
||||
|
||||
const handleSubmit = form.handleSubmit(
|
||||
async (values) => {
|
||||
const reqData = {
|
||||
update: values.variants.map((variant, ind) => ({
|
||||
id: product.variants[ind].id,
|
||||
prices: Object.entries(variant.prices || {}).map(
|
||||
([key, value]: any) => ({
|
||||
currency_code: key,
|
||||
amount: castNumber(value),
|
||||
})
|
||||
),
|
||||
})),
|
||||
}
|
||||
const reqData = values.variants.map((variant, ind) => ({
|
||||
id: product.variants[ind].id,
|
||||
prices: Object.entries(variant.prices || {}).map(
|
||||
([currency_code, value]: any) => {
|
||||
const id = product.variants[ind].prices.find(
|
||||
(p) => p.currency_code === currency_code
|
||||
)?.id
|
||||
|
||||
const amount = castNumber(value)
|
||||
|
||||
return id
|
||||
? { id, amount, currency_code }
|
||||
: { currency_code, amount }
|
||||
}
|
||||
),
|
||||
}))
|
||||
await mutateAsync(reqData, {
|
||||
onSuccess: () => {
|
||||
handleSuccess(`/products/${product.id}`)
|
||||
|
||||
@@ -26,15 +26,23 @@ export const batchProductVariantsWorkflow = createWorkflow(
|
||||
>
|
||||
>
|
||||
): WorkflowData<BatchWorkflowOutput<ProductTypes.ProductVariantDTO>> => {
|
||||
const normalizedInput = transform({ input }, (data) => {
|
||||
return {
|
||||
create: data.input.create ?? [],
|
||||
update: data.input.update ?? [],
|
||||
delete: data.input.delete ?? [],
|
||||
}
|
||||
})
|
||||
|
||||
const res = parallelize(
|
||||
createProductVariantsWorkflow.runAsStep({
|
||||
input: { product_variants: input.create ?? [] },
|
||||
input: { product_variants: normalizedInput.create },
|
||||
}),
|
||||
updateProductVariantsWorkflow.runAsStep({
|
||||
input: { product_variants: input.update ?? [] },
|
||||
input: { product_variants: normalizedInput.update },
|
||||
}),
|
||||
deleteProductVariantsWorkflow.runAsStep({
|
||||
input: { ids: input.delete ?? [] },
|
||||
input: { ids: normalizedInput.delete },
|
||||
})
|
||||
)
|
||||
|
||||
|
||||
@@ -78,7 +78,7 @@ export const updateProductVariantsWorkflow = createWorkflow(
|
||||
}
|
||||
|
||||
if ("product_variants" in data.input) {
|
||||
return data.variantPriceSetLinks
|
||||
const priceSets = data.variantPriceSetLinks
|
||||
.map((link) => {
|
||||
if (!("product_variants" in data.input)) {
|
||||
return
|
||||
@@ -94,6 +94,8 @@ export const updateProductVariantsWorkflow = createWorkflow(
|
||||
} as PricingTypes.UpsertPriceSetDTO
|
||||
})
|
||||
.filter(Boolean)
|
||||
|
||||
return { price_sets: priceSets }
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user