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 <rmthamir@gmail.com>
This commit is contained in:
7
.changeset/friendly-planes-kiss.md
Normal file
7
.changeset/friendly-planes-kiss.md
Normal file
@@ -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
|
||||
@@ -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)
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
)
|
||||
@@ -1 +1,2 @@
|
||||
export * from "./create-stock-locations"
|
||||
export * from "./delete-stock-locations"
|
||||
|
||||
@@ -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<WorkflowInput>) => {
|
||||
deleteStockLocationsStep(input.ids)
|
||||
|
||||
removeRemoteLinkStep({
|
||||
[Modules.STOCK_LOCATION]: { stock_location_id: input.ids },
|
||||
})
|
||||
}
|
||||
)
|
||||
@@ -1 +1,2 @@
|
||||
export * from "./create-stock-locations"
|
||||
export * from "./delete-stock-locations"
|
||||
|
||||
@@ -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,
|
||||
})
|
||||
}
|
||||
@@ -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<void>
|
||||
|
||||
/**
|
||||
* Soft delete stock locations
|
||||
* @param stockLocationIds
|
||||
* @param config
|
||||
* @param sharedContext
|
||||
*/
|
||||
softDelete<TReturnableLinkableKeys extends string = string>(
|
||||
stockLocationIds: string[],
|
||||
config?: SoftDeleteReturn<TReturnableLinkableKeys>,
|
||||
sharedContext?: Context
|
||||
): Promise<Record<string, string[]> | 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<TReturnableLinkableKeys>} config - Restore config
|
||||
* @param {Context} context - A context used to share resources, such as transaction manager, between the application and the module.
|
||||
* @returns {Promise<void>} Resolves when the stock location(s) are successfully restored.
|
||||
*/
|
||||
restore<TReturnableLinkableKeys extends string = string>(
|
||||
stockLocationIds: string[],
|
||||
config?: RestoreReturn<TReturnableLinkableKeys>,
|
||||
sharedContext?: Context
|
||||
): Promise<Record<string, string[]> | void>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user