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
+7
View File
@@ -0,0 +1,7 @@
---
"@medusajs/inventory": patch
"@medusajs/medusa": patch
"@medusajs/stock-location": patch
---
feat(medusa, inventory, stock-location): Remove unnecessary transaction usage in the modules and list products end points
@@ -40,54 +40,34 @@ export default class InventoryItemService {
/** /**
* @param selector - Filter options for inventory items. * @param selector - Filter options for inventory items.
* @param config - Configuration for query. * @param config - Configuration for query.
* @param context
* @return Resolves to the list of inventory items that match the filter. * @return Resolves to the list of inventory items that match the filter.
*/ */
@InjectEntityManager()
async list( async list(
selector: FilterableInventoryItemProps = {}, selector: FilterableInventoryItemProps = {},
config: FindConfig<InventoryItem> = { relations: [], skip: 0, take: 10 }, config: FindConfig<InventoryItem> = { relations: [], skip: 0, take: 10 },
@MedusaContext() context: SharedContext = {} context: SharedContext = {}
): Promise<InventoryItemDTO[]> { ): Promise<InventoryItemDTO[]> {
const queryBuilder = getListQuery( const queryBuilder = getListQuery(
context.transactionManager!, context.transactionManager ?? this.manager_,
selector, selector,
config config
) )
return await queryBuilder.getMany() 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. * Retrieves an inventory item by its id.
* @param inventoryItemId - the id of the inventory item to retrieve. * @param inventoryItemId - the id of the inventory item to retrieve.
* @param config - the configuration options for the find operation. * @param config - the configuration options for the find operation.
* @param context
* @return The retrieved inventory item. * @return The retrieved inventory item.
* @throws If the inventory item id is not defined or if the inventory item is not found. * @throws If the inventory item id is not defined or if the inventory item is not found.
*/ */
@InjectEntityManager()
async retrieve( async retrieve(
inventoryItemId: string, inventoryItemId: string,
config: FindConfig<InventoryItem> = {}, config: FindConfig<InventoryItem> = {},
@MedusaContext() context: SharedContext = {} context: SharedContext = {}
): Promise<InventoryItem> { ): Promise<InventoryItem> {
if (!isDefined(inventoryItemId)) { if (!isDefined(inventoryItemId)) {
throw new MedusaError( 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 itemRepository = manager.getRepository(InventoryItem)
const query = buildQuery({ id: inventoryItemId }, config) as FindManyOptions 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. * @param selector - Filter options for inventory items.
* @return The newly created inventory item. * @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() @InjectEntityManager()
async create( async create(
@@ -152,7 +154,9 @@ export default class InventoryItemService {
/** /**
* @param inventoryItemId - The id of the inventory item to update. * @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. * @return The updated inventory item.
*/ */
@InjectEntityManager() @InjectEntityManager()
@@ -187,6 +191,7 @@ export default class InventoryItemService {
/** /**
* @param inventoryItemId - The id of the inventory item to delete. * @param inventoryItemId - The id of the inventory item to delete.
* @param context
*/ */
@InjectEntityManager() @InjectEntityManager()
async delete( async delete(
@@ -39,15 +39,15 @@ export default class InventoryLevelService {
* Retrieves a list of inventory levels based on the provided selector and configuration. * Retrieves a list of inventory levels based on the provided selector and configuration.
* @param selector - An object containing filterable properties for inventory levels. * @param selector - An object containing filterable properties for inventory levels.
* @param config - An object containing configuration options for the query. * @param config - An object containing configuration options for the query.
* @param context
* @return Array of inventory levels. * @return Array of inventory levels.
*/ */
@InjectEntityManager()
async list( async list(
selector: FilterableInventoryLevelProps = {}, selector: FilterableInventoryLevelProps = {},
config: FindConfig<InventoryLevel> = { relations: [], skip: 0, take: 10 }, config: FindConfig<InventoryLevel> = { relations: [], skip: 0, take: 10 },
@MedusaContext() context: SharedContext = {} context: SharedContext = {}
): Promise<InventoryLevel[]> { ): Promise<InventoryLevel[]> {
const manager = context.transactionManager! const manager = context.transactionManager ?? this.manager_
const levelRepository = manager.getRepository(InventoryLevel) const levelRepository = manager.getRepository(InventoryLevel)
const query = buildQuery(selector, config) as FindManyOptions 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. * 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 selector - An object containing filterable properties for inventory levels.
* @param config - An object containing configuration options for the query. * @param config - An object containing configuration options for the query.
* @param context
* @return An array of inventory levels and a count. * @return An array of inventory levels and a count.
*/ */
@InjectEntityManager()
async listAndCount( async listAndCount(
selector: FilterableInventoryLevelProps = {}, selector: FilterableInventoryLevelProps = {},
config: FindConfig<InventoryLevel> = { relations: [], skip: 0, take: 10 }, config: FindConfig<InventoryLevel> = { relations: [], skip: 0, take: 10 },
@MedusaContext() context: SharedContext = {} context: SharedContext = {}
): Promise<[InventoryLevel[], number]> { ): Promise<[InventoryLevel[], number]> {
const manager = context.transactionManager! const manager = context.transactionManager ?? this.manager_
const levelRepository = manager.getRepository(InventoryLevel) const levelRepository = manager.getRepository(InventoryLevel)
const query = buildQuery(selector, config) as FindManyOptions const query = buildQuery(selector, config) as FindManyOptions
@@ -77,14 +77,14 @@ export default class InventoryLevelService {
* Retrieves a single inventory level by its ID. * Retrieves a single inventory level by its ID.
* @param inventoryLevelId - The ID of the inventory level to retrieve. * @param inventoryLevelId - The ID of the inventory level to retrieve.
* @param config - An object containing configuration options for the query. * @param config - An object containing configuration options for the query.
* @param context
* @return A inventory level. * @return A inventory level.
* @throws If the inventory level ID is not defined or the given ID was not found. * @throws If the inventory level ID is not defined or the given ID was not found.
*/ */
@InjectEntityManager()
async retrieve( async retrieve(
inventoryLevelId: string, inventoryLevelId: string,
config: FindConfig<InventoryLevel> = {}, config: FindConfig<InventoryLevel> = {},
@MedusaContext() context: SharedContext = {} context: SharedContext = {}
): Promise<InventoryLevel> { ): Promise<InventoryLevel> {
if (!isDefined(inventoryLevelId)) { if (!isDefined(inventoryLevelId)) {
throw new MedusaError( 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 levelRepository = manager.getRepository(InventoryLevel)
const query = buildQuery( const query = buildQuery(
@@ -115,6 +115,7 @@ export default class InventoryLevelService {
/** /**
* Creates a new inventory level. * Creates a new inventory level.
* @param data - An object containing the properties for the new inventory level. * @param data - An object containing the properties for the new inventory level.
* @param context
* @return The created inventory level. * @return The created inventory level.
*/ */
@InjectEntityManager() @InjectEntityManager()
@@ -146,6 +147,7 @@ export default class InventoryLevelService {
* Updates an existing inventory level. * Updates an existing inventory level.
* @param inventoryLevelId - The ID of the inventory level to update. * @param inventoryLevelId - The ID of the inventory level to update.
* @param data - An object containing the properties to update on the inventory level. * @param data - An object containing the properties to update on the inventory level.
* @param context
* @return The updated inventory level. * @return The updated inventory level.
* @throws If the inventory level ID is not defined or the given ID was not found. * @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 inventoryItemId - The ID of the inventory item.
* @param locationId - The ID of the location. * @param locationId - The ID of the location.
* @param quantity - The quantity to adjust from the reserved quantity. * @param quantity - The quantity to adjust from the reserved quantity.
* @param context
*/ */
@InjectEntityManager() @InjectEntityManager()
async adjustReservedQuantity( async adjustReservedQuantity(
@@ -210,6 +213,7 @@ export default class InventoryLevelService {
/** /**
* Deletes inventory levels by inventory Item ID. * Deletes inventory levels by inventory Item ID.
* @param inventoryItemId - The ID or IDs of the inventory item to delete inventory levels for. * @param inventoryItemId - The ID or IDs of the inventory item to delete inventory levels for.
* @param context
*/ */
@InjectEntityManager() @InjectEntityManager()
async deleteByInventoryItemId( async deleteByInventoryItemId(
@@ -233,6 +237,7 @@ export default class InventoryLevelService {
/** /**
* Deletes an inventory level by ID. * Deletes an inventory level by ID.
* @param inventoryLevelId - The ID or IDs of the inventory level to delete. * @param inventoryLevelId - The ID or IDs of the inventory level to delete.
* @param context
*/ */
@InjectEntityManager() @InjectEntityManager()
async delete( async delete(
@@ -256,6 +261,7 @@ export default class InventoryLevelService {
/** /**
* Deletes inventory levels by location ID. * Deletes inventory levels by location ID.
* @param locationId - The ID of the location to delete inventory levels for. * @param locationId - The ID of the location to delete inventory levels for.
* @param context
*/ */
@InjectEntityManager() @InjectEntityManager()
async deleteByLocationId( async deleteByLocationId(
@@ -276,19 +282,19 @@ export default class InventoryLevelService {
* Gets the total stocked quantity for a specific inventory item at multiple locations. * Gets the total stocked quantity for a specific inventory item at multiple locations.
* @param inventoryItemId - The ID of the inventory item. * @param inventoryItemId - The ID of the inventory item.
* @param locationIds - The IDs of the locations. * @param locationIds - The IDs of the locations.
* @param context
* @return The total stocked quantity. * @return The total stocked quantity.
*/ */
@InjectEntityManager()
async getStockedQuantity( async getStockedQuantity(
inventoryItemId: string, inventoryItemId: string,
locationIds: string[] | string, locationIds: string[] | string,
@MedusaContext() context: SharedContext = {} context: SharedContext = {}
): Promise<number> { ): Promise<number> {
if (!Array.isArray(locationIds)) { if (!Array.isArray(locationIds)) {
locationIds = [locationIds] locationIds = [locationIds]
} }
const manager = context.transactionManager! const manager = context.transactionManager ?? this.manager_
const levelRepository = manager.getRepository(InventoryLevel) const levelRepository = manager.getRepository(InventoryLevel)
const result = await levelRepository 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. * Gets the total available quantity for a specific inventory item at multiple locations.
* @param inventoryItemId - The ID of the inventory item. * @param inventoryItemId - The ID of the inventory item.
* @param locationIds - The IDs of the locations. * @param locationIds - The IDs of the locations.
* @param context
* @return The total available quantity. * @return The total available quantity.
*/ */
@InjectEntityManager()
async getAvailableQuantity( async getAvailableQuantity(
inventoryItemId: string, inventoryItemId: string,
locationIds: string[] | string, locationIds: string[] | string,
@MedusaContext() context: SharedContext = {} context: SharedContext = {}
): Promise<number> { ): Promise<number> {
if (!Array.isArray(locationIds)) { if (!Array.isArray(locationIds)) {
locationIds = [locationIds] locationIds = [locationIds]
} }
const manager = context.transactionManager! const manager = context.transactionManager ?? this.manager_
const levelRepository = manager.getRepository(InventoryLevel) const levelRepository = manager.getRepository(InventoryLevel)
const result = await levelRepository 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. * Gets the total reserved quantity for a specific inventory item at multiple locations.
* @param inventoryItemId - The ID of the inventory item. * @param inventoryItemId - The ID of the inventory item.
* @param locationIds - The IDs of the locations. * @param locationIds - The IDs of the locations.
* @param context
* @return The total reserved quantity. * @return The total reserved quantity.
*/ */
@InjectEntityManager()
async getReservedQuantity( async getReservedQuantity(
inventoryItemId: string, inventoryItemId: string,
locationIds: string[] | string, locationIds: string[] | string,
@MedusaContext() context: SharedContext = {} context: SharedContext = {}
): Promise<number> { ): Promise<number> {
if (!Array.isArray(locationIds)) { if (!Array.isArray(locationIds)) {
locationIds = [locationIds] locationIds = [locationIds]
} }
const manager = context.transactionManager! const manager = context.transactionManager ?? this.manager_
const levelRepository = manager.getRepository(InventoryLevel) const levelRepository = manager.getRepository(InventoryLevel)
const result = await levelRepository const result = await levelRepository
+35 -48
View File
@@ -59,16 +59,13 @@ export default class InventoryService implements IInventoryService {
* Lists inventory items that match the given selector * Lists inventory items that match the given selector
* @param selector - the selector to filter inventory items by * @param selector - the selector to filter inventory items by
* @param config - the find configuration to use * @param config - the find configuration to use
* @param context
* @return A tuple of inventory items and their total count * @return A tuple of inventory items and their total count
*/ */
@InjectEntityManager(
(target) =>
target.moduleDeclaration?.resources === MODULE_RESOURCE_TYPE.ISOLATED
)
async listInventoryItems( async listInventoryItems(
selector: FilterableInventoryItemProps, selector: FilterableInventoryItemProps,
config: FindConfig<InventoryItemDTO> = { relations: [], skip: 0, take: 10 }, config: FindConfig<InventoryItemDTO> = { relations: [], skip: 0, take: 10 },
@MedusaContext() context: SharedContext = {} context: SharedContext = {}
): Promise<[InventoryItemDTO[], number]> { ): Promise<[InventoryItemDTO[], number]> {
return await this.inventoryItemService_.listAndCount( return await this.inventoryItemService_.listAndCount(
selector, selector,
@@ -81,12 +78,9 @@ export default class InventoryService implements IInventoryService {
* Lists inventory levels that match the given selector * Lists inventory levels that match the given selector
* @param selector - the selector to filter inventory levels by * @param selector - the selector to filter inventory levels by
* @param config - the find configuration to use * @param config - the find configuration to use
* @param context
* @return A tuple of inventory levels and their total count * @return A tuple of inventory levels and their total count
*/ */
@InjectEntityManager(
(target) =>
target.moduleDeclaration?.resources === MODULE_RESOURCE_TYPE.ISOLATED
)
async listInventoryLevels( async listInventoryLevels(
selector: FilterableInventoryLevelProps, selector: FilterableInventoryLevelProps,
config: FindConfig<InventoryLevelDTO> = { config: FindConfig<InventoryLevelDTO> = {
@@ -94,7 +88,7 @@ export default class InventoryService implements IInventoryService {
skip: 0, skip: 0,
take: 10, take: 10,
}, },
@MedusaContext() context: SharedContext = {} context: SharedContext = {}
): Promise<[InventoryLevelDTO[], number]> { ): Promise<[InventoryLevelDTO[], number]> {
return await this.inventoryLevelService_.listAndCount( return await this.inventoryLevelService_.listAndCount(
selector, selector,
@@ -107,12 +101,9 @@ export default class InventoryService implements IInventoryService {
* Lists reservation items that match the given selector * Lists reservation items that match the given selector
* @param selector - the selector to filter reservation items by * @param selector - the selector to filter reservation items by
* @param config - the find configuration to use * @param config - the find configuration to use
* @param context
* @return A tuple of reservation items and their total count * @return A tuple of reservation items and their total count
*/ */
@InjectEntityManager(
(target) =>
target.moduleDeclaration?.resources === MODULE_RESOURCE_TYPE.ISOLATED
)
async listReservationItems( async listReservationItems(
selector: FilterableReservationItemProps, selector: FilterableReservationItemProps,
config: FindConfig<ReservationItemDTO> = { config: FindConfig<ReservationItemDTO> = {
@@ -120,7 +111,7 @@ export default class InventoryService implements IInventoryService {
skip: 0, skip: 0,
take: 10, take: 10,
}, },
@MedusaContext() context: SharedContext = {} context: SharedContext = {}
): Promise<[ReservationItemDTO[], number]> { ): Promise<[ReservationItemDTO[], number]> {
return await this.reservationItemService_.listAndCount( return await this.reservationItemService_.listAndCount(
selector, selector,
@@ -133,16 +124,13 @@ export default class InventoryService implements IInventoryService {
* Retrieves an inventory item with the given id * Retrieves an inventory item with the given id
* @param inventoryItemId - the id of the inventory item to retrieve * @param inventoryItemId - the id of the inventory item to retrieve
* @param config - the find configuration to use * @param config - the find configuration to use
* @param context
* @return The retrieved inventory item * @return The retrieved inventory item
*/ */
@InjectEntityManager(
(target) =>
target.moduleDeclaration?.resources === MODULE_RESOURCE_TYPE.ISOLATED
)
async retrieveInventoryItem( async retrieveInventoryItem(
inventoryItemId: string, inventoryItemId: string,
config?: FindConfig<InventoryItemDTO>, config?: FindConfig<InventoryItemDTO>,
@MedusaContext() context: SharedContext = {} context: SharedContext = {}
): Promise<InventoryItemDTO> { ): Promise<InventoryItemDTO> {
const inventoryItem = await this.inventoryItemService_.retrieve( const inventoryItem = await this.inventoryItemService_.retrieve(
inventoryItemId, inventoryItemId,
@@ -156,16 +144,13 @@ export default class InventoryService implements IInventoryService {
* Retrieves an inventory level for a given inventory item and location * Retrieves an inventory level for a given inventory item and location
* @param inventoryItemId - the id of the inventory item * @param inventoryItemId - the id of the inventory item
* @param locationId - the id of the location * @param locationId - the id of the location
* @param context
* @return the retrieved inventory level * @return the retrieved inventory level
*/ */
@InjectEntityManager(
(target) =>
target.moduleDeclaration?.resources === MODULE_RESOURCE_TYPE.ISOLATED
)
async retrieveInventoryLevel( async retrieveInventoryLevel(
inventoryItemId: string, inventoryItemId: string,
locationId: string, locationId: string,
@MedusaContext() context: SharedContext = {} context: SharedContext = {}
): Promise<InventoryLevelDTO> { ): Promise<InventoryLevelDTO> {
const [inventoryLevel] = await this.inventoryLevelService_.list( const [inventoryLevel] = await this.inventoryLevelService_.list(
{ inventory_item_id: inventoryItemId, location_id: locationId }, { inventory_item_id: inventoryItemId, location_id: locationId },
@@ -183,16 +168,14 @@ export default class InventoryService implements IInventoryService {
/** /**
* Retrieves a reservation item * Retrieves a reservation item
* @param inventoryItemId - the id of the reservation item * @param reservationId
* @return the retrieved reservation level * @param context
* @param reservationId
* @param context
*/ */
@InjectEntityManager(
(target) =>
target.moduleDeclaration?.resources === MODULE_RESOURCE_TYPE.ISOLATED
)
async retrieveReservationItem( async retrieveReservationItem(
reservationId: string, reservationId: string,
@MedusaContext() context: SharedContext = {} context: SharedContext = {}
): Promise<ReservationItemDTO> { ): Promise<ReservationItemDTO> {
return await this.reservationItemService_.retrieve( return await this.reservationItemService_.retrieve(
reservationId, reservationId,
@@ -204,6 +187,7 @@ export default class InventoryService implements IInventoryService {
/** /**
* Creates a reservation item * Creates a reservation item
* @param input - the input object * @param input - the input object
* @param context
* @return The created reservation item * @return The created reservation item
*/ */
@InjectEntityManager( @InjectEntityManager(
@@ -242,6 +226,7 @@ export default class InventoryService implements IInventoryService {
/** /**
* Creates an inventory item * Creates an inventory item
* @param input - the input object * @param input - the input object
* @param context
* @return The created inventory item * @return The created inventory item
*/ */
@InjectEntityManager( @InjectEntityManager(
@@ -262,6 +247,7 @@ export default class InventoryService implements IInventoryService {
/** /**
* Creates an inventory item * Creates an inventory item
* @param input - the input object * @param input - the input object
* @param context
* @return The created inventory level * @return The created inventory level
*/ */
@InjectEntityManager( @InjectEntityManager(
@@ -279,6 +265,7 @@ export default class InventoryService implements IInventoryService {
* Updates an inventory item * Updates an inventory item
* @param inventoryItemId - the id of the inventory item to update * @param inventoryItemId - the id of the inventory item to update
* @param input - the input object * @param input - the input object
* @param context
* @return The updated inventory item * @return The updated inventory item
*/ */
@InjectEntityManager( @InjectEntityManager(
@@ -301,6 +288,7 @@ export default class InventoryService implements IInventoryService {
/** /**
* Deletes an inventory item * Deletes an inventory item
* @param inventoryItemId - the id of the inventory item to delete * @param inventoryItemId - the id of the inventory item to delete
* @param context
*/ */
@InjectEntityManager( @InjectEntityManager(
(target) => (target) =>
@@ -350,6 +338,7 @@ export default class InventoryService implements IInventoryService {
* Deletes an inventory level * Deletes an inventory level
* @param inventoryItemId - the id of the inventory item associated with the 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 locationId - the id of the location associated with the level
* @param context
*/ */
@InjectEntityManager( @InjectEntityManager(
(target) => (target) =>
@@ -378,6 +367,7 @@ export default class InventoryService implements IInventoryService {
* @param inventoryItemId - the id of the inventory item associated with the 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 locationId - the id of the location associated with the level
* @param input - the input object * @param input - the input object
* @param context
* @return The updated inventory level * @return The updated inventory level
*/ */
@InjectEntityManager( @InjectEntityManager(
@@ -412,8 +402,10 @@ export default class InventoryService implements IInventoryService {
/** /**
* Updates a reservation item * Updates a reservation item
* @param inventoryItemId - the id of the inventory item associated with the level * @param reservationItemId
* @param input - the input object * @param input - the input object
* @param context
* @param context
* @return The updated inventory level * @return The updated inventory level
*/ */
@InjectEntityManager( @InjectEntityManager(
@@ -435,6 +427,7 @@ export default class InventoryService implements IInventoryService {
/** /**
* Deletes reservation items by line item * Deletes reservation items by line item
* @param lineItemId - the id of the line item associated with the reservation item * @param lineItemId - the id of the line item associated with the reservation item
* @param context
*/ */
@InjectEntityManager( @InjectEntityManager(
(target) => (target) =>
@@ -453,6 +446,7 @@ export default class InventoryService implements IInventoryService {
/** /**
* Deletes a reservation item * Deletes a reservation item
* @param reservationItemId - the id of the reservation item to delete * @param reservationItemId - the id of the reservation item to delete
* @param context
*/ */
@InjectEntityManager( @InjectEntityManager(
(target) => (target) =>
@@ -470,6 +464,7 @@ export default class InventoryService implements IInventoryService {
* @param inventoryItemId - the id of the inventory item * @param inventoryItemId - the id of the inventory item
* @param locationId - the id of the location * @param locationId - the id of the location
* @param adjustment - the number to adjust the inventory by (can be positive or negative) * @param adjustment - the number to adjust the inventory by (can be positive or negative)
* @param context
* @return The updated inventory level * @return The updated inventory level
* @throws when the inventory level is not found * @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. * Retrieves the available quantity of a given inventory item in a given location.
* @param inventoryItemId - the id of the inventory item * @param inventoryItemId - the id of the inventory item
* @param locationIds - the ids of the locations to check * @param locationIds - the ids of the locations to check
* @param context
* @return The available quantity * @return The available quantity
* @throws when the inventory item is not found * @throws when the inventory item is not found
*/ */
@InjectEntityManager(
(target) =>
target.moduleDeclaration?.resources === MODULE_RESOURCE_TYPE.ISOLATED
)
async retrieveAvailableQuantity( async retrieveAvailableQuantity(
inventoryItemId: string, inventoryItemId: string,
locationIds: string[], locationIds: string[],
@MedusaContext() context: SharedContext = {} context: SharedContext = {}
): Promise<number> { ): Promise<number> {
// Throws if item does not exist // Throws if item does not exist
await this.inventoryItemService_.retrieve( 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. * Retrieves the stocked quantity of a given inventory item in a given location.
* @param inventoryItemId - the id of the inventory item * @param inventoryItemId - the id of the inventory item
* @param locationIds - the ids of the locations to check * @param locationIds - the ids of the locations to check
* @param context
* @return The stocked quantity * @return The stocked quantity
* @throws when the inventory item is not found * @throws when the inventory item is not found
*/ */
@InjectEntityManager(
(target) =>
target.moduleDeclaration?.resources === MODULE_RESOURCE_TYPE.ISOLATED
)
async retrieveStockedQuantity( async retrieveStockedQuantity(
inventoryItemId: string, inventoryItemId: string,
locationIds: string[], locationIds: string[],
@MedusaContext() context: SharedContext = {} context: SharedContext = {}
): Promise<number> { ): Promise<number> {
// Throws if item does not exist // Throws if item does not exist
await this.inventoryItemService_.retrieve( 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. * Retrieves the reserved quantity of a given inventory item in a given location.
* @param inventoryItemId - the id of the inventory item * @param inventoryItemId - the id of the inventory item
* @param locationIds - the ids of the locations to check * @param locationIds - the ids of the locations to check
* @param context
* @return The reserved quantity * @return The reserved quantity
* @throws when the inventory item is not found * @throws when the inventory item is not found
*/ */
@InjectEntityManager(
(target) =>
target.moduleDeclaration?.resources === MODULE_RESOURCE_TYPE.ISOLATED
)
async retrieveReservedQuantity( async retrieveReservedQuantity(
inventoryItemId: string, inventoryItemId: string,
locationIds: string[], locationIds: string[],
@MedusaContext() context: SharedContext = {} context: SharedContext = {}
): Promise<number> { ): Promise<number> {
// Throws if item does not exist // Throws if item does not exist
await this.inventoryItemService_.retrieve( await this.inventoryItemService_.retrieve(
@@ -628,6 +614,7 @@ export default class InventoryService implements IInventoryService {
* @param inventoryItemId - the id of the inventory item * @param inventoryItemId - the id of the inventory item
* @param locationIds - the ids of the locations to check * @param locationIds - the ids of the locations to check
* @param quantity - the quantity to check * @param quantity - the quantity to check
* @param context
* @return Whether there is sufficient inventory * @return Whether there is sufficient inventory
*/ */
@InjectEntityManager( @InjectEntityManager(
@@ -48,15 +48,15 @@ export default class ReservationItemService {
* Lists reservation items that match the provided filter. * Lists reservation items that match the provided filter.
* @param selector - Filters to apply to the reservation items. * @param selector - Filters to apply to the reservation items.
* @param config - Configuration for the query. * @param config - Configuration for the query.
* @param context
* @return Array of reservation items that match the selector. * @return Array of reservation items that match the selector.
*/ */
@InjectEntityManager()
async list( async list(
selector: FilterableReservationItemProps = {}, selector: FilterableReservationItemProps = {},
config: FindConfig<ReservationItem> = { relations: [], skip: 0, take: 10 }, config: FindConfig<ReservationItem> = { relations: [], skip: 0, take: 10 },
@MedusaContext() context: SharedContext = {} context: SharedContext = {}
): Promise<ReservationItem[]> { ): Promise<ReservationItem[]> {
const manager = context.transactionManager! const manager = context.transactionManager ?? this.manager_
const itemRepository = manager.getRepository(ReservationItem) const itemRepository = manager.getRepository(ReservationItem)
const query = buildQuery(selector, config) as FindManyOptions 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. * Lists reservation items that match the provided filter and returns the total count.
* @param selector - Filters to apply to the reservation items. * @param selector - Filters to apply to the reservation items.
* @param config - Configuration for the query. * @param config - Configuration for the query.
* @param context
* @return Array of reservation items that match the selector and the total count. * @return Array of reservation items that match the selector and the total count.
*/ */
@InjectEntityManager()
async listAndCount( async listAndCount(
selector: FilterableReservationItemProps = {}, selector: FilterableReservationItemProps = {},
config: FindConfig<ReservationItem> = { relations: [], skip: 0, take: 10 }, config: FindConfig<ReservationItem> = { relations: [], skip: 0, take: 10 },
@MedusaContext() context: SharedContext = {} context: SharedContext = {}
): Promise<[ReservationItem[], number]> { ): Promise<[ReservationItem[], number]> {
const manager = context.transactionManager! const manager = context.transactionManager ?? this.manager_
const itemRepository = manager.getRepository(ReservationItem) const itemRepository = manager.getRepository(ReservationItem)
const query = buildQuery(selector, config) as FindManyOptions const query = buildQuery(selector, config) as FindManyOptions
@@ -88,14 +88,14 @@ export default class ReservationItemService {
* Retrieves a reservation item by its id. * Retrieves a reservation item by its id.
* @param reservationItemId - The id of the reservation item to retrieve. * @param reservationItemId - The id of the reservation item to retrieve.
* @param config - Configuration for the query. * @param config - Configuration for the query.
* @param context
* @return The reservation item with the provided id. * @return The reservation item with the provided id.
* @throws If reservationItemId is not defined or if the reservation item was not found. * @throws If reservationItemId is not defined or if the reservation item was not found.
*/ */
@InjectEntityManager()
async retrieve( async retrieve(
reservationItemId: string, reservationItemId: string,
config: FindConfig<ReservationItem> = {}, config: FindConfig<ReservationItem> = {},
@MedusaContext() context: SharedContext = {} context: SharedContext = {}
): Promise<ReservationItem> { ): Promise<ReservationItem> {
if (!isDefined(reservationItemId)) { if (!isDefined(reservationItemId)) {
throw new MedusaError( 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 reservationItemRepository = manager.getRepository(ReservationItem)
const query = buildQuery( const query = buildQuery(
@@ -126,6 +126,7 @@ export default class ReservationItemService {
/** /**
* Create a new reservation item. * Create a new reservation item.
* @param data - The reservation item data. * @param data - The reservation item data.
* @param context
* @return The created reservation item. * @return The created reservation item.
*/ */
@InjectEntityManager() @InjectEntityManager()
@@ -168,6 +169,7 @@ export default class ReservationItemService {
* Update a reservation item. * Update a reservation item.
* @param reservationItemId - The reservation item's id. * @param reservationItemId - The reservation item's id.
* @param data - The reservation item data to update. * @param data - The reservation item data to update.
* @param context
* @return The updated reservation item. * @return The updated reservation item.
*/ */
@InjectEntityManager() @InjectEntityManager()
@@ -232,6 +234,7 @@ export default class ReservationItemService {
/** /**
* Deletes a reservation item by line item id. * Deletes a reservation item by line item id.
* @param lineItemId - the id of the line item to delete. * @param lineItemId - the id of the line item to delete.
* @param context
*/ */
@InjectEntityManager() @InjectEntityManager()
async deleteByLineItem( async deleteByLineItem(
@@ -274,6 +277,7 @@ export default class ReservationItemService {
/** /**
* Deletes reservation items by location ID. * Deletes reservation items by location ID.
* @param locationId - The ID of the location to delete reservations for. * @param locationId - The ID of the location to delete reservations for.
* @param context
*/ */
@InjectEntityManager() @InjectEntityManager()
async deleteByLocationId( async deleteByLocationId(
@@ -298,7 +302,9 @@ export default class ReservationItemService {
/** /**
* Deletes a reservation item by id. * Deletes a reservation item by id.
* @param reservationItemId - the id of the reservation item to delete. * @param reservationItemId - the id of the reservation item to delete.
* @param context
*/ */
@InjectEntityManager()
async delete( async delete(
reservationItemId: string | string[], reservationItemId: string | string[],
@MedusaContext() context: SharedContext = {} @MedusaContext() context: SharedContext = {}
@@ -233,45 +233,37 @@ export default async (req, res) => {
const manager = req.scope.resolve("manager") const manager = req.scope.resolve("manager")
const [products, count] = await manager.transaction( const [rawProducts, count] = await productService.listAndCount(
async (transactionManager) => { req.filterableFields,
const [rawProducts, count] = await productService req.listConfig
.withTransaction(transactionManager)
.listAndCount(req.filterableFields, req.listConfig)
let products: (Product | PricedProduct)[] = rawProducts
// We only set prices if variants.prices are requested
const shouldSetPricing = ["variants", "variants.prices"].every(
(relation) => relations?.includes(relation)
)
if (shouldSetPricing) {
products = await pricingService
.withTransaction(transactionManager)
.setProductPrices(rawProducts)
}
// We only set availability if variants are requested
const shouldSetAvailability = relations?.includes("variants")
if (inventoryService && shouldSetAvailability) {
const [salesChannelsIds] = await salesChannelService
.withTransaction(transactionManager)
.listAndCount({}, { select: ["id"] })
products = await productVariantInventoryService
.withTransaction(transactionManager)
.setProductAvailability(
products,
salesChannelsIds.map((salesChannel) => salesChannel.id)
)
}
return [products, count]
}
) )
let products: (Product | PricedProduct)[] = rawProducts
// We only set prices if variants.prices are requested
const shouldSetPricing = ["variants", "variants.prices"].every((relation) =>
relations?.includes(relation)
)
if (shouldSetPricing) {
products = await pricingService.setProductPrices(rawProducts)
}
// We only set availability if variants are requested
const shouldSetAvailability = relations?.includes("variants")
if (inventoryService && shouldSetAvailability) {
const [salesChannelsIds] = await salesChannelService.listAndCount(
{},
{ select: ["id"] }
)
products = await productVariantInventoryService.setProductAvailability(
products,
salesChannelsIds.map((salesChannel) => salesChannel.id)
)
}
res.json({ res.json({
products, products,
count, count,
@@ -216,82 +216,64 @@ export default async (req, res) => {
} }
} }
const manager = req.scope.resolve("manager") const promises: Promise<any>[] = []
const [computedProducts, count] = await manager.transaction( promises.push(productService.listAndCount(filterableFields, listConfig))
async (transactionManager) => {
const promises: Promise<any>[] = []
promises.push( if (validated.cart_id) {
productService promises.push(
.withTransaction(transactionManager) cartService.retrieve(validated.cart_id, {
.listAndCount(filterableFields, listConfig) select: ["id", "region_id"] as any,
) relations: ["region"],
})
)
}
if (validated.cart_id) { const [[rawProducts, count], cart] = await Promise.all(promises)
promises.push(
cartService
.withTransaction(transactionManager)
.retrieve(validated.cart_id, {
select: ["id", "region_id"] as any,
relations: ["region"],
})
)
}
const [[rawProducts, count], cart] = await Promise.all(promises) if (validated.cart_id) {
regionId = cart.region_id
currencyCode = cart.region.currency_code
}
if (validated.cart_id) { // Create a new reference just for naming purpose
regionId = cart.region_id const computedProducts = rawProducts
currencyCode = cart.region.currency_code
}
// Create a new reference just for naming purpose // We only set prices if variants.prices are requested
const computedProducts = rawProducts const shouldSetPricing = ["variants", "variants.prices"].every((relation) =>
listConfig.relations?.includes(relation)
// We only set prices if variants.prices are requested
const shouldSetPricing = ["variants", "variants.prices"].every(
(relation) => listConfig.relations?.includes(relation)
)
// We only set availability if variants are requested
const shouldSetAvailability = listConfig.relations?.includes("variants")
const decoratePromises: Promise<any>[] = []
if (shouldSetPricing) {
decoratePromises.push(
pricingService
.withTransaction(transactionManager)
.setProductPrices(computedProducts, {
cart_id: cart_id,
region_id: regionId,
currency_code: currencyCode,
customer_id: req.user?.customer_id,
include_discount_prices: true,
})
)
}
if (shouldSetAvailability) {
decoratePromises.push(
productVariantInventoryService
.withTransaction(transactionManager)
.setProductAvailability(
computedProducts,
filterableFields.sales_channel_id
)
)
}
// We can run them concurrently as the new properties are assigned to the references
// of the appropriate entity
await Promise.all(decoratePromises)
return [computedProducts, count]
}
) )
// We only set availability if variants are requested
const shouldSetAvailability = listConfig.relations?.includes("variants")
const decoratePromises: Promise<any>[] = []
if (shouldSetPricing) {
decoratePromises.push(
pricingService.setProductPrices(computedProducts, {
cart_id: cart_id,
region_id: regionId,
currency_code: currencyCode,
customer_id: req.user?.customer_id,
include_discount_prices: true,
})
)
}
if (shouldSetAvailability) {
decoratePromises.push(
productVariantInventoryService.setProductAvailability(
computedProducts,
filterableFields.sales_channel_id
)
)
}
// We can run them concurrently as the new properties are assigned to the references
// of the appropriate entity
await Promise.all(decoratePromises)
res.json({ res.json({
products: cleanResponseData(computedProducts, req.allowedProperties || []), products: cleanResponseData(computedProducts, req.allowedProperties || []),
count, count,
@@ -375,6 +375,10 @@ class ProductVariantInventoryService extends TransactionBaseService {
} }
let locationId = context.locationId let locationId = context.locationId
const moduleContext = {
transactionManager: this.activeManager_,
}
if (!isDefined(locationId) && context.salesChannelId) { if (!isDefined(locationId) && context.salesChannelId) {
const locationIds = await this.salesChannelLocationService_ const locationIds = await this.salesChannelLocationService_
.withTransaction(this.activeManager_) .withTransaction(this.activeManager_)
@@ -388,10 +392,14 @@ class ProductVariantInventoryService extends TransactionBaseService {
} }
const [locations, count] = const [locations, count] =
await this.inventoryService_.listInventoryLevels({ await this.inventoryService_.listInventoryLevels(
location_id: locationIds, {
inventory_item_id: variantInventory[0].inventory_item_id, location_id: locationIds,
}) inventory_item_id: variantInventory[0].inventory_item_id,
},
undefined,
moduleContext
)
if (count === 0) { if (count === 0) {
throw new MedusaError( throw new MedusaError(
@@ -406,12 +414,15 @@ class ProductVariantInventoryService extends TransactionBaseService {
const reservationItems = await Promise.all( const reservationItems = await Promise.all(
variantInventory.map(async (inventoryPart) => { variantInventory.map(async (inventoryPart) => {
const itemQuantity = inventoryPart.required_quantity * quantity const itemQuantity = inventoryPart.required_quantity * quantity
return await this.inventoryService_.createReservationItem({ return await this.inventoryService_.createReservationItem(
...toReserve, {
location_id: locationId as string, ...toReserve,
inventory_item_id: inventoryPart.inventory_item_id, location_id: locationId as string,
quantity: itemQuantity, inventory_item_id: inventoryPart.inventory_item_id,
}) quantity: itemQuantity,
},
moduleContext
)
}) })
) )
@@ -456,6 +467,9 @@ class ProductVariantInventoryService extends TransactionBaseService {
) )
} }
const context = {
transactionManager: this.activeManager_,
}
const [reservations, reservationCount] = const [reservations, reservationCount] =
await this.inventoryService_.listReservationItems( await this.inventoryService_.listReservationItems(
{ {
@@ -463,7 +477,8 @@ class ProductVariantInventoryService extends TransactionBaseService {
}, },
{ {
order: { created_at: "DESC" }, order: { created_at: "DESC" },
} },
context
) )
reservations.sort((a, _) => { reservations.sort((a, _) => {
@@ -485,7 +500,10 @@ class ProductVariantInventoryService extends TransactionBaseService {
(r) => r.quantity === deltaUpdate && r.location_id === locationId (r) => r.quantity === deltaUpdate && r.location_id === locationId
) )
if (exactReservation) { if (exactReservation) {
await this.inventoryService_.deleteReservationItem(exactReservation.id) await this.inventoryService_.deleteReservationItem(
exactReservation.id,
context
)
return return
} }
@@ -505,7 +523,8 @@ class ProductVariantInventoryService extends TransactionBaseService {
if (reservationsToDelete.length) { if (reservationsToDelete.length) {
await this.inventoryService_.deleteReservationItem( await this.inventoryService_.deleteReservationItem(
reservationsToDelete.map((r) => r.id) reservationsToDelete.map((r) => r.id),
context
) )
} }
@@ -514,7 +533,8 @@ class ProductVariantInventoryService extends TransactionBaseService {
reservationToUpdate.id, reservationToUpdate.id,
{ {
quantity: reservationToUpdate.quantity - remainingQuantity, quantity: reservationToUpdate.quantity - remainingQuantity,
} },
context
) )
} }
} }
@@ -543,11 +563,19 @@ class ProductVariantInventoryService extends TransactionBaseService {
continue continue
} }
const context = {
transactionManager: this.activeManager_,
}
const [inventoryLevels, inventoryLevelCount] = const [inventoryLevels, inventoryLevelCount] =
await this.inventoryService_.listInventoryLevels({ await this.inventoryService_.listInventoryLevels(
inventory_item_id: pvInventoryItems.map((i) => i.inventory_item_id), {
location_id: locationId, inventory_item_id: pvInventoryItems.map((i) => i.inventory_item_id),
}) location_id: locationId,
},
undefined,
context
)
if (!inventoryLevelCount) { if (!inventoryLevelCount) {
throw new MedusaError( throw new MedusaError(
@@ -653,7 +681,10 @@ class ProductVariantInventoryService extends TransactionBaseService {
return await this.inventoryService_.adjustInventory( return await this.inventoryService_.adjustInventory(
inventoryPart.inventory_item_id, inventoryPart.inventory_item_id,
locationId, locationId,
itemQuantity itemQuantity,
{
transactionManager: this.activeManager_,
}
) )
}) })
) )
@@ -5,7 +5,6 @@ import { TransactionBaseService } from "../interfaces"
import { SalesChannelLocation } from "../models/sales-channel-location" import { SalesChannelLocation } from "../models/sales-channel-location"
import SalesChannelService from "./sales-channel" import SalesChannelService from "./sales-channel"
type InjectedDependencies = { type InjectedDependencies = {
stockLocationService: IStockLocationService stockLocationService: IStockLocationService
salesChannelService: SalesChannelService salesChannelService: SalesChannelService
@@ -80,18 +79,20 @@ class SalesChannelLocationService extends TransactionBaseService {
if (this.stockLocationService_) { if (this.stockLocationService_) {
// trhows error if not found // trhows error if not found
await this.stockLocationService_.retrieve(locationId) await this.stockLocationService_.retrieve(locationId, undefined, {
transactionManager: this.activeManager_,
})
} }
const salesChannelLocation = this.activeManager_.create( const salesChannelLocationRepo =
SalesChannelLocation, this.activeManager_.getRepository(SalesChannelLocation)
{
sales_channel_id: salesChannel.id,
location_id: locationId,
}
)
await this.activeManager_.save(salesChannelLocation) const salesChannelLocation = salesChannelLocationRepo.create({
sales_channel_id: salesChannel.id,
location_id: locationId,
})
await salesChannelLocationRepo.save(salesChannelLocation)
} }
/** /**
@@ -129,10 +130,18 @@ class SalesChannelLocationService extends TransactionBaseService {
* @returns {Promise<string[]>} A promise that resolves with an array of sales channel IDs. * @returns {Promise<string[]>} A promise that resolves with an array of sales channel IDs.
*/ */
async listSalesChannelIds(locationId: string): Promise<string[]> { async listSalesChannelIds(locationId: string): Promise<string[]> {
const manager = this.transactionManager_ || this.manager_ const location = await this.stockLocationService_.retrieve(
const location = await this.stockLocationService_.retrieve(locationId) locationId,
undefined,
{
transactionManager: this.activeManager_,
}
)
const salesChannelLocations = await manager.find(SalesChannelLocation, { const salesChannelRepo =
this.activeManager_.getRepository(SalesChannelLocation)
const salesChannelLocations = await salesChannelRepo.find({
where: { location_id: location.id }, where: { location_id: location.id },
select: ["sales_channel_id"], select: ["sales_channel_id"],
}) })
@@ -53,18 +53,15 @@ export default class StockLocationService {
* Lists all stock locations that match the given selector. * Lists all stock locations that match the given selector.
* @param selector - Properties to filter by. * @param selector - Properties to filter by.
* @param config - Additional configuration for the query. * @param config - Additional configuration for the query.
* @param context
* @return A list of stock locations. * @return A list of stock locations.
*/ */
@InjectEntityManager(
(target) =>
target.moduleDeclaration?.resources === MODULE_RESOURCE_TYPE.ISOLATED
)
async list( async list(
selector: FilterableStockLocationProps = {}, selector: FilterableStockLocationProps = {},
config: FindConfig<StockLocation> = { relations: [], skip: 0, take: 10 }, config: FindConfig<StockLocation> = { relations: [], skip: 0, take: 10 },
@MedusaContext() context: SharedContext = {} context: SharedContext = {}
): Promise<StockLocation[]> { ): Promise<StockLocation[]> {
const manager = context.transactionManager! const manager = context.transactionManager ?? this.manager_
const locationRepo = manager.getRepository(StockLocation) const locationRepo = manager.getRepository(StockLocation)
const query = buildQuery(selector, config) const query = buildQuery(selector, config)
@@ -75,18 +72,15 @@ export default class StockLocationService {
* Lists all stock locations that match the given selector and returns the count of matching stock locations. * Lists all stock locations that match the given selector and returns the count of matching stock locations.
* @param selector - Properties to filter by. * @param selector - Properties to filter by.
* @param config - Additional configuration for the query. * @param config - Additional configuration for the query.
* @param context
* @return A list of stock locations and the count of matching stock locations. * @return A list of stock locations and the count of matching stock locations.
*/ */
@InjectEntityManager(
(target) =>
target.moduleDeclaration?.resources === MODULE_RESOURCE_TYPE.ISOLATED
)
async listAndCount( async listAndCount(
selector: FilterableStockLocationProps = {}, selector: FilterableStockLocationProps = {},
config: FindConfig<StockLocation> = { relations: [], skip: 0, take: 10 }, config: FindConfig<StockLocation> = { relations: [], skip: 0, take: 10 },
@MedusaContext() context: SharedContext = {} context: SharedContext = {}
): Promise<[StockLocation[], number]> { ): Promise<[StockLocation[], number]> {
const manager = context.transactionManager! const manager = context.transactionManager ?? this.manager_
const locationRepo = manager.getRepository(StockLocation) const locationRepo = manager.getRepository(StockLocation)
const query = buildQuery(selector, config) const query = buildQuery(selector, config)
@@ -97,17 +91,14 @@ export default class StockLocationService {
* Retrieves a Stock Location by its ID. * Retrieves a Stock Location by its ID.
* @param stockLocationId - The ID of the stock location. * @param stockLocationId - The ID of the stock location.
* @param config - Additional configuration for the query. * @param config - Additional configuration for the query.
* @param context
* @return The stock location. * @return The stock location.
* @throws If the stock location ID is not definedor the stock location with the given ID was not found. * @throws If the stock location ID is not definedor the stock location with the given ID was not found.
*/ */
@InjectEntityManager(
(target) =>
target.moduleDeclaration?.resources === MODULE_RESOURCE_TYPE.ISOLATED
)
async retrieve( async retrieve(
stockLocationId: string, stockLocationId: string,
config: FindConfig<StockLocation> = {}, config: FindConfig<StockLocation> = {},
@MedusaContext() context: SharedContext = {} context: SharedContext = {}
): Promise<StockLocation> { ): Promise<StockLocation> {
if (!isDefined(stockLocationId)) { if (!isDefined(stockLocationId)) {
throw new MedusaError( throw new MedusaError(
@@ -116,7 +107,7 @@ export default class StockLocationService {
) )
} }
const manager = context.transactionManager! const manager = context.transactionManager ?? this.manager_
const locationRepo = manager.getRepository(StockLocation) const locationRepo = manager.getRepository(StockLocation)
const query = buildQuery({ id: stockLocationId }, config) const query = buildQuery({ id: stockLocationId }, config)
@@ -135,6 +126,7 @@ export default class StockLocationService {
/** /**
* Creates a new stock location. * Creates a new stock location.
* @param data - The input data for creating a Stock Location. * @param data - The input data for creating a Stock Location.
* @param context
* @returns The created stock location. * @returns The created stock location.
*/ */
@InjectEntityManager( @InjectEntityManager(
@@ -182,6 +174,7 @@ export default class StockLocationService {
* Updates an existing stock location. * Updates an existing stock location.
* @param stockLocationId - The ID of the stock location to update. * @param stockLocationId - The ID of the stock location to update.
* @param updateData - The update data for the stock location. * @param updateData - The update data for the stock location.
* @param context
* @returns The updated stock location. * @returns The updated stock location.
*/ */
@InjectEntityManager( @InjectEntityManager(
@@ -231,6 +224,7 @@ export default class StockLocationService {
* Updates an address for a Stock Location. * Updates an address for a Stock Location.
* @param addressId - The ID of the address to update. * @param addressId - The ID of the address to update.
* @param address - The update data for the address. * @param address - The update data for the address.
* @param context
* @returns The updated stock location address. * @returns The updated stock location address.
*/ */
@InjectEntityManager( @InjectEntityManager(
@@ -275,6 +269,7 @@ export default class StockLocationService {
/** /**
* Deletes a Stock Location. * Deletes a Stock Location.
* @param id - The ID of the stock location to delete. * @param id - The ID of the stock location to delete.
* @param context
* @returns An empty promise. * @returns An empty promise.
*/ */
@InjectEntityManager( @InjectEntityManager(