Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pricing, types, utils): Soft delete pricing entities #5858

Merged
merged 40 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
1fa9017
soft delete money amounts
pKorsholm Dec 11, 2023
18b57c7
fix cascading soft deletes for one to one relationships
pKorsholm Dec 13, 2023
135df2f
undo
pKorsholm Dec 13, 2023
734e11b
fix final tests
pKorsholm Dec 13, 2023
b47432d
clean up util
pKorsholm Dec 13, 2023
4586aa3
Merge branch 'develop' into feat/soft-delete-pricing-entities
pKorsholm Dec 13, 2023
979c93e
fix up pr
pKorsholm Dec 13, 2023
340b0e8
fix integration tests
pKorsholm Dec 13, 2023
4e3819b
fix conflict
pKorsholm Dec 13, 2023
cb658bc
undo cascade changes
pKorsholm Dec 13, 2023
de07dff
snapshot update
pKorsholm Dec 13, 2023
bd16e67
undo model changes
pKorsholm Dec 13, 2023
08701e2
nit: reorder update and delete rules
pKorsholm Dec 13, 2023
5585fc8
update snapshot
pKorsholm Dec 13, 2023
a8bb966
add changeset
pKorsholm Dec 13, 2023
d3c8dc5
update changeset
pKorsholm Dec 13, 2023
7d3aeaa
Merge branch 'develop' into feat/soft-delete-pricing-entities
pKorsholm Dec 14, 2023
de22775
update restore method
pKorsholm Dec 15, 2023
8136566
update dto's
pKorsholm Dec 15, 2023
465a208
Merge branch 'develop' into feat/soft-delete-pricing-entities
pKorsholm Dec 15, 2023
471094d
update dtos
pKorsholm Dec 15, 2023
16da3b5
deleted at not optional
pKorsholm Dec 15, 2023
8e99ce4
update types
pKorsholm Dec 18, 2023
a29715d
Merge branch 'develop' into feat/soft-delete-pricing-entities
pKorsholm Dec 18, 2023
8e1cd6d
fix build issue
pKorsholm Dec 18, 2023
d0b1de3
Merge branch 'develop' into feat/soft-delete-pricing-entities
pKorsholm Dec 18, 2023
66c535d
Merge branch 'develop' into feat/soft-delete-pricing-entities
pKorsholm Dec 20, 2023
db78efb
Merge branch 'develop' into feat/soft-delete-pricing-entities
pKorsholm Dec 21, 2023
fc0b7b0
fix build
pKorsholm Dec 21, 2023
427a2ad
Merge branch 'develop' into feat/soft-delete-pricing-entities
pKorsholm Dec 21, 2023
615779a
Merge branch 'develop' into feat/soft-delete-pricing-entities
pKorsholm Jan 2, 2024
81dab43
update pricing types
pKorsholm Jan 2, 2024
c2ee082
add comment for work waiting for mikro-orm version upgrade
pKorsholm Jan 2, 2024
6c1efc0
add todo
pKorsholm Jan 2, 2024
62dc802
Merge branch 'develop' into feat/soft-delete-pricing-entities
pKorsholm Jan 3, 2024
ea6638c
fix type with updated mikro-orm
pKorsholm Jan 3, 2024
13cd56d
Merge branch 'develop' into feat/soft-delete-pricing-entities
pKorsholm Jan 3, 2024
739f196
fix build
pKorsholm Jan 3, 2024
cc6287c
Merge branch 'develop' into feat/soft-delete-pricing-entities
olivermrbl Jan 3, 2024
4234fa3
Merge branch 'develop' into feat/soft-delete-pricing-entities
olivermrbl Jan 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/fuzzy-ears-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@medusajs/pricing": patch
"@medusajs/types": patch
"@medusajs/utils": patch
---

