feat(core-flows,medusa,types): add automatic-taxes to region + generate tax lines endpoint (#6667)
what: - endpoint to generate tax lines - update workflows to force calculate tax lines with a flag - added automatic_taxes to region
This commit is contained in:
@@ -9,6 +9,7 @@ import {
|
||||
TaxableItemDTO,
|
||||
TaxableShippingDTO,
|
||||
} from "@medusajs/types"
|
||||
import { MedusaError } from "@medusajs/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
import { ModuleRegistrationName } from "../../../../../modules-sdk/dist"
|
||||
|
||||
@@ -16,14 +17,28 @@ interface StepInput {
|
||||
cart: CartWorkflowDTO
|
||||
items: CartLineItemDTO[]
|
||||
shipping_methods: CartShippingMethodDTO[]
|
||||
force_tax_calculation?: boolean
|
||||
}
|
||||
|
||||
function normalizeTaxModuleContext(
|
||||
cart: CartWorkflowDTO
|
||||
cart: CartWorkflowDTO,
|
||||
forceTaxCalculation: boolean
|
||||
): TaxCalculationContext | null {
|
||||
const address = cart.shipping_address
|
||||
const shouldCalculateTax = forceTaxCalculation || cart.region?.automatic_taxes
|
||||
|
||||
if (!address || !address.country_code) {
|
||||
if (!shouldCalculateTax) {
|
||||
return null
|
||||
}
|
||||
|
||||
if (forceTaxCalculation && !address?.country_code) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`country code is required to calculate taxes`
|
||||
)
|
||||
}
|
||||
|
||||
if (!address?.country_code) {
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -83,12 +98,17 @@ export const getItemTaxLinesStepId = "get-item-tax-lines"
|
||||
export const getItemTaxLinesStep = createStep(
|
||||
getItemTaxLinesStepId,
|
||||
async (data: StepInput, { container }) => {
|
||||
const { cart, items, shipping_methods: shippingMethods } = data
|
||||
const {
|
||||
cart,
|
||||
items,
|
||||
shipping_methods: shippingMethods,
|
||||
force_tax_calculation: forceTaxCalculation = false,
|
||||
} = data
|
||||
const taxService = container.resolve<ITaxModuleService>(
|
||||
ModuleRegistrationName.TAX
|
||||
)
|
||||
|
||||
const taxContext = normalizeTaxModuleContext(cart)
|
||||
const taxContext = normalizeTaxModuleContext(cart, forceTaxCalculation)
|
||||
|
||||
if (!taxContext) {
|
||||
return new StepResponse({
|
||||
|
||||
@@ -10,6 +10,7 @@ interface StepInput {
|
||||
cart_or_cart_id: CartWorkflowDTO | string
|
||||
items?: CartLineItemDTO[]
|
||||
shipping_methods?: CartShippingMethodDTO[]
|
||||
force_tax_calculation?: boolean
|
||||
}
|
||||
|
||||
export const updateTaxLinesStepId = "update-tax-lines-step"
|
||||
|
||||
@@ -18,6 +18,8 @@ const cartFields = [
|
||||
"id",
|
||||
"currency_code",
|
||||
"email",
|
||||
"region.id",
|
||||
"region.automatic_taxes",
|
||||
"items.id",
|
||||
"items.variant_id",
|
||||
"items.product_id",
|
||||
@@ -62,6 +64,7 @@ type WorkflowInput = {
|
||||
cart_or_cart_id: string | CartWorkflowDTO
|
||||
items?: CartLineItemDTO[]
|
||||
shipping_methods?: CartShippingMethodDTO[]
|
||||
force_tax_calculation?: boolean
|
||||
}
|
||||
|
||||
export const updateTaxLinesWorkflowId = "update-tax-lines"
|
||||
@@ -79,6 +82,7 @@ export const updateTaxLinesWorkflow = createWorkflow(
|
||||
items: data.input.items || data.cart.items,
|
||||
shipping_methods:
|
||||
data.input.shipping_methods || data.cart.shipping_methods,
|
||||
force_tax_calculation: data.input.force_tax_calculation,
|
||||
}))
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user