feat(core-flows,types,cart): add credit lines to cart (#11419)
* feat(core-flows,types,cart): add credit lines to cart * chore: fix specs * chore: credit lines hook * chore: update types * chore: added credit line totals * chore: add totals fields to query config * chore: add complete cart hook * chore: add credit lines creation to order * chore: pr ready for review * chore: fix tests * Apply suggestions from code review * chore: fix types * chore: adjust summary calculations with new totals
This commit is contained in:
@@ -95,6 +95,7 @@ export const completeCartFields = [
|
||||
"shipping_address.*",
|
||||
"billing_address.*",
|
||||
"region.*",
|
||||
"credit_lines.*",
|
||||
"payment_collection.*",
|
||||
"payment_collection.payment_sessions.*",
|
||||
"items.variant.id",
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import {
|
||||
CartCreditLineDTO,
|
||||
CartWorkflowDTO,
|
||||
UsageComputedActions,
|
||||
} from "@medusajs/framework/types"
|
||||
@@ -210,6 +211,18 @@ export const completeCartWorkflow = createWorkflow(
|
||||
.map((adjustment) => adjustment.code)
|
||||
.filter(Boolean)
|
||||
|
||||
const creditLines = (cart.credit_lines ?? []).map(
|
||||
(creditLine: CartCreditLineDTO) => {
|
||||
return {
|
||||
amount: creditLine.amount,
|
||||
raw_amount: creditLine.raw_amount,
|
||||
reference: creditLine.reference,
|
||||
reference_id: creditLine.reference_id,
|
||||
metadata: creditLine.metadata,
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
return {
|
||||
region_id: cart.region?.id,
|
||||
customer_id: cart.customer?.id,
|
||||
@@ -222,6 +235,7 @@ export const completeCartWorkflow = createWorkflow(
|
||||
no_notification: false,
|
||||
items: allItems,
|
||||
shipping_methods: shippingMethods,
|
||||
credit_lines: creditLines,
|
||||
metadata: cart.metadata,
|
||||
promo_codes: promoCodes,
|
||||
transactions,
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import {
|
||||
CartCreditLineDTO,
|
||||
CreateCartCreditLineDTO,
|
||||
} from "@medusajs/framework/types"
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
WorkflowResponse,
|
||||
createWorkflow,
|
||||
} from "@medusajs/framework/workflows-sdk"
|
||||
import { createEntitiesStep } from "../../common/steps/create-entities"
|
||||
|
||||
export const createCartCreditLinesWorkflowId = "create-cart-credit-lines"
|
||||
export const createCartCreditLinesWorkflow = createWorkflow(
|
||||
createCartCreditLinesWorkflowId,
|
||||
(
|
||||
input: WorkflowData<CreateCartCreditLineDTO[]>
|
||||
): WorkflowResponse<CartCreditLineDTO[]> => {
|
||||
const creditLines = createEntitiesStep({
|
||||
moduleRegistrationName: Modules.CART,
|
||||
invokeMethod: "createCreditLines",
|
||||
compensateMethod: "deleteCreditLines",
|
||||
data: input,
|
||||
})
|
||||
|
||||
return new WorkflowResponse(creditLines)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,22 @@
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
WorkflowResponse,
|
||||
createWorkflow,
|
||||
} from "@medusajs/framework/workflows-sdk"
|
||||
import { deleteEntitiesStep } from "../../common/steps/delete-entities"
|
||||
|
||||
export const deleteCartCreditLinesWorkflowId = "delete-cart-credit-lines"
|
||||
export const deleteCartCreditLinesWorkflow = createWorkflow(
|
||||
deleteCartCreditLinesWorkflowId,
|
||||
(input: WorkflowData<{ id: string[] }>) => {
|
||||
deleteEntitiesStep({
|
||||
moduleRegistrationName: Modules.CART,
|
||||
invokeMethod: "softDeleteCreditLines",
|
||||
compensateMethod: "restoreCreditLines",
|
||||
data: input.id,
|
||||
})
|
||||
|
||||
return new WorkflowResponse(void 0)
|
||||
}
|
||||
)
|
||||
@@ -2,13 +2,15 @@ export * from "./add-shipping-method-to-cart"
|
||||
export * from "./add-to-cart"
|
||||
export * from "./complete-cart"
|
||||
export * from "./confirm-variant-inventory"
|
||||
export * from "./create-cart-credit-lines"
|
||||
export * from "./create-carts"
|
||||
export * from "./create-payment-collection-for-cart"
|
||||
export * from "./delete-cart-credit-lines"
|
||||
export * from "./list-shipping-options-for-cart"
|
||||
export * from "./list-shipping-options-for-cart-with-pricing"
|
||||
export * from "./refresh-payment-collection"
|
||||
export * from "./refresh-cart-items"
|
||||
export * from "./refresh-cart-shipping-methods"
|
||||
export * from "./refresh-payment-collection"
|
||||
export * from "./transfer-cart-customer"
|
||||
export * from "./update-cart"
|
||||
export * from "./update-cart-promotions"
|
||||
|
||||
Reference in New Issue
Block a user