feat: Add support in BE for setting tax inclusivity on currency (#8037)
This commit is contained in:
@@ -118,6 +118,8 @@ export const AddCurrenciesForm = ({ store }: AddCurrenciesFormProps) => {
|
||||
supported_currencies: currencies.map((c) => ({
|
||||
currency_code: c,
|
||||
is_default: c === defaultCurrency,
|
||||
// TODO: Add UI to manage this
|
||||
is_tax_inclsuive: false,
|
||||
})),
|
||||
})
|
||||
toast.success(t("general.success"), {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
)
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
interface AdminUpdateStoreSupportedCurrency {
|
||||
currency_code: string
|
||||
is_default?: boolean
|
||||
code: string
|
||||
is_tax_inclusive?: boolean
|
||||
}
|
||||
|
||||
export interface AdminUpdateStore {
|
||||
name?: string | null
|
||||
name?: string
|
||||
supported_currencies?: AdminUpdateStoreSupportedCurrency[]
|
||||
default_sales_channel_id?: string | null
|
||||
default_region_id?: string | null
|
||||
|
||||
@@ -71,20 +71,20 @@ export interface UpdateStoreDTO {
|
||||
/**
|
||||
* The associated default sales channel's ID.
|
||||
*/
|
||||
default_sales_channel_id?: string
|
||||
default_sales_channel_id?: string | null
|
||||
|
||||
/**
|
||||
* The associated default region's ID.
|
||||
*/
|
||||
default_region_id?: string
|
||||
default_region_id?: string | null
|
||||
|
||||
/**
|
||||
* The associated default location's ID.
|
||||
*/
|
||||
default_location_id?: string
|
||||
default_location_id?: string | null
|
||||
|
||||
/**
|
||||
* Holds custom data in key-value pairs.
|
||||
*/
|
||||
metadata?: Record<string, any>
|
||||
metadata?: Record<string, any> | null
|
||||
}
|
||||
|
||||
@@ -11,3 +11,4 @@ export * as ReservationWorkflow from "./reservation"
|
||||
export * as UserWorkflow from "./user"
|
||||
export * as OrderWorkflow from "./order"
|
||||
export * as PricingWorkflow from "./pricing"
|
||||
export * as StoreWorkflow from "./store"
|
||||
|
||||
18
packages/core/types/src/workflow/store/index.ts
Normal file
18
packages/core/types/src/workflow/store/index.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { AdminUpdateStore } from "../../http"
|
||||
import { CreateStoreDTO, FilterableStoreProps } from "../../store"
|
||||
|
||||
export type CreateStoreWorkflowInput = Omit<
|
||||
CreateStoreDTO,
|
||||
"supported_currencies"
|
||||
> & {
|
||||
supported_currencies: {
|
||||
currency_code: string
|
||||
is_default?: boolean
|
||||
is_tax_inclusive?: boolean
|
||||
}[]
|
||||
}
|
||||
|
||||
export interface UpdateStoreWorkflowInput {
|
||||
selector: FilterableStoreProps
|
||||
update: AdminUpdateStore
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
import { updateStoresWorkflow } from "@medusajs/core-flows"
|
||||
import { UpdateStoreDTO } from "@medusajs/types"
|
||||
import {
|
||||
remoteQueryObjectFromString,
|
||||
ContainerRegistrationKeys,
|
||||
@@ -35,7 +34,7 @@ export const POST = async (
|
||||
const { result } = await updateStoresWorkflow(req.scope).run({
|
||||
input: {
|
||||
selector: { id: req.params.id },
|
||||
update: req.validatedBody as UpdateStoreDTO,
|
||||
update: req.validatedBody,
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
@@ -20,12 +20,13 @@ export const AdminGetStoresParams = createFindParams({
|
||||
|
||||
export type AdminUpdateStoreType = z.infer<typeof AdminUpdateStore>
|
||||
export const AdminUpdateStore = z.object({
|
||||
name: z.string().nullish(),
|
||||
name: z.string().optional(),
|
||||
supported_currencies: z
|
||||
.array(
|
||||
z.object({
|
||||
currency_code: z.string(),
|
||||
is_default: z.boolean().optional(),
|
||||
is_tax_inclusive: z.boolean().optional(),
|
||||
})
|
||||
)
|
||||
.optional(),
|
||||
|
||||
@@ -196,7 +196,9 @@ export default class StoreModuleService
|
||||
)
|
||||
}
|
||||
|
||||
private static validateCreateRequest(stores: StoreTypes.CreateStoreDTO[]) {
|
||||
private static validateCreateRequest(
|
||||
stores: StoreTypes.CreateStoreDTO[] | StoreTypes.UpdateStoreDTO[]
|
||||
) {
|
||||
for (const store of stores) {
|
||||
if (store.supported_currencies?.length) {
|
||||
const duplicates = getDuplicates(
|
||||
|
||||
Reference in New Issue
Block a user