diff --git a/.changeset/yellow-moose-laugh.md b/.changeset/yellow-moose-laugh.md new file mode 100644 index 0000000000000..52c6c560ce615 --- /dev/null +++ b/.changeset/yellow-moose-laugh.md @@ -0,0 +1,6 @@ +--- +"@medusajs/core-flows": patch +"@medusajs/medusa": patch +--- + +feat(medusa, core-flows): add retrieve stock location endpoint to api-v2 diff --git a/integration-tests/api/__tests__/admin/stock-location/index.spec.ts b/integration-tests/api/__tests__/admin/stock-location/index.spec.ts index 04b74876a65ee..c97a8fc7f38de 100644 --- a/integration-tests/api/__tests__/admin/stock-location/index.spec.ts +++ b/integration-tests/api/__tests__/admin/stock-location/index.spec.ts @@ -56,6 +56,47 @@ medusaIntegrationTestRunner({ }) }) + describe("Get stock location", () => { + let locationId + const location = { + name: "Test Location", + } + beforeEach(async () => { + const createLocationRespones = await api.post( + "/admin/stock-locations", + { + ...location, + }, + adminHeaders + ) + locationId = createLocationRespones.data.stock_location.id + }) + + it("should get a stock location", async () => { + const response = await api.get( + `/admin/stock-locations/${locationId}`, + adminHeaders + ) + + expect(response.status).toEqual(200) + expect(response.data.stock_location).toEqual( + expect.objectContaining({ id: locationId, ...location }) + ) + }) + + it("should get a stock location", async () => { + let error + await api + .get(`/admin/stock-locations/does-not-exist`, adminHeaders) + .catch((e) => (error = e)) + + expect(error.response.status).toEqual(404) + expect(error.response.data.message).toEqual( + `Stock location with id: does-not-exist was not found` + ) + }) + }) + describe("Delete stock location", () => { let stockLocationId beforeEach(async () => { diff --git a/packages/medusa/src/api-v2/admin/stock-locations/[id]/route.ts b/packages/medusa/src/api-v2/admin/stock-locations/[id]/route.ts index bf559e8cff1d8..678d4b86da9fa 100644 --- a/packages/medusa/src/api-v2/admin/stock-locations/[id]/route.ts +++ b/packages/medusa/src/api-v2/admin/stock-locations/[id]/route.ts @@ -1,7 +1,36 @@ +import { + ContainerRegistrationKeys, + remoteQueryObjectFromString, +} from "@medusajs/utils" import { MedusaRequest, MedusaResponse } from "../../../../types/routing" +import { MedusaError } from "@medusajs/utils" import { deleteStockLocationsWorkflow } from "@medusajs/core-flows" +export const GET = async (req: MedusaRequest, res: MedusaResponse) => { + const { id } = req.params + const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY) + + const [stock_location] = await remoteQuery( + remoteQueryObjectFromString({ + entryPoint: "stock_locations", + variables: { + id, + }, + fields: req.remoteQueryConfig.fields, + }) + ) + + if (!stock_location) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `Stock location with id: ${id} was not found` + ) + } + + res.status(200).json({ stock_location }) +} + export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => { const { id } = req.params diff --git a/packages/medusa/src/api-v2/admin/stock-locations/middlewares.ts b/packages/medusa/src/api-v2/admin/stock-locations/middlewares.ts index 6c03ade1728e2..6e0f77fae9662 100644 --- a/packages/medusa/src/api-v2/admin/stock-locations/middlewares.ts +++ b/packages/medusa/src/api-v2/admin/stock-locations/middlewares.ts @@ -1,6 +1,7 @@ import * as QueryConfig from "./query-config" import { + AdminGetStockLocationsLocationParams, AdminPostStockLocationsParams, AdminPostStockLocationsReq, } from "./validators" @@ -26,4 +27,14 @@ export const adminStockLocationRoutesMiddlewares: MiddlewareRoute[] = [ ), ], }, + { + method: ["GET"], + matcher: "/admin/stock-locations/:id", + middlewares: [ + transformQuery( + AdminGetStockLocationsLocationParams, + QueryConfig.retrieveTransformQueryConfig + ), + ], + }, ] diff --git a/packages/medusa/src/api-v2/admin/stock-locations/validators.ts b/packages/medusa/src/api-v2/admin/stock-locations/validators.ts index f5d757e82fa3a..5cfeb83a55687 100644 --- a/packages/medusa/src/api-v2/admin/stock-locations/validators.ts +++ b/packages/medusa/src/api-v2/admin/stock-locations/validators.ts @@ -137,3 +137,5 @@ export class AdminPostStockLocationsReq { } export class AdminPostStockLocationsParams extends FindParams {} + +export class AdminGetStockLocationsLocationParams extends FindParams {}