fix(core-flows,utils,medusa): fix bug where payment collection across orders were getting updated (#8401)
This took embarrassingly long to debug. :| what: - fixes a bug where the payment collection of other orders were getting updated - adds order status to payments section <img width="1069" alt="Screenshot 2024-08-02 at 08 37 38" src="https://github.com/user-attachments/assets/31776bd3-e6b9-4d23-8be6-f972f7316cf3"> <img width="1072" alt="Screenshot 2024-08-02 at 08 37 48" src="https://github.com/user-attachments/assets/38cdd8a1-9f31-4920-91bf-a3554e298960">
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { isPresent } from "@medusajs/utils"
|
||||
import {
|
||||
StepResponse,
|
||||
WorkflowData,
|
||||
@@ -39,7 +40,7 @@ export const refreshPaymentCollectionForCartWorkflowId =
|
||||
export const refreshPaymentCollectionForCartWorkflow = createWorkflow(
|
||||
refreshPaymentCollectionForCartWorkflowId,
|
||||
(input: WorkflowData<WorklowInput>): WorkflowData<void> => {
|
||||
const carts = useRemoteQueryStep({
|
||||
const cart = useRemoteQueryStep({
|
||||
entry_point: "cart",
|
||||
fields: [
|
||||
"id",
|
||||
@@ -50,30 +51,40 @@ export const refreshPaymentCollectionForCartWorkflow = createWorkflow(
|
||||
],
|
||||
variables: { id: input.cart_id },
|
||||
throw_if_key_not_found: true,
|
||||
list: false,
|
||||
})
|
||||
|
||||
const cart = transform({ carts }, (data) => data.carts[0])
|
||||
const deletePaymentSessionInput = transform(
|
||||
{ paymentCollection: cart.payment_collection },
|
||||
(data) => {
|
||||
return {
|
||||
ids:
|
||||
data.paymentCollection?.payment_sessions?.map((ps) => ps.id) || [],
|
||||
data.paymentCollection?.payment_sessions
|
||||
?.map((ps) => ps.id)
|
||||
?.flat(1) || [],
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
const updatePaymentCollectionInput = transform({ cart }, (data) => {
|
||||
if (!isPresent(data.cart?.payment_collection?.id)) {
|
||||
return
|
||||
}
|
||||
|
||||
return {
|
||||
selector: { id: data.cart.payment_collection.id },
|
||||
update: {
|
||||
amount: data.cart.total,
|
||||
currency_code: data.cart.currency_code,
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
parallelize(
|
||||
deletePaymentSessionsWorkflow.runAsStep({
|
||||
input: deletePaymentSessionInput,
|
||||
}),
|
||||
updatePaymentCollectionStep({
|
||||
selector: { id: cart.payment_collection.id },
|
||||
update: {
|
||||
amount: cart.total,
|
||||
currency_code: cart.currency_code,
|
||||
},
|
||||
})
|
||||
updatePaymentCollectionStep(updatePaymentCollectionInput)
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
import {
|
||||
ModuleRegistrationName,
|
||||
getSelectsAndRelationsFromObjectArray,
|
||||
isPresent,
|
||||
} from "@medusajs/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
@@ -18,6 +19,10 @@ export const updatePaymentCollectionStepId = "update-payment-collection"
|
||||
export const updatePaymentCollectionStep = createStep(
|
||||
updatePaymentCollectionStepId,
|
||||
async (data: StepInput, { container }) => {
|
||||
if (!isPresent(data) || !isPresent(data.selector)) {
|
||||
return new StepResponse([], [])
|
||||
}
|
||||
|
||||
const paymentModuleService = container.resolve<IPaymentModuleService>(
|
||||
ModuleRegistrationName.PAYMENT
|
||||
)
|
||||
@@ -42,7 +47,7 @@ export const updatePaymentCollectionStep = createStep(
|
||||
return new StepResponse(updated, prevData)
|
||||
},
|
||||
async (prevData, { container }) => {
|
||||
if (!prevData) {
|
||||
if (!prevData?.length) {
|
||||
return
|
||||
}
|
||||
const paymentModuleService = container.resolve<IPaymentModuleService>(
|
||||
|
||||
Reference in New Issue
Block a user