Skip to content

Commit

Permalink
feat(pricing): pricing models are made soft deletable (#6732)
Browse files Browse the repository at this point in the history
what:

- pricing models are made soft deletable
- adds missing timestamp attributes
- removes unwanted relationships + cascade cleanup
  • Loading branch information
riqwan authored Mar 20, 2024
1 parent 7085939 commit 20243e2
Show file tree
Hide file tree
Showing 36 changed files with 970 additions and 1,693 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ export const createVariantPriceSet = async ({
})

return await pricingModuleService.retrieve(priceSet.id, {
relations: ["money_amounts"],
relations: ["price_set_money_amounts.money_amount"],
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ export const deletePriceListsStep = createStep(
ModuleRegistrationName.PRICING
)

// TODO: Implement soft delete price lists
await pricingModule.deletePriceLists(ids)
await pricingModule.softDeletePriceLists(ids)

return new StepResponse(void 0, ids)
},
Expand All @@ -24,7 +23,6 @@ export const deletePriceListsStep = createStep(
ModuleRegistrationName.PRICING
)

// TODO: Implement restore price lists
// await pricingModule.restorePriceLists(idsToRestore)
await pricingModule.restorePriceLists(idsToRestore)
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,24 @@ export const removePriceListPricesStep = createStep(
{ relations: ["price_list"] }
)

await pricingModule.removePrices(psmas.map((psma) => psma.id))
await pricingModule.softDeletePriceSetMoneyAmounts(
psmas.map((psma) => psma.id)
)

return new StepResponse(
null,
psmas.map((psma) => psma.id)
)
},
async (ids, { container }) => {
if (!ids) {
if (!ids?.length) {
return
}

const pricingModule = container.resolve<IPricingModuleService>(
ModuleRegistrationName.PRICING
)

// TODO: This needs to be implemented
// pricingModule.restorePrices(ids)
await pricingModule.restorePriceSetMoneyAmounts(ids)
}
)
14 changes: 10 additions & 4 deletions packages/medusa/src/api-v2/admin/products/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ export const remapKeysForProduct = (selectFields: string[]) => {
const pricingFields = selectFields
.filter((fieldName: string) => isPricing(fieldName))
.map((fieldName: string) =>
fieldName.replace("variants.prices", "variants.price_set.money_amounts")
fieldName.replace(
"variants.prices.",
"variants.price_set.price_set_money_amounts.money_amount."
)
)

return [...productFields, ...pricingFields]
Expand All @@ -28,7 +31,10 @@ export const remapKeysForVariant = (selectFields: string[]) => {
const pricingFields = selectFields
.filter((fieldName: string) => isPricing(fieldName))
.map((fieldName: string) =>
fieldName.replace("prices", "price_set.money_amounts")
fieldName.replace(
"prices.",
"price_set.price_set_money_amounts.money_amount."
)
)

return [...variantFields, ...pricingFields]
Expand All @@ -44,8 +50,8 @@ export const remapProduct = (p: ProductDTO) => {
export const remapVariant = (v: ProductVariantDTO) => {
return {
...v,
prices: (v as any).price_set?.money_amounts?.map((ma) => ({
...ma,
prices: (v as any).price_set?.price_set_money_amounts?.map((psma) => ({
...psma.money_amount,
variant_id: v.id,
})),
price_set: undefined,
Expand Down
5 changes: 3 additions & 2 deletions packages/medusa/src/api-v2/admin/products/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import {
isString,
remoteQueryObjectFromString,
} from "@medusajs/utils"
import { MedusaContainer } from "medusa-core-utils"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../types/routing"
import { listPriceLists } from "../price-lists/queries"
import { AdminGetProductsParams } from "./validators"
import { refetchProduct, remapKeysForProduct, remapProduct } from "./helpers"
import { MedusaContainer } from "medusa-core-utils"
import { AdminGetProductsParams } from "./validators"

const applyVariantFiltersForPriceList = async (
scope: MedusaContainer,
Expand Down Expand Up @@ -59,6 +59,7 @@ export const GET = async (
) => {
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
let filterableFields: AdminGetProductsParams = { ...req.filterableFields }

filterableFields = await applyVariantFiltersForPriceList(
req.scope,
filterableFields
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import {
createPriceSetMoneyAmounts,
defaultPriceSetMoneyAmountsData,
} from "./price-set-money-amount"
import {
createPriceSetMoneyAmountRules,
defaultPriceSetMoneyAmountRulesData,
} from "./price-set-money-amount-rules"
import { createRuleTypes, defaultRuleTypesData } from "./rule-type"

jest.setTimeout(30000)
Expand All @@ -22,7 +18,6 @@ export async function seedPriceData(
priceSetsData = defaultPriceSetsData,
priceRuleData = defaultPriceRuleData,
priceSetMoneyAmountsData = defaultPriceSetMoneyAmountsData,
priceSetMoneyAmountRulesData = defaultPriceSetMoneyAmountRulesData,
ruleTypesData = defaultRuleTypesData,
} = {}
) {
Expand All @@ -31,8 +26,4 @@ export async function seedPriceData(
await createPriceSetMoneyAmounts(testManager, priceSetMoneyAmountsData)
await createRuleTypes(testManager, ruleTypesData)
await createPriceRules(testManager, priceRuleData)
await createPriceSetMoneyAmountRules(
testManager,
priceSetMoneyAmountRulesData
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,6 @@ moduleIntegrationTestRunner({
moneyAmountsData,
priceSetsData,
priceSetMoneyAmountsData,
priceSetMoneyAmountRulesData: [],
priceRuleData,
ruleTypesData,
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Modules } from "@medusajs/modules-sdk"
import { IPricingModuleService } from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
import { createMoneyAmounts } from "../../../__fixtures__/money-amount"
import { createPriceRules } from "../../../__fixtures__/price-rule"
import { createPriceSets } from "../../../__fixtures__/price-set"
import { createPriceSetMoneyAmounts } from "../../../__fixtures__/price-set-money-amount"
import { createPriceSetMoneyAmountRules } from "../../../__fixtures__/price-set-money-amount-rules"
import { createRuleTypes } from "../../../__fixtures__/rule-type"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"

jest.setTimeout(30000)

Expand Down Expand Up @@ -245,18 +244,16 @@ moduleIntegrationTestRunner({
describe("softDeleteMoneyAmounts", () => {
const id = "money-amount-USD"

it("should softDelete priceSetMoneyAmount and PriceRule when soft-deleting money amount", async () => {
it("should softDelete money amounts successfully", 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],
},
{ id: [id] },
{
relations: [
"price_set_money_amount",
Expand All @@ -274,10 +271,10 @@ moduleIntegrationTestRunner({
expect.objectContaining({
deleted_at: deletedAt,
price_set_money_amount: expect.objectContaining({
deleted_at: deletedAt,
deleted_at: null,
price_rules: [
expect.objectContaining({
deleted_at: deletedAt,
deleted_at: null,
}),
],
}),
Expand All @@ -294,7 +291,6 @@ moduleIntegrationTestRunner({
await createRuleTypes(testManager)
await createPriceSetMoneyAmounts(testManager)
await createPriceRules(testManager)
await createPriceSetMoneyAmountRules(testManager)
await service.softDeleteMoneyAmounts([id])
await service.restoreMoneyAmounts([id])

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { Modules } from "@medusajs/modules-sdk"
import { CreatePriceRuleDTO, IPricingModuleService } from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"

import { SuiteOptions, moduleIntegrationTestRunner } from "medusa-test-utils"
import { PriceSetMoneyAmount } from "../../../../src"
import { createMoneyAmounts } from "../../../__fixtures__/money-amount"
import { createPriceRules } from "../../../__fixtures__/price-rule"
import { createPriceSets } from "../../../__fixtures__/price-set"
import { createPriceSetMoneyAmounts } from "../../../__fixtures__/price-set-money-amount"
import { createPriceSetMoneyAmountRules } from "../../../__fixtures__/price-set-money-amount-rules"
import { createRuleTypes } from "../../../__fixtures__/rule-type"
import { Modules } from "@medusajs/modules-sdk"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"

jest.setTimeout(30000)

Expand All @@ -28,7 +26,6 @@ moduleIntegrationTestRunner({
await createPriceSets(testManager)
await createRuleTypes(testManager)
await createPriceSetMoneyAmounts(testManager)
await createPriceSetMoneyAmountRules(testManager)
await createPriceRules(testManager)
})

Expand Down
Loading

0 comments on commit 20243e2

Please sign in to comment.