Skip to content

Commit

Permalink
feat(types, medusa, core-flows): add delete-stock-location endpoint t…
Browse files Browse the repository at this point in the history
…o 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>
  • Loading branch information
pKorsholm and riqwan authored Mar 25, 2024
1 parent 71efa15 commit deab12e
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changeset/friendly-planes-kiss.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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")

Expand Down Expand Up @@ -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)
})
})
},
})
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions packages/core-flows/src/stock-location/steps/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./create-stock-locations"
export * from "./delete-stock-locations"
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions packages/core-flows/src/stock-location/workflows/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./create-stock-locations"
export * from "./delete-stock-locations"
22 changes: 22 additions & 0 deletions packages/medusa/src/api-v2/admin/stock-locations/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -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,
})
}
27 changes: 27 additions & 0 deletions packages/types/src/stock-location/service-next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
UpdateStockLocationInput,
UpdateStockLocationNextInput,
} from "./common"
import { RestoreReturn, SoftDeleteReturn } from "../dal"

import { Context } from "../shared-context"
import { FindConfig } from "../common/common"
Expand Down Expand Up @@ -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>
}

0 comments on commit deab12e

Please sign in to comment.