Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {
ContainerRegistrationKeys,
MedusaError,
} from "@medusajs/framework/utils";
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http";

import { deleteInventoryLevelsWorkflow } from "@medusajs/core-flows";
import { HttpTypes } from "@medusajs/framework/types";
import { refetchInventoryItem } from "@medusajs/medusa/api/admin/inventory-items/helpers";

// Overridden admin default delete inventory level route to force delete the inventory level with stocked items at the locations.

export const DELETE = async (
req: MedusaRequest,
res: MedusaResponse<HttpTypes.AdminInventoryLevelDeleteResponse>
) => {
const { id, location_id } = req.params;

const query = req.scope.resolve(ContainerRegistrationKeys.QUERY);

const result = await query.graph({
entity: "inventory_level",
filters: { inventory_item_id: id, location_id },
fields: ["id", "reserved_quantity"],
});

if (!result.data.length) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap in try catch (not sure if needed) and add to query.graph

{ throwIfKeyNotFound: true }

throw new MedusaError(
MedusaError.Types.NOT_FOUND,
`Inventory Level for Item ${id} at Location ${location_id} not found`
);
}

const { id: levelId, reserved_quantity: reservedQuantity } = result.data[0];

if (reservedQuantity > 0) {
throw new MedusaError(
MedusaError.Types.NOT_ALLOWED,
`Cannot remove Inventory Level ${id} at Location ${location_id} because there are reservations at location`
);
}

const deleteInventoryLevelWorkflow = deleteInventoryLevelsWorkflow(req.scope);

await deleteInventoryLevelWorkflow.run({
input: {
id: [levelId],
force: true,
},
});

const inventoryItem = await refetchInventoryItem(
id,
req.scope,
req.queryConfig.fields
);

res.status(200).json({
id: levelId,
object: "inventory-level",
deleted: true,
parent: inventoryItem,
});
};
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { AuthenticatedMedusaRequest, MedusaResponse } from '@medusajs/framework'
import { ContainerRegistrationKeys, Modules } from '@medusajs/framework/utils'
import { updateInventoryLevelsWorkflow } from '@medusajs/medusa/core-flows'
import { ContainerRegistrationKeys, Modules, MedusaError } from '@medusajs/framework/utils'
import { updateInventoryLevelsWorkflow, deleteInventoryLevelsWorkflow } from '@medusajs/medusa/core-flows'

import { IntermediateEvents } from '@mercurjs/framework'

import { VendorUpdateInventoryLevelType } from '../../../validators'
import { VendorInventoryLevelDeleteResponse } from '../../../validators'
import { refetchInventoryItem } from '@medusajs/medusa/api/admin/inventory-items/helpers'

/**
* @oas [post] /vendor/inventory-items/{id}/location-levels/{location_id}
Expand Down Expand Up @@ -128,3 +130,87 @@ export const GET = async (
location_level
})
}

/**
* @oas [delete] /vendor/inventory-items/{id}/location-levels/{location_id}
* operationId: "VendorDeleteInventoryLevel"
* summary: "Delete inventory level"
* description: "Deletes inventory level of the InventoryItem in the specified location"
* x-authenticated: true
* parameters:
* - in: path
* name: id
* required: true
* description: The ID of the InventoryItem.
* schema:
* type: string
* - in: path
* name: location_id
* required: true
* description: The ID of the Stock Location.
* schema:
* type: string
* responses:
* "200":
* description: Inventory level
* tags:
* - Vendor Inventory Items
* security:
* - api_token: []
* - cookie_auth: []
*/

export const DELETE = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse<VendorInventoryLevelDeleteResponse>
) => {
const { id, location_id } = req.params

const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)

const result = await query.graph({
entity: "inventory_level",
filters: { inventory_item_id: id, location_id },
fields: ["id", "reserved_quantity"],
})

if (!result.data.length) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap in try catch (not sure if needed) and add to query.graph

{ throwIfKeyNotFound: true }

throw new MedusaError(
MedusaError.Types.NOT_FOUND,
`Inventory Level for Item ${id} at Location ${location_id} not found`
)
}

const { id: levelId, reserved_quantity: reservedQuantity } = result.data[0]

if (reservedQuantity > 0) {
throw new MedusaError(
MedusaError.Types.NOT_ALLOWED,
`Cannot remove Inventory Level ${id} at Location ${location_id} because there are reservations at location`
)
}

const deleteInventoryLevelWorkflow = deleteInventoryLevelsWorkflow(req.scope)

await deleteInventoryLevelWorkflow.run({
input: {
id: [levelId],
force: true,
},
})

const inventoryItem = await refetchInventoryItem(
id,
req.scope,
req.queryConfig.fields
)

res.status(200).json({
id: levelId,
object: "inventory-level",
deleted: true,
parent: inventoryItem,
})
}


Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const POST = async (
...u,
inventory_item_id: id
})) ?? [],
force: req.validatedBody.force ?? false
force: req.validatedBody.force ?? true
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,15 @@ export const vendorInventoryItemsMiddlewares: MiddlewareRoute[] = [
})
)
]
}
},
{
method: ["DELETE"],
matcher: "/vendor/inventory-items/:id/location-levels/:location_id",
middlewares: [
validateAndTransformQuery(
VendorGetInventoryItemsParams,
vendorInventoryLevelQueryConfig.retrieve
),
],
},
]
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { z } from 'zod'

import { applyAndAndOrOperators } from '@medusajs/medusa/api/utils/common-validators/common'
import { createFindParams } from '@medusajs/medusa/api/utils/validators'
import { HttpTypes } from '@medusajs/framework/types'

export const VendorGetInventoryItemsParamsFields = z.object({
q: z.string().optional(),
Expand Down Expand Up @@ -240,3 +241,5 @@ export const VendorBatchInventoryItemLocationsLevel = z.object({
delete: z.array(z.string()).optional(),
force: z.boolean().optional()
})

export type VendorInventoryLevelDeleteResponse = HttpTypes.AdminInventoryLevelDeleteResponse