|
| 1 | +import { |
| 2 | + CartWorkflowDTO, |
| 3 | + CreateLineItemTaxLineDTO, |
| 4 | + CreateShippingMethodTaxLineDTO, |
| 5 | + ICartModuleService, |
| 6 | + ItemTaxLineDTO, |
| 7 | + LineItemTaxLineDTO, |
| 8 | + ShippingMethodTaxLineDTO, |
| 9 | + ShippingTaxLineDTO, |
| 10 | +} from "@medusajs/framework/types" |
| 11 | +import { Modules, promiseAll } from "@medusajs/framework/utils" |
| 12 | +import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk" |
| 13 | + |
| 14 | +/** |
| 15 | + * The details of the tax lines to set in a cart. |
| 16 | + */ |
| 17 | +export interface SetTaxLinesForItemsStepInput { |
| 18 | + /** |
| 19 | + * The cart's details. |
| 20 | + */ |
| 21 | + cart: CartWorkflowDTO |
| 22 | + /** |
| 23 | + * The tax lines to set for line items. |
| 24 | + */ |
| 25 | + item_tax_lines: ItemTaxLineDTO[] |
| 26 | + /** |
| 27 | + * The tax lines to set for shipping methods. |
| 28 | + */ |
| 29 | + shipping_tax_lines: ShippingTaxLineDTO[] |
| 30 | +} |
| 31 | + |
| 32 | +export const upsertTaxLinesForItemsStepId = "set-tax-lines-for-items" |
| 33 | +/** |
| 34 | + * This step sets the tax lines of shipping methods and line items in a cart. |
| 35 | + * |
| 36 | + * :::tip |
| 37 | + * |
| 38 | + * You can use the {@link retrieveCartStep} to retrieve a cart's details. |
| 39 | + * |
| 40 | + * ::: |
| 41 | + * |
| 42 | + * @example |
| 43 | + * const data = upsertTaxLinesForItemsStep({ |
| 44 | + * // retrieve the details of the cart from another workflow |
| 45 | + * // or in another step using the Cart Module's service |
| 46 | + * cart, |
| 47 | + * "item_tax_lines": [{ |
| 48 | + * "rate": 48, |
| 49 | + * "code": "CODE123", |
| 50 | + * "name": "Tax rate 2", |
| 51 | + * "provider_id": "provider_1", |
| 52 | + * "line_item_id": "litem_123" |
| 53 | + * }], |
| 54 | + * "shipping_tax_lines": [{ |
| 55 | + * "rate": 49, |
| 56 | + * "code": "CODE456", |
| 57 | + * "name": "Tax rate 1", |
| 58 | + * "provider_id": "provider_1", |
| 59 | + * "shipping_line_id": "sm_123" |
| 60 | + * }] |
| 61 | + * }) |
| 62 | + */ |
| 63 | +export const upsertTaxLinesForItemsStep = createStep( |
| 64 | + upsertTaxLinesForItemsStepId, |
| 65 | + async (data: SetTaxLinesForItemsStepInput, { container }) => { |
| 66 | + const { cart, item_tax_lines, shipping_tax_lines } = data |
| 67 | + const cartService = container.resolve<ICartModuleService>(Modules.CART) |
| 68 | + |
| 69 | + const [existingShippingMethodTaxLines, existingLineItemTaxLines] = |
| 70 | + await promiseAll([ |
| 71 | + shipping_tax_lines.length |
| 72 | + ? cartService.listShippingMethodTaxLines({ |
| 73 | + shipping_method_id: shipping_tax_lines.map( |
| 74 | + (t) => t.shipping_line_id |
| 75 | + ), |
| 76 | + }) |
| 77 | + : [], |
| 78 | + |
| 79 | + item_tax_lines.length |
| 80 | + ? cartService.listLineItemTaxLines({ |
| 81 | + item_id: item_tax_lines.map((t) => t.line_item_id), |
| 82 | + }) |
| 83 | + : [], |
| 84 | + ]) |
| 85 | + |
| 86 | + const itemsTaxLinesData = normalizeItemTaxLinesForCart( |
| 87 | + item_tax_lines, |
| 88 | + existingLineItemTaxLines |
| 89 | + ) |
| 90 | + const shippingTaxLinesData = normalizeShippingTaxLinesForCart( |
| 91 | + shipping_tax_lines, |
| 92 | + existingShippingMethodTaxLines |
| 93 | + ) |
| 94 | + |
| 95 | + await promiseAll([ |
| 96 | + itemsTaxLinesData.length |
| 97 | + ? cartService.upsertLineItemTaxLines(itemsTaxLinesData) |
| 98 | + : [], |
| 99 | + shippingTaxLinesData.length |
| 100 | + ? cartService.upsertShippingMethodTaxLines(shippingTaxLinesData) |
| 101 | + : [], |
| 102 | + ]) |
| 103 | + |
| 104 | + return new StepResponse(null, { |
| 105 | + cart, |
| 106 | + existingLineItemTaxLines, |
| 107 | + existingShippingMethodTaxLines, |
| 108 | + }) |
| 109 | + }, |
| 110 | + async (revertData, { container }) => { |
| 111 | + if (!revertData) { |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + const { existingLineItemTaxLines, existingShippingMethodTaxLines } = |
| 116 | + revertData |
| 117 | + |
| 118 | + const cartService = container.resolve<ICartModuleService>(Modules.CART) |
| 119 | + |
| 120 | + if (existingLineItemTaxLines) { |
| 121 | + await cartService.upsertLineItemTaxLines( |
| 122 | + existingLineItemTaxLines.map((taxLine) => ({ |
| 123 | + description: taxLine.description, |
| 124 | + tax_rate_id: taxLine.tax_rate_id, |
| 125 | + code: taxLine.code, |
| 126 | + rate: taxLine.rate, |
| 127 | + provider_id: taxLine.provider_id, |
| 128 | + item_id: taxLine.item_id, |
| 129 | + })) |
| 130 | + ) |
| 131 | + } |
| 132 | + |
| 133 | + await cartService.upsertShippingMethodTaxLines( |
| 134 | + existingShippingMethodTaxLines.map((taxLine) => ({ |
| 135 | + description: taxLine.description, |
| 136 | + tax_rate_id: taxLine.tax_rate_id, |
| 137 | + code: taxLine.code, |
| 138 | + rate: taxLine.rate, |
| 139 | + provider_id: taxLine.provider_id, |
| 140 | + shipping_method_id: taxLine.shipping_method_id, |
| 141 | + })) |
| 142 | + ) |
| 143 | + } |
| 144 | +) |
| 145 | + |
| 146 | +function normalizeItemTaxLinesForCart( |
| 147 | + taxLines: ItemTaxLineDTO[], |
| 148 | + existingTaxLines: LineItemTaxLineDTO[] |
| 149 | +): CreateLineItemTaxLineDTO[] { |
| 150 | + return taxLines.map((taxLine: ItemTaxLineDTO & { id?: string }) => ({ |
| 151 | + id: existingTaxLines.find((t) => t.item_id === taxLine.line_item_id)?.id, |
| 152 | + description: taxLine.name, |
| 153 | + tax_rate_id: taxLine.rate_id, |
| 154 | + code: taxLine.code!, |
| 155 | + rate: taxLine.rate!, |
| 156 | + provider_id: taxLine.provider_id, |
| 157 | + item_id: taxLine.line_item_id, |
| 158 | + })) |
| 159 | +} |
| 160 | + |
| 161 | +function normalizeShippingTaxLinesForCart( |
| 162 | + taxLines: ShippingTaxLineDTO[], |
| 163 | + existingTaxLines: ShippingMethodTaxLineDTO[] |
| 164 | +): CreateShippingMethodTaxLineDTO[] { |
| 165 | + return taxLines.map((taxLine: ShippingTaxLineDTO & { id?: string }) => ({ |
| 166 | + id: existingTaxLines.find( |
| 167 | + (t) => t.shipping_method_id === taxLine.shipping_line_id |
| 168 | + )?.id, |
| 169 | + description: taxLine.name, |
| 170 | + tax_rate_id: taxLine.rate_id, |
| 171 | + code: taxLine.code!, |
| 172 | + rate: taxLine.rate!, |
| 173 | + provider_id: taxLine.provider_id, |
| 174 | + shipping_method_id: taxLine.shipping_line_id, |
| 175 | + })) |
| 176 | +} |
0 commit comments