feat(core-flows, types, medusa): Add Update location level endpoint for api-v2 (#6743)
* initialize update-location-level * update middlewares * readd middleware * pr feedback
This commit is contained in:
@@ -5,7 +5,9 @@ import {
|
||||
} from "@medusajs/utils"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../../../types/routing"
|
||||
|
||||
import { AdminPostInventoryItemsItemLocationLevelsLevelReq } from "../../../validators"
|
||||
import { deleteInventoryLevelsWorkflow } from "@medusajs/core-flows"
|
||||
import { updateInventoryLevelsWorkflow } from "@medusajs/core-flows"
|
||||
|
||||
export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const { id, location_id } = req.params
|
||||
@@ -45,3 +47,36 @@ export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
deleted: true,
|
||||
})
|
||||
}
|
||||
|
||||
export const POST = async (
|
||||
req: MedusaRequest<AdminPostInventoryItemsItemLocationLevelsLevelReq>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const { id: inventory_item_id, location_id } = req.params
|
||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
|
||||
const { errors } = await updateInventoryLevelsWorkflow(req.scope).run({
|
||||
input: {
|
||||
updates: [{ inventory_item_id, location_id, ...req.validatedBody }],
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const [inventory_item] = await remoteQuery(
|
||||
remoteQueryObjectFromString({
|
||||
entryPoint: "inventory",
|
||||
variables: {
|
||||
id: inventory_item_id,
|
||||
},
|
||||
fields: req.remoteQueryConfig.fields,
|
||||
})
|
||||
)
|
||||
|
||||
res.status(200).json({
|
||||
inventory_item,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import {
|
||||
AdminGetInventoryItemsParams,
|
||||
AdminPostInventoryItemsInventoryItemParams,
|
||||
AdminPostInventoryItemsInventoryItemReq,
|
||||
AdminPostInventoryItemsItemLocationLevelsLevelParams,
|
||||
AdminPostInventoryItemsItemLocationLevelsLevelReq,
|
||||
AdminPostInventoryItemsItemLocationLevelsReq,
|
||||
AdminPostInventoryItemsReq,
|
||||
} from "./validators"
|
||||
@@ -39,11 +41,6 @@ export const adminInventoryRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/inventory-items/:id/location-levels",
|
||||
middlewares: [transformBody(AdminPostInventoryItemsItemLocationLevelsReq)],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/inventory-items",
|
||||
@@ -55,6 +52,22 @@ export const adminInventoryRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/inventory-items/:id/location-levels",
|
||||
middlewares: [transformBody(AdminPostInventoryItemsItemLocationLevelsReq)],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/inventory-items/:id/location-levels/:location_id",
|
||||
middlewares: [
|
||||
transformBody(AdminPostInventoryItemsItemLocationLevelsLevelReq),
|
||||
transformQuery(
|
||||
AdminPostInventoryItemsItemLocationLevelsLevelParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/inventory-items/:id",
|
||||
|
||||
@@ -45,6 +45,17 @@ export const retrieveTransformQueryConfig = {
|
||||
isList: false,
|
||||
}
|
||||
|
||||
export const retrieveLocationLevelsTransformQueryConfig = {
|
||||
defaults: defaultAdminLocationLevelFields,
|
||||
allowed: defaultAdminLocationLevelFields,
|
||||
isList: false,
|
||||
}
|
||||
|
||||
export const listLocationLevelsTransformQueryConfig = {
|
||||
...retrieveLocationLevelsTransformQueryConfig,
|
||||
isList: true,
|
||||
}
|
||||
|
||||
export const listTransformQueryConfig = {
|
||||
...retrieveTransformQueryConfig,
|
||||
isList: true,
|
||||
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
IsObject,
|
||||
IsOptional,
|
||||
IsString,
|
||||
Min,
|
||||
ValidateNested,
|
||||
} from "class-validator"
|
||||
import { Transform, Type } from "class-transformer"
|
||||
@@ -258,6 +259,31 @@ export class AdminPostInventoryItemsReq {
|
||||
metadata?: Record<string, unknown>
|
||||
}
|
||||
|
||||
/**
|
||||
* @schema AdminPostInventoryItemsItemLocationLevelsLevelReq
|
||||
* type: object
|
||||
* properties:
|
||||
* stocked_quantity:
|
||||
* description: the total stock quantity of an inventory item at the given location ID
|
||||
* type: number
|
||||
* incoming_quantity:
|
||||
* description: the incoming stock quantity of an inventory item at the given location ID
|
||||
* type: number
|
||||
*/
|
||||
export class AdminPostInventoryItemsItemLocationLevelsLevelReq {
|
||||
@IsOptional()
|
||||
@IsNumber()
|
||||
@Min(0)
|
||||
incoming_quantity?: number
|
||||
|
||||
@IsOptional()
|
||||
@IsNumber()
|
||||
@Min(0)
|
||||
stocked_quantity?: number
|
||||
}
|
||||
|
||||
// eslint-disable-next-line
|
||||
export class AdminPostInventoryItemsItemLocationLevelsLevelParams extends FindParams {}
|
||||
/**
|
||||
* @schema AdminPostInventoryItemsInventoryItemReq
|
||||
* type: object
|
||||
|
||||
Reference in New Issue
Block a user