Skip to content

Commit

Permalink
feat(pricing, types, utils): Soft delete pricing entities (#5858)
Browse files Browse the repository at this point in the history
  • Loading branch information
pKorsholm authored Jan 4, 2024
1 parent 1468646 commit 3550750
Show file tree
Hide file tree
Showing 30 changed files with 791 additions and 155 deletions.
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
1 change: 1 addition & 0 deletions packages/pricing/src/joiner-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const LinkableKeys = {
price_list_id: PriceList.name,
price_set_money_amount_id: PriceSetMoneyAmount.name,
}

const entityLinkableKeysMap: MapToConfig = {}
Object.entries(LinkableKeys).forEach(([key, value]) => {
entityLinkableKeysMap[value] ??= []
Expand Down
Loading

0 comments on commit 3550750

Please sign in to comment.