feat(core-flows, medusa): add update stock location endpoint to api-v2 (#6800)

* initial create

* add changeset

* redo changes for stock locatino module'

* initial update

* add changeset

* move integration tests

* update update method

* fix integration tests

* Update packages/stock-location-next/src/services/stock-location.ts

Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>

* update service

* initial fixes

* pr feedback

* update types

* expand revert clause for update

* update versioning

---------

Co-authored-by: Riqwan Thamir <rmthamir@gmail.com>
Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
Philip Korsholm
2024-03-27 10:31:17 +01:00
committed by GitHub
co-authored by Oli Juhl Riqwan Thamir
parent 0b23e7efb8
commit 4cf71af07d
13 changed files with 378 additions and 27 deletions
@@ -4,8 +4,40 @@ import {
} from "@medusajs/utils"
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
import { AdminPostStockLocationsLocationReq } from "../validators"
import { MedusaError } from "@medusajs/utils"
import { deleteStockLocationsWorkflow } from "@medusajs/core-flows"
import { updateStockLocationsWorkflow } from "@medusajs/core-flows"
export const POST = async (
req: MedusaRequest<AdminPostStockLocationsLocationReq>,
res: MedusaResponse
) => {
const { id } = req.params
await updateStockLocationsWorkflow(req.scope).run({
input: {
selector: { id: req.params.id },
update: req.validatedBody,
},
})
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
const [stock_location] = await remoteQuery(
remoteQueryObjectFromString({
entryPoint: "stock_locations",
variables: {
id,
},
fields: req.remoteQueryConfig.fields,
})
)
res.status(200).json({
stock_location,
})
}
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
const { id } = req.params
@@ -2,6 +2,8 @@ import * as QueryConfig from "./query-config"
import {
AdminGetStockLocationsLocationParams,
AdminPostStockLocationsLocationParams,
AdminPostStockLocationsLocationReq,
AdminPostStockLocationsParams,
AdminPostStockLocationsReq,
} from "./validators"
@@ -27,6 +29,17 @@ export const adminStockLocationRoutesMiddlewares: MiddlewareRoute[] = [
),
],
},
{
method: ["POST"],
matcher: "/admin/stock-locations/:id",
middlewares: [
transformBody(AdminPostStockLocationsLocationReq),
transformQuery(
AdminPostStockLocationsLocationParams,
QueryConfig.retrieveTransformQueryConfig
),
],
},
{
method: ["GET"],
matcher: "/admin/stock-locations/:id",
@@ -60,7 +60,7 @@ import { IsType } from "../../../utils"
* description: Stock location address' province
* example: Sinaloa
*/
class StockLocationAddress {
class StockLocationCreateAddress {
@IsString()
address_1: string
@@ -124,8 +124,8 @@ export class AdminPostStockLocationsReq {
@IsOptional()
@ValidateNested()
@Type(() => StockLocationAddress)
address?: StockLocationAddress
@Type(() => StockLocationCreateAddress)
address?: StockLocationCreateAddress
@IsOptional()
@IsString()
@@ -138,4 +138,106 @@ export class AdminPostStockLocationsReq {
export class AdminPostStockLocationsParams extends FindParams {}
/**
* The attributes of a stock location address to create or update.
*/
class StockLocationUpdateAddress {
/**
* First line address.
*/
@IsString()
address_1: string
/**
* Second line address.
*/
@IsOptional()
@IsString()
address_2?: string
/**
* Company.
*/
@IsOptional()
@IsString()
company?: string
/**
* City.
*/
@IsOptional()
@IsString()
city?: string
/**
* Country code.
*/
@IsString()
country_code: string
/**
* Phone.
*/
@IsOptional()
@IsString()
phone?: string
/**
* Postal code.
*/
@IsOptional()
@IsString()
postal_code?: string
/**
* Province.
*/
@IsOptional()
@IsString()
province?: string
}
/**
* @schema AdminPostStockLocationsLocationReq
* type: object
* description: "The details to update of the stock location."
* properties:
* name:
* description: the name of the stock location
* type: string
* address_id:
* description: the stock location address ID
* type: string
* metadata:
* type: object
* description: An optional key-value map with additional details
* example: {car: "white"}
* externalDocs:
* description: "Learn about the metadata attribute, and how to delete and update it."
* url: "https://docs.medusajs.com/development/entities/overview#metadata-attribute"
* address:
* description: The data of an associated address to create or update.
* $ref: "#/components/schemas/StockLocationAddressInput"
*/
export class AdminPostStockLocationsLocationReq {
@IsOptional()
@IsString()
name?: string
@IsOptional()
@ValidateNested()
@Type(() => StockLocationUpdateAddress)
address?: StockLocationUpdateAddress
@IsOptional()
@IsString()
address_id?: string
@IsObject()
@IsOptional()
metadata?: Record<string, unknown>
}
export class AdminPostStockLocationsLocationParams extends FindParams {}
export class AdminGetStockLocationsLocationParams extends FindParams {}