Feat(medusa) - delete cascade modules associations (#3190)
* delete cascade sales channel x locations, variant x inventory item
This commit is contained in:
@@ -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[]
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import { MigrationInterface, QueryRunner } from "typeorm"
|
||||
|
||||
export class multiLocationSoftDelete1675689306130
|
||||
implements MigrationInterface
|
||||
{
|
||||
name = "multiLocationSoftDelete1675689306130"
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`
|
||||
ALTER TABLE sales_channel_location
|
||||
ADD COLUMN "deleted_at" TIMESTAMP WITH TIME ZONE;
|
||||
|
||||
DROP INDEX "IDX_6caaa358f12ed0b846f00e2dcd";
|
||||
DROP INDEX "IDX_c2203162ca946a71aeb98390b0";
|
||||
|
||||
CREATE INDEX "IDX_sales_channel_location_sales_channel_id" ON "sales_channel_location" ("sales_channel_id") WHERE deleted_at IS NULL;
|
||||
CREATE INDEX "IDX_sales_channel_location_location_id" ON "sales_channel_location" ("location_id") WHERE deleted_at IS NULL;
|
||||
|
||||
ALTER TABLE product_variant_inventory_item
|
||||
ADD COLUMN "deleted_at" TIMESTAMP WITH TIME ZONE;
|
||||
|
||||
DROP INDEX "IDX_c74e8c2835094a37dead376a3b";
|
||||
DROP INDEX "IDX_bf5386e7f2acc460adbf96d6f3";
|
||||
|
||||
CREATE INDEX "IDX_product_variant_inventory_item_inventory_item_id" ON "product_variant_inventory_item" ("inventory_item_id") WHERE deleted_at IS NULL;
|
||||
CREATE INDEX "IDX_product_variant_inventory_item_variant_id" ON "product_variant_inventory_item" ("variant_id") WHERE deleted_at IS NULL;
|
||||
`)
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`
|
||||
DROP INDEX "IDX_sales_channel_location_sales_channel_id";
|
||||
DROP INDEX "IDX_sales_channel_location_location_id";
|
||||
|
||||
DROP INDEX "IDX_product_variant_inventory_item_inventory_item_id";
|
||||
DROP INDEX "IDX_product_variant_inventory_item_variant_id";
|
||||
|
||||
CREATE INDEX "IDX_6caaa358f12ed0b846f00e2dcd" ON "sales_channel_location" ("sales_channel_id");
|
||||
CREATE INDEX "IDX_c2203162ca946a71aeb98390b0" ON "sales_channel_location" ("location_id");
|
||||
|
||||
CREATE INDEX "IDX_c74e8c2835094a37dead376a3b" ON "product_variant_inventory_item" ("inventory_item_id");
|
||||
CREATE INDEX "IDX_bf5386e7f2acc460adbf96d6f3" ON "product_variant_inventory_item" ("variant_id");
|
||||
|
||||
ALTER TABLE sales_channel_location
|
||||
DROP COLUMN "deleted_at";
|
||||
|
||||
ALTER TABLE product_variant_inventory_item
|
||||
DROP COLUMN "deleted_at";
|
||||
`)
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,29 @@
|
||||
import { Index, Unique, BeforeInsert, Column, Entity } from "typeorm"
|
||||
import { BaseEntity } from "../interfaces/models/base-entity"
|
||||
import { DbAwareColumn, generateEntityId } from "../utils"
|
||||
import {
|
||||
Index,
|
||||
BeforeInsert,
|
||||
Column,
|
||||
Entity,
|
||||
ManyToOne,
|
||||
JoinColumn,
|
||||
} from "typeorm"
|
||||
import { SoftDeletableEntity } from "../interfaces"
|
||||
import { generateEntityId } from "../utils"
|
||||
import { ProductVariant } from "./product-variant"
|
||||
|
||||
@Entity()
|
||||
@Unique(["variant_id", "inventory_item_id"])
|
||||
export class ProductVariantInventoryItem extends BaseEntity {
|
||||
export class ProductVariantInventoryItem extends SoftDeletableEntity {
|
||||
@Index()
|
||||
@DbAwareColumn({ type: "text" })
|
||||
@Column({ type: "text" })
|
||||
inventory_item_id: string
|
||||
|
||||
@Index()
|
||||
@DbAwareColumn({ type: "text" })
|
||||
@Column({ type: "text" })
|
||||
variant_id: string
|
||||
|
||||
@ManyToOne(() => ProductVariant, (variant) => variant.inventory_items)
|
||||
@JoinColumn({ name: "variant_id" })
|
||||
variant: ProductVariant
|
||||
|
||||
@Column({ type: "int", default: 1 })
|
||||
required_quantity: number
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ import { Product } from "./product"
|
||||
import { ProductOptionValue } from "./product-option-value"
|
||||
import { SoftDeletableEntity } from "../interfaces/models/soft-deletable-entity"
|
||||
import { generateEntityId } from "../utils/generate-entity-id"
|
||||
import { ProductVariantInventoryItem } from "./product-variant-inventory-item"
|
||||
|
||||
@Entity()
|
||||
export class ProductVariant extends SoftDeletableEntity {
|
||||
@@ -91,6 +92,15 @@ export class ProductVariant extends SoftDeletableEntity {
|
||||
})
|
||||
options: ProductOptionValue[]
|
||||
|
||||
@OneToMany(
|
||||
() => ProductVariantInventoryItem,
|
||||
(inventoryItem) => inventoryItem.variant,
|
||||
{
|
||||
cascade: ["soft-remove", "remove"],
|
||||
}
|
||||
)
|
||||
inventory_items: ProductVariantInventoryItem[]
|
||||
|
||||
@DbAwareColumn({ type: "jsonb", nullable: true })
|
||||
metadata: Record<string, unknown>
|
||||
|
||||
@@ -199,6 +209,11 @@ export class ProductVariant extends SoftDeletableEntity {
|
||||
* type: array
|
||||
* items:
|
||||
* $ref: "#/components/schemas/ProductOptionValue"
|
||||
* inventory_items:
|
||||
* description: The Inventory Items related to the product variant. Available if the relation `inventory_items` is expanded.
|
||||
* type: array
|
||||
* items:
|
||||
* $ref: "#/components/schemas/ProductVariantInventoryItem"
|
||||
* created_at:
|
||||
* type: string
|
||||
* description: "The date with timezone at which the resource was created."
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { BeforeInsert, Index, Column } from "typeorm"
|
||||
import { BeforeInsert, Index, Column, ManyToOne, JoinColumn } from "typeorm"
|
||||
|
||||
import { FeatureFlagEntity } from "../utils/feature-flag-decorators"
|
||||
import { BaseEntity } from "../interfaces"
|
||||
import { SoftDeletableEntity } from "../interfaces"
|
||||
import { generateEntityId } from "../utils"
|
||||
import { SalesChannel } from "./sales-channel"
|
||||
|
||||
@FeatureFlagEntity("sales_channels")
|
||||
export class SalesChannelLocation extends BaseEntity {
|
||||
export class SalesChannelLocation extends SoftDeletableEntity {
|
||||
@Index()
|
||||
@Column({ type: "text" })
|
||||
sales_channel_id: string
|
||||
@@ -14,8 +15,42 @@ export class SalesChannelLocation extends BaseEntity {
|
||||
@Column({ type: "text" })
|
||||
location_id: string
|
||||
|
||||
@ManyToOne(() => SalesChannel, (sc) => sc.locations)
|
||||
@JoinColumn({ name: "sales_channel_id" })
|
||||
sales_channel: SalesChannel
|
||||
|
||||
@BeforeInsert()
|
||||
private beforeInsert(): void {
|
||||
this.id = generateEntityId(this.id, "scloc")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @schema SalesChannelLocation
|
||||
* title: "Sales Channel Stock Location"
|
||||
* description: "Sales Channel Stock Location link sales channels with stock locations."
|
||||
* type: object
|
||||
* properties:
|
||||
* id:
|
||||
* type: string
|
||||
* description: The Sales Channel Stock Location's ID
|
||||
* example: scloc_01G8X9A7ESKAJXG2H0E6F1MW7A
|
||||
* sales_channel_id:
|
||||
* description: "The id of the Sales Channel"
|
||||
* type: string
|
||||
* location_id:
|
||||
* description: "The id of the Location Stock."
|
||||
* type: string
|
||||
* created_at:
|
||||
* type: string
|
||||
* description: "The date with timezone at which the resource was created."
|
||||
* format: date-time
|
||||
* updated_at:
|
||||
* type: string
|
||||
* description: "The date with timezone at which the resource was updated."
|
||||
* format: date-time
|
||||
* deleted_at:
|
||||
* type: string
|
||||
* description: "The date with timezone at which the resource was deleted."
|
||||
* format: date-time
|
||||
*/
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { BeforeInsert, Column } from "typeorm"
|
||||
import { BeforeInsert, Column, OneToMany } from "typeorm"
|
||||
|
||||
import { FeatureFlagEntity } from "../utils/feature-flag-decorators"
|
||||
import { SoftDeletableEntity } from "../interfaces"
|
||||
import { generateEntityId } from "../utils"
|
||||
import { SalesChannelLocation } from "./sales-channel-location"
|
||||
|
||||
@FeatureFlagEntity("sales_channels")
|
||||
export class SalesChannel extends SoftDeletableEntity {
|
||||
@@ -15,6 +16,15 @@ export class SalesChannel extends SoftDeletableEntity {
|
||||
@Column({ default: false })
|
||||
is_disabled: boolean
|
||||
|
||||
@OneToMany(
|
||||
() => SalesChannelLocation,
|
||||
(scLocation) => scLocation.sales_channel,
|
||||
{
|
||||
cascade: ["soft-remove", "remove"],
|
||||
}
|
||||
)
|
||||
locations: SalesChannelLocation[]
|
||||
|
||||
@BeforeInsert()
|
||||
private beforeInsert(): void {
|
||||
this.id = generateEntityId(this.id, "sc")
|
||||
@@ -45,6 +55,11 @@ export class SalesChannel extends SoftDeletableEntity {
|
||||
* description: "Specify if the sales channel is enabled or disabled."
|
||||
* type: boolean
|
||||
* default: false
|
||||
* locations:
|
||||
* description: The Stock Locations related to the sales channel. Available if the relation `locations` is expanded.
|
||||
* type: array
|
||||
* items:
|
||||
* $ref: "#/components/schemas/SalesChannelLocation"
|
||||
* created_at:
|
||||
* type: string
|
||||
* description: "The date with timezone at which the resource was created."
|
||||
|
||||
@@ -311,12 +311,12 @@ class ProductVariantInventoryService extends TransactionBaseService {
|
||||
|
||||
/**
|
||||
* Remove a variant from an inventory item
|
||||
* @param variantId variant id
|
||||
* @param variantId variant id or undefined if all the variants will be affected
|
||||
* @param inventoryItemId inventory item id
|
||||
*/
|
||||
async detachInventoryItem(
|
||||
variantId: string,
|
||||
inventoryItemId: string
|
||||
inventoryItemId: string,
|
||||
variantId?: string
|
||||
): Promise<void> {
|
||||
const manager = this.transactionManager_ || this.manager_
|
||||
|
||||
@@ -324,15 +324,19 @@ class ProductVariantInventoryService extends TransactionBaseService {
|
||||
ProductVariantInventoryItem
|
||||
)
|
||||
|
||||
const existing = await variantInventoryRepo.findOne({
|
||||
where: {
|
||||
variant_id: variantId,
|
||||
inventory_item_id: inventoryItemId,
|
||||
},
|
||||
const where: any = {
|
||||
inventory_item_id: inventoryItemId,
|
||||
}
|
||||
if (variantId) {
|
||||
where.variant_id = variantId
|
||||
}
|
||||
|
||||
const varInvItems = await variantInventoryRepo.find({
|
||||
where,
|
||||
})
|
||||
|
||||
if (existing) {
|
||||
await variantInventoryRepo.remove(existing)
|
||||
if (varInvItems.length) {
|
||||
await variantInventoryRepo.remove(varInvItems)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -655,7 +655,7 @@ class ProductVariantService extends TransactionBaseService {
|
||||
|
||||
const variant = await variantRepo.findOne({
|
||||
where: { id: variantId },
|
||||
relations: ["prices", "options"],
|
||||
relations: ["prices", "options", "inventory_items"],
|
||||
})
|
||||
|
||||
if (!variant) {
|
||||
|
||||
@@ -33,19 +33,19 @@ class SalesChannelInventoryService {
|
||||
/**
|
||||
* Retrieves the available quantity of an item across all sales channel locations
|
||||
* @param salesChannelId Sales channel id
|
||||
* @param itemId Item id
|
||||
* @param inventoryItemId Item id
|
||||
* @returns available quantity of item across all sales channel locations
|
||||
*/
|
||||
async retrieveAvailableItemQuantity(
|
||||
salesChannelId: string,
|
||||
itemId: string
|
||||
inventoryItemId: string
|
||||
): Promise<number> {
|
||||
const locations = await this.salesChannelLocationService_.listLocations(
|
||||
salesChannelId
|
||||
)
|
||||
|
||||
return await this.inventoryService_.retrieveAvailableQuantity(
|
||||
itemId,
|
||||
inventoryItemId,
|
||||
locations
|
||||
)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { EntityManager } from "typeorm"
|
||||
import { IStockLocationService, TransactionBaseService } from "../interfaces"
|
||||
import { SalesChannelService, EventBusService } from "./"
|
||||
|
||||
import { SalesChannelLocation } from "../models"
|
||||
import { SalesChannelLocation } from "../models/sales-channel-location"
|
||||
|
||||
type InjectedDependencies = {
|
||||
stockLocationService: IStockLocationService
|
||||
@@ -40,26 +40,39 @@ class SalesChannelLocationService extends TransactionBaseService {
|
||||
|
||||
/**
|
||||
* Removes an association between a sales channel and a stock location.
|
||||
* @param {string} salesChannelId - The ID of the sales channel.
|
||||
* @param {string} locationId - The ID of the stock location.
|
||||
* @returns {Promise<void>} A promise that resolves when the association has been removed.
|
||||
* @param salesChannelId - The ID of the sales channel or undefined if all the sales channel will be affected.
|
||||
* @param locationId - The ID of the stock location.
|
||||
* @returns A promise that resolves when the association has been removed.
|
||||
*/
|
||||
async removeLocation(
|
||||
salesChannelId: string,
|
||||
locationId: string
|
||||
locationId: string,
|
||||
salesChannelId?: string
|
||||
): Promise<void> {
|
||||
const manager = this.transactionManager_ || this.manager_
|
||||
await manager.delete(SalesChannelLocation, {
|
||||
sales_channel_id: salesChannelId,
|
||||
|
||||
const salesChannelLocationRepo = manager.getRepository(SalesChannelLocation)
|
||||
|
||||
const where: any = {
|
||||
location_id: locationId,
|
||||
}
|
||||
if (salesChannelId) {
|
||||
where.sales_channel_id = salesChannelId
|
||||
}
|
||||
|
||||
const scLoc = await salesChannelLocationRepo.find({
|
||||
where,
|
||||
})
|
||||
|
||||
if (scLoc.length) {
|
||||
await salesChannelLocationRepo.remove(scLoc)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Associates a sales channel with a stock location.
|
||||
* @param {string} salesChannelId - The ID of the sales channel.
|
||||
* @param {string} locationId - The ID of the stock location.
|
||||
* @returns {Promise<void>} A promise that resolves when the association has been created.
|
||||
* @param salesChannelId - The ID of the sales channel.
|
||||
* @param locationId - The ID of the stock location.
|
||||
* @returns A promise that resolves when the association has been created.
|
||||
*/
|
||||
async associateLocation(
|
||||
salesChannelId: string,
|
||||
@@ -70,16 +83,14 @@ class SalesChannelLocationService extends TransactionBaseService {
|
||||
.withTransaction(manager)
|
||||
.retrieve(salesChannelId)
|
||||
|
||||
const stockLocationId = locationId
|
||||
|
||||
if (this.stockLocationService) {
|
||||
const stockLocation = await this.stockLocationService.retrieve(locationId)
|
||||
locationId = stockLocation.id
|
||||
// trhows error if not found
|
||||
await this.stockLocationService.retrieve(locationId)
|
||||
}
|
||||
|
||||
const salesChannelLocation = manager.create(SalesChannelLocation, {
|
||||
sales_channel_id: salesChannel.id,
|
||||
location_id: stockLocationId,
|
||||
location_id: locationId,
|
||||
})
|
||||
|
||||
await manager.save(salesChannelLocation)
|
||||
@@ -87,8 +98,8 @@ class SalesChannelLocationService extends TransactionBaseService {
|
||||
|
||||
/**
|
||||
* Lists the stock locations associated with a sales channel.
|
||||
* @param {string} salesChannelId - The ID of the sales channel.
|
||||
* @returns {Promise<string[]>} A promise that resolves with an array of location IDs.
|
||||
* @param salesChannelId - The ID of the sales channel.
|
||||
* @returns A promise that resolves with an array of location IDs.
|
||||
*/
|
||||
async listLocations(salesChannelId: string): Promise<string[]> {
|
||||
const manager = this.transactionManager_ || this.manager_
|
||||
|
||||
@@ -230,9 +230,9 @@ class SalesChannelService extends TransactionBaseService {
|
||||
this.salesChannelRepository_
|
||||
)
|
||||
|
||||
const salesChannel = await this.retrieve(salesChannelId).catch(
|
||||
() => void 0
|
||||
)
|
||||
const salesChannel = await this.retrieve(salesChannelId, {
|
||||
relations: ["locations"],
|
||||
}).catch(() => void 0)
|
||||
|
||||
if (!salesChannel) {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user