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:
1 parent
5f348d88f4
commit
48d8b2c623
4 files changed
+31
-13
No files matched your search
@@ -166,7 +166,10 @@ export const useUpdateProductVariantsBatch = (
|
|||||||
return useMutation({
|
return useMutation({
|
||||||
mutationFn: (
|
mutationFn: (
|
||||||
payload: HttpTypes.AdminBatchProductVariantRequest["update"]
|
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) => {
|
onSuccess: (data: any, variables: any, context: any) => {
|
||||||
queryClient.invalidateQueries({ queryKey: variantsQueryKeys.lists() })
|
queryClient.invalidateQueries({ queryKey: variantsQueryKeys.lists() })
|
||||||
queryClient.invalidateQueries({
|
queryClient.invalidateQueries({
|
||||||
|
|||||||
@@ -48,17 +48,22 @@ export const PricingEdit = ({
|
|||||||
|
|
||||||
const handleSubmit = form.handleSubmit(
|
const handleSubmit = form.handleSubmit(
|
||||||
async (values) => {
|
async (values) => {
|
||||||
const reqData = {
|
const reqData = values.variants.map((variant, ind) => ({
|
||||||
update: values.variants.map((variant, ind) => ({
|
|
||||||
id: product.variants[ind].id,
|
id: product.variants[ind].id,
|
||||||
prices: Object.entries(variant.prices || {}).map(
|
prices: Object.entries(variant.prices || {}).map(
|
||||||
([key, value]: any) => ({
|
([currency_code, value]: any) => {
|
||||||
currency_code: key,
|
const id = product.variants[ind].prices.find(
|
||||||
amount: castNumber(value),
|
(p) => p.currency_code === currency_code
|
||||||
})
|
)?.id
|
||||||
),
|
|
||||||
})),
|
const amount = castNumber(value)
|
||||||
|
|
||||||
|
return id
|
||||||
|
? { id, amount, currency_code }
|
||||||
|
: { currency_code, amount }
|
||||||
}
|
}
|
||||||
|
),
|
||||||
|
}))
|
||||||
await mutateAsync(reqData, {
|
await mutateAsync(reqData, {
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
handleSuccess(`/products/${product.id}`)
|
handleSuccess(`/products/${product.id}`)
|
||||||
|
|||||||
@@ -26,15 +26,23 @@ export const batchProductVariantsWorkflow = createWorkflow(
|
|||||||
>
|
>
|
||||||
>
|
>
|
||||||
): WorkflowData<BatchWorkflowOutput<ProductTypes.ProductVariantDTO>> => {
|
): 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(
|
const res = parallelize(
|
||||||
createProductVariantsWorkflow.runAsStep({
|
createProductVariantsWorkflow.runAsStep({
|
||||||
input: { product_variants: input.create ?? [] },
|
input: { product_variants: normalizedInput.create },
|
||||||
}),
|
}),
|
||||||
updateProductVariantsWorkflow.runAsStep({
|
updateProductVariantsWorkflow.runAsStep({
|
||||||
input: { product_variants: input.update ?? [] },
|
input: { product_variants: normalizedInput.update },
|
||||||
}),
|
}),
|
||||||
deleteProductVariantsWorkflow.runAsStep({
|
deleteProductVariantsWorkflow.runAsStep({
|
||||||
input: { ids: input.delete ?? [] },
|
input: { ids: normalizedInput.delete },
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ export const updateProductVariantsWorkflow = createWorkflow(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ("product_variants" in data.input) {
|
if ("product_variants" in data.input) {
|
||||||
return data.variantPriceSetLinks
|
const priceSets = data.variantPriceSetLinks
|
||||||
.map((link) => {
|
.map((link) => {
|
||||||
if (!("product_variants" in data.input)) {
|
if (!("product_variants" in data.input)) {
|
||||||
return
|
return
|
||||||
@@ -94,6 +94,8 @@ export const updateProductVariantsWorkflow = createWorkflow(
|
|||||||
} as PricingTypes.UpsertPriceSetDTO
|
} as PricingTypes.UpsertPriceSetDTO
|
||||||
})
|
})
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
|
|
||||||
|
return { price_sets: priceSets }
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
Reference in new issue
Block a user