feat(pricing, types): add soft deletion for money-amounts
feat(utils): cascade soft delete across 1:1 relationships
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import { initialize } from "../../../../src"
import { createCurrencies } from "../../../__fixtures__/currency"
import { createMoneyAmounts } from "../../../__fixtures__/money-amount"
import { DB_URL, MikroOrmWrapper } from "../../../utils"
import { createPriceSetMoneyAmounts } from "../../../__fixtures__/price-set-money-amount"
import { createPriceSets } from "../../../__fixtures__/price-set"
import { createRuleTypes } from "../../../__fixtures__/rule-type"
import { createPriceRules } from "../../../__fixtures__/price-rule"
import { createPriceSetMoneyAmountRules } from "../../../__fixtures__/price-set-money-amount-rules"

jest.setTimeout(30000)

Expand Down Expand Up @@ -264,6 +269,94 @@ describe("PricingModule Service - MoneyAmount", () => {
})
})

describe("softDeleteMoneyAmounts", () => {
const id = "money-amount-USD"

it("should softDelete priceSetMoneyAmount and PriceRule when soft-deleting money amount", async () => {
await createPriceSets(testManager)
await createRuleTypes(testManager)
await createPriceSetMoneyAmounts(testManager)
await createPriceRules(testManager)
await createPriceSetMoneyAmountRules(testManager)
await service.softDeleteMoneyAmounts([id])

const [moneyAmount] = await service.listMoneyAmounts(
{
id: [id],
},
{
relations: [
"price_set_money_amount",
"price_set_money_amount.price_rules",
],
withDeleted: true,
}
)

expect(moneyAmount).toBeTruthy()

const deletedAt = moneyAmount.deleted_at

expect(moneyAmount).toEqual(
expect.objectContaining({
deleted_at: deletedAt,
price_set_money_amount: expect.objectContaining({
deleted_at: deletedAt,
price_rules: [
expect.objectContaining({
deleted_at: deletedAt,
}),
],
}),
})
)
})
})

describe("restoreDeletedMoneyAmounts", () => {
const id = "money-amount-USD"

it("should restore softDeleted priceSetMoneyAmount and PriceRule when restoring soft-deleting money amount", async () => {
await createPriceSets(testManager)
await createRuleTypes(testManager)
await createPriceSetMoneyAmounts(testManager)
await createPriceRules(testManager)
await createPriceSetMoneyAmountRules(testManager)
await service.softDeleteMoneyAmounts([id])
await service.restoreDeletedMoneyAmounts([id])

const [moneyAmount] = await service.listMoneyAmounts(
{
id: [id],
},
{
relations: [
"price_set_money_amount",
"price_set_money_amount.price_rules",
],
}
)

expect(moneyAmount).toBeTruthy()

const deletedAt = null

expect(moneyAmount).toEqual(
expect.objectContaining({
deleted_at: deletedAt,
price_set_money_amount: expect.objectContaining({
deleted_at: deletedAt,
price_rules: [
expect.objectContaining({
deleted_at: deletedAt,
}),
],
}),
})
)
})
})

describe("updateMoneyAmounts", () => {
const id = "money-amount-USD"

Expand Down
82 changes: 82 additions & 0 deletions packages/pricing/src/migrations/.snapshot-medusa-pricing.json
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,38 @@
"primary": false,
"nullable": true,
"mappedType": "text"
},
"created_at": {
"name": "created_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"default": "now()",
"mappedType": "datetime"
},
"updated_at": {
"name": "updated_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"default": "now()",
"mappedType": "datetime"
},
"deleted_at": {
"name": "deleted_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 6,
"mappedType": "datetime"
}
},
"name": "price_set_money_amount",
Expand Down Expand Up @@ -423,6 +455,15 @@
"primary": false,
"unique": false
},
{
"columnNames": [
"deleted_at"
],
"composite": false,
"keyName": "IDX_price_set_money_amount_deleted_at",
"primary": false,
"unique": false
},
{
"keyName": "price_set_money_amount_pkey",
"columnNames": ["id"],
Expand Down Expand Up @@ -750,6 +791,38 @@
"primary": false,
"nullable": false,
"mappedType": "text"
},
"created_at": {
"name": "created_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"default": "now()",
"mappedType": "datetime"
},
"updated_at": {
"name": "updated_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"default": "now()",
"mappedType": "datetime"
},
"deleted_at": {
"name": "deleted_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 6,
"mappedType": "datetime"
}
},
"name": "price_rule",
Expand All @@ -776,6 +849,15 @@
"primary": false,
"unique": false
},
{
"columnNames": [
"deleted_at"
],
"composite": false,
"keyName": "IDX_price_rule_deleted_at",
"primary": false,
"unique": false
},
{
"keyName": "price_rule_pkey",
"columnNames": ["id"],
Expand Down
37 changes: 37 additions & 0 deletions packages/pricing/src/migrations/Migration20231212134238.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Migration } from "@mikro-orm/migrations"

