feat(medusa,stock-location,inventory): Integration tests (#3149)
This commit is contained in:
1 parent
71fdd28198
commit
923ccece24
17 files changed
+2019
-32
No files matched your search
@@ -223,8 +223,12 @@ export default class InventoryLevelService extends TransactionBaseService {
|
||||
*/
|
||||
async getStockedQuantity(
|
||||
inventoryItemId: string,
|
||||
locationIds: string[]
|
||||
locationIds: string[] | string
|
||||
): Promise<number> {
|
||||
if (!Array.isArray(locationIds)) {
|
||||
locationIds = [locationIds]
|
||||
}
|
||||
|
||||
const manager = this.getManager()
|
||||
const levelRepository = manager.getRepository(InventoryLevel)
|
||||
|
||||
@@ -235,7 +239,7 @@ export default class InventoryLevelService extends TransactionBaseService {
|
||||
.andWhere("location_id IN (:...locationIds)", { locationIds })
|
||||
.getRawOne()
|
||||
|
||||
return result.quantity
|
||||
return parseFloat(result.quantity)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -246,8 +250,12 @@ export default class InventoryLevelService extends TransactionBaseService {
|
||||
*/
|
||||
async getAvailableQuantity(
|
||||
inventoryItemId: string,
|
||||
locationIds: string[]
|
||||
locationIds: string[] | string
|
||||
): Promise<number> {
|
||||
if (!Array.isArray(locationIds)) {
|
||||
locationIds = [locationIds]
|
||||
}
|
||||
|
||||
const manager = this.getManager()
|
||||
const levelRepository = manager.getRepository(InventoryLevel)
|
||||
|
||||
@@ -258,7 +266,7 @@ export default class InventoryLevelService extends TransactionBaseService {
|
||||
.andWhere("location_id IN (:...locationIds)", { locationIds })
|
||||
.getRawOne()
|
||||
|
||||
return result.quantity
|
||||
return parseFloat(result.quantity)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -269,8 +277,12 @@ export default class InventoryLevelService extends TransactionBaseService {
|
||||
*/
|
||||
async getReservedQuantity(
|
||||
inventoryItemId: string,
|
||||
locationIds: string[]
|
||||
locationIds: string[] | string
|
||||
): Promise<number> {
|
||||
if (!Array.isArray(locationIds)) {
|
||||
locationIds = [locationIds]
|
||||
}
|
||||
|
||||
const manager = this.getManager()
|
||||
const levelRepository = manager.getRepository(InventoryLevel)
|
||||
|
||||
@@ -281,6 +293,6 @@ export default class InventoryLevelService extends TransactionBaseService {
|
||||
.andWhere("location_id IN (:...locationIds)", { locationIds })
|
||||
.getRawOne()
|
||||
|
||||
return result.quantity
|
||||
return parseFloat(result.quantity)
|
||||
}
|
||||
}
|
||||
@@ -173,8 +173,31 @@ export default class ReservationItemService extends TransactionBaseService {
|
||||
const shouldUpdateQuantity =
|
||||
isDefined(data.quantity) && data.quantity !== item.quantity
|
||||
|
||||
const shouldUpdateLocation =
|
||||
isDefined(data.location_id) &&
|
||||
isDefined(data.quantity) &&
|
||||
data.location_id !== item.location_id
|
||||
|
||||
const ops: Promise<unknown>[] = []
|
||||
if (shouldUpdateQuantity) {
|
||||
|
||||
if (shouldUpdateLocation) {
|
||||
ops.push(
|
||||
this.inventoryLevelService_
|
||||
.withTransaction(manager)
|
||||
.adjustReservedQuantity(
|
||||
item.inventory_item_id,
|
||||
item.location_id,
|
||||
item.quantity * -1
|
||||
),
|
||||
this.inventoryLevelService_
|
||||
.withTransaction(manager)
|
||||
.adjustReservedQuantity(
|
||||
item.inventory_item_id,
|
||||
data.location_id!,
|
||||
data.quantity!
|
||||
)
|
||||
)
|
||||
} else if (shouldUpdateQuantity) {
|
||||
const quantityDiff = data.quantity! - item.quantity
|
||||
ops.push(
|
||||
this.inventoryLevelService_
|
||||
|
||||
@@ -12,9 +12,9 @@ export const buildLevelsByInventoryItemId = (
|
||||
inventoryLevels: InventoryLevelDTO[],
|
||||
locationIds: string[]
|
||||
) => {
|
||||
const filteredLevels = inventoryLevels.filter((level) =>
|
||||
locationIds?.includes(level.location_id)
|
||||
)
|
||||
const filteredLevels = inventoryLevels.filter((level) => {
|
||||
return !locationIds.length || locationIds.includes(level.location_id)
|
||||
})
|
||||
|
||||
return filteredLevels.reduce((acc, level) => {
|
||||
acc[level.inventory_item_id] = acc[level.inventory_item_id] ?? []
|
||||
@@ -58,6 +58,7 @@ export const joinLevels = async (
|
||||
locationIds,
|
||||
inventoryService
|
||||
)
|
||||
|
||||
return inventoryItems.map((inventoryItem) => ({
|
||||
...inventoryItem,
|
||||
location_levels: levelsByItemId[inventoryItem.id] || [],
|
||||
|
||||
@@ -82,12 +82,7 @@ export const createVariantTransaction = async (
|
||||
const productVariantServiceTx = productVariantService.withTransaction(manager)
|
||||
|
||||
async function createVariant(variantInput: CreateProductVariantInput) {
|
||||
const variant = await productVariantServiceTx.create(
|
||||
productId,
|
||||
variantInput
|
||||
)
|
||||
|
||||
return { variant }
|
||||
return await productVariantServiceTx.create(productId, variantInput)
|
||||
}
|
||||
|
||||
async function removeVariant(variant: ProductVariant) {
|
||||
@@ -101,7 +96,7 @@ export const createVariantTransaction = async (
|
||||
return
|
||||
}
|
||||
|
||||
const inventoryItem = await inventoryServiceTx!.createInventoryItem({
|
||||
return await inventoryServiceTx!.createInventoryItem({
|
||||
sku: variant.sku,
|
||||
origin_country: variant.origin_country,
|
||||
hs_code: variant.hs_code,
|
||||
@@ -112,8 +107,6 @@ export const createVariantTransaction = async (
|
||||
height: variant.height,
|
||||
width: variant.width,
|
||||
})
|
||||
|
||||
return { inventoryItem }
|
||||
}
|
||||
|
||||
async function removeInventoryItem(inventoryItem: InventoryItemDTO) {
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
export const InventoryServiceMock = {
|
||||
withTransaction: function () {
|
||||
return this
|
||||
},
|
||||
|
||||
listInventoryItems: jest.fn().mockImplementation((id, config) => {
|
||||
return Promise.resolve()
|
||||
}),
|
||||
|
||||
listReservationItems: jest.fn().mockImplementation((id, config) => {
|
||||
return Promise.resolve()
|
||||
}),
|
||||
|
||||
listInventoryLevels: jest.fn().mockImplementation((id, config) => {
|
||||
return Promise.resolve()
|
||||
}),
|
||||
|
||||
retrieveInventoryItem: jest.fn().mockImplementation((id, config) => {
|
||||
return Promise.resolve()
|
||||
}),
|
||||
|
||||
retrieveInventoryLevel: jest.fn().mockImplementation((id, config) => {
|
||||
return Promise.resolve()
|
||||
}),
|
||||
|
||||
createReservationItem: jest.fn().mockImplementation((id, config) => {
|
||||
return Promise.resolve()
|
||||
}),
|
||||
|
||||
createInventoryItem: jest.fn().mockImplementation((id, config) => {
|
||||
return Promise.resolve()
|
||||
}),
|
||||
|
||||
createInventoryLevel: jest.fn().mockImplementation((id, config) => {
|
||||
return Promise.resolve()
|
||||
}),
|
||||
|
||||
updateInventoryLevel: jest.fn().mockImplementation((id, config) => {
|
||||
return Promise.resolve()
|
||||
}),
|
||||
|
||||
updateInventoryItem: jest.fn().mockImplementation((id, config) => {
|
||||
return Promise.resolve()
|
||||
}),
|
||||
|
||||
updateReservationItem: jest.fn().mockImplementation((id, config) => {
|
||||
return Promise.resolve()
|
||||
}),
|
||||
|
||||
deleteReservationItemsByLineItem: jest
|
||||
.fn()
|
||||
.mockImplementation((id, config) => {
|
||||
return Promise.resolve()
|
||||
}),
|
||||
|
||||
deleteReservationItem: jest.fn().mockImplementation((id, config) => {
|
||||
return Promise.resolve()
|
||||
}),
|
||||
|
||||
deleteInventoryItem: jest.fn().mockImplementation((id, config) => {
|
||||
return Promise.resolve()
|
||||
}),
|
||||
|
||||
deleteInventoryLevel: jest.fn().mockImplementation((id, config) => {
|
||||
return Promise.resolve()
|
||||
}),
|
||||
|
||||
adjustInventory: jest.fn().mockImplementation((id, config) => {
|
||||
return Promise.resolve()
|
||||
}),
|
||||
|
||||
confirmInventory: jest.fn().mockImplementation((id, config) => {
|
||||
return Promise.resolve()
|
||||
}),
|
||||
|
||||
retrieveAvailableQuantity: jest.fn().mockImplementation((id, config) => {
|
||||
return Promise.resolve(10)
|
||||
}),
|
||||
|
||||
retrieveStockedQuantity: jest.fn().mockImplementation((id, config) => {
|
||||
return Promise.resolve(9)
|
||||
}),
|
||||
|
||||
retrieveReservedQuantity: jest.fn().mockImplementation((id, config) => {
|
||||
return Promise.resolve(1)
|
||||
}),
|
||||
}
|
||||
|
||||
const mock = jest.fn().mockImplementation(() => {
|
||||
return InventoryServiceMock
|
||||
})
|
||||
|
||||
export default mock
|
||||
@@ -0,0 +1,46 @@
|
||||
import { IdMap } from "medusa-test-utils"
|
||||
|
||||
export const StockLocationServiceMock = {
|
||||
withTransaction: function () {
|
||||
return this
|
||||
},
|
||||
|
||||
retrieve: jest.fn().mockImplementation((id, config) => {
|
||||
return Promise.resolve({
|
||||
id: id,
|
||||
name: "stock location 1 name",
|
||||
})
|
||||
}),
|
||||
update: jest.fn().mockImplementation((id, data) => {
|
||||
return Promise.resolve({ id, ...data })
|
||||
}),
|
||||
|
||||
listAndCount: jest.fn().mockImplementation(() => {
|
||||
return Promise.resolve([
|
||||
[
|
||||
{
|
||||
id: IdMap.getId("stock_location_1"),
|
||||
name: "stock location 1 name",
|
||||
},
|
||||
],
|
||||
1,
|
||||
])
|
||||
}),
|
||||
|
||||
create: jest.fn().mockImplementation((data) => {
|
||||
return Promise.resolve({
|
||||
id: id,
|
||||
...data,
|
||||
})
|
||||
}),
|
||||
|
||||
delete: jest.fn().mockImplementation((id, config) => {
|
||||
return Promise.resolve()
|
||||
}),
|
||||
}
|
||||
|
||||
const mock = jest.fn().mockImplementation(() => {
|
||||
return StockLocationServiceMock
|
||||
})
|
||||
|
||||
export default mock
|
||||
@@ -69,11 +69,17 @@ class SalesChannelLocationService extends TransactionBaseService {
|
||||
const salesChannel = await this.salesChannelService_
|
||||
.withTransaction(manager)
|
||||
.retrieve(salesChannelId)
|
||||
const stockLocation = await this.stockLocationService.retrieve(locationId)
|
||||
|
||||
const stockLocationId = locationId
|
||||
|
||||
if (this.stockLocationService) {
|
||||
const stockLocation = await this.stockLocationService.retrieve(locationId)
|
||||
locationId = stockLocation.id
|
||||
}
|
||||
|
||||
const salesChannelLocation = manager.create(SalesChannelLocation, {
|
||||
sales_channel_id: salesChannel.id,
|
||||
location_id: stockLocation.id,
|
||||
location_id: stockLocationId,
|
||||
})
|
||||
|
||||
await manager.save(salesChannelLocation)
|
||||
|
||||
@@ -209,7 +209,6 @@ export type CreateInventoryItemInput = {
|
||||
}
|
||||
|
||||
export type CreateReservationItemInput = {
|
||||
type?: string
|
||||
line_item_id?: string
|
||||
inventory_item_id: string
|
||||
location_id: string
|
||||
|
||||
@@ -199,7 +199,7 @@ export default class StockLocationService extends TransactionBaseService {
|
||||
}
|
||||
}
|
||||
|
||||
const { metadata, ...fields } = updateData
|
||||
const { metadata, ...fields } = data
|
||||
|
||||
const toSave = locationRepo.merge(item, fields)
|
||||
if (metadata) {
|
||||
|
||||
Reference in new issue
Block a user