feat(core-flows,types,medusa): Add tax region update API (#9634)

* feat(core-flows,types,medusa): Add tax region update API

* chore: added specs
This commit is contained in:
Riqwan Thamir
2024-10-17 14:19:29 +02:00
committed by GitHub
parent 2cba362537
commit 3b50c6d019
9 changed files with 314 additions and 13 deletions
@@ -0,0 +1,58 @@
import {
ITaxModuleService,
UpdateTaxRegionDTO,
} from "@medusajs/framework/types"
import {
Modules,
getSelectsAndRelationsFromObjectArray,
removeUndefined,
} from "@medusajs/framework/utils"
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk"
export const updateTaxRegionsStepId = "update-tax-regions"
/**
* This step updates tax regions
*/
export const updateTaxRegionsStep = createStep(
updateTaxRegionsStepId,
async (data: UpdateTaxRegionDTO[], { container }) => {
const service = container.resolve<ITaxModuleService>(Modules.TAX)
const { selects, relations } = getSelectsAndRelationsFromObjectArray(data)
const prevData = await service.listTaxRegions(
{ id: data.map((d) => d.id) },
{
select: selects,
relations,
}
)
const updateData = removeUndefined(
data.map((d) => ({
id: d.id,
province_code: d.province_code,
metadata: d.metadata,
}))
)
const taxRegions = await service.updateTaxRegions(updateData)
return new StepResponse(taxRegions, prevData)
},
async (prevData, { container }) => {
if (!prevData?.length) {
return
}
const service = container.resolve<ITaxModuleService>(Modules.TAX)
const updateData = removeUndefined(
prevData.map((d) => ({
id: d.id,
province_code: d.province_code,
metadata: d.metadata,
}))
)
await service.updateTaxRegions(updateData)
}
)
@@ -1,8 +1,9 @@
export * from "./create-tax-regions"
export * from "./delete-tax-regions"
export * from "./create-tax-rates"
export * from "./update-tax-rates"
export * from "./delete-tax-rates"
export * from "./set-tax-rate-rules"
export * from "./create-tax-rate-rules"
export * from "./create-tax-rates"
export * from "./create-tax-regions"
export * from "./delete-tax-rate-rules"
export * from "./delete-tax-rates"
export * from "./delete-tax-regions"
export * from "./set-tax-rate-rules"
export * from "./update-tax-rates"
export * from "./update-tax-regions"
@@ -0,0 +1,20 @@
import { TaxRegionDTO, UpdateTaxRegionDTO } from "@medusajs/framework/types"
import {
WorkflowData,
WorkflowResponse,
createWorkflow,
} from "@medusajs/framework/workflows-sdk"
import { updateTaxRegionsStep } from "../steps/update-tax-regions"
export const updateTaxRegionsWorkflowId = "update-tax-regions"
/**
* This workflow updates one or more tax regions.
*/
export const updateTaxRegionsWorkflow = createWorkflow(
updateTaxRegionsWorkflowId,
(
input: WorkflowData<UpdateTaxRegionDTO[]>
): WorkflowResponse<TaxRegionDTO[]> => {
return new WorkflowResponse(updateTaxRegionsStep(input))
}
)
+20
View File
@@ -206,6 +206,26 @@ export interface CreateTaxRegionDTO {
}
}
/**
* The tax region to be updated.
*/
export interface UpdateTaxRegionDTO {
/**
* The id of the tax region to update
*/
id: string
/**
* The province code of the tax region.
*/
province_code?: string | null
/**
* Holds custom data in key-value pairs.
*/
metadata?: MetadataType
}
/**
* The tax rate rule to be created.
*/
+42
View File
@@ -20,6 +20,7 @@ import {
CreateTaxRateRuleDTO,
CreateTaxRegionDTO,
UpdateTaxRateDTO,
UpdateTaxRegionDTO,
UpsertTaxRateDTO,
} from "./mutations"
@@ -407,6 +408,47 @@ export interface ITaxModuleService extends IModuleService {
sharedContext?: Context
): Promise<TaxRegionDTO[]>
/**
* This method updates a tax region.
*
* @param {UpdateTaxRegionDTO} data - The tax region to be updated.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<TaxRegionDTO>} The updated tax region.
*
* @example
* const taxRegion = await taxModule.updateTaxRegions({
* province_code: "be",
* })
*/
updateTaxRegions(
data: UpdateTaxRegionDTO,
sharedContext?: Context
): Promise<TaxRegionDTO>
/**
* This method updates tax regions.
*
* @param {UpdateTaxRegionDTO[]} data - The tax regions to be updated.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<TaxRegionDTO[]>} The updated tax regions.
*
* @example
* const taxRegions = await taxModule.updateTaxRegions([
* {
* id: "tx-1",
* province_code: "be",
* },
* {
* id: "tx-2",
* province_code: "ca",
* },
* ])
*/
updateTaxRegions(
data: UpdateTaxRegionDTO[],
sharedContext?: Context
): Promise<TaxRegionDTO[]>
/**
* This method deletes tax regions by their IDs.
*