export class Migration20231212134238 extends Migration {
async up(): Promise<void> {
this.addSql(
'alter table "price_set_money_amount" add column if not exists "created_at" timestamptz not null default now(), add column if not exists "updated_at" timestamptz not null default now(), add column if not exists "deleted_at" timestamptz null;'
)
this.addSql(
'create index if not exists "IDX_price_set_money_amount_deleted_at" on "price_set_money_amount" ("deleted_at");'
)

this.addSql(
'alter table "price_rule" add column if not exists "created_at" timestamptz not null default now(), add column if not exists "updated_at" timestamptz not null default now(), add column if not exists "deleted_at" timestamptz null;'
)
this.addSql(
'create index if not exists "IDX_price_rule_deleted_at" on "price_rule" ("deleted_at");'
)
}

async down(): Promise<void> {
this.addSql('drop index if exists "IDX_price_set_money_amount_deleted_at";')
this.addSql(
'alter table "price_set_money_amount" drop column if exists "created_at";'
)
this.addSql(
'alter table "price_set_money_amount" drop column if exists "updated_at";'
)
this.addSql(
'alter table "price_set_money_amount" drop column if exists "deleted_at";'
)

this.addSql('drop index if exists "IDX_price_rule_deleted_at";')
this.addSql('alter table "price_rule" drop column if exists "created_at";')
this.addSql('alter table "price_rule" drop column if exists "updated_at";')
this.addSql('alter table "price_rule" drop column if exists "deleted_at";')
}
}
5 changes: 4 additions & 1 deletion packages/pricing/src/models/money-amount.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { generateEntityId } from "@medusajs/utils"
import { DALUtils, generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Collection,
Entity,
Filter,
Index,
ManyToMany,
ManyToOne,
Expand All @@ -17,6 +18,7 @@ import { PriceSetMoneyAmount } from "./index"
import PriceSet from "./price-set"

@Entity()
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
class MoneyAmount {
[OptionalProps]?:
| "created_at"
Expand All @@ -39,6 +41,7 @@ class MoneyAmount {
@OneToOne({
entity: () => PriceSetMoneyAmount,
mappedBy: (psma) => psma.money_amount,
cascade: ["soft-remove"] as any,
})
price_set_money_amount: PriceSetMoneyAmount

Expand Down
32 changes: 30 additions & 2 deletions packages/pricing/src/models/price-rule.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import {
BeforeCreate,
Entity,
Filter,
Index,
ManyToOne,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import { DALUtils, generateEntityId } from "@medusajs/utils"

import { generateEntityId } from "@medusajs/utils"
import PriceSet from "./price-set"
import PriceSetMoneyAmount from "./price-set-money-amount"
import RuleType from "./rule-type"

type OptionalFields = "id" | "is_dynamic" | "priority"
type OptionalFields =
| "id"
| "is_dynamic"
| "priority"
| "created_at"
| "updated_at"
| "deleted_at"
type OptionalRelations = "price_set" | "rule_type" | "price_set_money_amount"

@Entity()
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class PriceRule {
[OptionalProps]: OptionalFields | OptionalRelations

Expand Down Expand Up @@ -54,6 +63,25 @@ export default class PriceRule {
})
price_set_money_amount: PriceSetMoneyAmount

@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

@Index({ name: "IDX_price_rule_deleted_at" })
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date
pKorsholm marked this conversation as resolved.
Show resolved Hide resolved

@BeforeCreate()
beforeCreate() {
this.id = generateEntityId(this.id, "prule")
Expand Down
Loading