-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core-flows): create or update payment collections in RMA flows
- Loading branch information
Showing
8 changed files
with
176 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
packages/core/core-flows/src/order/workflows/create-or-update-order-payment-collection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { PaymentCollectionDTO } from "@medusajs/types" | ||
import { MedusaError, PaymentCollectionStatus } from "@medusajs/utils" | ||
import { | ||
createWorkflow, | ||
transform, | ||
when, | ||
WorkflowData, | ||
WorkflowResponse, | ||
} from "@medusajs/workflows-sdk" | ||
import { useRemoteQueryStep } from "../../common" | ||
import { updatePaymentCollectionStep } from "../../payment-collection" | ||
import { createOrderPaymentCollectionWorkflow } from "./create-order-payment-collection" | ||
|
||
export const createOrUpdateOrderPaymentCollectionWorkflowId = | ||
"create-or-update-order-payment-collection" | ||
/** | ||
* This workflow creates or updates payment collection for an order. | ||
*/ | ||
export const createOrUpdateOrderPaymentCollectionWorkflow = createWorkflow( | ||
createOrUpdateOrderPaymentCollectionWorkflowId, | ||
( | ||
input: WorkflowData<{ | ||
order_id: string | ||
amount?: number | ||
}> | ||
) => { | ||
const order = useRemoteQueryStep({ | ||
entry_point: "order", | ||
fields: ["id", "summary", "currency_code", "region_id"], | ||
variables: { id: input.order_id }, | ||
throw_if_key_not_found: true, | ||
list: false, | ||
}) | ||
|
||
const orderPaymentCollections = useRemoteQueryStep({ | ||
entry_point: "order_payment_collection", | ||
fields: ["payment_collection_id"], | ||
variables: { order_id: order.id }, | ||
}).config({ name: "order-payment-collection-query" }) | ||
|
||
const orderPaymentCollectionIds = transform( | ||
{ orderPaymentCollections }, | ||
({ orderPaymentCollections }) => | ||
orderPaymentCollections.map((opc) => opc.payment_collection_id) | ||
) | ||
|
||
const existingPaymentCollection = useRemoteQueryStep({ | ||
entry_point: "payment_collection", | ||
fields: ["id", "status"], | ||
variables: { | ||
filters: { | ||
id: orderPaymentCollectionIds, | ||
status: [PaymentCollectionStatus.NOT_PAID], | ||
}, | ||
}, | ||
list: false, | ||
}).config({ name: "payment-collection-query" }) | ||
|
||
const amountPending = transform({ order, input }, ({ order, input }) => { | ||
const pendingPayment = order.summary.pending_difference | ||
|
||
if (input.amount && input.amount > pendingPayment) { | ||
throw new MedusaError( | ||
MedusaError.Types.NOT_ALLOWED, | ||
`Amount cannot be greater than ${pendingPayment}` | ||
) | ||
} | ||
|
||
return pendingPayment | ||
}) | ||
|
||
const updatedPaymentCollections = when( | ||
{ existingPaymentCollection, amountPending }, | ||
({ existingPaymentCollection, amountPending }) => { | ||
return !!existingPaymentCollection?.id && amountPending > 0 | ||
} | ||
).then(() => { | ||
return updatePaymentCollectionStep({ | ||
selector: { id: existingPaymentCollection.id }, | ||
update: { | ||
amount: amountPending, | ||
}, | ||
}) as PaymentCollectionDTO[] | ||
}) | ||
|
||
const createdPaymentCollection = when( | ||
{ existingPaymentCollection, amountPending }, | ||
({ existingPaymentCollection, amountPending }) => { | ||
return !!!existingPaymentCollection?.id && amountPending > 0 | ||
} | ||
).then(() => { | ||
return createOrderPaymentCollectionWorkflow.runAsStep({ | ||
input: { | ||
order_id: order.id, | ||
amount: amountPending, | ||
}, | ||
}) as PaymentCollectionDTO[] | ||
}) | ||
|
||
const paymentCollections = transform( | ||
{ updatedPaymentCollections, createdPaymentCollection }, | ||
({ updatedPaymentCollections, createdPaymentCollection }) => | ||
updatedPaymentCollections || createdPaymentCollection | ||
) | ||
|
||
return new WorkflowResponse(paymentCollections) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.