feat(medusa, admin-ui, medusa-react, medusa-js): Allow toggling of manage inventory (#3435)

**What**
- Toggle manage inventory in the inventory management modal

**How**
- Create/update/remove inventory item based on if `manage_inventory` is set and if an inventory item already exists
- Move all stock location updates to when the modal is submitted
- Add create-inventory-item endpoint in the core

Fixes CORE-1196

Co-authored-by: Sebastian Rindom <7554214+srindom@users.noreply.github.com>
This commit is contained in:
Philip Korsholm
2023-03-14 16:14:31 +00:00
committed by GitHub
co-authored by Sebastian Rindom
parent 30a3203640
commit fe9eea4c18
18 changed files with 844 additions and 219 deletions
@@ -1,4 +1,4 @@
import { DeepPartial, EntityManager, FindManyOptions } from "typeorm"
import { DeepPartial, EntityManager, FindManyOptions, In } from "typeorm"
import { isDefined, MedusaError } from "medusa-core-utils"
import {
buildQuery,
@@ -190,14 +190,34 @@ export default class InventoryLevelService extends TransactionBaseService {
}
/**
* Deletes an inventory level by ID.
* @param inventoryLevelId - The ID of the inventory level to delete.
*/
async delete(inventoryLevelId: string): Promise<void> {
* Deletes inventory levels by inventory Item ID.
* @param inventoryItemId - The ID or IDs of the inventory item to delete inventory levels for.
*/
async deleteByInventoryItemId(inventoryItemId: string | string[]): Promise<void> {
const ids = Array.isArray(inventoryItemId) ? inventoryItemId : [inventoryItemId]
await this.atomicPhase_(async (manager) => {
const levelRepository = manager.getRepository(InventoryLevel)
await levelRepository.delete({ id: inventoryLevelId })
await levelRepository.delete({ inventory_item_id: In(ids) })
await this.eventBusService_
.withTransaction(manager)
.emit(InventoryLevelService.Events.DELETED, {
inventory_item_id: inventoryItemId,
})
})
}
/**
* Deletes an inventory level by ID.
* @param inventoryLevelId - The ID or IDs of the inventory level to delete.
*/
async delete(inventoryLevelId: string | string[]): Promise<void> {
const ids = Array.isArray(inventoryLevelId) ? inventoryLevelId : [inventoryLevelId]
await this.atomicPhase_(async (manager) => {
const levelRepository = manager.getRepository(InventoryLevel)
await levelRepository.delete({ id: In(ids) })
await this.eventBusService_
.withTransaction(manager)
@@ -245,6 +245,10 @@ export default class InventoryService
* @param inventoryItemId - the id of the inventory item to delete
*/
async deleteInventoryItem(inventoryItemId: string): Promise<void> {
await this.inventoryLevelService_
.withTransaction(this.activeManager_)
.deleteByInventoryItemId(inventoryItemId)
return await this.inventoryItemService_
.withTransaction(this.activeManager_)
.delete(inventoryItemId)