feat: Add support in BE for setting tax inclusivity on currency (#8037)
This commit is contained in:
@@ -2,4 +2,5 @@ export * from "./create-price-sets"
|
||||
export * from "./update-price-sets"
|
||||
export * from "./create-price-preferences"
|
||||
export * from "./update-price-preferences"
|
||||
export * from "./update-price-preferences-as-array"
|
||||
export * from "./delete-price-preferences"
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
import { PricingWorkflow, IPricingModuleService } from "@medusajs/types"
|
||||
import {
|
||||
MedusaError,
|
||||
ModuleRegistrationName,
|
||||
arrayDifference,
|
||||
} from "@medusajs/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
type StepInput = PricingWorkflow.UpdatePricePreferencesWorkflowInput["update"][]
|
||||
|
||||
export const updatePricePreferencesAsArrayStepId =
|
||||
"update-price-preferences-as-array"
|
||||
export const updatePricePreferencesAsArrayStep = createStep(
|
||||
updatePricePreferencesAsArrayStepId,
|
||||
async (input: StepInput, { container }) => {
|
||||
const service = container.resolve<IPricingModuleService>(
|
||||
ModuleRegistrationName.PRICING
|
||||
)
|
||||
|
||||
const prevData = await service.listPricePreferences({
|
||||
$or: input.map(
|
||||
(entry) => {
|
||||
if (!entry.attribute || !entry.value) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
"Attribute and value must be provided when updating price preferences"
|
||||
)
|
||||
}
|
||||
|
||||
return { attribute: entry.attribute, value: entry.value }
|
||||
},
|
||||
{ take: null }
|
||||
),
|
||||
})
|
||||
|
||||
const toUpsert = input.map((entry) => {
|
||||
const prevEntry = prevData.find(
|
||||
(prevEntry) =>
|
||||
prevEntry.attribute === entry.attribute &&
|
||||
prevEntry.value === entry.value
|
||||
)
|
||||
return {
|
||||
id: prevEntry?.id,
|
||||
attribute: entry.attribute,
|
||||
value: entry.value,
|
||||
is_tax_inclusive: entry.is_tax_inclusive ?? prevEntry?.is_tax_inclusive,
|
||||
}
|
||||
})
|
||||
|
||||
const upsertedPricePreferences = await service.upsertPricePreferences(
|
||||
toUpsert
|
||||
)
|
||||
|
||||
const newIds = arrayDifference(
|
||||
upsertedPricePreferences.map((p) => p.id),
|
||||
prevData.map((p) => p.id)
|
||||
)
|
||||
|
||||
return new StepResponse(upsertedPricePreferences, {
|
||||
prevData,
|
||||
newDataIds: newIds,
|
||||
})
|
||||
},
|
||||
async (compensationData, { container }) => {
|
||||
if (!compensationData) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IPricingModuleService>(
|
||||
ModuleRegistrationName.PRICING
|
||||
)
|
||||
|
||||
await service.upsertPricePreferences(compensationData.prevData)
|
||||
await service.deletePricePreferences(compensationData.newDataIds)
|
||||
}
|
||||
)
|
||||
@@ -2,9 +2,7 @@ import { CreateStoreDTO, IStoreModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
type CreateStoresStepInput = {
|
||||
stores: CreateStoreDTO[]
|
||||
}
|
||||
type CreateStoresStepInput = CreateStoreDTO[]
|
||||
|
||||
export const createStoresStepId = "create-stores"
|
||||
export const createStoresStep = createStep(
|
||||
@@ -14,7 +12,7 @@ export const createStoresStep = createStep(
|
||||
ModuleRegistrationName.STORE
|
||||
)
|
||||
|
||||
const created = await service.createStores(data.stores)
|
||||
const created = await service.createStores(data)
|
||||
return new StepResponse(
|
||||
created,
|
||||
created.map((store) => store.id)
|
||||
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
} from "@medusajs/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
type UpdateStoresStepInput = {
|
||||
type WorkflowInputData = {
|
||||
selector: FilterableStoreProps
|
||||
update: UpdateStoreDTO
|
||||
}
|
||||
@@ -17,7 +17,7 @@ type UpdateStoresStepInput = {
|
||||
export const updateStoresStepId = "update-stores"
|
||||
export const updateStoresStep = createStep(
|
||||
updateStoresStepId,
|
||||
async (data: UpdateStoresStepInput, { container }) => {
|
||||
async (data: WorkflowInputData, { container }) => {
|
||||
const service = container.resolve<IStoreModuleService>(
|
||||
ModuleRegistrationName.STORE
|
||||
)
|
||||
|
||||
@@ -1,13 +1,54 @@
|
||||
import { StoreDTO, CreateStoreDTO } from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { StoreDTO, StoreWorkflow } from "@medusajs/types"
|
||||
import {
|
||||
WorkflowData,
|
||||
createWorkflow,
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { createStoresStep } from "../steps"
|
||||
import { updatePricePreferencesAsArrayStep } from "../../pricing"
|
||||
|
||||
type WorkflowInput = { stores: CreateStoreDTO[] }
|
||||
type WorkflowInputData = { stores: StoreWorkflow.CreateStoreWorkflowInput[] }
|
||||
|
||||
export const createStoresWorkflowId = "create-stores"
|
||||
export const createStoresWorkflow = createWorkflow(
|
||||
createStoresWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<StoreDTO[]> => {
|
||||
return createStoresStep(input)
|
||||
(input: WorkflowData<WorkflowInputData>): WorkflowData<StoreDTO[]> => {
|
||||
const normalizedInput = transform({ input }, (data) => {
|
||||
return data.input.stores.map((store) => {
|
||||
return {
|
||||
...store,
|
||||
supported_currencies: store.supported_currencies?.map((currency) => {
|
||||
return {
|
||||
currency_code: currency.currency_code,
|
||||
is_default: currency.is_default,
|
||||
}
|
||||
}),
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
const stores = createStoresStep(normalizedInput)
|
||||
|
||||
const upsertPricePreferences = transform({ input }, (data) => {
|
||||
const toUpsert = new Map<
|
||||
string,
|
||||
{ attribute: string; value: string; is_tax_inclusive?: boolean }
|
||||
>()
|
||||
|
||||
data.input.stores.forEach((store) => {
|
||||
store.supported_currencies.forEach((currency) => {
|
||||
toUpsert.set(currency.currency_code, {
|
||||
attribute: "currency_code",
|
||||
value: currency.currency_code,
|
||||
is_tax_inclusive: currency.is_tax_inclusive,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
return Array.from(toUpsert.values())
|
||||
})
|
||||
|
||||
updatePricePreferencesAsArrayStep(upsertPricePreferences)
|
||||
return stores
|
||||
}
|
||||
)
|
||||
|
||||
@@ -1,18 +1,58 @@
|
||||
import { StoreDTO, FilterableStoreProps, UpdateStoreDTO } from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { StoreDTO, StoreWorkflow } from "@medusajs/types"
|
||||
import {
|
||||
WorkflowData,
|
||||
createWorkflow,
|
||||
transform,
|
||||
when,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { updateStoresStep } from "../steps"
|
||||
import { updatePricePreferencesAsArrayStep } from "../../pricing"
|
||||
|
||||
type UpdateStoresStepInput = {
|
||||
selector: FilterableStoreProps
|
||||
update: UpdateStoreDTO
|
||||
}
|
||||
|
||||
type WorkflowInput = UpdateStoresStepInput
|
||||
type WorkflowInputData = StoreWorkflow.UpdateStoreWorkflowInput
|
||||
|
||||
export const updateStoresWorkflowId = "update-stores"
|
||||
export const updateStoresWorkflow = createWorkflow(
|
||||
updateStoresWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<StoreDTO[]> => {
|
||||
return updateStoresStep(input)
|
||||
(input: WorkflowData<WorkflowInputData>): WorkflowData<StoreDTO[]> => {
|
||||
const normalizedInput = transform({ input }, (data) => {
|
||||
if (!data.input.update.supported_currencies?.length) {
|
||||
return data.input
|
||||
}
|
||||
|
||||
return {
|
||||
selector: data.input.selector,
|
||||
update: {
|
||||
...data.input.update,
|
||||
supported_currencies: data.input.update.supported_currencies.map(
|
||||
(currency) => {
|
||||
return {
|
||||
currency_code: currency.currency_code,
|
||||
is_default: currency.is_default,
|
||||
}
|
||||
}
|
||||
),
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
const stores = updateStoresStep(normalizedInput)
|
||||
|
||||
when({ input }, (data) => {
|
||||
return !!data.input.update.supported_currencies?.length
|
||||
}).then(() => {
|
||||
const upsertPricePreferences = transform({ input }, (data) => {
|
||||
return data.input.update.supported_currencies!.map((currency) => {
|
||||
return {
|
||||
attribute: "currency_code",
|
||||
value: currency.currency_code,
|
||||
is_tax_inclusive: currency.is_tax_inclusive,
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
updatePricePreferencesAsArrayStep(upsertPricePreferences)
|
||||
})
|
||||
|
||||
return stores
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user