fix(medusa): allow price list prices update when region_id is provided (#1472)

This commit is contained in:
Frane Polić authored and GitHub committed 2022-05-11 07:43:19 +02:00
1 parent 327614e126
commit 03c5617896
4 files changed
+279 -4

No files matched your search

+36 -2
View File
@@ -12,14 +12,17 @@ import {
CreatePriceListInput,
FilterablePriceListProps,
PriceListPriceCreateInput,
PriceListPriceUpdateInput,
UpdatePriceListInput,
} from "../types/price-list"
import { formatException } from "../utils/exception-formatter"
import RegionService from "./region"
import ProductService from "./product"
type PriceListConstructorProps = {
manager: EntityManager
customerGroupService: CustomerGroupService
regionService: RegionService
productService: ProductService
priceListRepository: typeof PriceListRepository
moneyAmountRepository: typeof MoneyAmountRepository
@@ -32,6 +35,7 @@ type PriceListConstructorProps = {
class PriceListService extends BaseService {
private manager_: EntityManager
private customerGroupService_: CustomerGroupService
private regionService_: RegionService
private productService_: ProductService
private priceListRepo_: typeof PriceListRepository
private moneyAmountRepo_: typeof MoneyAmountRepository
@@ -39,6 +43,7 @@ class PriceListService extends BaseService {
constructor({
manager,
customerGroupService,
regionService,
productService,
priceListRepository,
moneyAmountRepository,
@@ -47,6 +52,7 @@ class PriceListService extends BaseService {
this.manager_ = manager
this.customerGroupService_ = customerGroupService
this.productService_ = productService
this.regionService_ = regionService
this.priceListRepo_ = priceListRepository
this.moneyAmountRepo_ = moneyAmountRepository
}
@@ -60,6 +66,7 @@ class PriceListService extends BaseService {
manager: transactionManager,
customerGroupService: this.customerGroupService_,
productService: this.productService_,
regionService: this.regionService_,
priceListRepository: this.priceListRepo_,
moneyAmountRepository: this.moneyAmountRepo_,
})
@@ -152,7 +159,8 @@ class PriceListService extends BaseService {
await priceListRepo.save(priceList)
if (prices) {
await moneyAmountRepo.updatePriceListPrices(id, prices)
const prices_ = await this.addCurrencyFromRegion(prices)
await moneyAmountRepo.updatePriceListPrices(id, prices_)
}
if (customer_groups) {
@@ -184,7 +192,8 @@ class PriceListService extends BaseService {
const priceList = await this.retrieve(id, { select: ["id"] })
await moneyAmountRepo.addPriceListPrices(priceList.id, prices, replace)
const prices_ = await this.addCurrencyFromRegion(prices)
await moneyAmountRepo.addPriceListPrices(priceList.id, prices_, replace)
const result = await this.retrieve(priceList.id, {
relations: ["prices"],
@@ -336,6 +345,31 @@ class PriceListService extends BaseService {
return [productsWithPrices, count]
})
}
/**
* Add `currency_code` to an MA record if `region_id`is passed.
* @param prices - a list of PriceListPrice(Create/Update)Input records
* @return {Promise} updated `prices` list
*/
protected async addCurrencyFromRegion<
T extends PriceListPriceUpdateInput | PriceListPriceCreateInput
>(prices: T[]): Promise<T[]> {
const prices_: typeof prices = []
for (const p of prices) {
if (p.region_id) {
const region = await this.regionService_
.withTransaction(this.manager_)
.retrieve(p.region_id)
p.currency_code = region.currency_code
}
prices_.push(p)
}
return prices_
}
}
export default PriceListService