feat(fulfillment): Soft deletes (#6630)
**What** - Ensure soft delete works properly according to the soft delete configuration and validate all relation of the entire data model - Add is_enabled to the providers in order to manage new providers to enabled or disabled - include joiner config update FIXES CORE-1853 FIXES CORE-1830 FIXES CORE-1719
This commit is contained in:
@@ -1,66 +1,19 @@
|
||||
import {
|
||||
createPsqlIndexStatementHelper,
|
||||
DALUtils,
|
||||
generateEntityId,
|
||||
} from "@medusajs/utils"
|
||||
|
||||
import { DAL } from "@medusajs/types"
|
||||
import { generateEntityId } from "@medusajs/utils"
|
||||
import {
|
||||
BeforeCreate,
|
||||
Collection,
|
||||
Entity,
|
||||
Filter,
|
||||
OneToMany,
|
||||
OnInit,
|
||||
OptionalProps,
|
||||
PrimaryKey,
|
||||
Property,
|
||||
} from "@mikro-orm/core"
|
||||
import ShippingOption from "./shipping-option"
|
||||
|
||||
type FulfillmentProviderOptionalProps = DAL.SoftDeletableEntityDateColumns
|
||||
|
||||
const DeletedAtIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "fulfillment_provider",
|
||||
columns: "deleted_at",
|
||||
where: "deleted_at IS NOT NULL",
|
||||
})
|
||||
|
||||
@Entity()
|
||||
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
|
||||
export default class FulfillmentProvider {
|
||||
[OptionalProps]?: FulfillmentProviderOptionalProps
|
||||
|
||||
@PrimaryKey({ columnType: "text" })
|
||||
id: string
|
||||
|
||||
@Property({ columnType: "jsonb", nullable: true })
|
||||
metadata: Record<string, unknown> | null = null
|
||||
|
||||
@OneToMany(
|
||||
() => ShippingOption,
|
||||
(shippingOption) => shippingOption.fulfillment_provider
|
||||
)
|
||||
shipping_options = new Collection<ShippingOption>(this)
|
||||
|
||||
@Property({
|
||||
onCreate: () => new Date(),
|
||||
columnType: "timestamptz",
|
||||
defaultRaw: "now()",
|
||||
})
|
||||
created_at: Date
|
||||
|
||||
@Property({
|
||||
onCreate: () => new Date(),
|
||||
onUpdate: () => new Date(),
|
||||
columnType: "timestamptz",
|
||||
defaultRaw: "now()",
|
||||
})
|
||||
updated_at: Date
|
||||
|
||||
@DeletedAtIndex.MikroORMIndex()
|
||||
@Property({ columnType: "timestamptz", nullable: true })
|
||||
deleted_at: Date | null = null
|
||||
@Property({ columnType: "boolean", defaultRaw: "true" })
|
||||
is_enabled: boolean = true
|
||||
|
||||
@BeforeCreate()
|
||||
onCreate() {
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
import { DAL } from "@medusajs/types"
|
||||
import {
|
||||
BeforeCreate,
|
||||
Cascade,
|
||||
Collection,
|
||||
Entity,
|
||||
Filter,
|
||||
@@ -93,6 +94,8 @@ export default class Fulfillment {
|
||||
columnType: "text",
|
||||
fieldName: "provider_id",
|
||||
mapToPk: true,
|
||||
nullable: true,
|
||||
onDelete: "set null",
|
||||
})
|
||||
@FulfillmentProviderIdIndex.MikroORMIndex()
|
||||
provider_id: string
|
||||
@@ -102,6 +105,7 @@ export default class Fulfillment {
|
||||
fieldName: "shipping_option_id",
|
||||
nullable: true,
|
||||
mapToPk: true,
|
||||
onDelete: "set null",
|
||||
})
|
||||
@FulfillmentShippingOptionIdIndex.MikroORMIndex()
|
||||
shipping_option_id: string | null = null
|
||||
@@ -115,13 +119,25 @@ export default class Fulfillment {
|
||||
@ManyToOne(() => FulfillmentProvider, { persist: false })
|
||||
provider: FulfillmentProvider
|
||||
|
||||
@OneToOne()
|
||||
@OneToOne({
|
||||
entity: () => Address,
|
||||
owner: true,
|
||||
cascade: [Cascade.PERSIST, "soft-remove"] as any,
|
||||
nullable: true,
|
||||
onDelete: "cascade",
|
||||
})
|
||||
delivery_address!: Address
|
||||
|
||||
@OneToMany(() => FulfillmentItem, (item) => item.fulfillment)
|
||||
@OneToMany(() => FulfillmentItem, (item) => item.fulfillment, {
|
||||
cascade: [Cascade.PERSIST, "soft-remove"] as any,
|
||||
orphanRemoval: true,
|
||||
})
|
||||
items = new Collection<FulfillmentItem>(this)
|
||||
|
||||
@OneToMany(() => FulfillmentLabel, (label) => label.fulfillment)
|
||||
@OneToMany(() => FulfillmentLabel, (label) => label.fulfillment, {
|
||||
cascade: [Cascade.PERSIST, "soft-remove"] as any,
|
||||
orphanRemoval: true,
|
||||
})
|
||||
labels = new Collection<FulfillmentLabel>(this)
|
||||
|
||||
@Property({
|
||||
|
||||
@@ -78,11 +78,11 @@ export default class GeoZone {
|
||||
type: "text",
|
||||
mapToPk: true,
|
||||
fieldName: "service_zone_id",
|
||||
onDelete: "cascade",
|
||||
})
|
||||
@ServiceZoneIdIndex.MikroORMIndex()
|
||||
service_zone_id: string
|
||||
|
||||
// TODO: Do we have an example or idea of what would be stored in this field? like lat/long for example?
|
||||
@Property({ columnType: "jsonb", nullable: true })
|
||||
postal_expression: Record<string, unknown> | null = null
|
||||
|
||||
|
||||
@@ -65,6 +65,7 @@ export default class ServiceZone {
|
||||
type: "text",
|
||||
mapToPk: true,
|
||||
fieldName: "fulfillment_set_id",
|
||||
onDelete: "cascade",
|
||||
})
|
||||
@FulfillmentSetIdIndex.MikroORMIndex()
|
||||
fulfillment_set_id: string
|
||||
@@ -80,7 +81,11 @@ export default class ServiceZone {
|
||||
|
||||
@OneToMany(
|
||||
() => ShippingOption,
|
||||
(shippingOption) => shippingOption.service_zone
|
||||
(shippingOption) => shippingOption.service_zone,
|
||||
{
|
||||
cascade: [Cascade.PERSIST, "soft-remove"] as any,
|
||||
orphanRemoval: true,
|
||||
}
|
||||
)
|
||||
shipping_options = new Collection<ShippingOption>(this)
|
||||
|
||||
|
||||
@@ -57,6 +57,7 @@ export default class ShippingOptionRule {
|
||||
type: "text",
|
||||
mapToPk: true,
|
||||
fieldName: "shipping_option_id",
|
||||
onDelete: "cascade",
|
||||
})
|
||||
@ShippingOptionIdIndex.MikroORMIndex()
|
||||
shipping_option_id: string
|
||||
|
||||
@@ -50,6 +50,7 @@ export default class ShippingOptionType {
|
||||
|
||||
@OneToOne(() => ShippingOption, (so) => so.type, {
|
||||
type: "text",
|
||||
onDelete: "cascade",
|
||||
})
|
||||
shipping_option: ShippingOption
|
||||
|
||||
|
||||
@@ -81,6 +81,7 @@ export default class ShippingOption {
|
||||
type: "text",
|
||||
fieldName: "service_zone_id",
|
||||
mapToPk: true,
|
||||
onDelete: "cascade",
|
||||
})
|
||||
@ServiceZoneIdIndex.MikroORMIndex()
|
||||
service_zone_id: string
|
||||
@@ -90,6 +91,7 @@ export default class ShippingOption {
|
||||
fieldName: "shipping_profile_id",
|
||||
mapToPk: true,
|
||||
nullable: true,
|
||||
onDelete: "set null",
|
||||
})
|
||||
@ShippingProfileIdIndex.MikroORMIndex()
|
||||
shipping_profile_id: string | null
|
||||
@@ -128,9 +130,10 @@ export default class ShippingOption {
|
||||
|
||||
@OneToOne(() => ShippingOptionType, (so) => so.shipping_option, {
|
||||
owner: true,
|
||||
cascade: [Cascade.PERSIST, Cascade.REMOVE, "soft-remove"] as any,
|
||||
cascade: [Cascade.PERSIST, "soft-remove"] as any,
|
||||
orphanRemoval: true,
|
||||
fieldName: "shipping_option_type_id",
|
||||
onDelete: "cascade",
|
||||
})
|
||||
type: ShippingOptionType
|
||||
|
||||
|
||||
Reference in New Issue
Block a user