feat(medusa, stock-location, types): Add q and order params to list-stock-locations (#6197)

**What**
- add `q` and `order` params to list-stock-location endpoint

closes #6189
This commit is contained in:
Philip Korsholm authored and GitHub committed 2024-01-29 09:14:39 +00:00
1 parent 3db2f95e65
commit d1c18a3090
9 files changed
+263 -33

No files matched your search

@@ -0,0 +1,88 @@
const path = require("path")
const {
startBootstrapApp,
} = require("../../../../environment-helpers/bootstrap-app")
const { initDb, useDb } = require("../../../../environment-helpers/use-db")
const {
useApi,
useExpressServer,
} = require("../../../../environment-helpers/use-api")
const adminSeeder = require("../../../../helpers/admin-seeder")
const {
getContainer,
} = require("../../../../environment-helpers/use-container")
jest.setTimeout(30000)
describe("Sales channels", () => {
let appContainer
let dbConnection
let shutdownServer
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd })
shutdownServer = await startBootstrapApp({ cwd })
appContainer = getContainer()
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
afterEach(async () => {
jest.clearAllMocks()
const db = useDb()
return await db.teardown()
})
describe("Stock Locations", () => {
it("When deleting a sales channel, removes all associated locations with it", async () => {
await adminSeeder(dbConnection)
const api = useApi()
const stockLocationService = appContainer.resolve("stockLocationService")
const salesChannelService = appContainer.resolve("salesChannelService")
const salesChannelLocationService = appContainer.resolve(
"salesChannelLocationService"
)
const loc = await stockLocationService.create({
name: "warehouse",
})
const loc2 = await stockLocationService.create({
name: "other place",
})
const sc = await salesChannelService.create({ name: "channel test" })
await salesChannelLocationService.associateLocation(sc.id, loc.id)
await salesChannelLocationService.associateLocation(sc.id, loc2.id)
expect(await salesChannelService.retrieve(sc.id)).toEqual(
expect.objectContaining({
id: sc.id,
name: "channel test",
})
)
expect(
await salesChannelLocationService.listLocationIds(sc.id)
).toHaveLength(2)
await api.delete(`/admin/sales-channels/${sc.id}`, {
headers: { "x-medusa-access-token": "test_token" },
})
await expect(salesChannelService.retrieve(sc.id)).rejects.toThrowError()
await expect(
salesChannelLocationService.listLocationIds(sc.id)
).rejects.toThrowError()
})
})
})
@@ -0,0 +1,96 @@
const path = require("path")
const {
startBootstrapApp,
} = require("../../../../environment-helpers/bootstrap-app")
const { initDb, useDb } = require("../../../../environment-helpers/use-db")
const {
useApi,
useExpressServer,
} = require("../../../../environment-helpers/use-api")
const adminSeeder = require("../../../../helpers/admin-seeder")
const {
getContainer,
} = require("../../../../environment-helpers/use-container")
jest.setTimeout(30000)
describe("Sales channels", () => {
let appContainer
let dbConnection
let shutdownServer
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd })
shutdownServer = await startBootstrapApp({ cwd })
appContainer = getContainer()
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
afterEach(async () => {
jest.clearAllMocks()
const db = useDb()
return await db.teardown()
})
describe("Stock Locations", () => {
it("When deleting a stock location, removes all associated sales channels with it", async () => {
await adminSeeder(dbConnection)
const api = useApi()
const stockLocationService = appContainer.resolve("stockLocationService")
const salesChannelService = appContainer.resolve("salesChannelService")
const salesChannelLocationService = appContainer.resolve(
"salesChannelLocationService"
)
const loc = await stockLocationService.create({
name: "warehouse",
})
const saleChannel = await salesChannelService.create({
name: "channel test",
})
const otherChannel = await salesChannelService.create({
name: "yet another channel",
})
await salesChannelLocationService.associateLocation(
saleChannel.id,
loc.id
)
await salesChannelLocationService.associateLocation(
otherChannel.id,
loc.id
)
expect(
await salesChannelLocationService.listLocationIds(saleChannel.id)
).toHaveLength(1)
expect(
await salesChannelLocationService.listLocationIds(otherChannel.id)
).toHaveLength(1)
await api.delete(`/admin/stock-locations/${loc.id}`, {
headers: { "x-medusa-access-token": "test_token" },
})
expect(
await salesChannelLocationService.listLocationIds(saleChannel.id)
).toHaveLength(0)
expect(
await salesChannelLocationService.listLocationIds(otherChannel.id)
).toHaveLength(0)
})
})
})
@@ -0,0 +1,162 @@
const path = require("path")
const {
startBootstrapApp,
} = require("../../../../environment-helpers/bootstrap-app")
const { initDb, useDb } = require("../../../../environment-helpers/use-db")
const {
useApi,
useExpressServer,
} = require("../../../../environment-helpers/use-api")
const adminSeeder = require("../../../../helpers/admin-seeder")
const {
getContainer,
} = require("../../../../environment-helpers/use-container")
jest.setTimeout(30000)
const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } }
describe("Sales channels", () => {
let appContainer
let dbConnection
let shutdownServer
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd })
shutdownServer = await startBootstrapApp({ cwd })
appContainer = getContainer()
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
afterEach(async () => {
jest.clearAllMocks()
const db = useDb()
return await db.teardown()
})
describe("Stock Locations", () => {
describe("CORE", () => {
it("When listing a sales channel, it brings all associated locations with it", async () => {
await adminSeeder(dbConnection)
const stockLocationService = appContainer.resolve(
"stockLocationService"
)
const salesChannelService = appContainer.resolve("salesChannelService")
const salesChannelLocationService = appContainer.resolve(
"salesChannelLocationService"
)
const loc = await stockLocationService.create({
name: "warehouse",
})
const loc2 = await stockLocationService.create({
name: "other place",
})
const sc = await salesChannelService.create({ name: "channel test" })
await salesChannelLocationService.associateLocation(sc.id, loc.id)
await salesChannelLocationService.associateLocation(sc.id, loc2.id)
expect(
await salesChannelLocationService.listLocationIds(sc.id)
).toHaveLength(2)
const [channels] = await salesChannelService.listAndCount(
{},
{
relations: ["locations"],
}
)
const createdSC = channels.find((c) => c.id === sc.id)
expect(channels).toHaveLength(2)
expect(createdSC.locations).toHaveLength(2)
expect(createdSC).toEqual(
expect.objectContaining({
id: sc.id,
name: "channel test",
locations: expect.arrayContaining([
expect.objectContaining({
sales_channel_id: sc.id,
location_id: loc.id,
}),
expect.objectContaining({
sales_channel_id: sc.id,
location_id: loc2.id,
}),
]),
})
)
})
})
describe("API", () => {
it("Filters stock locations based on sales channel ids", async () => {
const api = useApi()
await adminSeeder(dbConnection)
const stockLocationService = appContainer.resolve(
"stockLocationService"
)
const salesChannelService = appContainer.resolve("salesChannelService")
const salesChannelLocationService = appContainer.resolve(
"salesChannelLocationService"
)
const loc = await stockLocationService.create({
name: "warehouse",
})
const loc2 = await stockLocationService.create({
name: "other place",
})
const sc = await salesChannelService.create({ name: "Default Channel" })
const sc2 = await salesChannelService.create({ name: "Physical store" })
await salesChannelLocationService.associateLocation(sc.id, loc.id)
await salesChannelLocationService.associateLocation(sc.id, loc2.id)
await salesChannelLocationService.associateLocation(sc2.id, loc2.id)
const defaultSalesChannelFilterRes = await api.get(
`/admin/stock-locations?sales_channel_id=${sc.id}`,
adminHeaders
)
expect(defaultSalesChannelFilterRes.data.stock_locations).toHaveLength(
2
)
expect(defaultSalesChannelFilterRes.data.stock_locations).toEqual(
expect.arrayContaining([
expect.objectContaining({ name: "warehouse" }),
expect.objectContaining({ name: "other place" }),
])
)
const physicalStoreSalesChannelFilterRes = await api.get(
`/admin/stock-locations?sales_channel_id=${sc2.id}`,
adminHeaders
)
expect(
physicalStoreSalesChannelFilterRes.data.stock_locations
).toHaveLength(1)
expect(physicalStoreSalesChannelFilterRes.data.stock_locations).toEqual(
expect.arrayContaining([
expect.objectContaining({ name: "other place" }),
])
)
})
})
})
})
@@ -0,0 +1,273 @@
const path = require("path")
const {
startBootstrapApp,
} = require("../../../../environment-helpers/bootstrap-app")
const { initDb, useDb } = require("../../../../environment-helpers/use-db")
const {
getContainer,
} = require("../../../../environment-helpers/use-container")
const { useExpressServer } = require("../../../../environment-helpers/use-api")
jest.setTimeout(30000)
describe("Stock Location Module", () => {
let appContainer
let dbConnection
let shutdownServer
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd })
shutdownServer = await startBootstrapApp({ cwd })
appContainer = getContainer()
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
afterEach(async () => {
const db = useDb()
return await db.teardown()
})
describe("Stock Location Module Interface", () => {
it("create", async () => {
const stockLocationService = appContainer.resolve("stockLocationService")
expect(
await stockLocationService.create({
name: "first location",
})
).toEqual(
expect.objectContaining({
id: expect.any(String),
name: "first location",
deleted_at: null,
address_id: null,
metadata: null,
created_at: expect.any(Date),
updated_at: expect.any(Date),
})
)
expect(
await stockLocationService.create({
name: "second location",
metadata: {
extra: "abc",
},
address: {
address_1: "addr_1",
address_2: "line 2",
country_code: "DK",
city: "city",
phone: "111222333",
province: "province",
postal_code: "555-714",
metadata: {
abc: 123,
},
},
})
).toEqual(
expect.objectContaining({
id: expect.any(String),
name: "second location",
metadata: {
extra: "abc",
},
address_id: expect.any(String),
})
)
})
it("update", async () => {
const stockLocationService = appContainer.resolve("stockLocationService")
const loc = await stockLocationService.create({
name: "location",
address: {
address_1: "addr_1",
address_2: "line 2",
country_code: "DK",
city: "city",
phone: "111222333",
province: "province",
postal_code: "555-714",
metadata: {
abc: 123,
},
},
})
const addressId = loc.address_id
expect(
await stockLocationService.retrieve(loc.id, {
relations: ["address"],
})
).toEqual(
expect.objectContaining({
id: loc.id,
created_at: expect.any(Date),
updated_at: expect.any(Date),
deleted_at: null,
name: "location",
address_id: addressId,
metadata: null,
address: expect.objectContaining({
id: addressId,
created_at: expect.any(Date),
updated_at: expect.any(Date),
deleted_at: null,
address_1: "addr_1",
address_2: "line 2",
company: null,
city: "city",
country_code: "DK",
phone: "111222333",
province: "province",
postal_code: "555-714",
metadata: { abc: 123 },
}),
})
)
expect(
await stockLocationService.update(loc.id, {
name: "location name",
address_id: addressId,
address: {
address_1: "addr_1 updated",
country_code: "US",
},
})
).toEqual(
expect.objectContaining({
id: loc.id,
name: "location name",
address_id: addressId,
})
)
expect(
await stockLocationService.retrieve(loc.id, {
relations: ["address"],
})
).toEqual(
expect.objectContaining({
id: loc.id,
created_at: expect.any(Date),
updated_at: expect.any(Date),
deleted_at: null,
name: "location name",
address_id: addressId,
metadata: null,
address: expect.objectContaining({
id: addressId,
created_at: expect.any(Date),
updated_at: expect.any(Date),
deleted_at: null,
address_1: "addr_1 updated",
address_2: "line 2",
company: null,
city: "city",
country_code: "US",
phone: "111222333",
province: "province",
postal_code: "555-714",
metadata: { abc: 123 },
}),
})
)
})
it("updateAddress", async () => {
const stockLocationService = appContainer.resolve("stockLocationService")
const loc = await stockLocationService.create({
name: "location",
address: {
address_1: "addr_1",
address_2: "line 2",
country_code: "DK",
city: "city",
phone: "111222333",
province: "province",
postal_code: "555-714",
metadata: {
abc: 123,
},
},
})
const addressId = loc.address_id
expect(
await stockLocationService.updateAddress(addressId, {
address_1: "addr_1 updated",
country_code: "US",
})
).toEqual(
expect.objectContaining({
id: addressId,
address_1: "addr_1 updated",
address_2: "line 2",
country_code: "US",
city: "city",
phone: "111222333",
province: "province",
postal_code: "555-714",
metadata: {
abc: 123,
},
})
)
expect(
await stockLocationService.retrieve(loc.id, {
relations: ["address"],
})
).toEqual(
expect.objectContaining({
id: loc.id,
address_id: addressId,
address: expect.objectContaining({
id: addressId,
address_1: "addr_1 updated",
}),
})
)
})
it("delete", async () => {
const stockLocationService = appContainer.resolve("stockLocationService")
const loc = await stockLocationService.create({
name: "location",
address: {
address_1: "addr_1",
address_2: "line 2",
country_code: "DK",
city: "city",
phone: "111222333",
province: "province",
postal_code: "555-714",
metadata: {
abc: 123,
},
},
})
await stockLocationService.delete(loc.id)
const deletedItem = stockLocationService.retrieve(loc.id)
await expect(deletedItem).rejects.toThrow(
`StockLocation with id ${loc.id} was not found`
)
})
})
})