feat(medusa, inventory, stock-location): Remove unnecessary transaction usage in the modules and the list product end points (#4232)

This commit is contained in:
Adrien de Peretti
2023-06-05 12:11:12 +02:00
committed by GitHub
parent d76ba0cd29
commit af2dc4f75a
10 changed files with 279 additions and 259 deletions

View File

@@ -40,54 +40,34 @@ export default class InventoryItemService {
/**
* @param selector - Filter options for inventory items.
* @param config - Configuration for query.
* @param context
* @return Resolves to the list of inventory items that match the filter.
*/
@InjectEntityManager()
async list(
selector: FilterableInventoryItemProps = {},
config: FindConfig<InventoryItem> = { relations: [], skip: 0, take: 10 },
@MedusaContext() context: SharedContext = {}
context: SharedContext = {}
): Promise<InventoryItemDTO[]> {
const queryBuilder = getListQuery(
context.transactionManager!,
context.transactionManager ?? this.manager_,
selector,
config
)
return await queryBuilder.getMany()
}
/**
* @param selector - Filter options for inventory items.
* @param config - Configuration for query.
* @return - Resolves to the list of inventory items that match the filter and the count of all matching items.
*/
@InjectEntityManager()
async listAndCount(
selector: FilterableInventoryItemProps = {},
config: FindConfig<InventoryItem> = { relations: [], skip: 0, take: 10 },
@MedusaContext() context: SharedContext = {}
): Promise<[InventoryItemDTO[], number]> {
const queryBuilder = getListQuery(
context.transactionManager!,
selector,
config
)
return await queryBuilder.getManyAndCount()
}
/**
* Retrieves an inventory item by its id.
* @param inventoryItemId - the id of the inventory item to retrieve.
* @param config - the configuration options for the find operation.
* @param context
* @return The retrieved inventory item.
* @throws If the inventory item id is not defined or if the inventory item is not found.
*/
@InjectEntityManager()
async retrieve(
inventoryItemId: string,
config: FindConfig<InventoryItem> = {},
@MedusaContext() context: SharedContext = {}
context: SharedContext = {}
): Promise<InventoryItem> {
if (!isDefined(inventoryItemId)) {
throw new MedusaError(
@@ -96,7 +76,7 @@ export default class InventoryItemService {
)
}
const manager = context.transactionManager!
const manager = context.transactionManager ?? this.manager_
const itemRepository = manager.getRepository(InventoryItem)
const query = buildQuery({ id: inventoryItemId }, config) as FindManyOptions
@@ -113,8 +93,30 @@ export default class InventoryItemService {
}
/**
* @param input - Input for creating a new inventory item.
* @return The newly created inventory item.
* @param selector - Filter options for inventory items.
* @param config - Configuration for query.
* @param context
* @return - Resolves to the list of inventory items that match the filter and the count of all matching items.
*/
async listAndCount(
selector: FilterableInventoryItemProps = {},
config: FindConfig<InventoryItem> = { relations: [], skip: 0, take: 10 },
context: SharedContext = {}
): Promise<[InventoryItemDTO[], number]> {
const queryBuilder = getListQuery(
context.transactionManager ?? this.manager_,
selector,
config
)
return await queryBuilder.getManyAndCount()
}
/**
* @param data
* @param context
* @param data
* @param context
*/
@InjectEntityManager()
async create(
@@ -152,7 +154,9 @@ export default class InventoryItemService {
/**
* @param inventoryItemId - The id of the inventory item to update.
* @param update - The updates to apply to the inventory item.
* @param data
* @param context
* @param context
* @return The updated inventory item.
*/
@InjectEntityManager()
@@ -187,6 +191,7 @@ export default class InventoryItemService {
/**
* @param inventoryItemId - The id of the inventory item to delete.
* @param context
*/
@InjectEntityManager()
async delete(

View File

@@ -39,15 +39,15 @@ export default class InventoryLevelService {
* Retrieves a list of inventory levels based on the provided selector and configuration.
* @param selector - An object containing filterable properties for inventory levels.
* @param config - An object containing configuration options for the query.
* @param context
* @return Array of inventory levels.
*/
@InjectEntityManager()
async list(
selector: FilterableInventoryLevelProps = {},
config: FindConfig<InventoryLevel> = { relations: [], skip: 0, take: 10 },
@MedusaContext() context: SharedContext = {}
context: SharedContext = {}
): Promise<InventoryLevel[]> {
const manager = context.transactionManager!
const manager = context.transactionManager ?? this.manager_
const levelRepository = manager.getRepository(InventoryLevel)
const query = buildQuery(selector, config) as FindManyOptions
@@ -58,15 +58,15 @@ export default class InventoryLevelService {
* Retrieves a list of inventory levels and a count based on the provided selector and configuration.
* @param selector - An object containing filterable properties for inventory levels.
* @param config - An object containing configuration options for the query.
* @param context
* @return An array of inventory levels and a count.
*/
@InjectEntityManager()
async listAndCount(
selector: FilterableInventoryLevelProps = {},
config: FindConfig<InventoryLevel> = { relations: [], skip: 0, take: 10 },
@MedusaContext() context: SharedContext = {}
context: SharedContext = {}
): Promise<[InventoryLevel[], number]> {
const manager = context.transactionManager!
const manager = context.transactionManager ?? this.manager_
const levelRepository = manager.getRepository(InventoryLevel)
const query = buildQuery(selector, config) as FindManyOptions
@@ -77,14 +77,14 @@ export default class InventoryLevelService {
* Retrieves a single inventory level by its ID.
* @param inventoryLevelId - The ID of the inventory level to retrieve.
* @param config - An object containing configuration options for the query.
* @param context
* @return A inventory level.
* @throws If the inventory level ID is not defined or the given ID was not found.
*/
@InjectEntityManager()
async retrieve(
inventoryLevelId: string,
config: FindConfig<InventoryLevel> = {},
@MedusaContext() context: SharedContext = {}
context: SharedContext = {}
): Promise<InventoryLevel> {
if (!isDefined(inventoryLevelId)) {
throw new MedusaError(
@@ -93,7 +93,7 @@ export default class InventoryLevelService {
)
}
const manager = context.transactionManager!
const manager = context.transactionManager ?? this.manager_
const levelRepository = manager.getRepository(InventoryLevel)
const query = buildQuery(
@@ -115,6 +115,7 @@ export default class InventoryLevelService {
/**
* Creates a new inventory level.
* @param data - An object containing the properties for the new inventory level.
* @param context
* @return The created inventory level.
*/
@InjectEntityManager()
@@ -146,6 +147,7 @@ export default class InventoryLevelService {
* Updates an existing inventory level.
* @param inventoryLevelId - The ID of the inventory level to update.
* @param data - An object containing the properties to update on the inventory level.
* @param context
* @return The updated inventory level.
* @throws If the inventory level ID is not defined or the given ID was not found.
*/
@@ -187,6 +189,7 @@ export default class InventoryLevelService {
* @param inventoryItemId - The ID of the inventory item.
* @param locationId - The ID of the location.
* @param quantity - The quantity to adjust from the reserved quantity.
* @param context
*/
@InjectEntityManager()
async adjustReservedQuantity(
@@ -210,6 +213,7 @@ export default class InventoryLevelService {
/**
* Deletes inventory levels by inventory Item ID.
* @param inventoryItemId - The ID or IDs of the inventory item to delete inventory levels for.
* @param context
*/
@InjectEntityManager()
async deleteByInventoryItemId(
@@ -233,6 +237,7 @@ export default class InventoryLevelService {
/**
* Deletes an inventory level by ID.
* @param inventoryLevelId - The ID or IDs of the inventory level to delete.
* @param context
*/
@InjectEntityManager()
async delete(
@@ -256,6 +261,7 @@ export default class InventoryLevelService {
/**
* Deletes inventory levels by location ID.
* @param locationId - The ID of the location to delete inventory levels for.
* @param context
*/
@InjectEntityManager()
async deleteByLocationId(
@@ -276,19 +282,19 @@ export default class InventoryLevelService {
* Gets the total stocked quantity for a specific inventory item at multiple locations.
* @param inventoryItemId - The ID of the inventory item.
* @param locationIds - The IDs of the locations.
* @param context
* @return The total stocked quantity.
*/
@InjectEntityManager()
async getStockedQuantity(
inventoryItemId: string,
locationIds: string[] | string,
@MedusaContext() context: SharedContext = {}
context: SharedContext = {}
): Promise<number> {
if (!Array.isArray(locationIds)) {
locationIds = [locationIds]
}
const manager = context.transactionManager!
const manager = context.transactionManager ?? this.manager_
const levelRepository = manager.getRepository(InventoryLevel)
const result = await levelRepository
@@ -305,19 +311,19 @@ export default class InventoryLevelService {
* Gets the total available quantity for a specific inventory item at multiple locations.
* @param inventoryItemId - The ID of the inventory item.
* @param locationIds - The IDs of the locations.
* @param context
* @return The total available quantity.
*/
@InjectEntityManager()
async getAvailableQuantity(
inventoryItemId: string,
locationIds: string[] | string,
@MedusaContext() context: SharedContext = {}
context: SharedContext = {}
): Promise<number> {
if (!Array.isArray(locationIds)) {
locationIds = [locationIds]
}
const manager = context.transactionManager!
const manager = context.transactionManager ?? this.manager_
const levelRepository = manager.getRepository(InventoryLevel)
const result = await levelRepository
@@ -334,19 +340,19 @@ export default class InventoryLevelService {
* Gets the total reserved quantity for a specific inventory item at multiple locations.
* @param inventoryItemId - The ID of the inventory item.
* @param locationIds - The IDs of the locations.
* @param context
* @return The total reserved quantity.
*/
@InjectEntityManager()
async getReservedQuantity(
inventoryItemId: string,
locationIds: string[] | string,
@MedusaContext() context: SharedContext = {}
context: SharedContext = {}
): Promise<number> {
if (!Array.isArray(locationIds)) {
locationIds = [locationIds]
}
const manager = context.transactionManager!
const manager = context.transactionManager ?? this.manager_
const levelRepository = manager.getRepository(InventoryLevel)
const result = await levelRepository

View File

@@ -59,16 +59,13 @@ export default class InventoryService implements IInventoryService {
* Lists inventory items that match the given selector
* @param selector - the selector to filter inventory items by
* @param config - the find configuration to use
* @param context
* @return A tuple of inventory items and their total count
*/
@InjectEntityManager(
(target) =>
target.moduleDeclaration?.resources === MODULE_RESOURCE_TYPE.ISOLATED
)
async listInventoryItems(
selector: FilterableInventoryItemProps,
config: FindConfig<InventoryItemDTO> = { relations: [], skip: 0, take: 10 },
@MedusaContext() context: SharedContext = {}
context: SharedContext = {}
): Promise<[InventoryItemDTO[], number]> {
return await this.inventoryItemService_.listAndCount(
selector,
@@ -81,12 +78,9 @@ export default class InventoryService implements IInventoryService {
* Lists inventory levels that match the given selector
* @param selector - the selector to filter inventory levels by
* @param config - the find configuration to use
* @param context
* @return A tuple of inventory levels and their total count
*/
@InjectEntityManager(
(target) =>
target.moduleDeclaration?.resources === MODULE_RESOURCE_TYPE.ISOLATED
)
async listInventoryLevels(
selector: FilterableInventoryLevelProps,
config: FindConfig<InventoryLevelDTO> = {
@@ -94,7 +88,7 @@ export default class InventoryService implements IInventoryService {
skip: 0,
take: 10,
},
@MedusaContext() context: SharedContext = {}
context: SharedContext = {}
): Promise<[InventoryLevelDTO[], number]> {
return await this.inventoryLevelService_.listAndCount(
selector,
@@ -107,12 +101,9 @@ export default class InventoryService implements IInventoryService {
* Lists reservation items that match the given selector
* @param selector - the selector to filter reservation items by
* @param config - the find configuration to use
* @param context
* @return A tuple of reservation items and their total count
*/
@InjectEntityManager(
(target) =>
target.moduleDeclaration?.resources === MODULE_RESOURCE_TYPE.ISOLATED
)
async listReservationItems(
selector: FilterableReservationItemProps,
config: FindConfig<ReservationItemDTO> = {
@@ -120,7 +111,7 @@ export default class InventoryService implements IInventoryService {
skip: 0,
take: 10,
},
@MedusaContext() context: SharedContext = {}
context: SharedContext = {}
): Promise<[ReservationItemDTO[], number]> {
return await this.reservationItemService_.listAndCount(
selector,
@@ -133,16 +124,13 @@ export default class InventoryService implements IInventoryService {
* Retrieves an inventory item with the given id
* @param inventoryItemId - the id of the inventory item to retrieve
* @param config - the find configuration to use
* @param context
* @return The retrieved inventory item
*/
@InjectEntityManager(
(target) =>
target.moduleDeclaration?.resources === MODULE_RESOURCE_TYPE.ISOLATED
)
async retrieveInventoryItem(
inventoryItemId: string,
config?: FindConfig<InventoryItemDTO>,
@MedusaContext() context: SharedContext = {}
context: SharedContext = {}
): Promise<InventoryItemDTO> {
const inventoryItem = await this.inventoryItemService_.retrieve(
inventoryItemId,
@@ -156,16 +144,13 @@ export default class InventoryService implements IInventoryService {
* Retrieves an inventory level for a given inventory item and location
* @param inventoryItemId - the id of the inventory item
* @param locationId - the id of the location
* @param context
* @return the retrieved inventory level
*/
@InjectEntityManager(
(target) =>
target.moduleDeclaration?.resources === MODULE_RESOURCE_TYPE.ISOLATED
)
async retrieveInventoryLevel(
inventoryItemId: string,
locationId: string,
@MedusaContext() context: SharedContext = {}
context: SharedContext = {}
): Promise<InventoryLevelDTO> {
const [inventoryLevel] = await this.inventoryLevelService_.list(
{ inventory_item_id: inventoryItemId, location_id: locationId },
@@ -183,16 +168,14 @@ export default class InventoryService implements IInventoryService {
/**
* Retrieves a reservation item
* @param inventoryItemId - the id of the reservation item
* @return the retrieved reservation level
* @param reservationId
* @param context
* @param reservationId
* @param context
*/
@InjectEntityManager(
(target) =>
target.moduleDeclaration?.resources === MODULE_RESOURCE_TYPE.ISOLATED
)
async retrieveReservationItem(
reservationId: string,
@MedusaContext() context: SharedContext = {}
context: SharedContext = {}
): Promise<ReservationItemDTO> {
return await this.reservationItemService_.retrieve(
reservationId,
@@ -204,6 +187,7 @@ export default class InventoryService implements IInventoryService {
/**
* Creates a reservation item
* @param input - the input object
* @param context
* @return The created reservation item
*/
@InjectEntityManager(
@@ -242,6 +226,7 @@ export default class InventoryService implements IInventoryService {
/**
* Creates an inventory item
* @param input - the input object
* @param context
* @return The created inventory item
*/
@InjectEntityManager(
@@ -262,6 +247,7 @@ export default class InventoryService implements IInventoryService {
/**
* Creates an inventory item
* @param input - the input object
* @param context
* @return The created inventory level
*/
@InjectEntityManager(
@@ -279,6 +265,7 @@ export default class InventoryService implements IInventoryService {
* Updates an inventory item
* @param inventoryItemId - the id of the inventory item to update
* @param input - the input object
* @param context
* @return The updated inventory item
*/
@InjectEntityManager(
@@ -301,6 +288,7 @@ export default class InventoryService implements IInventoryService {
/**
* Deletes an inventory item
* @param inventoryItemId - the id of the inventory item to delete
* @param context
*/
@InjectEntityManager(
(target) =>
@@ -350,6 +338,7 @@ export default class InventoryService implements IInventoryService {
* Deletes an inventory level
* @param inventoryItemId - the id of the inventory item associated with the level
* @param locationId - the id of the location associated with the level
* @param context
*/
@InjectEntityManager(
(target) =>
@@ -378,6 +367,7 @@ export default class InventoryService implements IInventoryService {
* @param inventoryItemId - the id of the inventory item associated with the level
* @param locationId - the id of the location associated with the level
* @param input - the input object
* @param context
* @return The updated inventory level
*/
@InjectEntityManager(
@@ -412,8 +402,10 @@ export default class InventoryService implements IInventoryService {
/**
* Updates a reservation item
* @param inventoryItemId - the id of the inventory item associated with the level
* @param reservationItemId
* @param input - the input object
* @param context
* @param context
* @return The updated inventory level
*/
@InjectEntityManager(
@@ -435,6 +427,7 @@ export default class InventoryService implements IInventoryService {
/**
* Deletes reservation items by line item
* @param lineItemId - the id of the line item associated with the reservation item
* @param context
*/
@InjectEntityManager(
(target) =>
@@ -453,6 +446,7 @@ export default class InventoryService implements IInventoryService {
/**
* Deletes a reservation item
* @param reservationItemId - the id of the reservation item to delete
* @param context
*/
@InjectEntityManager(
(target) =>
@@ -470,6 +464,7 @@ export default class InventoryService implements IInventoryService {
* @param inventoryItemId - the id of the inventory item
* @param locationId - the id of the location
* @param adjustment - the number to adjust the inventory by (can be positive or negative)
* @param context
* @return The updated inventory level
* @throws when the inventory level is not found
*/
@@ -510,17 +505,14 @@ export default class InventoryService implements IInventoryService {
* Retrieves the available quantity of a given inventory item in a given location.
* @param inventoryItemId - the id of the inventory item
* @param locationIds - the ids of the locations to check
* @param context
* @return The available quantity
* @throws when the inventory item is not found
*/
@InjectEntityManager(
(target) =>
target.moduleDeclaration?.resources === MODULE_RESOURCE_TYPE.ISOLATED
)
async retrieveAvailableQuantity(
inventoryItemId: string,
locationIds: string[],
@MedusaContext() context: SharedContext = {}
context: SharedContext = {}
): Promise<number> {
// Throws if item does not exist
await this.inventoryItemService_.retrieve(
@@ -549,17 +541,14 @@ export default class InventoryService implements IInventoryService {
* Retrieves the stocked quantity of a given inventory item in a given location.
* @param inventoryItemId - the id of the inventory item
* @param locationIds - the ids of the locations to check
* @param context
* @return The stocked quantity
* @throws when the inventory item is not found
*/
@InjectEntityManager(
(target) =>
target.moduleDeclaration?.resources === MODULE_RESOURCE_TYPE.ISOLATED
)
async retrieveStockedQuantity(
inventoryItemId: string,
locationIds: string[],
@MedusaContext() context: SharedContext = {}
context: SharedContext = {}
): Promise<number> {
// Throws if item does not exist
await this.inventoryItemService_.retrieve(
@@ -588,17 +577,14 @@ export default class InventoryService implements IInventoryService {
* Retrieves the reserved quantity of a given inventory item in a given location.
* @param inventoryItemId - the id of the inventory item
* @param locationIds - the ids of the locations to check
* @param context
* @return The reserved quantity
* @throws when the inventory item is not found
*/
@InjectEntityManager(
(target) =>
target.moduleDeclaration?.resources === MODULE_RESOURCE_TYPE.ISOLATED
)
async retrieveReservedQuantity(
inventoryItemId: string,
locationIds: string[],
@MedusaContext() context: SharedContext = {}
context: SharedContext = {}
): Promise<number> {
// Throws if item does not exist
await this.inventoryItemService_.retrieve(
@@ -628,6 +614,7 @@ export default class InventoryService implements IInventoryService {
* @param inventoryItemId - the id of the inventory item
* @param locationIds - the ids of the locations to check
* @param quantity - the quantity to check
* @param context
* @return Whether there is sufficient inventory
*/
@InjectEntityManager(

View File

@@ -48,15 +48,15 @@ export default class ReservationItemService {
* Lists reservation items that match the provided filter.
* @param selector - Filters to apply to the reservation items.
* @param config - Configuration for the query.
* @param context
* @return Array of reservation items that match the selector.
*/
@InjectEntityManager()
async list(
selector: FilterableReservationItemProps = {},
config: FindConfig<ReservationItem> = { relations: [], skip: 0, take: 10 },
@MedusaContext() context: SharedContext = {}
context: SharedContext = {}
): Promise<ReservationItem[]> {
const manager = context.transactionManager!
const manager = context.transactionManager ?? this.manager_
const itemRepository = manager.getRepository(ReservationItem)
const query = buildQuery(selector, config) as FindManyOptions
@@ -68,15 +68,15 @@ export default class ReservationItemService {
* Lists reservation items that match the provided filter and returns the total count.
* @param selector - Filters to apply to the reservation items.
* @param config - Configuration for the query.
* @param context
* @return Array of reservation items that match the selector and the total count.
*/
@InjectEntityManager()
async listAndCount(
selector: FilterableReservationItemProps = {},
config: FindConfig<ReservationItem> = { relations: [], skip: 0, take: 10 },
@MedusaContext() context: SharedContext = {}
context: SharedContext = {}
): Promise<[ReservationItem[], number]> {
const manager = context.transactionManager!
const manager = context.transactionManager ?? this.manager_
const itemRepository = manager.getRepository(ReservationItem)
const query = buildQuery(selector, config) as FindManyOptions
@@ -88,14 +88,14 @@ export default class ReservationItemService {
* Retrieves a reservation item by its id.
* @param reservationItemId - The id of the reservation item to retrieve.
* @param config - Configuration for the query.
* @param context
* @return The reservation item with the provided id.
* @throws If reservationItemId is not defined or if the reservation item was not found.
*/
@InjectEntityManager()
async retrieve(
reservationItemId: string,
config: FindConfig<ReservationItem> = {},
@MedusaContext() context: SharedContext = {}
context: SharedContext = {}
): Promise<ReservationItem> {
if (!isDefined(reservationItemId)) {
throw new MedusaError(
@@ -104,7 +104,7 @@ export default class ReservationItemService {
)
}
const manager = context.transactionManager!
const manager = context.transactionManager ?? this.manager_
const reservationItemRepository = manager.getRepository(ReservationItem)
const query = buildQuery(
@@ -126,6 +126,7 @@ export default class ReservationItemService {
/**
* Create a new reservation item.
* @param data - The reservation item data.
* @param context
* @return The created reservation item.
*/
@InjectEntityManager()
@@ -168,6 +169,7 @@ export default class ReservationItemService {
* Update a reservation item.
* @param reservationItemId - The reservation item's id.
* @param data - The reservation item data to update.
* @param context
* @return The updated reservation item.
*/
@InjectEntityManager()
@@ -232,6 +234,7 @@ export default class ReservationItemService {
/**
* Deletes a reservation item by line item id.
* @param lineItemId - the id of the line item to delete.
* @param context
*/
@InjectEntityManager()
async deleteByLineItem(
@@ -274,6 +277,7 @@ export default class ReservationItemService {
/**
* Deletes reservation items by location ID.
* @param locationId - The ID of the location to delete reservations for.
* @param context
*/
@InjectEntityManager()
async deleteByLocationId(
@@ -298,7 +302,9 @@ export default class ReservationItemService {
/**
* Deletes a reservation item by id.
* @param reservationItemId - the id of the reservation item to delete.
* @param context
*/
@InjectEntityManager()
async delete(
reservationItemId: string | string[],
@MedusaContext() context: SharedContext = {}