fix(medusa): Deleting product prices (#3152)

This commit is contained in:
Kasper Fabricius Kristensen authored and GitHub committed 2023-02-08 13:13:34 +01:00
1 parent 08324355a4
commit 86c87c7b10
5 files changed
+313 -81

No files matched your search

@@ -5,15 +5,16 @@ import {
In,
IsNull,
Not,
ObjectLiteral,
Repository,
WhereExpressionBuilder,
} from "typeorm"
import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity"
import { MoneyAmount } from "../models/money-amount"
import {
PriceListPriceCreateInput,
PriceListPriceUpdateInput,
} from "../types/price-list"
import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity"
type Price = Partial<
Omit<MoneyAmount, "created_at" | "updated_at" | "deleted_at">
@@ -23,6 +24,11 @@ type Price = Partial<
@EntityRepository(MoneyAmount)
export class MoneyAmountRepository extends Repository<MoneyAmount> {
/**
* Will be removed in a future release.
* Use `deleteVariantPricesNotIn` instead.
* @deprecated
*/
public async findVariantPricesNotIn(
variantId: string,
prices: Price[]
@@ -43,6 +49,44 @@ export class MoneyAmountRepository extends Repository<MoneyAmount> {
return pricesNotInPricesPayload
}
public async deleteVariantPricesNotIn(
variantId: string,
prices: Price[]
): Promise<void> {
const where = {
variant_id: variantId,
price_list_id: IsNull(),
}
const orWhere: ObjectLiteral[] = []
for (const price of prices) {
if (price.currency_code) {
orWhere.push(
{
currency_code: Not(price.currency_code),
},
{
region_id: price.region_id ? Not(price.region_id) : Not(IsNull()),
currency_code: price.currency_code,
}
)
}
if (price.region_id) {
orWhere.push({
region_id: Not(price.region_id),
})
}
}
await this.createQueryBuilder()
.delete()
.where(where)
.andWhere(orWhere)
.execute()
}
public async upsertVariantCurrencyPrice(
variantId: string,
price: Price
@@ -477,7 +477,7 @@ describe("ProductVariantService", () => {
},
]
moneyAmountRepository.findVariantPricesNotIn = jest
moneyAmountRepository.deleteVariantPricesNotIn = jest
.fn()
.mockImplementation(() => Promise.resolve(oldPrices))
@@ -641,7 +641,7 @@ describe("ProductVariantService", () => {
},
]
moneyAmountRepository.findVariantPricesNotIn = jest
moneyAmountRepository.deleteVariantPricesNotIn = jest
.fn()
.mockImplementation(() => Promise.resolve(oldPrices))
@@ -692,7 +692,7 @@ describe("ProductVariantService", () => {
])
expect(
moneyAmountRepository.findVariantPricesNotIn
moneyAmountRepository.deleteVariantPricesNotIn
).toHaveBeenCalledTimes(1)
expect(
@@ -704,9 +704,6 @@ describe("ProductVariantService", () => {
currency_code: "usd",
amount: 4000,
})
expect(moneyAmountRepository.remove).toHaveBeenCalledTimes(1)
expect(moneyAmountRepository.remove).toHaveBeenCalledWith(oldPrices)
})
it("successfully creates new a region price", async () => {
@@ -19,8 +19,6 @@ import {
FindWithRelationsOptions,
ProductVariantRepository,
} from "../repositories/product-variant"
import EventBusService from "./event-bus"
import RegionService from "./region"
import { FindConfig } from "../types/common"
import {
CreateProductVariantInput,
@@ -30,6 +28,8 @@ import {
UpdateProductVariantInput,
} from "../types/product-variant"
import { buildQuery, setMetadata } from "../utils"
import EventBusService from "./event-bus"
import RegionService from "./region"
class ProductVariantService extends TransactionBaseService {
static Events = {
@@ -341,11 +341,8 @@ class ProductVariantService extends TransactionBaseService {
this.moneyAmountRepository_
)
// get prices to be deleted
const obsoletePrices = await moneyAmountRepo.findVariantPricesNotIn(
variantId,
prices
)
// Delete obsolete prices
await moneyAmountRepo.deleteVariantPricesNotIn(variantId, prices)
const regionsServiceTx = this.regionService_.withTransaction(manager)
@@ -362,8 +359,6 @@ class ProductVariantService extends TransactionBaseService {
await this.setCurrencyPrice(variantId, price)
}
}
await moneyAmountRepo.remove(obsoletePrices)
})
}