feat(tax): soft deletes (#6486)

**What**
- Adds soft deletes
This commit is contained in:
Sebastian Rindom
2024-02-26 08:34:46 +00:00
committed by GitHub
parent 3b18f399c1
commit d983329481
6 changed files with 387 additions and 31 deletions
+70 -10
View File
@@ -1,35 +1,77 @@
import { DAL } from "@medusajs/types"
import {
DALUtils,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
Cascade,
Entity,
ManyToOne,
PrimaryKey,
PrimaryKeyProp,
Property,
Filter,
OptionalProps,
BeforeCreate,
OnInit,
} from "@mikro-orm/core"
import TaxRate from "./tax-rate"
const TABLE_NAME = "tax_rate_rule"
type OptionalRuleProps = DAL.SoftDeletableEntityDateColumns
const taxRateIdIndexName = "IDX_tax_rate_rule_tax_rate_id"
const taxRateIdIndexStatement = createPsqlIndexStatementHelper({
name: taxRateIdIndexName,
tableName: TABLE_NAME,
columns: "tax_rate_id",
where: "deleted_at IS NULL",
})
const referenceIdIndexName = "IDX_tax_rate_rule_reference_id"
const referenceIdIndexStatement = createPsqlIndexStatementHelper({
name: referenceIdIndexName,
tableName: TABLE_NAME,
columns: "reference_id",
where: "deleted_at IS NULL",
})
const uniqueRateReferenceIndexName = "IDX_tax_rate_rule_unique_rate_reference"
const uniqueRateReferenceIndexStatement = createPsqlIndexStatementHelper({
name: uniqueRateReferenceIndexName,
tableName: TABLE_NAME,
columns: ["tax_rate_id", "reference_id"],
unique: true,
where: "deleted_at IS NULL",
})
@Entity({ tableName: TABLE_NAME })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
@uniqueRateReferenceIndexStatement.MikroORMIndex()
export default class TaxRateRule {
@PrimaryKey({ columnType: "text" })
tax_rate_id!: string
[OptionalProps]?: OptionalRuleProps
@PrimaryKey({ columnType: "text" })
reference_id!: string;
id!: string
[PrimaryKeyProp]?: ["tax_rate_id", "reference_id"]
@ManyToOne(() => TaxRate, {
type: "text",
fieldName: "tax_rate_id",
mapToPk: true,
cascade: [Cascade.REMOVE],
})
@taxRateIdIndexStatement.MikroORMIndex()
tax_rate_id: string
@Property({ columnType: "text" })
@referenceIdIndexStatement.MikroORMIndex()
reference_id: string
@Property({ columnType: "text" })
reference: string
@ManyToOne(() => TaxRate, {
fieldName: "tax_rate_id",
index: taxRateIdIndexName,
cascade: [Cascade.REMOVE, Cascade.PERSIST],
})
@ManyToOne(() => TaxRate, { persist: false })
tax_rate: TaxRate
@Property({ columnType: "jsonb", nullable: true })
@@ -52,4 +94,22 @@ export default class TaxRateRule {
@Property({ columnType: "text", nullable: true })
created_by: string | null = null
@createPsqlIndexStatementHelper({
tableName: TABLE_NAME,
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "txr")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "txr")
}
}
+33 -12
View File
@@ -1,9 +1,11 @@
import { DAL } from "@medusajs/types"
import {
DALUtils,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
Filter,
BeforeCreate,
Cascade,
Collection,
@@ -18,25 +20,32 @@ import {
import TaxRegion from "./tax-region"
import TaxRateRule from "./tax-rate-rule"
type OptionalTaxRateProps = DAL.EntityDateColumns
type OptionalTaxRateProps = DAL.SoftDeletableEntityDateColumns
const TABLE_NAME = "tax_rate"
const taxRegionIdIndexName = "IDX_tax_rate_tax_region_id"
const singleDefaultRegionIndexName = "IDX_single_default_region"
const singleDefaultRegionIndexStatement = createPsqlIndexStatementHelper({
name: singleDefaultRegionIndexName,
tableName: TABLE_NAME,
columns: "tax_region_id",
unique: true,
where: "is_default = true",
where: "is_default = true AND deleted_at IS NULL",
})
const taxRegionIdIndexName = "IDX_tax_rate_tax_region_id"
const taxRegionIdIndexStatement = createPsqlIndexStatementHelper({
name: taxRegionIdIndexName,
tableName: TABLE_NAME,
columns: "tax_region_id",
where: "deleted_at IS NULL",
})
@singleDefaultRegionIndexStatement.MikroORMIndex()
@Entity({ tableName: TABLE_NAME })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class TaxRate {
[OptionalProps]: OptionalTaxRateProps
[OptionalProps]?: OptionalTaxRateProps
@PrimaryKey({ columnType: "text" })
id!: string
@@ -56,17 +65,21 @@ export default class TaxRate {
@Property({ columnType: "bool", default: false })
is_combinable = false
@Property({ columnType: "text" })
@ManyToOne(() => TaxRegion, {
type: "text",
fieldName: "tax_region_id",
mapToPk: true,
cascade: [Cascade.REMOVE],
})
@taxRegionIdIndexStatement.MikroORMIndex()
tax_region_id: string
@ManyToOne(() => TaxRegion, {
fieldName: "tax_region_id",
index: taxRegionIdIndexName,
cascade: [Cascade.REMOVE, Cascade.PERSIST],
})
@ManyToOne({ entity: () => TaxRegion, persist: false })
tax_region: TaxRegion
@OneToMany(() => TaxRateRule, (rule) => rule.tax_rate)
@OneToMany(() => TaxRateRule, (rule) => rule.tax_rate, {
cascade: ["soft-remove" as Cascade],
})
rules = new Collection<TaxRateRule>(this)
@Property({ columnType: "jsonb", nullable: true })
@@ -90,6 +103,14 @@ export default class TaxRate {
@Property({ columnType: "text", nullable: true })
created_by: string | null = null
@createPsqlIndexStatementHelper({
tableName: TABLE_NAME,
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "txr")
+27 -8
View File
@@ -1,9 +1,11 @@
import { DAL } from "@medusajs/types"
import {
DALUtils,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
Filter,
BeforeCreate,
Collection,
Entity,
@@ -18,7 +20,7 @@ import {
} from "@mikro-orm/core"
import TaxRate from "./tax-rate"
type OptionalTaxRegionProps = DAL.EntityDateColumns
type OptionalTaxRegionProps = DAL.SoftDeletableEntityDateColumns
const TABLE_NAME = "tax_region"
@@ -37,8 +39,9 @@ const taxRegionCountryTopLevelCheckName = "CK_tax_region_country_top_level"
})
@countryCodeProvinceIndexStatement.MikroORMIndex()
@Entity({ tableName: TABLE_NAME })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class TaxRegion {
[OptionalProps]: OptionalTaxRegionProps
[OptionalProps]?: OptionalTaxRegionProps
@PrimaryKey({ columnType: "text" })
id!: string
@@ -49,20 +52,28 @@ export default class TaxRegion {
@Property({ columnType: "text", nullable: true })
province_code: string | null = null
@Property({ columnType: "text", nullable: true })
parent_id: string | null = null
@ManyToOne(() => TaxRegion, {
index: "IDX_tax_region_parent_id",
cascade: [Cascade.PERSIST],
onDelete: "set null",
fieldName: "parent_id",
cascade: [Cascade.REMOVE],
mapToPk: true,
nullable: true,
})
parent_id: string | null = null
@ManyToOne(() => TaxRegion, { persist: false })
parent: TaxRegion
@OneToMany(() => TaxRate, (label) => label.tax_region)
@OneToMany(() => TaxRate, (label) => label.tax_region, {
cascade: ["soft-remove" as Cascade],
})
tax_rates = new Collection<TaxRate>(this)
@OneToMany(() => TaxRegion, (label) => label.parent, {
cascade: ["soft-remove" as Cascade],
})
children = new Collection<TaxRegion>(this)
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | null = null
@@ -84,6 +95,14 @@ export default class TaxRegion {
@Property({ columnType: "text", nullable: true })
created_by: string | null = null
@createPsqlIndexStatementHelper({
tableName: TABLE_NAME,
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "txreg")