forked from medusajs/medusa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core-flows,types,medusa): Add tax region update API (medusajs#9634)
* feat(core-flows,types,medusa): Add tax region update API * chore: added specs
- Loading branch information
Showing
9 changed files
with
313 additions
and
12 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
integration-tests/http/__tests__/tax-region/admin/tax-region.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" | ||
import { createAdminUser } from "../../../../helpers/create-admin-user" | ||
|
||
jest.setTimeout(50000) | ||
|
||
const env = { MEDUSA_FF_MEDUSA_V2: true } | ||
const adminHeaders = { | ||
headers: { "x-medusa-access-token": "test_token" }, | ||
} | ||
|
||
medusaIntegrationTestRunner({ | ||
env, | ||
testSuite: ({ dbConnection, getContainer, api }) => { | ||
describe("/admin/tax-regions", () => { | ||
beforeEach(async () => { | ||
await createAdminUser(dbConnection, adminHeaders, getContainer()) | ||
}) | ||
|
||
describe("POST /admin/tax-regions/:id", () => { | ||
let taxRegion | ||
|
||
beforeEach(async () => { | ||
taxRegion = ( | ||
await api.post( | ||
"/admin/tax-regions", | ||
{ | ||
country_code: "us", | ||
province_code: "tx", | ||
metadata: { test: "created" }, | ||
}, | ||
adminHeaders | ||
) | ||
).data.tax_region | ||
}) | ||
|
||
it("should successfully update a tax region's fieleds", async () => { | ||
let taxRegionResponse = await api.post( | ||
`/admin/tax-regions/${taxRegion.id}`, | ||
{ | ||
province_code: "ny", | ||
metadata: { test: "updated" }, | ||
}, | ||
adminHeaders | ||
) | ||
|
||
expect(taxRegionResponse.status).toEqual(200) | ||
expect(taxRegionResponse.data.tax_region).toEqual( | ||
expect.objectContaining({ | ||
id: expect.any(String), | ||
province_code: "ny", | ||
metadata: { test: "updated" }, | ||
}) | ||
) | ||
|
||
taxRegionResponse = await api.post( | ||
`/admin/tax-regions/${taxRegion.id}`, | ||
{ metadata: { test: "updated 2" } }, | ||
adminHeaders | ||
) | ||
|
||
expect(taxRegionResponse.status).toEqual(200) | ||
expect(taxRegionResponse.data.tax_region).toEqual( | ||
expect.objectContaining({ | ||
id: expect.any(String), | ||
province_code: "ny", | ||
metadata: { test: "updated 2" }, | ||
}) | ||
) | ||
|
||
taxRegionResponse = await api.post( | ||
`/admin/tax-regions/${taxRegion.id}`, | ||
{ province_code: "ca" }, | ||
adminHeaders | ||
) | ||
|
||
expect(taxRegionResponse.status).toEqual(200) | ||
expect(taxRegionResponse.data.tax_region).toEqual( | ||
expect.objectContaining({ | ||
id: expect.any(String), | ||
province_code: "ca", | ||
metadata: { test: "updated 2" }, | ||
}) | ||
) | ||
}) | ||
|
||
it("should throw if tax region does not exist", async () => { | ||
const { | ||
response: { status, data }, | ||
} = await api | ||
.post( | ||
`/admin/tax-regions/does-not-exist`, | ||
{ province_code: "ny", metadata: { test: "updated" } }, | ||
adminHeaders | ||
) | ||
.catch((err) => err) | ||
|
||
expect(status).toEqual(404) | ||
expect(data).toEqual({ | ||
message: 'TaxRegion with id "does-not-exist" not found', | ||
type: "not_found", | ||
}) | ||
}) | ||
}) | ||
}) | ||
}, | ||
}) |
58 changes: 58 additions & 0 deletions
58
packages/core/core-flows/src/tax/steps/update-tax-regions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
export * from "./create-tax-regions" | ||
export * from "./delete-tax-regions" | ||
export * from "./create-tax-rate-rules" | ||
export * from "./create-tax-rates" | ||
export * from "./update-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 "./create-tax-rate-rules" | ||
export * from "./delete-tax-rate-rules" | ||
export * from "./update-tax-rates" | ||
export * from "./update-tax-regions" |
20 changes: 20 additions & 0 deletions
20
packages/core/core-flows/src/tax/workflows/update-tax-regions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters