From deab12e27e8249e26d24d7bc904c18195679ff24 Mon Sep 17 00:00:00 2001 From: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com> Date: Mon, 25 Mar 2024 16:29:36 +0100 Subject: [PATCH] feat(types, medusa, core-flows): add delete-stock-location endpoint to api-v2 (#6801) * initial create * add changeset * redo changes for stock locatino module' * initial delete stock location * add changeset * pr prep * propagate deletion with common step * move integration tests --------- Co-authored-by: Riqwan Thamir --- .changeset/friendly-planes-kiss.md | 7 +++ .../admin/stock-location/index.spec.ts | 62 ++++++++++++++++++- .../steps/delete-stock-locations.ts | 23 +++++++ .../src/stock-location/steps/index.ts | 1 + .../workflows/delete-stock-locations.ts | 21 +++++++ .../src/stock-location/workflows/index.ts | 1 + .../admin/stock-locations/[id]/route.ts | 22 +++++++ .../types/src/stock-location/service-next.ts | 27 ++++++++ 8 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 .changeset/friendly-planes-kiss.md create mode 100644 packages/core-flows/src/stock-location/steps/delete-stock-locations.ts create mode 100644 packages/core-flows/src/stock-location/workflows/delete-stock-locations.ts create mode 100644 packages/medusa/src/api-v2/admin/stock-locations/[id]/route.ts diff --git a/.changeset/friendly-planes-kiss.md b/.changeset/friendly-planes-kiss.md new file mode 100644 index 0000000000..463fdc6484 --- /dev/null +++ b/.changeset/friendly-planes-kiss.md @@ -0,0 +1,7 @@ +--- +"@medusajs/core-flows": patch +"@medusajs/medusa": patch +"@medusajs/types": patch +--- + +feat(types, medusa, core-flows): add delete-stock-location endpoint to api-v2 diff --git a/integration-tests/api/__tests__/admin/stock-location/index.spec.ts b/integration-tests/api/__tests__/admin/stock-location/index.spec.ts index 04de2bda46..04b74876a6 100644 --- a/integration-tests/api/__tests__/admin/stock-location/index.spec.ts +++ b/integration-tests/api/__tests__/admin/stock-location/index.spec.ts @@ -1,10 +1,11 @@ +import { ModuleRegistrationName, Modules } from "@medusajs/modules-sdk" import { adminHeaders, createAdminUser, } from "../../../../helpers/create-admin-user" +import { ContainerRegistrationKeys } from "@medusajs/utils" import { IStockLocationServiceNext } from "@medusajs/types" -import { ModuleRegistrationName } from "@medusajs/modules-sdk" const { medusaIntegrationTestRunner } = require("medusa-test-utils") @@ -54,5 +55,64 @@ medusaIntegrationTestRunner({ ) }) }) + + describe("Delete stock location", () => { + let stockLocationId + beforeEach(async () => { + const stockLocationCreateResponse = await api.post( + `/admin/stock-locations`, + { name: "test location" }, + adminHeaders + ) + + stockLocationId = stockLocationCreateResponse.data.stock_location.id + }) + + it("should successfully delete stock location", async () => { + const stockLocationDeleteResponse = await api.delete( + `/admin/stock-locations/${stockLocationId}`, + adminHeaders + ) + + expect(stockLocationDeleteResponse.status).toEqual(200) + expect(stockLocationDeleteResponse.data).toEqual({ + id: stockLocationId, + object: "stock_location", + deleted: true, + }) + }) + + it("should successfully delete stock location associations", async () => { + const remoteLink = appContainer.resolve( + ContainerRegistrationKeys.REMOTE_LINK + ) + + await remoteLink.create([ + { + [Modules.SALES_CHANNEL]: { + sales_channel_id: "default", + }, + [Modules.STOCK_LOCATION]: { + stock_location_id: stockLocationId, + }, + }, + ]) + + await api.delete( + `/admin/stock-locations/${stockLocationId}`, + adminHeaders + ) + + const linkService = remoteLink.getLinkModule( + Modules.SALES_CHANNEL, + "sales_channel_id", + Modules.STOCK_LOCATION, + "stock_location_id" + ) + + const stockLocationLinks = await linkService.list() + expect(stockLocationLinks).toHaveLength(0) + }) + }) }, }) diff --git a/packages/core-flows/src/stock-location/steps/delete-stock-locations.ts b/packages/core-flows/src/stock-location/steps/delete-stock-locations.ts new file mode 100644 index 0000000000..06b3b85a22 --- /dev/null +++ b/packages/core-flows/src/stock-location/steps/delete-stock-locations.ts @@ -0,0 +1,23 @@ +import { StepResponse, createStep } from "@medusajs/workflows-sdk" + +import { ModuleRegistrationName } from "@medusajs/modules-sdk" + +export const deleteStockLocationsStepId = "delete-stock-locations-step" +export const deleteStockLocationsStep = createStep( + deleteStockLocationsStepId, + async (input: string[], { container }) => { + const service = container.resolve(ModuleRegistrationName.STOCK_LOCATION) + + await service.softDelete(input) + + return new StepResponse(void 0, input) + }, + async (deletedLocaitonIds, { container }) => { + if (!deletedLocaitonIds?.length) { + return + } + const service = container.resolve(ModuleRegistrationName.STOCK_LOCATION) + + await service.restore(deletedLocaitonIds) + } +) diff --git a/packages/core-flows/src/stock-location/steps/index.ts b/packages/core-flows/src/stock-location/steps/index.ts index 6f009f0cf5..f15c405fec 100644 --- a/packages/core-flows/src/stock-location/steps/index.ts +++ b/packages/core-flows/src/stock-location/steps/index.ts @@ -1 +1,2 @@ export * from "./create-stock-locations" +export * from "./delete-stock-locations" diff --git a/packages/core-flows/src/stock-location/workflows/delete-stock-locations.ts b/packages/core-flows/src/stock-location/workflows/delete-stock-locations.ts new file mode 100644 index 0000000000..ad4b114497 --- /dev/null +++ b/packages/core-flows/src/stock-location/workflows/delete-stock-locations.ts @@ -0,0 +1,21 @@ +import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk" + +import { Modules } from "@medusajs/modules-sdk" +import { deleteStockLocationsStep } from "../steps" +import { removeRemoteLinkStep } from "../../common/steps/remove-remote-links" + +interface WorkflowInput { + ids: string[] +} + +export const deleteStockLocationsWorkflowId = "delete-stock-locations-workflow" +export const deleteStockLocationsWorkflow = createWorkflow( + deleteStockLocationsWorkflowId, + (input: WorkflowData) => { + deleteStockLocationsStep(input.ids) + + removeRemoteLinkStep({ + [Modules.STOCK_LOCATION]: { stock_location_id: input.ids }, + }) + } +) diff --git a/packages/core-flows/src/stock-location/workflows/index.ts b/packages/core-flows/src/stock-location/workflows/index.ts index 6f009f0cf5..f15c405fec 100644 --- a/packages/core-flows/src/stock-location/workflows/index.ts +++ b/packages/core-flows/src/stock-location/workflows/index.ts @@ -1 +1,2 @@ export * from "./create-stock-locations" +export * from "./delete-stock-locations" diff --git a/packages/medusa/src/api-v2/admin/stock-locations/[id]/route.ts b/packages/medusa/src/api-v2/admin/stock-locations/[id]/route.ts new file mode 100644 index 0000000000..bf559e8cff --- /dev/null +++ b/packages/medusa/src/api-v2/admin/stock-locations/[id]/route.ts @@ -0,0 +1,22 @@ +import { MedusaRequest, MedusaResponse } from "../../../../types/routing" + +import { deleteStockLocationsWorkflow } from "@medusajs/core-flows" + +export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => { + const { id } = req.params + + const { errors } = await deleteStockLocationsWorkflow(req.scope).run({ + input: { ids: [id] }, + throwOnError: false, + }) + + if (Array.isArray(errors) && errors[0]) { + throw errors[0].error + } + + res.status(200).json({ + id, + object: "stock_location", + deleted: true, + }) +} diff --git a/packages/types/src/stock-location/service-next.ts b/packages/types/src/stock-location/service-next.ts index 148b170f9b..4387f569e8 100644 --- a/packages/types/src/stock-location/service-next.ts +++ b/packages/types/src/stock-location/service-next.ts @@ -5,6 +5,7 @@ import { UpdateStockLocationInput, UpdateStockLocationNextInput, } from "./common" +import { RestoreReturn, SoftDeleteReturn } from "../dal" import { Context } from "../shared-context" import { FindConfig } from "../common/common" @@ -301,4 +302,30 @@ export interface IStockLocationServiceNext extends IModuleService { * } */ delete(id: string, context?: Context): Promise + + /** + * Soft delete stock locations + * @param stockLocationIds + * @param config + * @param sharedContext + */ + softDelete( + stockLocationIds: string[], + config?: SoftDeleteReturn, + sharedContext?: Context + ): Promise | void> + + /** + * This method is used to restore a stock location or multiple stock locations that were previously deleted using the {@link softDelete} method. + * + * @param {string[]} stockLocationIds - The ID(s) of the stock location(s) to restore. + * @param {RestoreReturn} config - Restore config + * @param {Context} context - A context used to share resources, such as transaction manager, between the application and the module. + * @returns {Promise} Resolves when the stock location(s) are successfully restored. + */ + restore( + stockLocationIds: string[], + config?: RestoreReturn, + sharedContext?: Context + ): Promise | void> }