fix(types,order,medusa): Create credit lines + hooks (#11569)
what: - api/workflows to create credit lines - hooks to enable extending credit lines
This commit is contained in:
@@ -136,6 +136,10 @@ export const completeCartWorkflow = createWorkflow(
|
||||
|
||||
const paymentSessions = validateCartPaymentsStep({ cart })
|
||||
|
||||
createHook("beforePaymentAuthorization", {
|
||||
input,
|
||||
})
|
||||
|
||||
const payment = authorizePaymentSessionStep({
|
||||
// We choose the first payment session, as there will only be one active payment session
|
||||
// This might change in the future.
|
||||
@@ -200,17 +204,6 @@ export const completeCartWorkflow = createWorkflow(
|
||||
}
|
||||
})
|
||||
|
||||
const itemAdjustments = allItems
|
||||
.map((item) => item.adjustments ?? [])
|
||||
.flat(1)
|
||||
const shippingAdjustments = shippingMethods
|
||||
.map((sm) => sm.adjustments ?? [])
|
||||
.flat(1)
|
||||
|
||||
const promoCodes = [...itemAdjustments, ...shippingAdjustments]
|
||||
.map((adjustment) => adjustment.code)
|
||||
.filter(Boolean)
|
||||
|
||||
const creditLines = (cart.credit_lines ?? []).map(
|
||||
(creditLine: CartCreditLineDTO) => {
|
||||
return {
|
||||
@@ -223,6 +216,17 @@ export const completeCartWorkflow = createWorkflow(
|
||||
}
|
||||
)
|
||||
|
||||
const itemAdjustments = allItems
|
||||
.map((item) => item.adjustments ?? [])
|
||||
.flat(1)
|
||||
const shippingAdjustments = shippingMethods
|
||||
.map((sm) => sm.adjustments ?? [])
|
||||
.flat(1)
|
||||
|
||||
const promoCodes = [...itemAdjustments, ...shippingAdjustments]
|
||||
.map((adjustment) => adjustment.code)
|
||||
.filter(Boolean)
|
||||
|
||||
return {
|
||||
region_id: cart.region?.id,
|
||||
customer_id: cart.customer?.id,
|
||||
@@ -235,10 +239,10 @@ export const completeCartWorkflow = createWorkflow(
|
||||
no_notification: false,
|
||||
items: allItems,
|
||||
shipping_methods: shippingMethods,
|
||||
credit_lines: creditLines,
|
||||
metadata: cart.metadata,
|
||||
promo_codes: promoCodes,
|
||||
transactions,
|
||||
credit_lines: creditLines,
|
||||
}
|
||||
})
|
||||
|
||||
@@ -330,6 +334,11 @@ export const completeCartWorkflow = createWorkflow(
|
||||
|
||||
registerUsageStep(promotionUsage)
|
||||
|
||||
createHook("orderCreated", {
|
||||
order_id: createdOrder.id,
|
||||
cart_id: cart.id,
|
||||
})
|
||||
|
||||
return createdOrder
|
||||
})
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
PromotionActions,
|
||||
} from "@medusajs/framework/utils"
|
||||
import {
|
||||
createHook,
|
||||
createWorkflow,
|
||||
transform,
|
||||
when,
|
||||
@@ -228,6 +229,8 @@ export const refreshCartItemsWorkflow = createWorkflow(
|
||||
},
|
||||
})
|
||||
|
||||
createHook("beforeRefreshingPaymentCollection", { input })
|
||||
|
||||
refreshPaymentCollectionForCartWorkflow.runAsStep({
|
||||
input: { cart_id: input.cart_id },
|
||||
})
|
||||
|
||||
@@ -29,7 +29,7 @@ export const confirmOrderChanges = createStep(
|
||||
const orderModuleService = container.resolve(Modules.ORDER)
|
||||
|
||||
const currentChanges: Partial<OrderChangeDTO>[] = []
|
||||
await orderModuleService.confirmOrderChange(
|
||||
const orderChanges = await orderModuleService.confirmOrderChange(
|
||||
input.changes.map((action) => {
|
||||
const update = {
|
||||
id: action.id,
|
||||
@@ -46,7 +46,7 @@ export const confirmOrderChanges = createStep(
|
||||
})
|
||||
)
|
||||
|
||||
return new StepResponse(null, currentChanges)
|
||||
return new StepResponse(orderChanges, currentChanges)
|
||||
},
|
||||
async (currentChanges, { container }) => {
|
||||
if (!currentChanges?.length) {
|
||||
|
||||
@@ -0,0 +1,170 @@
|
||||
import { CreateOrderCreditLineDTO, OrderDTO } from "@medusajs/framework/types"
|
||||
import {
|
||||
ChangeActionType,
|
||||
MathBN,
|
||||
MedusaError,
|
||||
OrderChangeStatus,
|
||||
OrderChangeType,
|
||||
} from "@medusajs/framework/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
WorkflowResponse,
|
||||
createHook,
|
||||
createStep,
|
||||
createWorkflow,
|
||||
transform,
|
||||
} from "@medusajs/framework/workflows-sdk"
|
||||
import { useQueryGraphStep } from "../../common"
|
||||
import { confirmOrderChanges } from "../steps/confirm-order-changes"
|
||||
import { createOrderChangeStep } from "../steps/create-order-change"
|
||||
import { createOrderChangeActionsWorkflow } from "./create-order-change-actions"
|
||||
|
||||
export const validateOrderCreditLinesStep = createStep(
|
||||
"validate-order-credit-lines",
|
||||
async function ({
|
||||
order,
|
||||
creditLines,
|
||||
}: {
|
||||
order: OrderDTO
|
||||
creditLines: Omit<CreateOrderCreditLineDTO, "order_id">[]
|
||||
}) {
|
||||
const pendingDifference = MathBN.convert(order.summary?.pending_difference!)
|
||||
const creditLinesAmount = creditLines.reduce((acc, creditLine) => {
|
||||
return MathBN.add(acc, MathBN.convert(creditLine.amount))
|
||||
}, MathBN.convert(0))
|
||||
|
||||
if (MathBN.eq(pendingDifference, 0)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`Can only create credit lines if the order has a positive or negative pending difference`
|
||||
)
|
||||
}
|
||||
|
||||
if (MathBN.gt(pendingDifference, 0) && MathBN.lt(creditLinesAmount, 0)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`Can only create positive credit lines if the order has a positive pending difference`
|
||||
)
|
||||
}
|
||||
|
||||
if (MathBN.lt(pendingDifference, 0) && MathBN.gt(creditLinesAmount, 0)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`Can only create negative credit lines if the order has a negative pending difference`
|
||||
)
|
||||
}
|
||||
|
||||
if (MathBN.lt(pendingDifference, 0)) {
|
||||
if (
|
||||
MathBN.gt(
|
||||
creditLinesAmount.multipliedBy(-1),
|
||||
pendingDifference.multipliedBy(-1)
|
||||
)
|
||||
) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`Cannot create more negative credit lines with amount more than the pending difference`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (MathBN.gt(pendingDifference, 0)) {
|
||||
if (MathBN.gt(creditLinesAmount, pendingDifference)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`Cannot create more positive credit lines with amount more than the pending difference`
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
export const createOrderCreditLinesWorkflowId = "create-order-credit-lines"
|
||||
export const createOrderCreditLinesWorkflow = createWorkflow(
|
||||
createOrderCreditLinesWorkflowId,
|
||||
(
|
||||
input: WorkflowData<{
|
||||
id: string
|
||||
credit_lines: Omit<CreateOrderCreditLineDTO, "order_id">[]
|
||||
}>
|
||||
) => {
|
||||
const orderQuery = useQueryGraphStep({
|
||||
entity: "orders",
|
||||
fields: ["id", "status", "summary"],
|
||||
filters: { id: input.id },
|
||||
options: { throwIfKeyNotFound: true },
|
||||
}).config({ name: "get-order" })
|
||||
|
||||
const order = transform(
|
||||
{ orderQuery },
|
||||
({ orderQuery }) => orderQuery.data[0]
|
||||
)
|
||||
|
||||
validateOrderCreditLinesStep({ order, creditLines: input.credit_lines })
|
||||
|
||||
const orderChangeInput = transform({ input }, ({ input }) => ({
|
||||
change_type: OrderChangeType.CREDIT_LINE,
|
||||
order_id: input.id,
|
||||
}))
|
||||
|
||||
const createdOrderChange = createOrderChangeStep(orderChangeInput)
|
||||
|
||||
const orderChangeActionInput = transform(
|
||||
{ order, orderChange: createdOrderChange, input },
|
||||
({ order, orderChange, input }) => {
|
||||
return input.credit_lines.map((creditLine) => {
|
||||
return {
|
||||
order_change_id: orderChange.id,
|
||||
order_id: order.id,
|
||||
version: orderChange.version,
|
||||
action: ChangeActionType.CREDIT_LINE_ADD,
|
||||
reference: creditLine.reference!,
|
||||
reference_id: creditLine.reference_id!,
|
||||
amount: creditLine.amount,
|
||||
}
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
createOrderChangeActionsWorkflow.runAsStep({
|
||||
input: orderChangeActionInput,
|
||||
})
|
||||
|
||||
const orderChangeQuery = useQueryGraphStep({
|
||||
entity: "order_change",
|
||||
fields: [
|
||||
"id",
|
||||
"status",
|
||||
"change_type",
|
||||
"actions.id",
|
||||
"actions.order_id",
|
||||
"actions.action",
|
||||
"actions.details",
|
||||
"actions.reference",
|
||||
"actions.reference_id",
|
||||
"actions.internal_note",
|
||||
],
|
||||
filters: {
|
||||
order_id: input.id,
|
||||
status: [OrderChangeStatus.PENDING],
|
||||
},
|
||||
}).config({ name: "order-change-query" })
|
||||
|
||||
const orderChange = transform(
|
||||
{ orderChangeQuery },
|
||||
({ orderChangeQuery }) => orderChangeQuery.data[0]
|
||||
)
|
||||
|
||||
const orderChanges = confirmOrderChanges({
|
||||
changes: [orderChange],
|
||||
orderId: order.id,
|
||||
})
|
||||
|
||||
createHook("creditLinesCreated", {
|
||||
order_id: input.id,
|
||||
credit_lines: orderChanges.credit_lines,
|
||||
})
|
||||
|
||||
return new WorkflowResponse(orderChanges.credit_lines)
|
||||
}
|
||||
)
|
||||
@@ -19,10 +19,12 @@ export * from "./claim/update-claim-item"
|
||||
export * from "./claim/update-claim-shipping-method"
|
||||
export * from "./complete-orders"
|
||||
export * from "./create-fulfillment"
|
||||
export * from "./create-or-update-order-payment-collection"
|
||||
export * from "./create-order"
|
||||
export * from "./create-order-change"
|
||||
export * from "./create-order-change-actions"
|
||||
export * from "./create-order-credit-lines"
|
||||
export * from "./create-order-payment-collection"
|
||||
export * from "./create-order"
|
||||
export * from "./create-shipment"
|
||||
export * from "./decline-order-change"
|
||||
export * from "./delete-order-change"
|
||||
@@ -75,12 +77,11 @@ export * from "./return/update-receive-item-return-request"
|
||||
export * from "./return/update-request-item-return"
|
||||
export * from "./return/update-return"
|
||||
export * from "./return/update-return-shipping-method"
|
||||
export * from "./update-order-change-actions"
|
||||
export * from "./update-order-changes"
|
||||
export * from "./update-tax-lines"
|
||||
export * from "./transfer/request-order-transfer"
|
||||
export * from "./transfer/accept-order-transfer"
|
||||
export * from "./transfer/cancel-order-transfer"
|
||||
export * from "./transfer/decline-order-transfer"
|
||||
export * from "./transfer/request-order-transfer"
|
||||
export * from "./update-order"
|
||||
export * from "./create-or-update-order-payment-collection"
|
||||
export * from "./update-order-change-actions"
|
||||
export * from "./update-order-changes"
|
||||
export * from "./update-tax-lines"
|
||||
|
||||
Reference in New Issue
Block a user