feat(product, pricing, utils): Transaction issues and reference issues (#5533)
* feat(product, pricing, utils): Transaction issues and reference issues * fixes decorators * cleanup * fix product module upsert * fix missing active manager * increase timeout * revert package.json * WIP * try another node version based on findings with memory issues with jest introduced after 16.11 but fixed in 21 * re add bail * fix variant options * chore: bulk create pricing * chore: workflow bulk * Create big-chefs-dream.md * fix missing update for upserty * Add integration tests for product options upsert * rm unnecessary return * fix product prices workflow issue * cleanup * fix flag * fix model --------- Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com> Co-authored-by: Carlos R. L. Rodrigues <rodrigolr@gmail.com> Co-authored-by: Riqwan Thamir <rmthamir@gmail.com>
This commit is contained in:
co-authored by
Carlos R. L. Rodrigues
Carlos R. L. Rodrigues
Riqwan Thamir
parent
57c3a24ad3
commit
f88d75b0a7
@@ -9,8 +9,6 @@ import {
|
||||
import { Currency } from "@models"
|
||||
import { CurrencyRepository } from "@repositories"
|
||||
|
||||
import { doNotForceTransaction, shouldForceTransaction } from "@medusajs/utils"
|
||||
|
||||
type InjectedDependencies = {
|
||||
currencyRepository: DAL.RepositoryService
|
||||
}
|
||||
@@ -75,7 +73,7 @@ export default class CurrencyService<TEntity extends Currency = Currency> {
|
||||
return queryOptions
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "currencyRepository_")
|
||||
@InjectTransactionManager("currencyRepository_")
|
||||
async create(
|
||||
data: PricingTypes.CreateCurrencyDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -86,7 +84,7 @@ export default class CurrencyService<TEntity extends Currency = Currency> {
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "currencyRepository_")
|
||||
@InjectTransactionManager("currencyRepository_")
|
||||
async update(
|
||||
data: PricingTypes.UpdateCurrencyDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -97,7 +95,7 @@ export default class CurrencyService<TEntity extends Currency = Currency> {
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(doNotForceTransaction, "currencyRepository_")
|
||||
@InjectTransactionManager("currencyRepository_")
|
||||
async delete(
|
||||
ids: string[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
|
||||
@@ -9,8 +9,6 @@ import {
|
||||
import { MoneyAmount } from "@models"
|
||||
import { MoneyAmountRepository } from "@repositories"
|
||||
|
||||
import { doNotForceTransaction, shouldForceTransaction } from "@medusajs/utils"
|
||||
|
||||
type InjectedDependencies = {
|
||||
moneyAmountRepository: DAL.RepositoryService
|
||||
}
|
||||
@@ -73,7 +71,7 @@ export default class MoneyAmountService<
|
||||
)) as [TEntity[], number]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "moneyAmountRepository_")
|
||||
@InjectTransactionManager("moneyAmountRepository_")
|
||||
async create(
|
||||
data: PricingTypes.CreateMoneyAmountDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -84,7 +82,7 @@ export default class MoneyAmountService<
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "moneyAmountRepository_")
|
||||
@InjectTransactionManager("moneyAmountRepository_")
|
||||
async update(
|
||||
data: PricingTypes.UpdateMoneyAmountDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -95,7 +93,7 @@ export default class MoneyAmountService<
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(doNotForceTransaction, "moneyAmountRepository_")
|
||||
@InjectTransactionManager("moneyAmountRepository_")
|
||||
async delete(
|
||||
ids: string[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
|
||||
@@ -4,9 +4,7 @@ import {
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
ModulesSdkUtils,
|
||||
doNotForceTransaction,
|
||||
retrieveEntity,
|
||||
shouldForceTransaction,
|
||||
} from "@medusajs/utils"
|
||||
import { PriceRule } from "@models"
|
||||
import { PriceRuleRepository } from "@repositories"
|
||||
@@ -65,7 +63,7 @@ export default class PriceRuleService<TEntity extends PriceRule = PriceRule> {
|
||||
)) as [TEntity[], number]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "priceRuleRepository_")
|
||||
@InjectTransactionManager("priceRuleRepository_")
|
||||
async create(
|
||||
data: PricingTypes.CreatePriceRuleDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -76,7 +74,7 @@ export default class PriceRuleService<TEntity extends PriceRule = PriceRule> {
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "priceRuleRepository_")
|
||||
@InjectTransactionManager("priceRuleRepository_")
|
||||
async update(
|
||||
data: PricingTypes.UpdatePriceRuleDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -87,7 +85,7 @@ export default class PriceRuleService<TEntity extends PriceRule = PriceRule> {
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(doNotForceTransaction, "priceRuleRepository_")
|
||||
@InjectTransactionManager("priceRuleRepository_")
|
||||
async delete(
|
||||
ids: string[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
|
||||
@@ -8,8 +8,6 @@ import {
|
||||
} from "@medusajs/utils"
|
||||
import { PriceSetMoneyAmountRules } from "@models"
|
||||
|
||||
import { doNotForceTransaction, shouldForceTransaction } from "@medusajs/utils"
|
||||
|
||||
type InjectedDependencies = {
|
||||
priceSetMoneyAmountRulesRepository: DAL.RepositoryService
|
||||
}
|
||||
@@ -79,10 +77,7 @@ export default class PriceSetMoneyAmountRulesService<
|
||||
return queryOptions
|
||||
}
|
||||
|
||||
@InjectTransactionManager(
|
||||
shouldForceTransaction,
|
||||
"priceSetMoneyAmountRulesRepository_"
|
||||
)
|
||||
@InjectTransactionManager("priceSetMoneyAmountRulesRepository_")
|
||||
async create(
|
||||
data: PricingTypes.CreatePriceSetMoneyAmountRulesDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -93,10 +88,7 @@ export default class PriceSetMoneyAmountRulesService<
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(
|
||||
shouldForceTransaction,
|
||||
"priceSetMoneyAmountRulesRepository_"
|
||||
)
|
||||
@InjectTransactionManager("priceSetMoneyAmountRulesRepository_")
|
||||
async update(
|
||||
data: PricingTypes.UpdatePriceSetMoneyAmountRulesDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -107,10 +99,7 @@ export default class PriceSetMoneyAmountRulesService<
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(
|
||||
doNotForceTransaction,
|
||||
"priceSetMoneyAmountRulesRepository_"
|
||||
)
|
||||
@InjectTransactionManager("priceSetMoneyAmountRulesRepository_")
|
||||
async delete(
|
||||
ids: string[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import { Context, DAL, FindConfig, PricingTypes } from "@medusajs/types"
|
||||
import {
|
||||
doNotForceTransaction,
|
||||
InjectManager,
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
ModulesSdkUtils,
|
||||
doNotForceTransaction,
|
||||
retrieveEntity,
|
||||
shouldForceTransaction,
|
||||
} from "@medusajs/utils"
|
||||
import { PriceSet, PriceSetMoneyAmount } from "@models"
|
||||
import { PriceSetMoneyAmountRepository } from "@repositories"
|
||||
@@ -79,36 +78,29 @@ export default class PriceSetMoneyAmountService<
|
||||
return queryOptions
|
||||
}
|
||||
|
||||
@InjectTransactionManager(
|
||||
shouldForceTransaction,
|
||||
"priceSetMoneyAmountRepository_"
|
||||
)
|
||||
@InjectTransactionManager("priceSetMoneyAmountRepository_")
|
||||
async create(
|
||||
data: PricingTypes.CreatePriceSetMoneyAmountDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
return (await (
|
||||
this.priceSetMoneyAmountRepository_ as unknown as PriceSetMoneyAmountRepository
|
||||
this
|
||||
.priceSetMoneyAmountRepository_ as unknown as PriceSetMoneyAmountRepository
|
||||
).create(data, sharedContext)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(
|
||||
shouldForceTransaction,
|
||||
"priceSetMoneyAmountRepository_"
|
||||
)
|
||||
@InjectTransactionManager("priceSetMoneyAmountRepository_")
|
||||
async update(
|
||||
data: PricingTypes.UpdatePriceSetMoneyAmountDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
return (await (
|
||||
this.priceSetMoneyAmountRepository_ as unknown as PriceSetMoneyAmountRepository
|
||||
this
|
||||
.priceSetMoneyAmountRepository_ as unknown as PriceSetMoneyAmountRepository
|
||||
).update(data, sharedContext)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(
|
||||
doNotForceTransaction,
|
||||
"priceSetMoneyAmountRepository_"
|
||||
)
|
||||
@InjectTransactionManager("priceSetMoneyAmountRepository_")
|
||||
async delete(
|
||||
ids: string[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
|
||||
@@ -4,9 +4,7 @@ import {
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
ModulesSdkUtils,
|
||||
doNotForceTransaction,
|
||||
retrieveEntity,
|
||||
shouldForceTransaction,
|
||||
} from "@medusajs/utils"
|
||||
import { PriceSet, PriceSetRuleType } from "@models"
|
||||
import { PriceSetRuleTypeRepository } from "src/repositories/price-set-rule-type"
|
||||
@@ -15,7 +13,9 @@ type InjectedDependencies = {
|
||||
priceSetRuleTypeRepository: DAL.RepositoryService
|
||||
}
|
||||
|
||||
export default class PriceSetRuleTypeService<TEntity extends PriceSetRuleType = PriceSetRuleType> {
|
||||
export default class PriceSetRuleTypeService<
|
||||
TEntity extends PriceSetRuleType = PriceSetRuleType
|
||||
> {
|
||||
protected readonly priceSetRuleTypeRepository_: DAL.RepositoryService
|
||||
|
||||
constructor({ priceSetRuleTypeRepository }: InjectedDependencies) {
|
||||
@@ -28,7 +28,10 @@ export default class PriceSetRuleTypeService<TEntity extends PriceSetRuleType =
|
||||
config: FindConfig<PricingTypes.PriceSetRuleTypeDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity> {
|
||||
return (await retrieveEntity<PriceSetRuleType, PricingTypes.PriceSetRuleTypeDTO>({
|
||||
return (await retrieveEntity<
|
||||
PriceSetRuleType,
|
||||
PricingTypes.PriceSetRuleTypeDTO
|
||||
>({
|
||||
id: priceSetId,
|
||||
entityName: PriceSet.name,
|
||||
repository: this.priceSetRuleTypeRepository_,
|
||||
@@ -74,29 +77,27 @@ export default class PriceSetRuleTypeService<TEntity extends PriceSetRuleType =
|
||||
return queryOptions
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "priceSetRuleTypeRepository_")
|
||||
@InjectTransactionManager("priceSetRuleTypeRepository_")
|
||||
async create(
|
||||
data: PricingTypes.CreatePriceSetRuleTypeDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
return (await (this.priceSetRuleTypeRepository_ as PriceSetRuleTypeRepository).create(
|
||||
data,
|
||||
sharedContext
|
||||
)) as TEntity[]
|
||||
return (await (
|
||||
this.priceSetRuleTypeRepository_ as PriceSetRuleTypeRepository
|
||||
).create(data, sharedContext)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "priceSetRuleTypeRepository_")
|
||||
@InjectTransactionManager("priceSetRuleTypeRepository_")
|
||||
async update(
|
||||
data: PricingTypes.UpdatePriceSetRuleTypeDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
return (await (this.priceSetRuleTypeRepository_ as PriceSetRuleTypeRepository).update(
|
||||
data,
|
||||
sharedContext
|
||||
)) as TEntity[]
|
||||
return (await (
|
||||
this.priceSetRuleTypeRepository_ as PriceSetRuleTypeRepository
|
||||
).update(data, sharedContext)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(doNotForceTransaction, "priceSetRuleTypeRepository_")
|
||||
@InjectTransactionManager("priceSetRuleTypeRepository_")
|
||||
async delete(
|
||||
ids: string[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
|
||||
@@ -4,9 +4,7 @@ import {
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
ModulesSdkUtils,
|
||||
doNotForceTransaction,
|
||||
retrieveEntity,
|
||||
shouldForceTransaction,
|
||||
} from "@medusajs/utils"
|
||||
import { PriceSet } from "@models"
|
||||
import { PriceSetRepository } from "@repositories"
|
||||
@@ -65,7 +63,7 @@ export default class PriceSetService<TEntity extends PriceSet = PriceSet> {
|
||||
)) as [TEntity[], number]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "priceSetRepository_")
|
||||
@InjectTransactionManager("priceSetRepository_")
|
||||
async create(
|
||||
data: Omit<PricingTypes.CreatePriceSetDTO, "rules">[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -76,7 +74,7 @@ export default class PriceSetService<TEntity extends PriceSet = PriceSet> {
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "priceSetRepository_")
|
||||
@InjectTransactionManager("priceSetRepository_")
|
||||
async update(
|
||||
data: Omit<PricingTypes.UpdatePriceSetDTO, "rules">[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -87,7 +85,7 @@ export default class PriceSetService<TEntity extends PriceSet = PriceSet> {
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(doNotForceTransaction, "priceSetRepository_")
|
||||
@InjectTransactionManager("priceSetRepository_")
|
||||
async delete(
|
||||
ids: string[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import {
|
||||
AddPricesDTO,
|
||||
Context,
|
||||
CreateMoneyAmountDTO,
|
||||
DAL,
|
||||
@@ -35,16 +36,13 @@ import {
|
||||
} from "@services"
|
||||
|
||||
import {
|
||||
groupBy,
|
||||
InjectManager,
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
MedusaError,
|
||||
groupBy,
|
||||
removeNullish,
|
||||
shouldForceTransaction,
|
||||
} from "@medusajs/utils"
|
||||
|
||||
import { AddPricesDTO } from "@medusajs/types"
|
||||
import { joinerConfig } from "../joiner-config"
|
||||
import { PricingRepositoryService } from "../types"
|
||||
|
||||
@@ -231,7 +229,8 @@ export default class PricingModuleService<
|
||||
{ id: priceSets.filter((p) => !!p).map((p) => p!.id) },
|
||||
{
|
||||
relations: ["rule_types", "money_amounts", "price_rules"],
|
||||
}
|
||||
},
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return (Array.isArray(data) ? dbPriceSets : dbPriceSets[0]) as unknown as
|
||||
@@ -239,7 +238,7 @@ export default class PricingModuleService<
|
||||
| PriceSetDTO[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
protected async create_(
|
||||
data: PricingTypes.CreatePriceSetDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -286,74 +285,89 @@ export default class PricingModuleService<
|
||||
)
|
||||
}
|
||||
|
||||
const priceSets = await Promise.all(
|
||||
data.map(async (d) => {
|
||||
const { rules, prices, ...rest } = d
|
||||
const [priceSet] = await this.priceSetService_.create(
|
||||
[rest],
|
||||
sharedContext
|
||||
)
|
||||
|
||||
if (rules?.length) {
|
||||
const priceSetRuleTypesCreate = rules!.map((r) => ({
|
||||
rule_type: ruleTypeMap.get(r.rule_attribute),
|
||||
price_set: priceSet,
|
||||
}))
|
||||
|
||||
await this.priceSetRuleTypeService_.create(
|
||||
priceSetRuleTypesCreate as unknown as PricingTypes.CreatePriceSetRuleTypeDTO[],
|
||||
sharedContext
|
||||
)
|
||||
}
|
||||
|
||||
if (prices?.length) {
|
||||
for (const ma of prices) {
|
||||
const [moneyAmount] = await this.moneyAmountService_.create(
|
||||
[ma] as unknown as CreateMoneyAmountDTO[],
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const cleanRules = ma.rules ? removeNullish(ma.rules) : {}
|
||||
|
||||
const numberOfRules = Object.entries(cleanRules).length
|
||||
|
||||
const [priceSetMoneyAmount] =
|
||||
await this.priceSetMoneyAmountService_.create(
|
||||
[
|
||||
{
|
||||
price_set: priceSet,
|
||||
money_amount: moneyAmount,
|
||||
title: "test",
|
||||
number_rules: numberOfRules,
|
||||
},
|
||||
] as unknown as PricingTypes.CreatePriceSetMoneyAmountDTO[],
|
||||
sharedContext
|
||||
)
|
||||
|
||||
if (numberOfRules) {
|
||||
const priceSetRulesCreate = Object.entries(cleanRules).map(
|
||||
([k, v]) => ({
|
||||
price_set_money_amount: priceSetMoneyAmount,
|
||||
rule_type: ruleTypeMap.get(k),
|
||||
price_set: priceSet,
|
||||
value: v,
|
||||
price_list_id: "test",
|
||||
})
|
||||
)
|
||||
|
||||
await this.priceRuleService_.create(
|
||||
priceSetRulesCreate as unknown as PricingTypes.CreatePriceRuleDTO[],
|
||||
sharedContext
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return priceSet
|
||||
})
|
||||
// Bulk create price sets
|
||||
const priceSetData = data.map(({ rules, prices, ...rest }) => rest)
|
||||
const createdPriceSets = await this.priceSetService_.create(
|
||||
priceSetData,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return priceSets
|
||||
// Price set rule types
|
||||
const ruleTypeData = data.flatMap(
|
||||
(item, index) =>
|
||||
item.rules?.map((rule) => ({
|
||||
rule_type: ruleTypeMap.get(rule.rule_attribute),
|
||||
price_set: createdPriceSets[index],
|
||||
})) || []
|
||||
)
|
||||
if (ruleTypeData.length > 0) {
|
||||
await this.priceSetRuleTypeService_.create(
|
||||
ruleTypeData as unknown as PricingTypes.CreatePriceSetRuleTypeDTO[],
|
||||
sharedContext
|
||||
)
|
||||
}
|
||||
|
||||
// Money amounts
|
||||
const moneyAmountData = data.flatMap((item) => item.prices || [])
|
||||
const createdMoneyAmounts = await this.moneyAmountService_.create(
|
||||
moneyAmountData,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
let moneyAmountIndex = 0
|
||||
const priceSetMoneyAmountData: unknown[] = []
|
||||
const priceRulesData: unknown[] = []
|
||||
|
||||
for (const [index, item] of data.entries()) {
|
||||
for (const ma of item.prices || []) {
|
||||
const cleanRules = ma.rules ? removeNullish(ma.rules) : {}
|
||||
const numberOfRules = Object.entries(cleanRules).length
|
||||
|
||||
const priceSetMoneyAmount = {
|
||||
price_set: createdPriceSets[index],
|
||||
money_amount: createdMoneyAmounts[moneyAmountIndex++],
|
||||
title: "test", // TODO: accept title
|
||||
number_rules: numberOfRules,
|
||||
}
|
||||
priceSetMoneyAmountData.push(priceSetMoneyAmount)
|
||||
|
||||
for (const [k, v] of Object.entries(cleanRules)) {
|
||||
priceRulesData.push({
|
||||
price_set_money_amount: null, // Updated later
|
||||
rule_type: ruleTypeMap.get(k),
|
||||
price_set: createdPriceSets[index],
|
||||
value: v,
|
||||
price_list_id: "test",
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Bulk create price set money amounts
|
||||
const createdPriceSetMoneyAmounts =
|
||||
await this.priceSetMoneyAmountService_.create(
|
||||
priceSetMoneyAmountData as PricingTypes.CreatePriceSetMoneyAmountDTO[],
|
||||
sharedContext
|
||||
)
|
||||
|
||||
// Update price set money amount references
|
||||
for (let i = 0, j = 0; i < priceSetMoneyAmountData.length; i++) {
|
||||
const rulesCount = (priceSetMoneyAmountData[i] as any).number_rules
|
||||
for (let k = 0; k < rulesCount; k++, j++) {
|
||||
;(priceRulesData[j] as any).price_set_money_amount =
|
||||
createdPriceSetMoneyAmounts[i]
|
||||
}
|
||||
}
|
||||
|
||||
// Price rules
|
||||
if (priceRulesData.length > 0) {
|
||||
await this.priceRuleService_.create(
|
||||
priceRulesData as PricingTypes.CreatePriceRuleDTO[],
|
||||
sharedContext
|
||||
)
|
||||
}
|
||||
|
||||
return createdPriceSets
|
||||
}
|
||||
|
||||
async addRules(
|
||||
@@ -380,14 +394,15 @@ export default class PricingModuleService<
|
||||
| PricingTypes.PriceSetDTO
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
protected async addRules_(
|
||||
inputs: PricingTypes.AddRulesDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<PricingTypes.PriceSetDTO[]> {
|
||||
const priceSets = await this.priceSetService_.list(
|
||||
{ id: inputs.map((d) => d.priceSetId) },
|
||||
{ relations: ["rule_types"] }
|
||||
{ relations: ["rule_types"] },
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const priceSetRuleTypeMap: Map<string, Map<string, RuleTypeDTO>> = new Map(
|
||||
@@ -491,11 +506,12 @@ export default class PricingModuleService<
|
||||
|
||||
return (await this.list(
|
||||
{ id: input.map((d) => d.priceSetId) },
|
||||
{ relations: ["money_amounts"] }
|
||||
{ relations: ["money_amounts"] },
|
||||
sharedContext
|
||||
)) as unknown as PricingTypes.PriceSetDTO[] | PricingTypes.PriceSetDTO
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
protected async addPrices_(
|
||||
input: AddPricesDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -543,73 +559,93 @@ export default class PricingModuleService<
|
||||
}
|
||||
})
|
||||
|
||||
for (const { priceSetId, prices } of input) {
|
||||
await Promise.all(
|
||||
prices.map(async (ma) => {
|
||||
const [moneyAmount] = await this.moneyAmountService_.create(
|
||||
[ma] as unknown as CreateMoneyAmountDTO[],
|
||||
sharedContext
|
||||
)
|
||||
// Money amounts
|
||||
const moneyAmountsBulkData = input.flatMap((entry) => entry.prices)
|
||||
const createdMoneyAmounts = await this.moneyAmountService_.create(
|
||||
moneyAmountsBulkData as unknown as CreateMoneyAmountDTO[],
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const numberOfRules = Object.entries(ma?.rules ?? {}).length
|
||||
|
||||
const [priceSetMoneyAmount] =
|
||||
await this.priceSetMoneyAmountService_.create(
|
||||
[
|
||||
{
|
||||
price_set: priceSetId,
|
||||
money_amount: moneyAmount,
|
||||
title: "test",
|
||||
number_rules: numberOfRules,
|
||||
},
|
||||
] as unknown as PricingTypes.CreatePriceSetMoneyAmountDTO[],
|
||||
sharedContext
|
||||
)
|
||||
|
||||
if (numberOfRules) {
|
||||
const priceSetRulesCreate = Object.entries(ma.rules!).map(
|
||||
([k, v]) => ({
|
||||
price_set_money_amount: priceSetMoneyAmount,
|
||||
rule_type: ruleTypeMap.get(priceSetId)!.get(k),
|
||||
price_set: priceSetId,
|
||||
value: v,
|
||||
price_list_id: "test",
|
||||
})
|
||||
)
|
||||
|
||||
await this.priceRuleService_.create(
|
||||
priceSetRulesCreate as unknown as PricingTypes.CreatePriceRuleDTO[],
|
||||
sharedContext
|
||||
)
|
||||
// Price set money amounts
|
||||
let maCursor = 0
|
||||
const priceSetMoneyAmountsBulkData = input.flatMap(
|
||||
({ priceSetId, prices }) =>
|
||||
prices.map(() => {
|
||||
const ma = createdMoneyAmounts[maCursor]
|
||||
const numberOfRules = Object.entries(
|
||||
prices[maCursor]?.rules ?? {}
|
||||
).length
|
||||
maCursor++
|
||||
return {
|
||||
price_set: priceSetId,
|
||||
money_amount: ma,
|
||||
title: "test", // TODO: accept title
|
||||
number_rules: numberOfRules,
|
||||
}
|
||||
|
||||
return moneyAmount
|
||||
})
|
||||
)
|
||||
const createdPriceSetMoneyAmounts =
|
||||
await this.priceSetMoneyAmountService_.create(
|
||||
priceSetMoneyAmountsBulkData as unknown as PricingTypes.CreatePriceSetMoneyAmountDTO[],
|
||||
sharedContext
|
||||
)
|
||||
|
||||
// Price rules
|
||||
let rulesCursor = 0
|
||||
const priceRulesBulkData = input.flatMap(({ priceSetId, prices }) =>
|
||||
prices.flatMap((ma) => {
|
||||
const rules = ma.rules ?? {}
|
||||
const priceSetMoneyAmount = createdPriceSetMoneyAmounts[rulesCursor]
|
||||
rulesCursor++
|
||||
return Object.entries(rules).map(([k, v]) => ({
|
||||
price_set_money_amount: priceSetMoneyAmount,
|
||||
rule_type: ruleTypeMap.get(priceSetId)!.get(k),
|
||||
price_set: priceSetId,
|
||||
value: v,
|
||||
price_list_id: "test", // TODO: accept title
|
||||
}))
|
||||
})
|
||||
)
|
||||
|
||||
if (priceRulesBulkData.length > 0) {
|
||||
await this.priceRuleService_.create(
|
||||
priceRulesBulkData as unknown as PricingTypes.CreatePriceRuleDTO[],
|
||||
sharedContext
|
||||
)
|
||||
}
|
||||
|
||||
return priceSets
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async removeRules(
|
||||
data: PricingTypes.RemovePriceSetRulesDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<void> {
|
||||
const priceSets = await this.priceSetService_.list({
|
||||
id: data.map((d) => d.id),
|
||||
})
|
||||
const priceSets = await this.priceSetService_.list(
|
||||
{
|
||||
id: data.map((d) => d.id),
|
||||
},
|
||||
undefined,
|
||||
sharedContext
|
||||
)
|
||||
const priceSetIds = priceSets.map((ps) => ps.id)
|
||||
|
||||
const ruleTypes = await this.ruleTypeService_.list({
|
||||
rule_attribute: data.map((d) => d.rules || []).flat(),
|
||||
})
|
||||
const ruleTypes = await this.ruleTypeService_.list(
|
||||
{
|
||||
rule_attribute: data.map((d) => d.rules || []).flat(),
|
||||
},
|
||||
undefined,
|
||||
sharedContext
|
||||
)
|
||||
const ruleTypeIds = ruleTypes.map((rt) => rt.id)
|
||||
|
||||
const priceSetRuleTypes = await this.priceSetRuleTypeService_.list({
|
||||
price_set_id: priceSetIds,
|
||||
rule_type_id: ruleTypeIds,
|
||||
})
|
||||
const priceSetRuleTypes = await this.priceSetRuleTypeService_.list(
|
||||
{
|
||||
price_set_id: priceSetIds,
|
||||
rule_type_id: ruleTypeIds,
|
||||
},
|
||||
undefined,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const priceRules = await this.priceRuleService_.list(
|
||||
{
|
||||
@@ -618,7 +654,8 @@ export default class PricingModuleService<
|
||||
},
|
||||
{
|
||||
select: ["price_set_money_amount"],
|
||||
}
|
||||
},
|
||||
sharedContext
|
||||
)
|
||||
|
||||
await this.priceSetRuleTypeService_.delete(
|
||||
@@ -632,7 +669,7 @@ export default class PricingModuleService<
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async update(
|
||||
data: PricingTypes.UpdatePriceSetDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -647,7 +684,7 @@ export default class PricingModuleService<
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async delete(
|
||||
ids: string[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -718,7 +755,7 @@ export default class PricingModuleService<
|
||||
]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async createMoneyAmounts(
|
||||
data: PricingTypes.CreateMoneyAmountDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -736,7 +773,7 @@ export default class PricingModuleService<
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async updateMoneyAmounts(
|
||||
data: PricingTypes.UpdateMoneyAmountDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -754,7 +791,7 @@ export default class PricingModuleService<
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async deleteMoneyAmounts(
|
||||
ids: string[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -822,7 +859,7 @@ export default class PricingModuleService<
|
||||
]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async createCurrencies(
|
||||
data: PricingTypes.CreateCurrencyDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -837,7 +874,7 @@ export default class PricingModuleService<
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async updateCurrencies(
|
||||
data: PricingTypes.UpdateCurrencyDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -852,7 +889,7 @@ export default class PricingModuleService<
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async deleteCurrencies(
|
||||
currencyCodes: string[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -920,7 +957,7 @@ export default class PricingModuleService<
|
||||
]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async createRuleTypes(
|
||||
data: PricingTypes.CreateRuleTypeDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -935,7 +972,7 @@ export default class PricingModuleService<
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async updateRuleTypes(
|
||||
data: PricingTypes.UpdateRuleTypeDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -950,7 +987,7 @@ export default class PricingModuleService<
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async deleteRuleTypes(
|
||||
ruleTypeIds: string[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -1062,7 +1099,7 @@ export default class PricingModuleService<
|
||||
]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async createPriceSetMoneyAmountRules(
|
||||
data: PricingTypes.CreatePriceSetMoneyAmountRulesDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -1079,7 +1116,7 @@ export default class PricingModuleService<
|
||||
})
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async updatePriceSetMoneyAmountRules(
|
||||
data: PricingTypes.UpdatePriceSetMoneyAmountRulesDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -1096,7 +1133,7 @@ export default class PricingModuleService<
|
||||
})
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async deletePriceSetMoneyAmountRules(
|
||||
ids: string[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -1167,7 +1204,7 @@ export default class PricingModuleService<
|
||||
]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async createPriceRules(
|
||||
data: PricingTypes.CreatePriceRuleDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -1182,7 +1219,7 @@ export default class PricingModuleService<
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async updatePriceRules(
|
||||
data: PricingTypes.UpdatePriceRuleDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -1197,7 +1234,7 @@ export default class PricingModuleService<
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async deletePriceRules(
|
||||
priceRuleIds: string[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
|
||||
@@ -8,8 +8,6 @@ import {
|
||||
} from "@medusajs/utils"
|
||||
import { RuleType } from "@models"
|
||||
|
||||
import { doNotForceTransaction, shouldForceTransaction } from "@medusajs/utils"
|
||||
|
||||
type InjectedDependencies = {
|
||||
ruleTypeRepository: DAL.RepositoryService
|
||||
}
|
||||
@@ -65,7 +63,7 @@ export default class RuleTypeService<TEntity extends RuleType = RuleType> {
|
||||
)) as [TEntity[], number]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "ruleTypeRepository_")
|
||||
@InjectTransactionManager("ruleTypeRepository_")
|
||||
async create(
|
||||
data: PricingTypes.CreateRuleTypeDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -76,7 +74,7 @@ export default class RuleTypeService<TEntity extends RuleType = RuleType> {
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "ruleTypeRepository_")
|
||||
@InjectTransactionManager("ruleTypeRepository_")
|
||||
async update(
|
||||
data: PricingTypes.UpdateRuleTypeDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -87,7 +85,7 @@ export default class RuleTypeService<TEntity extends RuleType = RuleType> {
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(doNotForceTransaction, "ruleTypeRepository_")
|
||||
@InjectTransactionManager("ruleTypeRepository_")
|
||||
async delete(
|
||||
ids: string[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
|
||||
Reference in New Issue
Block a user