Feat(medusa) - delete cascade modules associations (#3190)

* delete cascade sales channel x locations, variant x inventory item
This commit is contained in:
Carlos R. L. Rodrigues
2023-02-08 17:23:47 -03:00
committed by GitHub
parent 3b474ec35c
commit d859ccf551
18 changed files with 672 additions and 63 deletions
@@ -1,6 +1,7 @@
import { Request, Response } from "express"
import { EntityManager } from "typeorm"
import { IInventoryService } from "../../../../interfaces"
import { ProductVariantInventoryService } from "../../../../services"
/**
* @oas [delete] /inventory-items/{id}
@@ -47,8 +48,15 @@ export default async (req: Request, res: Response) => {
const inventoryService: IInventoryService =
req.scope.resolve("inventoryService")
const productVariantInventoryService: ProductVariantInventoryService =
req.scope.resolve("productVariantInventoryService")
const manager: EntityManager = req.scope.resolve("manager")
await manager.transaction(async (transactionManager) => {
await productVariantInventoryService
.withTransaction(transactionManager)
.detachInventoryItem(id)
await inventoryService
.withTransaction(transactionManager)
.deleteInventoryItem(id)
@@ -1,5 +1,6 @@
import { EntityManager } from "typeorm"
import { IStockLocationService } from "../../../../interfaces"
import { SalesChannelLocationService } from "../../../../services"
/**
* @oas [delete] /stock-locations/{id}
@@ -59,8 +60,15 @@ export default async (req, res) => {
"stockLocationService"
)
const salesChannelLocationService: SalesChannelLocationService =
req.scope.resolve("salesChannelLocationService")
const manager: EntityManager = req.scope.resolve("manager")
await manager.transaction(async (transactionManager) => {
await salesChannelLocationService
.withTransaction(transactionManager)
.removeLocation(id)
await stockLocationService.withTransaction(transactionManager).delete(id)
})
@@ -72,9 +72,7 @@ export default async (req, res) => {
const inventoryService: IInventoryService =
req.scope.resolve("inventoryService")
const channelLocationService: SalesChannelLocationService = req.scope.resolve(
"salesChannelLocationService"
)
const channelService: SalesChannelService = req.scope.resolve(
"salesChannelService"
)
@@ -93,15 +91,11 @@ export default async (req, res) => {
sales_channel_availability: [],
}
const [rawChannels] = await channelService.listAndCount({})
const channels: SalesChannelDTO[] = await Promise.all(
rawChannels.map(async (channel) => {
const locations = await channelLocationService.listLocations(channel.id)
return {
...channel,
locations,
}
})
const [channels] = await channelService.listAndCount(
{},
{
relations: ["locations"],
}
)
const inventory =
@@ -122,7 +116,7 @@ export default async (req, res) => {
const quantity = await inventoryService.retrieveAvailableQuantity(
inventory[0].id,
channel.locations
channel.locations.map((loc) => loc.id)
)
return {
@@ -139,10 +133,6 @@ export default async (req, res) => {
})
}
type SalesChannelDTO = Omit<SalesChannel, "beforeInsert"> & {
locations: string[]
}
type ResponseInventoryItem = Partial<InventoryItemDTO> & {
location_levels?: InventoryLevelDTO[]
}