Skip to content

Commit

Permalink
feat(medusa, core-flows): add retrieve stock location endpoint to api…
Browse files Browse the repository at this point in the history
…-v2 (#6791)

* initial create

* add changeset

* redo changes for stock locatino module'

* add get endpoint

* add changeset

* add exception if location is not found

* remove only

* initial delete stock location

* add changeset

* add changeset

* pr prep

* remote duplicate changeset

* propagate deletion with common step

* move integration tests

---------

Co-authored-by: Riqwan Thamir <[email protected]>
  • Loading branch information
pKorsholm and riqwan authored Mar 25, 2024
1 parent deab12e commit 20132d7
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/yellow-moose-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@medusajs/core-flows": patch
"@medusajs/medusa": patch
---

feat(medusa, core-flows): add retrieve stock location endpoint to api-v2
41 changes: 41 additions & 0 deletions integration-tests/api/__tests__/admin/stock-location/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
29 changes: 29 additions & 0 deletions packages/medusa/src/api-v2/admin/stock-locations/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -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

Expand Down
11 changes: 11 additions & 0 deletions packages/medusa/src/api-v2/admin/stock-locations/middlewares.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as QueryConfig from "./query-config"

import {
AdminGetStockLocationsLocationParams,
AdminPostStockLocationsParams,
AdminPostStockLocationsReq,
} from "./validators"
Expand All @@ -26,4 +27,14 @@ export const adminStockLocationRoutesMiddlewares: MiddlewareRoute[] = [
),
],
},
{
method: ["GET"],
matcher: "/admin/stock-locations/:id",
middlewares: [
transformQuery(
AdminGetStockLocationsLocationParams,
QueryConfig.retrieveTransformQueryConfig
),
],
},
]
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,5 @@ export class AdminPostStockLocationsReq {
}

export class AdminPostStockLocationsParams extends FindParams {}

export class AdminGetStockLocationsLocationParams extends FindParams {}

0 comments on commit 20132d7

Please sign in to comment.