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
@@ -0,0 +1,6 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/pricing": patch
|
||||||
|
"@medusajs/product": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
feat(product, pricing, utils): Transaction issues and reference issues + refactoring
|
||||||
@@ -5,7 +5,7 @@ inputs:
|
|||||||
node-version:
|
node-version:
|
||||||
description: "Node version"
|
description: "Node version"
|
||||||
required: false
|
required: false
|
||||||
default: "16.20.2"
|
default: "16.10.0"
|
||||||
cache-extension:
|
cache-extension:
|
||||||
description: "Extension for fetching cached dependencies"
|
description: "Extension for fetching cached dependencies"
|
||||||
required: true
|
required: true
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ jobs:
|
|||||||
- name: Setup Node.js environment
|
- name: Setup Node.js environment
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
node-version: "16.20.2"
|
node-version: "16.10.0"
|
||||||
cache: "yarn"
|
cache: "yarn"
|
||||||
|
|
||||||
- name: Assert changed
|
- name: Assert changed
|
||||||
@@ -48,7 +48,7 @@ jobs:
|
|||||||
- name: Setup Node.js environment
|
- name: Setup Node.js environment
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
node-version: "16.20.2"
|
node-version: "16.10.0"
|
||||||
cache: "yarn"
|
cache: "yarn"
|
||||||
|
|
||||||
- name: Assert changed
|
- name: Assert changed
|
||||||
@@ -171,7 +171,7 @@ jobs:
|
|||||||
- name: Setup Node.js environment
|
- name: Setup Node.js environment
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
node-version: "16.20.2"
|
node-version: "16.10.0"
|
||||||
cache: "yarn"
|
cache: "yarn"
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
@@ -223,7 +223,7 @@ jobs:
|
|||||||
- name: Setup Node.js environment
|
- name: Setup Node.js environment
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
node-version: "16.20.2"
|
node-version: "16.10.0"
|
||||||
cache: "yarn"
|
cache: "yarn"
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
@@ -275,7 +275,7 @@ jobs:
|
|||||||
- name: Setup Node.js environment
|
- name: Setup Node.js environment
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
node-version: "16.20.2"
|
node-version: "16.10.0"
|
||||||
cache: "yarn"
|
cache: "yarn"
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ jobs:
|
|||||||
- name: Setup Node.js environment
|
- name: Setup Node.js environment
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
node-version: "16.20.2"
|
node-version: "16.10.0"
|
||||||
cache: "yarn"
|
cache: "yarn"
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ jobs:
|
|||||||
- name: Setup Node.js environment
|
- name: Setup Node.js environment
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
node-version: "16.20.2"
|
node-version: "16.10.0"
|
||||||
cache: "yarn"
|
cache: "yarn"
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ jobs:
|
|||||||
- name: Setup Node.js environment
|
- name: Setup Node.js environment
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
node-version: "16.20.2"
|
node-version: "16.10.0"
|
||||||
cache: "yarn"
|
cache: "yarn"
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ jobs:
|
|||||||
- name: Setup Node.js environment
|
- name: Setup Node.js environment
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
node-version: "16.20.2"
|
node-version: "16.10.0"
|
||||||
cache: "yarn"
|
cache: "yarn"
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ jobs:
|
|||||||
- name: Setup Node.js environment
|
- name: Setup Node.js environment
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
node-version: "16.20.2"
|
node-version: "16.10.0"
|
||||||
cache: "yarn"
|
cache: "yarn"
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
@@ -89,7 +89,7 @@ jobs:
|
|||||||
- name: Setup Node.js environment
|
- name: Setup Node.js environment
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
node-version: "16.20.2"
|
node-version: "16.10.0"
|
||||||
cache: "yarn"
|
cache: "yarn"
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ jobs:
|
|||||||
- name: Setup Node.js environment
|
- name: Setup Node.js environment
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
node-version: "16.20.2"
|
node-version: "16.10.0"
|
||||||
cache: "yarn"
|
cache: "yarn"
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ jobs:
|
|||||||
- name: Setup Node.js environment
|
- name: Setup Node.js environment
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
node-version: "16.20.2"
|
node-version: "16.10.0"
|
||||||
cache: "yarn"
|
cache: "yarn"
|
||||||
|
|
||||||
- name: Post to Slack channel
|
- name: Post to Slack channel
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ module.exports = {
|
|||||||
|
|
||||||
const shutdown = async () => {
|
const shutdown = async () => {
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
|
container.dispose(),
|
||||||
expressServer.close(),
|
expressServer.close(),
|
||||||
db?.destroy(),
|
db?.destroy(),
|
||||||
pgConnection?.context?.destroy(),
|
pgConnection?.context?.destroy(),
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ const env = {
|
|||||||
MEDUSA_FF_ISOLATE_PRODUCT_DOMAIN: true,
|
MEDUSA_FF_ISOLATE_PRODUCT_DOMAIN: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
describe.skip("[Product & Pricing Module] POST /admin/products", () => {
|
describe("[Product & Pricing Module] POST /admin/products", () => {
|
||||||
let dbConnection
|
let dbConnection
|
||||||
let appContainer
|
let appContainer
|
||||||
let shutdownServer
|
let shutdownServer
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ const env = {
|
|||||||
MEDUSA_FF_ISOLATE_PRODUCT_DOMAIN: true,
|
MEDUSA_FF_ISOLATE_PRODUCT_DOMAIN: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
describe.skip("[Product & Pricing Module] POST /admin/products/:id/variants/:id", () => {
|
describe("[Product & Pricing Module] POST /admin/products/:id/variants/:id", () => {
|
||||||
let dbConnection
|
let dbConnection
|
||||||
let appContainer
|
let appContainer
|
||||||
let shutdownServer
|
let shutdownServer
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ const env = {
|
|||||||
MEDUSA_FF_ISOLATE_PRODUCT_DOMAIN: true,
|
MEDUSA_FF_ISOLATE_PRODUCT_DOMAIN: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
describe.skip("[Product & Pricing Module] POST /admin/products/:id", () => {
|
describe("[Product & Pricing Module] POST /admin/products/:id", () => {
|
||||||
let dbConnection
|
let dbConnection
|
||||||
let appContainer
|
let appContainer
|
||||||
let shutdownServer
|
let shutdownServer
|
||||||
@@ -172,9 +172,7 @@ describe.skip("[Product & Pricing Module] POST /admin/products/:id", () => {
|
|||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("I am here first")
|
|
||||||
await api.post(`/admin/products/${product.id}`, data, adminHeaders)
|
await api.post(`/admin/products/${product.id}`, data, adminHeaders)
|
||||||
console.log("I am here")
|
|
||||||
|
|
||||||
const response = await api.get(
|
const response = await api.get(
|
||||||
`/admin/products/${product.id}`,
|
`/admin/products/${product.id}`,
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import { getContainer } from "../../../../environment-helpers/use-container"
|
|||||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||||
import { simpleProductFactory } from "../../../../factories"
|
import { simpleProductFactory } from "../../../../factories"
|
||||||
|
|
||||||
jest.setTimeout(30000)
|
jest.setTimeout(100000)
|
||||||
|
|
||||||
describe("UpdateProduct workflow", function () {
|
describe("UpdateProduct workflow", function () {
|
||||||
let dbConnection
|
let dbConnection
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ module.exports = {
|
|||||||
`/www/`,
|
`/www/`,
|
||||||
`/dist/`,
|
`/dist/`,
|
||||||
`/node_modules/`,
|
`/node_modules/`,
|
||||||
"<rootDir>/node_modules",
|
`<rootDir>/node_modules/`,
|
||||||
`__tests__/fixtures`,
|
`__tests__/fixtures`,
|
||||||
`__testfixtures__`,
|
`__testfixtures__`,
|
||||||
`.cache`,
|
`.cache`,
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test:integration": "node --expose-gc ./../../node_modules/.bin/jest --silent=false --runInBand --bail --logHeapUsage --forceExit",
|
"test:integration": "node --expose-gc ./../../node_modules/.bin/jest --silent=false --runInBand --bail --detectOpenHandles --logHeapUsage --clearMocks --no-compilation-cache --forceExit",
|
||||||
"build": "babel src -d dist --extensions \".ts,.js\""
|
"build": "babel src -d dist --extensions \".ts,.js\""
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ export class PricingRepository
|
|||||||
pricingContext: PricingContext = { context: {} },
|
pricingContext: PricingContext = { context: {} },
|
||||||
sharedContext: Context = {}
|
sharedContext: Context = {}
|
||||||
): Promise<CalculatedPriceSetDTO[]> {
|
): Promise<CalculatedPriceSetDTO[]> {
|
||||||
const manager = this.getActiveManager<SqlEntityManager>()
|
const manager = this.getActiveManager<SqlEntityManager>(sharedContext)
|
||||||
const knex = manager.getKnex()
|
const knex = manager.getKnex()
|
||||||
const context = pricingContext.context || {}
|
const context = pricingContext.context || {}
|
||||||
|
|
||||||
|
|||||||
@@ -9,8 +9,6 @@ import {
|
|||||||
import { Currency } from "@models"
|
import { Currency } from "@models"
|
||||||
import { CurrencyRepository } from "@repositories"
|
import { CurrencyRepository } from "@repositories"
|
||||||
|
|
||||||
import { doNotForceTransaction, shouldForceTransaction } from "@medusajs/utils"
|
|
||||||
|
|
||||||
type InjectedDependencies = {
|
type InjectedDependencies = {
|
||||||
currencyRepository: DAL.RepositoryService
|
currencyRepository: DAL.RepositoryService
|
||||||
}
|
}
|
||||||
@@ -75,7 +73,7 @@ export default class CurrencyService<TEntity extends Currency = Currency> {
|
|||||||
return queryOptions
|
return queryOptions
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "currencyRepository_")
|
@InjectTransactionManager("currencyRepository_")
|
||||||
async create(
|
async create(
|
||||||
data: PricingTypes.CreateCurrencyDTO[],
|
data: PricingTypes.CreateCurrencyDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -86,7 +84,7 @@ export default class CurrencyService<TEntity extends Currency = Currency> {
|
|||||||
)) as TEntity[]
|
)) as TEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "currencyRepository_")
|
@InjectTransactionManager("currencyRepository_")
|
||||||
async update(
|
async update(
|
||||||
data: PricingTypes.UpdateCurrencyDTO[],
|
data: PricingTypes.UpdateCurrencyDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -97,7 +95,7 @@ export default class CurrencyService<TEntity extends Currency = Currency> {
|
|||||||
)) as TEntity[]
|
)) as TEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(doNotForceTransaction, "currencyRepository_")
|
@InjectTransactionManager("currencyRepository_")
|
||||||
async delete(
|
async delete(
|
||||||
ids: string[],
|
ids: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
|||||||
@@ -9,8 +9,6 @@ import {
|
|||||||
import { MoneyAmount } from "@models"
|
import { MoneyAmount } from "@models"
|
||||||
import { MoneyAmountRepository } from "@repositories"
|
import { MoneyAmountRepository } from "@repositories"
|
||||||
|
|
||||||
import { doNotForceTransaction, shouldForceTransaction } from "@medusajs/utils"
|
|
||||||
|
|
||||||
type InjectedDependencies = {
|
type InjectedDependencies = {
|
||||||
moneyAmountRepository: DAL.RepositoryService
|
moneyAmountRepository: DAL.RepositoryService
|
||||||
}
|
}
|
||||||
@@ -73,7 +71,7 @@ export default class MoneyAmountService<
|
|||||||
)) as [TEntity[], number]
|
)) as [TEntity[], number]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "moneyAmountRepository_")
|
@InjectTransactionManager("moneyAmountRepository_")
|
||||||
async create(
|
async create(
|
||||||
data: PricingTypes.CreateMoneyAmountDTO[],
|
data: PricingTypes.CreateMoneyAmountDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -84,7 +82,7 @@ export default class MoneyAmountService<
|
|||||||
)) as TEntity[]
|
)) as TEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "moneyAmountRepository_")
|
@InjectTransactionManager("moneyAmountRepository_")
|
||||||
async update(
|
async update(
|
||||||
data: PricingTypes.UpdateMoneyAmountDTO[],
|
data: PricingTypes.UpdateMoneyAmountDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -95,7 +93,7 @@ export default class MoneyAmountService<
|
|||||||
)) as TEntity[]
|
)) as TEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(doNotForceTransaction, "moneyAmountRepository_")
|
@InjectTransactionManager("moneyAmountRepository_")
|
||||||
async delete(
|
async delete(
|
||||||
ids: string[],
|
ids: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
|||||||
@@ -4,9 +4,7 @@ import {
|
|||||||
InjectTransactionManager,
|
InjectTransactionManager,
|
||||||
MedusaContext,
|
MedusaContext,
|
||||||
ModulesSdkUtils,
|
ModulesSdkUtils,
|
||||||
doNotForceTransaction,
|
|
||||||
retrieveEntity,
|
retrieveEntity,
|
||||||
shouldForceTransaction,
|
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import { PriceRule } from "@models"
|
import { PriceRule } from "@models"
|
||||||
import { PriceRuleRepository } from "@repositories"
|
import { PriceRuleRepository } from "@repositories"
|
||||||
@@ -65,7 +63,7 @@ export default class PriceRuleService<TEntity extends PriceRule = PriceRule> {
|
|||||||
)) as [TEntity[], number]
|
)) as [TEntity[], number]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "priceRuleRepository_")
|
@InjectTransactionManager("priceRuleRepository_")
|
||||||
async create(
|
async create(
|
||||||
data: PricingTypes.CreatePriceRuleDTO[],
|
data: PricingTypes.CreatePriceRuleDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -76,7 +74,7 @@ export default class PriceRuleService<TEntity extends PriceRule = PriceRule> {
|
|||||||
)) as TEntity[]
|
)) as TEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "priceRuleRepository_")
|
@InjectTransactionManager("priceRuleRepository_")
|
||||||
async update(
|
async update(
|
||||||
data: PricingTypes.UpdatePriceRuleDTO[],
|
data: PricingTypes.UpdatePriceRuleDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -87,7 +85,7 @@ export default class PriceRuleService<TEntity extends PriceRule = PriceRule> {
|
|||||||
)) as TEntity[]
|
)) as TEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(doNotForceTransaction, "priceRuleRepository_")
|
@InjectTransactionManager("priceRuleRepository_")
|
||||||
async delete(
|
async delete(
|
||||||
ids: string[],
|
ids: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ import {
|
|||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import { PriceSetMoneyAmountRules } from "@models"
|
import { PriceSetMoneyAmountRules } from "@models"
|
||||||
|
|
||||||
import { doNotForceTransaction, shouldForceTransaction } from "@medusajs/utils"
|
|
||||||
|
|
||||||
type InjectedDependencies = {
|
type InjectedDependencies = {
|
||||||
priceSetMoneyAmountRulesRepository: DAL.RepositoryService
|
priceSetMoneyAmountRulesRepository: DAL.RepositoryService
|
||||||
}
|
}
|
||||||
@@ -79,10 +77,7 @@ export default class PriceSetMoneyAmountRulesService<
|
|||||||
return queryOptions
|
return queryOptions
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(
|
@InjectTransactionManager("priceSetMoneyAmountRulesRepository_")
|
||||||
shouldForceTransaction,
|
|
||||||
"priceSetMoneyAmountRulesRepository_"
|
|
||||||
)
|
|
||||||
async create(
|
async create(
|
||||||
data: PricingTypes.CreatePriceSetMoneyAmountRulesDTO[],
|
data: PricingTypes.CreatePriceSetMoneyAmountRulesDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -93,10 +88,7 @@ export default class PriceSetMoneyAmountRulesService<
|
|||||||
)) as TEntity[]
|
)) as TEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(
|
@InjectTransactionManager("priceSetMoneyAmountRulesRepository_")
|
||||||
shouldForceTransaction,
|
|
||||||
"priceSetMoneyAmountRulesRepository_"
|
|
||||||
)
|
|
||||||
async update(
|
async update(
|
||||||
data: PricingTypes.UpdatePriceSetMoneyAmountRulesDTO[],
|
data: PricingTypes.UpdatePriceSetMoneyAmountRulesDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -107,10 +99,7 @@ export default class PriceSetMoneyAmountRulesService<
|
|||||||
)) as TEntity[]
|
)) as TEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(
|
@InjectTransactionManager("priceSetMoneyAmountRulesRepository_")
|
||||||
doNotForceTransaction,
|
|
||||||
"priceSetMoneyAmountRulesRepository_"
|
|
||||||
)
|
|
||||||
async delete(
|
async delete(
|
||||||
ids: string[],
|
ids: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
import { Context, DAL, FindConfig, PricingTypes } from "@medusajs/types"
|
import { Context, DAL, FindConfig, PricingTypes } from "@medusajs/types"
|
||||||
import {
|
import {
|
||||||
|
doNotForceTransaction,
|
||||||
InjectManager,
|
InjectManager,
|
||||||
InjectTransactionManager,
|
InjectTransactionManager,
|
||||||
MedusaContext,
|
MedusaContext,
|
||||||
ModulesSdkUtils,
|
ModulesSdkUtils,
|
||||||
doNotForceTransaction,
|
|
||||||
retrieveEntity,
|
retrieveEntity,
|
||||||
shouldForceTransaction,
|
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import { PriceSet, PriceSetMoneyAmount } from "@models"
|
import { PriceSet, PriceSetMoneyAmount } from "@models"
|
||||||
import { PriceSetMoneyAmountRepository } from "@repositories"
|
import { PriceSetMoneyAmountRepository } from "@repositories"
|
||||||
@@ -79,36 +78,29 @@ export default class PriceSetMoneyAmountService<
|
|||||||
return queryOptions
|
return queryOptions
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(
|
@InjectTransactionManager("priceSetMoneyAmountRepository_")
|
||||||
shouldForceTransaction,
|
|
||||||
"priceSetMoneyAmountRepository_"
|
|
||||||
)
|
|
||||||
async create(
|
async create(
|
||||||
data: PricingTypes.CreatePriceSetMoneyAmountDTO[],
|
data: PricingTypes.CreatePriceSetMoneyAmountDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
): Promise<TEntity[]> {
|
): Promise<TEntity[]> {
|
||||||
return (await (
|
return (await (
|
||||||
this.priceSetMoneyAmountRepository_ as unknown as PriceSetMoneyAmountRepository
|
this
|
||||||
|
.priceSetMoneyAmountRepository_ as unknown as PriceSetMoneyAmountRepository
|
||||||
).create(data, sharedContext)) as TEntity[]
|
).create(data, sharedContext)) as TEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(
|
@InjectTransactionManager("priceSetMoneyAmountRepository_")
|
||||||
shouldForceTransaction,
|
|
||||||
"priceSetMoneyAmountRepository_"
|
|
||||||
)
|
|
||||||
async update(
|
async update(
|
||||||
data: PricingTypes.UpdatePriceSetMoneyAmountDTO[],
|
data: PricingTypes.UpdatePriceSetMoneyAmountDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
): Promise<TEntity[]> {
|
): Promise<TEntity[]> {
|
||||||
return (await (
|
return (await (
|
||||||
this.priceSetMoneyAmountRepository_ as unknown as PriceSetMoneyAmountRepository
|
this
|
||||||
|
.priceSetMoneyAmountRepository_ as unknown as PriceSetMoneyAmountRepository
|
||||||
).update(data, sharedContext)) as TEntity[]
|
).update(data, sharedContext)) as TEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(
|
@InjectTransactionManager("priceSetMoneyAmountRepository_")
|
||||||
doNotForceTransaction,
|
|
||||||
"priceSetMoneyAmountRepository_"
|
|
||||||
)
|
|
||||||
async delete(
|
async delete(
|
||||||
ids: string[],
|
ids: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
|||||||
@@ -4,9 +4,7 @@ import {
|
|||||||
InjectTransactionManager,
|
InjectTransactionManager,
|
||||||
MedusaContext,
|
MedusaContext,
|
||||||
ModulesSdkUtils,
|
ModulesSdkUtils,
|
||||||
doNotForceTransaction,
|
|
||||||
retrieveEntity,
|
retrieveEntity,
|
||||||
shouldForceTransaction,
|
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import { PriceSet, PriceSetRuleType } from "@models"
|
import { PriceSet, PriceSetRuleType } from "@models"
|
||||||
import { PriceSetRuleTypeRepository } from "src/repositories/price-set-rule-type"
|
import { PriceSetRuleTypeRepository } from "src/repositories/price-set-rule-type"
|
||||||
@@ -15,7 +13,9 @@ type InjectedDependencies = {
|
|||||||
priceSetRuleTypeRepository: DAL.RepositoryService
|
priceSetRuleTypeRepository: DAL.RepositoryService
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class PriceSetRuleTypeService<TEntity extends PriceSetRuleType = PriceSetRuleType> {
|
export default class PriceSetRuleTypeService<
|
||||||
|
TEntity extends PriceSetRuleType = PriceSetRuleType
|
||||||
|
> {
|
||||||
protected readonly priceSetRuleTypeRepository_: DAL.RepositoryService
|
protected readonly priceSetRuleTypeRepository_: DAL.RepositoryService
|
||||||
|
|
||||||
constructor({ priceSetRuleTypeRepository }: InjectedDependencies) {
|
constructor({ priceSetRuleTypeRepository }: InjectedDependencies) {
|
||||||
@@ -28,7 +28,10 @@ export default class PriceSetRuleTypeService<TEntity extends PriceSetRuleType =
|
|||||||
config: FindConfig<PricingTypes.PriceSetRuleTypeDTO> = {},
|
config: FindConfig<PricingTypes.PriceSetRuleTypeDTO> = {},
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
): Promise<TEntity> {
|
): Promise<TEntity> {
|
||||||
return (await retrieveEntity<PriceSetRuleType, PricingTypes.PriceSetRuleTypeDTO>({
|
return (await retrieveEntity<
|
||||||
|
PriceSetRuleType,
|
||||||
|
PricingTypes.PriceSetRuleTypeDTO
|
||||||
|
>({
|
||||||
id: priceSetId,
|
id: priceSetId,
|
||||||
entityName: PriceSet.name,
|
entityName: PriceSet.name,
|
||||||
repository: this.priceSetRuleTypeRepository_,
|
repository: this.priceSetRuleTypeRepository_,
|
||||||
@@ -74,29 +77,27 @@ export default class PriceSetRuleTypeService<TEntity extends PriceSetRuleType =
|
|||||||
return queryOptions
|
return queryOptions
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "priceSetRuleTypeRepository_")
|
@InjectTransactionManager("priceSetRuleTypeRepository_")
|
||||||
async create(
|
async create(
|
||||||
data: PricingTypes.CreatePriceSetRuleTypeDTO[],
|
data: PricingTypes.CreatePriceSetRuleTypeDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
): Promise<TEntity[]> {
|
): Promise<TEntity[]> {
|
||||||
return (await (this.priceSetRuleTypeRepository_ as PriceSetRuleTypeRepository).create(
|
return (await (
|
||||||
data,
|
this.priceSetRuleTypeRepository_ as PriceSetRuleTypeRepository
|
||||||
sharedContext
|
).create(data, sharedContext)) as TEntity[]
|
||||||
)) as TEntity[]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "priceSetRuleTypeRepository_")
|
@InjectTransactionManager("priceSetRuleTypeRepository_")
|
||||||
async update(
|
async update(
|
||||||
data: PricingTypes.UpdatePriceSetRuleTypeDTO[],
|
data: PricingTypes.UpdatePriceSetRuleTypeDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
): Promise<TEntity[]> {
|
): Promise<TEntity[]> {
|
||||||
return (await (this.priceSetRuleTypeRepository_ as PriceSetRuleTypeRepository).update(
|
return (await (
|
||||||
data,
|
this.priceSetRuleTypeRepository_ as PriceSetRuleTypeRepository
|
||||||
sharedContext
|
).update(data, sharedContext)) as TEntity[]
|
||||||
)) as TEntity[]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(doNotForceTransaction, "priceSetRuleTypeRepository_")
|
@InjectTransactionManager("priceSetRuleTypeRepository_")
|
||||||
async delete(
|
async delete(
|
||||||
ids: string[],
|
ids: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
|||||||
@@ -4,9 +4,7 @@ import {
|
|||||||
InjectTransactionManager,
|
InjectTransactionManager,
|
||||||
MedusaContext,
|
MedusaContext,
|
||||||
ModulesSdkUtils,
|
ModulesSdkUtils,
|
||||||
doNotForceTransaction,
|
|
||||||
retrieveEntity,
|
retrieveEntity,
|
||||||
shouldForceTransaction,
|
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import { PriceSet } from "@models"
|
import { PriceSet } from "@models"
|
||||||
import { PriceSetRepository } from "@repositories"
|
import { PriceSetRepository } from "@repositories"
|
||||||
@@ -65,7 +63,7 @@ export default class PriceSetService<TEntity extends PriceSet = PriceSet> {
|
|||||||
)) as [TEntity[], number]
|
)) as [TEntity[], number]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "priceSetRepository_")
|
@InjectTransactionManager("priceSetRepository_")
|
||||||
async create(
|
async create(
|
||||||
data: Omit<PricingTypes.CreatePriceSetDTO, "rules">[],
|
data: Omit<PricingTypes.CreatePriceSetDTO, "rules">[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -76,7 +74,7 @@ export default class PriceSetService<TEntity extends PriceSet = PriceSet> {
|
|||||||
)) as TEntity[]
|
)) as TEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "priceSetRepository_")
|
@InjectTransactionManager("priceSetRepository_")
|
||||||
async update(
|
async update(
|
||||||
data: Omit<PricingTypes.UpdatePriceSetDTO, "rules">[],
|
data: Omit<PricingTypes.UpdatePriceSetDTO, "rules">[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -87,7 +85,7 @@ export default class PriceSetService<TEntity extends PriceSet = PriceSet> {
|
|||||||
)) as TEntity[]
|
)) as TEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(doNotForceTransaction, "priceSetRepository_")
|
@InjectTransactionManager("priceSetRepository_")
|
||||||
async delete(
|
async delete(
|
||||||
ids: string[],
|
ids: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
|
AddPricesDTO,
|
||||||
Context,
|
Context,
|
||||||
CreateMoneyAmountDTO,
|
CreateMoneyAmountDTO,
|
||||||
DAL,
|
DAL,
|
||||||
@@ -35,16 +36,13 @@ import {
|
|||||||
} from "@services"
|
} from "@services"
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
groupBy,
|
||||||
InjectManager,
|
InjectManager,
|
||||||
InjectTransactionManager,
|
InjectTransactionManager,
|
||||||
MedusaContext,
|
MedusaContext,
|
||||||
MedusaError,
|
MedusaError,
|
||||||
groupBy,
|
|
||||||
removeNullish,
|
removeNullish,
|
||||||
shouldForceTransaction,
|
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
|
|
||||||
import { AddPricesDTO } from "@medusajs/types"
|
|
||||||
import { joinerConfig } from "../joiner-config"
|
import { joinerConfig } from "../joiner-config"
|
||||||
import { PricingRepositoryService } from "../types"
|
import { PricingRepositoryService } from "../types"
|
||||||
|
|
||||||
@@ -231,7 +229,8 @@ export default class PricingModuleService<
|
|||||||
{ id: priceSets.filter((p) => !!p).map((p) => p!.id) },
|
{ id: priceSets.filter((p) => !!p).map((p) => p!.id) },
|
||||||
{
|
{
|
||||||
relations: ["rule_types", "money_amounts", "price_rules"],
|
relations: ["rule_types", "money_amounts", "price_rules"],
|
||||||
}
|
},
|
||||||
|
sharedContext
|
||||||
)
|
)
|
||||||
|
|
||||||
return (Array.isArray(data) ? dbPriceSets : dbPriceSets[0]) as unknown as
|
return (Array.isArray(data) ? dbPriceSets : dbPriceSets[0]) as unknown as
|
||||||
@@ -239,7 +238,7 @@ export default class PricingModuleService<
|
|||||||
| PriceSetDTO[]
|
| PriceSetDTO[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
protected async create_(
|
protected async create_(
|
||||||
data: PricingTypes.CreatePriceSetDTO[],
|
data: PricingTypes.CreatePriceSetDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -286,74 +285,89 @@ export default class PricingModuleService<
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const priceSets = await Promise.all(
|
// Bulk create price sets
|
||||||
data.map(async (d) => {
|
const priceSetData = data.map(({ rules, prices, ...rest }) => rest)
|
||||||
const { rules, prices, ...rest } = d
|
const createdPriceSets = await this.priceSetService_.create(
|
||||||
const [priceSet] = await this.priceSetService_.create(
|
priceSetData,
|
||||||
[rest],
|
sharedContext
|
||||||
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
|
|
||||||
})
|
|
||||||
)
|
)
|
||||||
|
|
||||||
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(
|
async addRules(
|
||||||
@@ -380,14 +394,15 @@ export default class PricingModuleService<
|
|||||||
| PricingTypes.PriceSetDTO
|
| PricingTypes.PriceSetDTO
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
protected async addRules_(
|
protected async addRules_(
|
||||||
inputs: PricingTypes.AddRulesDTO[],
|
inputs: PricingTypes.AddRulesDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
): Promise<PricingTypes.PriceSetDTO[]> {
|
): Promise<PricingTypes.PriceSetDTO[]> {
|
||||||
const priceSets = await this.priceSetService_.list(
|
const priceSets = await this.priceSetService_.list(
|
||||||
{ id: inputs.map((d) => d.priceSetId) },
|
{ id: inputs.map((d) => d.priceSetId) },
|
||||||
{ relations: ["rule_types"] }
|
{ relations: ["rule_types"] },
|
||||||
|
sharedContext
|
||||||
)
|
)
|
||||||
|
|
||||||
const priceSetRuleTypeMap: Map<string, Map<string, RuleTypeDTO>> = new Map(
|
const priceSetRuleTypeMap: Map<string, Map<string, RuleTypeDTO>> = new Map(
|
||||||
@@ -491,11 +506,12 @@ export default class PricingModuleService<
|
|||||||
|
|
||||||
return (await this.list(
|
return (await this.list(
|
||||||
{ id: input.map((d) => d.priceSetId) },
|
{ id: input.map((d) => d.priceSetId) },
|
||||||
{ relations: ["money_amounts"] }
|
{ relations: ["money_amounts"] },
|
||||||
|
sharedContext
|
||||||
)) as unknown as PricingTypes.PriceSetDTO[] | PricingTypes.PriceSetDTO
|
)) as unknown as PricingTypes.PriceSetDTO[] | PricingTypes.PriceSetDTO
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
protected async addPrices_(
|
protected async addPrices_(
|
||||||
input: AddPricesDTO[],
|
input: AddPricesDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -543,73 +559,93 @@ export default class PricingModuleService<
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
for (const { priceSetId, prices } of input) {
|
// Money amounts
|
||||||
await Promise.all(
|
const moneyAmountsBulkData = input.flatMap((entry) => entry.prices)
|
||||||
prices.map(async (ma) => {
|
const createdMoneyAmounts = await this.moneyAmountService_.create(
|
||||||
const [moneyAmount] = await this.moneyAmountService_.create(
|
moneyAmountsBulkData as unknown as CreateMoneyAmountDTO[],
|
||||||
[ma] as unknown as CreateMoneyAmountDTO[],
|
sharedContext
|
||||||
sharedContext
|
)
|
||||||
)
|
|
||||||
|
|
||||||
const numberOfRules = Object.entries(ma?.rules ?? {}).length
|
// Price set money amounts
|
||||||
|
let maCursor = 0
|
||||||
const [priceSetMoneyAmount] =
|
const priceSetMoneyAmountsBulkData = input.flatMap(
|
||||||
await this.priceSetMoneyAmountService_.create(
|
({ priceSetId, prices }) =>
|
||||||
[
|
prices.map(() => {
|
||||||
{
|
const ma = createdMoneyAmounts[maCursor]
|
||||||
price_set: priceSetId,
|
const numberOfRules = Object.entries(
|
||||||
money_amount: moneyAmount,
|
prices[maCursor]?.rules ?? {}
|
||||||
title: "test",
|
).length
|
||||||
number_rules: numberOfRules,
|
maCursor++
|
||||||
},
|
return {
|
||||||
] as unknown as PricingTypes.CreatePriceSetMoneyAmountDTO[],
|
price_set: priceSetId,
|
||||||
sharedContext
|
money_amount: ma,
|
||||||
)
|
title: "test", // TODO: accept title
|
||||||
|
number_rules: numberOfRules,
|
||||||
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
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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(
|
async removeRules(
|
||||||
data: PricingTypes.RemovePriceSetRulesDTO[],
|
data: PricingTypes.RemovePriceSetRulesDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const priceSets = await this.priceSetService_.list({
|
const priceSets = await this.priceSetService_.list(
|
||||||
id: data.map((d) => d.id),
|
{
|
||||||
})
|
id: data.map((d) => d.id),
|
||||||
|
},
|
||||||
|
undefined,
|
||||||
|
sharedContext
|
||||||
|
)
|
||||||
const priceSetIds = priceSets.map((ps) => ps.id)
|
const priceSetIds = priceSets.map((ps) => ps.id)
|
||||||
|
|
||||||
const ruleTypes = await this.ruleTypeService_.list({
|
const ruleTypes = await this.ruleTypeService_.list(
|
||||||
rule_attribute: data.map((d) => d.rules || []).flat(),
|
{
|
||||||
})
|
rule_attribute: data.map((d) => d.rules || []).flat(),
|
||||||
|
},
|
||||||
|
undefined,
|
||||||
|
sharedContext
|
||||||
|
)
|
||||||
const ruleTypeIds = ruleTypes.map((rt) => rt.id)
|
const ruleTypeIds = ruleTypes.map((rt) => rt.id)
|
||||||
|
|
||||||
const priceSetRuleTypes = await this.priceSetRuleTypeService_.list({
|
const priceSetRuleTypes = await this.priceSetRuleTypeService_.list(
|
||||||
price_set_id: priceSetIds,
|
{
|
||||||
rule_type_id: ruleTypeIds,
|
price_set_id: priceSetIds,
|
||||||
})
|
rule_type_id: ruleTypeIds,
|
||||||
|
},
|
||||||
|
undefined,
|
||||||
|
sharedContext
|
||||||
|
)
|
||||||
|
|
||||||
const priceRules = await this.priceRuleService_.list(
|
const priceRules = await this.priceRuleService_.list(
|
||||||
{
|
{
|
||||||
@@ -618,7 +654,8 @@ export default class PricingModuleService<
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
select: ["price_set_money_amount"],
|
select: ["price_set_money_amount"],
|
||||||
}
|
},
|
||||||
|
sharedContext
|
||||||
)
|
)
|
||||||
|
|
||||||
await this.priceSetRuleTypeService_.delete(
|
await this.priceSetRuleTypeService_.delete(
|
||||||
@@ -632,7 +669,7 @@ export default class PricingModuleService<
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async update(
|
async update(
|
||||||
data: PricingTypes.UpdatePriceSetDTO[],
|
data: PricingTypes.UpdatePriceSetDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -647,7 +684,7 @@ export default class PricingModuleService<
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async delete(
|
async delete(
|
||||||
ids: string[],
|
ids: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -718,7 +755,7 @@ export default class PricingModuleService<
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async createMoneyAmounts(
|
async createMoneyAmounts(
|
||||||
data: PricingTypes.CreateMoneyAmountDTO[],
|
data: PricingTypes.CreateMoneyAmountDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -736,7 +773,7 @@ export default class PricingModuleService<
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async updateMoneyAmounts(
|
async updateMoneyAmounts(
|
||||||
data: PricingTypes.UpdateMoneyAmountDTO[],
|
data: PricingTypes.UpdateMoneyAmountDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -754,7 +791,7 @@ export default class PricingModuleService<
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async deleteMoneyAmounts(
|
async deleteMoneyAmounts(
|
||||||
ids: string[],
|
ids: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -822,7 +859,7 @@ export default class PricingModuleService<
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async createCurrencies(
|
async createCurrencies(
|
||||||
data: PricingTypes.CreateCurrencyDTO[],
|
data: PricingTypes.CreateCurrencyDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -837,7 +874,7 @@ export default class PricingModuleService<
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async updateCurrencies(
|
async updateCurrencies(
|
||||||
data: PricingTypes.UpdateCurrencyDTO[],
|
data: PricingTypes.UpdateCurrencyDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -852,7 +889,7 @@ export default class PricingModuleService<
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async deleteCurrencies(
|
async deleteCurrencies(
|
||||||
currencyCodes: string[],
|
currencyCodes: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -920,7 +957,7 @@ export default class PricingModuleService<
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async createRuleTypes(
|
async createRuleTypes(
|
||||||
data: PricingTypes.CreateRuleTypeDTO[],
|
data: PricingTypes.CreateRuleTypeDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -935,7 +972,7 @@ export default class PricingModuleService<
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async updateRuleTypes(
|
async updateRuleTypes(
|
||||||
data: PricingTypes.UpdateRuleTypeDTO[],
|
data: PricingTypes.UpdateRuleTypeDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -950,7 +987,7 @@ export default class PricingModuleService<
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async deleteRuleTypes(
|
async deleteRuleTypes(
|
||||||
ruleTypeIds: string[],
|
ruleTypeIds: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -1062,7 +1099,7 @@ export default class PricingModuleService<
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async createPriceSetMoneyAmountRules(
|
async createPriceSetMoneyAmountRules(
|
||||||
data: PricingTypes.CreatePriceSetMoneyAmountRulesDTO[],
|
data: PricingTypes.CreatePriceSetMoneyAmountRulesDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -1079,7 +1116,7 @@ export default class PricingModuleService<
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async updatePriceSetMoneyAmountRules(
|
async updatePriceSetMoneyAmountRules(
|
||||||
data: PricingTypes.UpdatePriceSetMoneyAmountRulesDTO[],
|
data: PricingTypes.UpdatePriceSetMoneyAmountRulesDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -1096,7 +1133,7 @@ export default class PricingModuleService<
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async deletePriceSetMoneyAmountRules(
|
async deletePriceSetMoneyAmountRules(
|
||||||
ids: string[],
|
ids: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -1167,7 +1204,7 @@ export default class PricingModuleService<
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async createPriceRules(
|
async createPriceRules(
|
||||||
data: PricingTypes.CreatePriceRuleDTO[],
|
data: PricingTypes.CreatePriceRuleDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -1182,7 +1219,7 @@ export default class PricingModuleService<
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async updatePriceRules(
|
async updatePriceRules(
|
||||||
data: PricingTypes.UpdatePriceRuleDTO[],
|
data: PricingTypes.UpdatePriceRuleDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -1197,7 +1234,7 @@ export default class PricingModuleService<
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async deletePriceRules(
|
async deletePriceRules(
|
||||||
priceRuleIds: string[],
|
priceRuleIds: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ import {
|
|||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import { RuleType } from "@models"
|
import { RuleType } from "@models"
|
||||||
|
|
||||||
import { doNotForceTransaction, shouldForceTransaction } from "@medusajs/utils"
|
|
||||||
|
|
||||||
type InjectedDependencies = {
|
type InjectedDependencies = {
|
||||||
ruleTypeRepository: DAL.RepositoryService
|
ruleTypeRepository: DAL.RepositoryService
|
||||||
}
|
}
|
||||||
@@ -65,7 +63,7 @@ export default class RuleTypeService<TEntity extends RuleType = RuleType> {
|
|||||||
)) as [TEntity[], number]
|
)) as [TEntity[], number]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "ruleTypeRepository_")
|
@InjectTransactionManager("ruleTypeRepository_")
|
||||||
async create(
|
async create(
|
||||||
data: PricingTypes.CreateRuleTypeDTO[],
|
data: PricingTypes.CreateRuleTypeDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -76,7 +74,7 @@ export default class RuleTypeService<TEntity extends RuleType = RuleType> {
|
|||||||
)) as TEntity[]
|
)) as TEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "ruleTypeRepository_")
|
@InjectTransactionManager("ruleTypeRepository_")
|
||||||
async update(
|
async update(
|
||||||
data: PricingTypes.UpdateRuleTypeDTO[],
|
data: PricingTypes.UpdateRuleTypeDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -87,7 +85,7 @@ export default class RuleTypeService<TEntity extends RuleType = RuleType> {
|
|||||||
)) as TEntity[]
|
)) as TEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(doNotForceTransaction, "ruleTypeRepository_")
|
@InjectTransactionManager("ruleTypeRepository_")
|
||||||
async delete(
|
async delete(
|
||||||
ids: string[],
|
ids: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
|||||||
+3
-7
@@ -69,11 +69,6 @@ describe("ProductModuleService product collections", () => {
|
|||||||
|
|
||||||
productCollectionOne = productCollections[0]
|
productCollectionOne = productCollections[0]
|
||||||
productCollectionTwo = productCollections[1]
|
productCollectionTwo = productCollections[1]
|
||||||
|
|
||||||
await testManager.persistAndFlush([
|
|
||||||
productCollectionOne,
|
|
||||||
productCollectionTwo,
|
|
||||||
])
|
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
@@ -339,16 +334,17 @@ describe("ProductModuleService product collections", () => {
|
|||||||
relations: ["products"],
|
relations: ["products"],
|
||||||
})
|
})
|
||||||
|
|
||||||
|
expect(productCollection.products).toHaveLength(2)
|
||||||
expect(productCollection).toEqual(
|
expect(productCollection).toEqual(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
products: [
|
products: expect.arrayContaining([
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
id: productOne.id,
|
id: productOne.id,
|
||||||
}),
|
}),
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
id: productTwo.id,
|
id: productTwo.id,
|
||||||
}),
|
}),
|
||||||
],
|
]),
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -121,7 +121,9 @@ describe("ProductOption Service", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("should return product option and count when filtered", async () => {
|
it("should return product option and count when filtered", async () => {
|
||||||
const [optionResults, count] = await service.listAndCount({ id: "option-2" })
|
const [optionResults, count] = await service.listAndCount({
|
||||||
|
id: "option-2",
|
||||||
|
})
|
||||||
|
|
||||||
expect(count).toEqual(1)
|
expect(count).toEqual(1)
|
||||||
expect(optionResults).toEqual([
|
expect(optionResults).toEqual([
|
||||||
@@ -132,7 +134,10 @@ describe("ProductOption Service", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("should return product option and count when using skip and take", async () => {
|
it("should return product option and count when using skip and take", async () => {
|
||||||
const [optionResults, count] = await service.listAndCount({}, { skip: 1, take: 1 })
|
const [optionResults, count] = await service.listAndCount(
|
||||||
|
{},
|
||||||
|
{ skip: 1, take: 1 }
|
||||||
|
)
|
||||||
|
|
||||||
expect(count).toEqual(2)
|
expect(count).toEqual(2)
|
||||||
expect(optionResults).toEqual([
|
expect(optionResults).toEqual([
|
||||||
@@ -143,10 +148,13 @@ describe("ProductOption Service", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("should return requested fields", async () => {
|
it("should return requested fields", async () => {
|
||||||
const [optionResults, count] = await service.listAndCount({}, {
|
const [optionResults, count] = await service.listAndCount(
|
||||||
take: 1,
|
{},
|
||||||
select: ["title"],
|
{
|
||||||
})
|
take: 1,
|
||||||
|
select: ["title"],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
const serialized = JSON.parse(JSON.stringify(optionResults))
|
const serialized = JSON.parse(JSON.stringify(optionResults))
|
||||||
|
|
||||||
@@ -164,13 +172,11 @@ describe("ProductOption Service", () => {
|
|||||||
const optionValue = "Option 1"
|
const optionValue = "Option 1"
|
||||||
|
|
||||||
it("should return option for the given id", async () => {
|
it("should return option for the given id", async () => {
|
||||||
const option = await service.retrieve(
|
const option = await service.retrieve(optionId)
|
||||||
optionId,
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(option).toEqual(
|
expect(option).toEqual(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
id: optionId
|
id: optionId,
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
@@ -184,7 +190,9 @@ describe("ProductOption Service", () => {
|
|||||||
error = e
|
error = e
|
||||||
}
|
}
|
||||||
|
|
||||||
expect(error.message).toEqual('ProductOption with id: does-not-exist was not found')
|
expect(error.message).toEqual(
|
||||||
|
"ProductOption with id: does-not-exist was not found"
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should throw an error when an id is not provided", async () => {
|
it("should throw an error when an id is not provided", async () => {
|
||||||
@@ -200,21 +208,16 @@ describe("ProductOption Service", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("should return option based on config select param", async () => {
|
it("should return option based on config select param", async () => {
|
||||||
const option = await service.retrieve(
|
const option = await service.retrieve(optionId, {
|
||||||
optionId,
|
select: ["id", "title"],
|
||||||
{
|
})
|
||||||
select: ["id", "title"],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
const serialized = JSON.parse(JSON.stringify(option))
|
const serialized = JSON.parse(JSON.stringify(option))
|
||||||
|
|
||||||
expect(serialized).toEqual(
|
expect(serialized).toEqual({
|
||||||
{
|
id: optionId,
|
||||||
id: optionId,
|
title: optionValue,
|
||||||
title: optionValue,
|
})
|
||||||
}
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -222,13 +225,10 @@ describe("ProductOption Service", () => {
|
|||||||
const optionId = "option-1"
|
const optionId = "option-1"
|
||||||
|
|
||||||
it("should delete the product option given an ID successfully", async () => {
|
it("should delete the product option given an ID successfully", async () => {
|
||||||
|
await service.delete([optionId])
|
||||||
await service.delete(
|
|
||||||
[optionId],
|
|
||||||
)
|
|
||||||
|
|
||||||
const options = await service.list({
|
const options = await service.list({
|
||||||
id: optionId
|
id: optionId,
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(options).toHaveLength(0)
|
expect(options).toHaveLength(0)
|
||||||
@@ -239,12 +239,12 @@ describe("ProductOption Service", () => {
|
|||||||
const optionId = "option-1"
|
const optionId = "option-1"
|
||||||
|
|
||||||
it("should update the title of the option successfully", async () => {
|
it("should update the title of the option successfully", async () => {
|
||||||
await service.update(
|
await service.update([
|
||||||
[{
|
{
|
||||||
id: optionId,
|
id: optionId,
|
||||||
title: "UK",
|
title: "UK",
|
||||||
}]
|
},
|
||||||
)
|
])
|
||||||
|
|
||||||
const productOption = await service.retrieve(optionId)
|
const productOption = await service.retrieve(optionId)
|
||||||
|
|
||||||
@@ -252,23 +252,23 @@ describe("ProductOption Service", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("should update the relationship of the option successfully", async () => {
|
it("should update the relationship of the option successfully", async () => {
|
||||||
await service.update(
|
await service.update([
|
||||||
[{
|
{
|
||||||
id: optionId,
|
id: optionId,
|
||||||
product_id: productTwo.id,
|
product_id: productTwo.id,
|
||||||
}]
|
},
|
||||||
)
|
])
|
||||||
|
|
||||||
const productOption = await service.retrieve(optionId, {
|
const productOption = await service.retrieve(optionId, {
|
||||||
relations: ["product"]
|
relations: ["product"],
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(productOption).toEqual(
|
expect(productOption).toEqual(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
id: optionId,
|
id: optionId,
|
||||||
product: expect.objectContaining({
|
product: expect.objectContaining({
|
||||||
id: productTwo.id
|
id: productTwo.id,
|
||||||
})
|
}),
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
@@ -278,43 +278,98 @@ describe("ProductOption Service", () => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
await service.update([
|
await service.update([
|
||||||
{
|
{
|
||||||
id: "does-not-exist",
|
id: "does-not-exist",
|
||||||
title: "UK",
|
title: "UK",
|
||||||
}
|
},
|
||||||
])
|
])
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
error = e
|
error = e
|
||||||
}
|
}
|
||||||
|
|
||||||
expect(error.message).toEqual('ProductOption with id "does-not-exist" not found')
|
expect(error.message).toEqual(
|
||||||
|
'ProductOption with id "does-not-exist" not found'
|
||||||
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("create", () => {
|
describe("create", () => {
|
||||||
it("should create a option successfully", async () => {
|
it("should create a option successfully", async () => {
|
||||||
await service.create(
|
await service.create([
|
||||||
[{
|
{
|
||||||
title: "UK",
|
title: "UK",
|
||||||
product: productOne
|
product: productOne,
|
||||||
}]
|
},
|
||||||
|
])
|
||||||
|
|
||||||
|
const [productOption] = await service.list(
|
||||||
|
{
|
||||||
|
title: "UK",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
relations: ["product"],
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
const [productOption] = await service.list({
|
|
||||||
title: "UK"
|
|
||||||
}, {
|
|
||||||
relations: ["product"],
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
expect(productOption).toEqual(
|
expect(productOption).toEqual(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
title: "UK",
|
title: "UK",
|
||||||
product: expect.objectContaining({
|
product: expect.objectContaining({
|
||||||
id: productOne.id
|
id: productOne.id,
|
||||||
})
|
}),
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("upsert", function () {
|
||||||
|
it("should create an option and update another option successfully", async () => {
|
||||||
|
const productOption = (
|
||||||
|
await service.create([
|
||||||
|
{
|
||||||
|
title: "UK",
|
||||||
|
product: productOne,
|
||||||
|
},
|
||||||
|
])
|
||||||
|
)[0]
|
||||||
|
|
||||||
|
const optionToUpdate = {
|
||||||
|
id: productOption.id,
|
||||||
|
title: "US",
|
||||||
|
}
|
||||||
|
|
||||||
|
const newOption = {
|
||||||
|
title: "US2",
|
||||||
|
product_id: productOne.id,
|
||||||
|
}
|
||||||
|
|
||||||
|
await service.upsert([optionToUpdate, newOption])
|
||||||
|
|
||||||
|
const productOptions = await service.list(
|
||||||
|
{
|
||||||
|
title: "US%",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
relations: ["product"],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(JSON.parse(JSON.stringify(productOptions))).toEqual(
|
||||||
|
expect.arrayContaining([
|
||||||
|
expect.objectContaining({
|
||||||
|
title: "US",
|
||||||
|
product: expect.objectContaining({
|
||||||
|
id: productOne.id,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
expect.objectContaining({
|
||||||
|
title: newOption.title,
|
||||||
|
product: expect.objectContaining({
|
||||||
|
id: productOne.id,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
])
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import { Context, DAL, ProductTypes } from "@medusajs/types"
|
import { Context, DAL, ProductTypes } from "@medusajs/types"
|
||||||
import { DALUtils, MedusaError } from "@medusajs/utils"
|
import { DALUtils, MedusaError } from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
LoadStrategy,
|
|
||||||
FilterQuery as MikroFilterQuery,
|
FilterQuery as MikroFilterQuery,
|
||||||
FindOptions as MikroOptions,
|
FindOptions as MikroOptions,
|
||||||
|
LoadStrategy,
|
||||||
} from "@mikro-orm/core"
|
} from "@mikro-orm/core"
|
||||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||||
import { ProductCollection } from "@models"
|
import { ProductCollection } from "@models"
|
||||||
@@ -31,8 +31,7 @@ export class ProductCollectionRepository extends DALUtils.MikroOrmBaseRepository
|
|||||||
findOptions: DAL.FindOptions<ProductCollection> = { where: {} },
|
findOptions: DAL.FindOptions<ProductCollection> = { where: {} },
|
||||||
context: Context = {}
|
context: Context = {}
|
||||||
): Promise<ProductCollection[]> {
|
): Promise<ProductCollection[]> {
|
||||||
const manager = (context.transactionManager ??
|
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||||
this.manager_) as SqlEntityManager
|
|
||||||
|
|
||||||
const findOptions_ = { ...findOptions }
|
const findOptions_ = { ...findOptions }
|
||||||
findOptions_.options ??= {}
|
findOptions_.options ??= {}
|
||||||
@@ -52,8 +51,7 @@ export class ProductCollectionRepository extends DALUtils.MikroOrmBaseRepository
|
|||||||
findOptions: DAL.FindOptions<ProductCollection> = { where: {} },
|
findOptions: DAL.FindOptions<ProductCollection> = { where: {} },
|
||||||
context: Context = {}
|
context: Context = {}
|
||||||
): Promise<[ProductCollection[], number]> {
|
): Promise<[ProductCollection[], number]> {
|
||||||
const manager = (context.transactionManager ??
|
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||||
this.manager_) as SqlEntityManager
|
|
||||||
|
|
||||||
const findOptions_ = { ...findOptions }
|
const findOptions_ = { ...findOptions }
|
||||||
findOptions_.options ??= {}
|
findOptions_.options ??= {}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import { Context, DAL, ProductTypes } from "@medusajs/types"
|
import { Context, DAL, ProductTypes } from "@medusajs/types"
|
||||||
import { DALUtils, MedusaError } from "@medusajs/utils"
|
import { DALUtils, MedusaError } from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
LoadStrategy,
|
|
||||||
FilterQuery as MikroFilterQuery,
|
FilterQuery as MikroFilterQuery,
|
||||||
FindOptions as MikroOptions,
|
FindOptions as MikroOptions,
|
||||||
|
LoadStrategy,
|
||||||
} from "@mikro-orm/core"
|
} from "@mikro-orm/core"
|
||||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||||
import { Product, ProductOption } from "@models"
|
import { Product, ProductOption } from "@models"
|
||||||
@@ -149,4 +149,62 @@ export class ProductOptionRepository extends DALUtils.MikroOrmAbstractBaseReposi
|
|||||||
|
|
||||||
return productOptions
|
return productOptions
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async upsert(
|
||||||
|
data:
|
||||||
|
| ProductTypes.CreateProductOptionDTO[]
|
||||||
|
| ProductTypes.UpdateProductOptionDTO[],
|
||||||
|
context: Context = {}
|
||||||
|
): Promise<ProductOption[]> {
|
||||||
|
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||||
|
|
||||||
|
const optionIds = data.map((optionData) => optionData.id).filter((o) => o)
|
||||||
|
|
||||||
|
let existingOptions
|
||||||
|
let existingOptionsMap = new Map()
|
||||||
|
|
||||||
|
if (optionIds.length) {
|
||||||
|
existingOptions = await this.find(
|
||||||
|
{
|
||||||
|
where: {
|
||||||
|
id: {
|
||||||
|
$in: optionIds,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
context
|
||||||
|
)
|
||||||
|
|
||||||
|
existingOptionsMap = new Map(
|
||||||
|
existingOptions.map((option) => [option.id, option])
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const upsertedOptions: ProductOption[] = []
|
||||||
|
const optionsToCreate: ProductOption[] = []
|
||||||
|
const optionsToUpdate: ProductOption[] = []
|
||||||
|
|
||||||
|
data.forEach((option) => {
|
||||||
|
const existingOption = existingOptionsMap.get(option.id)
|
||||||
|
if (existingOption) {
|
||||||
|
const updatedOption = manager.assign(existingOption, option)
|
||||||
|
optionsToUpdate.push(updatedOption)
|
||||||
|
} else {
|
||||||
|
const newOption = manager.create(ProductOption, option)
|
||||||
|
optionsToCreate.push(newOption)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if (optionsToCreate.length) {
|
||||||
|
manager.persist(optionsToCreate)
|
||||||
|
upsertedOptions.push(...optionsToCreate)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (optionsToUpdate.length) {
|
||||||
|
manager.persist(optionsToUpdate)
|
||||||
|
upsertedOptions.push(...optionsToUpdate)
|
||||||
|
}
|
||||||
|
|
||||||
|
return upsertedOptions
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import {
|
|||||||
FilterQuery as MikroFilterQuery,
|
FilterQuery as MikroFilterQuery,
|
||||||
FindOptions as MikroOptions,
|
FindOptions as MikroOptions,
|
||||||
LoadStrategy,
|
LoadStrategy,
|
||||||
RequiredEntityData,
|
|
||||||
} from "@mikro-orm/core"
|
} from "@mikro-orm/core"
|
||||||
import { ProductTag } from "@models"
|
import { ProductTag } from "@models"
|
||||||
import {
|
import {
|
||||||
@@ -141,26 +140,28 @@ export class ProductTagRepository extends DALUtils.MikroOrmBaseRepository {
|
|||||||
)
|
)
|
||||||
|
|
||||||
const upsertedTags: ProductTag[] = []
|
const upsertedTags: ProductTag[] = []
|
||||||
const tagsToCreate: RequiredEntityData<ProductTag>[] = []
|
const tagsToCreate: ProductTag[] = []
|
||||||
|
const tagsToUpdate: ProductTag[] = []
|
||||||
|
|
||||||
tags.forEach((tag) => {
|
tags.forEach((tag) => {
|
||||||
const aTag = existingTagsMap.get(tag.value)
|
const aTag = existingTagsMap.get(tag.value)
|
||||||
if (aTag) {
|
if (aTag) {
|
||||||
upsertedTags.push(aTag)
|
const updatedTag = manager.assign(aTag, tag)
|
||||||
|
tagsToUpdate.push(updatedTag)
|
||||||
} else {
|
} else {
|
||||||
const newTag = (manager as SqlEntityManager).create(ProductTag, tag)
|
const newTag = manager.create(ProductTag, tag)
|
||||||
tagsToCreate.push(newTag)
|
tagsToCreate.push(newTag)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
if (tagsToCreate.length) {
|
if (tagsToCreate.length) {
|
||||||
const newTags: ProductTag[] = []
|
manager.persist(tagsToCreate)
|
||||||
tagsToCreate.forEach((tag) => {
|
upsertedTags.push(...tagsToCreate)
|
||||||
newTags.push((manager as SqlEntityManager).create(ProductTag, tag))
|
}
|
||||||
})
|
|
||||||
|
|
||||||
manager.persist(newTags)
|
if (tagsToUpdate.length) {
|
||||||
upsertedTags.push(...newTags)
|
manager.persist(tagsToUpdate)
|
||||||
|
upsertedTags.push(...tagsToUpdate)
|
||||||
}
|
}
|
||||||
|
|
||||||
return upsertedTags
|
return upsertedTags
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import {
|
|||||||
FilterQuery as MikroFilterQuery,
|
FilterQuery as MikroFilterQuery,
|
||||||
FindOptions as MikroOptions,
|
FindOptions as MikroOptions,
|
||||||
LoadStrategy,
|
LoadStrategy,
|
||||||
RequiredEntityData,
|
|
||||||
} from "@mikro-orm/core"
|
} from "@mikro-orm/core"
|
||||||
import { ProductType } from "@models"
|
import { ProductType } from "@models"
|
||||||
import {
|
import {
|
||||||
@@ -87,26 +86,28 @@ export class ProductTypeRepository extends DALUtils.MikroOrmBaseRepository {
|
|||||||
)
|
)
|
||||||
|
|
||||||
const upsertedTypes: ProductType[] = []
|
const upsertedTypes: ProductType[] = []
|
||||||
const typesToCreate: RequiredEntityData<ProductType>[] = []
|
const typesToCreate: ProductType[] = []
|
||||||
|
const typesToUpdate: ProductType[] = []
|
||||||
|
|
||||||
types.forEach((type) => {
|
types.forEach((type) => {
|
||||||
const aType = existingTypesMap.get(type.value)
|
const aType = existingTypesMap.get(type.value)
|
||||||
if (aType) {
|
if (aType) {
|
||||||
upsertedTypes.push(aType)
|
const updatedType = manager.assign(aType, type)
|
||||||
|
typesToUpdate.push(updatedType)
|
||||||
} else {
|
} else {
|
||||||
const newType = (manager as SqlEntityManager).create(ProductType, type)
|
const newType = manager.create(ProductType, type)
|
||||||
typesToCreate.push(newType)
|
typesToCreate.push(newType)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
if (typesToCreate.length) {
|
if (typesToCreate.length) {
|
||||||
const newTypes: ProductType[] = []
|
manager.persist(typesToCreate)
|
||||||
typesToCreate.forEach((type) => {
|
upsertedTypes.push(...typesToCreate)
|
||||||
newTypes.push((manager as SqlEntityManager).create(ProductType, type))
|
}
|
||||||
})
|
|
||||||
|
|
||||||
manager.persist(newTypes)
|
if (typesToUpdate.length) {
|
||||||
upsertedTypes.push(...newTypes)
|
manager.persist(typesToUpdate)
|
||||||
|
upsertedTypes.push(...typesToUpdate)
|
||||||
}
|
}
|
||||||
|
|
||||||
return upsertedTypes
|
return upsertedTypes
|
||||||
|
|||||||
@@ -9,8 +9,6 @@ import {
|
|||||||
MedusaError,
|
MedusaError,
|
||||||
ModulesSdkUtils,
|
ModulesSdkUtils,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
|
|
||||||
import { shouldForceTransaction } from "../utils"
|
|
||||||
import { ProductCategoryServiceTypes } from "../types"
|
import { ProductCategoryServiceTypes } from "../types"
|
||||||
|
|
||||||
type InjectedDependencies = {
|
type InjectedDependencies = {
|
||||||
@@ -114,10 +112,7 @@ export default class ProductCategoryService<
|
|||||||
)) as [TEntity[], number]
|
)) as [TEntity[], number]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(
|
@InjectTransactionManager("productCategoryRepository_")
|
||||||
shouldForceTransaction,
|
|
||||||
"productCategoryRepository_"
|
|
||||||
)
|
|
||||||
async create(
|
async create(
|
||||||
data: ProductCategoryServiceTypes.CreateProductCategoryDTO,
|
data: ProductCategoryServiceTypes.CreateProductCategoryDTO,
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -127,10 +122,7 @@ export default class ProductCategoryService<
|
|||||||
).create(data, sharedContext)) as TEntity
|
).create(data, sharedContext)) as TEntity
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(
|
@InjectTransactionManager("productCategoryRepository_")
|
||||||
shouldForceTransaction,
|
|
||||||
"productCategoryRepository_"
|
|
||||||
)
|
|
||||||
async update(
|
async update(
|
||||||
id: string,
|
id: string,
|
||||||
data: ProductCategoryServiceTypes.UpdateProductCategoryDTO,
|
data: ProductCategoryServiceTypes.UpdateProductCategoryDTO,
|
||||||
@@ -141,10 +133,7 @@ export default class ProductCategoryService<
|
|||||||
).update(id, data, sharedContext)) as TEntity
|
).update(id, data, sharedContext)) as TEntity
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(
|
@InjectTransactionManager("productCategoryRepository_")
|
||||||
shouldForceTransaction,
|
|
||||||
"productCategoryRepository_"
|
|
||||||
)
|
|
||||||
async delete(
|
async delete(
|
||||||
id: string,
|
id: string,
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
|||||||
@@ -6,8 +6,6 @@ import {
|
|||||||
ModulesSdkUtils,
|
ModulesSdkUtils,
|
||||||
retrieveEntity,
|
retrieveEntity,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
|
|
||||||
import { shouldForceTransaction } from "../utils"
|
|
||||||
import { ProductCollectionRepository } from "../repositories"
|
import { ProductCollectionRepository } from "../repositories"
|
||||||
|
|
||||||
import { ProductCollection } from "@models"
|
import { ProductCollection } from "@models"
|
||||||
@@ -85,10 +83,7 @@ export default class ProductCollectionService<
|
|||||||
return queryOptions
|
return queryOptions
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(
|
@InjectTransactionManager("productCollectionRepository_")
|
||||||
shouldForceTransaction,
|
|
||||||
"productCollectionRepository_"
|
|
||||||
)
|
|
||||||
async create(
|
async create(
|
||||||
data: ProductTypes.CreateProductCollectionDTO[],
|
data: ProductTypes.CreateProductCollectionDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -98,10 +93,7 @@ export default class ProductCollectionService<
|
|||||||
).create(data, sharedContext)) as TEntity[]
|
).create(data, sharedContext)) as TEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(
|
@InjectTransactionManager("productCollectionRepository_")
|
||||||
shouldForceTransaction,
|
|
||||||
"productCollectionRepository_"
|
|
||||||
)
|
|
||||||
async update(
|
async update(
|
||||||
data: ProductTypes.UpdateProductCollectionDTO[],
|
data: ProductTypes.UpdateProductCollectionDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -111,10 +103,7 @@ export default class ProductCollectionService<
|
|||||||
).update(data, sharedContext)) as TEntity[]
|
).update(data, sharedContext)) as TEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(
|
@InjectTransactionManager("productCollectionRepository_")
|
||||||
shouldForceTransaction,
|
|
||||||
"productCollectionRepository_"
|
|
||||||
)
|
|
||||||
async delete(
|
async delete(
|
||||||
ids: string[],
|
ids: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import { Image } from "@models"
|
import { Image } from "@models"
|
||||||
import { Context, DAL } from "@medusajs/types"
|
import { Context, DAL } from "@medusajs/types"
|
||||||
import { InjectTransactionManager, MedusaContext } from "@medusajs/utils"
|
import { InjectTransactionManager, MedusaContext } from "@medusajs/utils"
|
||||||
import { doNotForceTransaction } from "../utils"
|
|
||||||
import { ProductImageRepository } from "@repositories"
|
import { ProductImageRepository } from "@repositories"
|
||||||
|
|
||||||
type InjectedDependencies = {
|
type InjectedDependencies = {
|
||||||
@@ -15,7 +14,7 @@ export default class ProductImageService<TEntity extends Image = Image> {
|
|||||||
this.productImageRepository_ = productImageRepository
|
this.productImageRepository_ = productImageRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(doNotForceTransaction, "productImageRepository_")
|
@InjectTransactionManager("productImageRepository_")
|
||||||
async upsert(
|
async upsert(
|
||||||
urls: string[],
|
urls: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
|||||||
@@ -62,8 +62,6 @@ import {
|
|||||||
MedusaContext,
|
MedusaContext,
|
||||||
MedusaError,
|
MedusaError,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
|
|
||||||
import { shouldForceTransaction } from "../utils"
|
|
||||||
import {
|
import {
|
||||||
entityNameToLinkableKeysMap,
|
entityNameToLinkableKeysMap,
|
||||||
joinerConfig,
|
joinerConfig,
|
||||||
@@ -277,7 +275,7 @@ export default class ProductModuleService<
|
|||||||
return [JSON.parse(JSON.stringify(tags)), count]
|
return [JSON.parse(JSON.stringify(tags)), count]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async createTags(
|
async createTags(
|
||||||
data: ProductTypes.CreateProductTagDTO[],
|
data: ProductTypes.CreateProductTagDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -290,7 +288,7 @@ export default class ProductModuleService<
|
|||||||
return JSON.parse(JSON.stringify(productTags))
|
return JSON.parse(JSON.stringify(productTags))
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async updateTags(
|
async updateTags(
|
||||||
data: ProductTypes.UpdateProductTagDTO[],
|
data: ProductTypes.UpdateProductTagDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -303,7 +301,7 @@ export default class ProductModuleService<
|
|||||||
return JSON.parse(JSON.stringify(productTags))
|
return JSON.parse(JSON.stringify(productTags))
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async deleteTags(
|
async deleteTags(
|
||||||
productTagIds: string[],
|
productTagIds: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -356,7 +354,7 @@ export default class ProductModuleService<
|
|||||||
return [JSON.parse(JSON.stringify(types)), count]
|
return [JSON.parse(JSON.stringify(types)), count]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async createTypes(
|
async createTypes(
|
||||||
data: ProductTypes.CreateProductTypeDTO[],
|
data: ProductTypes.CreateProductTypeDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -369,7 +367,7 @@ export default class ProductModuleService<
|
|||||||
return JSON.parse(JSON.stringify(productTypes))
|
return JSON.parse(JSON.stringify(productTypes))
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async updateTypes(
|
async updateTypes(
|
||||||
data: ProductTypes.UpdateProductTypeDTO[],
|
data: ProductTypes.UpdateProductTypeDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -382,7 +380,7 @@ export default class ProductModuleService<
|
|||||||
return JSON.parse(JSON.stringify(productTypes))
|
return JSON.parse(JSON.stringify(productTypes))
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async deleteTypes(
|
async deleteTypes(
|
||||||
productTypeIds: string[],
|
productTypeIds: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -436,7 +434,7 @@ export default class ProductModuleService<
|
|||||||
return [JSON.parse(JSON.stringify(productOptions)), count]
|
return [JSON.parse(JSON.stringify(productOptions)), count]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async createOptions(
|
async createOptions(
|
||||||
data: ProductTypes.CreateProductOptionDTO[],
|
data: ProductTypes.CreateProductOptionDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -453,7 +451,7 @@ export default class ProductModuleService<
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async updateOptions(
|
async updateOptions(
|
||||||
data: ProductTypes.UpdateProductOptionDTO[],
|
data: ProductTypes.UpdateProductOptionDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -470,7 +468,7 @@ export default class ProductModuleService<
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async deleteOptions(
|
async deleteOptions(
|
||||||
productOptionIds: string[],
|
productOptionIds: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -523,7 +521,7 @@ export default class ProductModuleService<
|
|||||||
return JSON.parse(JSON.stringify(collections))
|
return JSON.parse(JSON.stringify(collections))
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async createCollections(
|
async createCollections(
|
||||||
data: ProductTypes.CreateProductCollectionDTO[],
|
data: ProductTypes.CreateProductCollectionDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -543,7 +541,7 @@ export default class ProductModuleService<
|
|||||||
return JSON.parse(JSON.stringify(productCollections))
|
return JSON.parse(JSON.stringify(productCollections))
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async updateCollections(
|
async updateCollections(
|
||||||
data: ProductTypes.UpdateProductCollectionDTO[],
|
data: ProductTypes.UpdateProductCollectionDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -563,7 +561,7 @@ export default class ProductModuleService<
|
|||||||
return JSON.parse(JSON.stringify(productCollections))
|
return JSON.parse(JSON.stringify(productCollections))
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async deleteCollections(
|
async deleteCollections(
|
||||||
productCollectionIds: string[],
|
productCollectionIds: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -611,7 +609,7 @@ export default class ProductModuleService<
|
|||||||
return JSON.parse(JSON.stringify(categories))
|
return JSON.parse(JSON.stringify(categories))
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async createCategory(
|
async createCategory(
|
||||||
data: CreateProductCategoryDTO,
|
data: CreateProductCategoryDTO,
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -629,7 +627,7 @@ export default class ProductModuleService<
|
|||||||
return JSON.parse(JSON.stringify(productCategory))
|
return JSON.parse(JSON.stringify(productCategory))
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async updateCategory(
|
async updateCategory(
|
||||||
categoryId: string,
|
categoryId: string,
|
||||||
data: UpdateProductCategoryDTO,
|
data: UpdateProductCategoryDTO,
|
||||||
@@ -649,7 +647,7 @@ export default class ProductModuleService<
|
|||||||
return JSON.parse(JSON.stringify(productCategory))
|
return JSON.parse(JSON.stringify(productCategory))
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async deleteCategory(
|
async deleteCategory(
|
||||||
categoryId: string,
|
categoryId: string,
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -677,6 +675,7 @@ export default class ProductModuleService<
|
|||||||
return JSON.parse(JSON.stringify(categories))
|
return JSON.parse(JSON.stringify(categories))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@InjectManager("baseRepository_")
|
||||||
async create(
|
async create(
|
||||||
data: ProductTypes.CreateProductDTO[],
|
data: ProductTypes.CreateProductDTO[],
|
||||||
sharedContext?: Context
|
sharedContext?: Context
|
||||||
@@ -698,6 +697,7 @@ export default class ProductModuleService<
|
|||||||
return createdProducts
|
return createdProducts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@InjectManager("baseRepository_")
|
||||||
async update(
|
async update(
|
||||||
data: ProductTypes.UpdateProductDTO[],
|
data: ProductTypes.UpdateProductDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -720,7 +720,7 @@ export default class ProductModuleService<
|
|||||||
return updatedProducts
|
return updatedProducts
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
protected async create_(
|
protected async create_(
|
||||||
data: ProductTypes.CreateProductDTO[],
|
data: ProductTypes.CreateProductDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -829,7 +829,7 @@ export default class ProductModuleService<
|
|||||||
return products
|
return products
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
protected async update_(
|
protected async update_(
|
||||||
data: ProductTypes.UpdateProductDTO[],
|
data: ProductTypes.UpdateProductDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -859,14 +859,11 @@ export default class ProductModuleService<
|
|||||||
)[]
|
)[]
|
||||||
>()
|
>()
|
||||||
|
|
||||||
const productOptionsMap = new Map<
|
const productOptionsMap = new Map<string, TProductOption[]>()
|
||||||
string,
|
|
||||||
ProductTypes.CreateProductOptionDTO[]
|
|
||||||
>()
|
|
||||||
|
|
||||||
const productsData = await Promise.all(
|
const productsData = await Promise.all(
|
||||||
data.map(async (product) => {
|
data.map(async (product) => {
|
||||||
const { variants, options, ...productData } = product
|
const { variants, ...productData } = product
|
||||||
|
|
||||||
if (!isDefined(productData.id)) {
|
if (!isDefined(productData.id)) {
|
||||||
throw new MedusaError(
|
throw new MedusaError(
|
||||||
@@ -876,7 +873,6 @@ export default class ProductModuleService<
|
|||||||
}
|
}
|
||||||
|
|
||||||
productVariantsMap.set(productData.id, variants ?? [])
|
productVariantsMap.set(productData.id, variants ?? [])
|
||||||
productOptionsMap.set(productData.id, options ?? [])
|
|
||||||
|
|
||||||
if (productData.is_giftcard) {
|
if (productData.is_giftcard) {
|
||||||
productData.discountable = false
|
productData.discountable = false
|
||||||
@@ -894,6 +890,15 @@ export default class ProductModuleService<
|
|||||||
productData,
|
productData,
|
||||||
sharedContext
|
sharedContext
|
||||||
)
|
)
|
||||||
|
await this.upsertAndAssignOptionsToProductData(
|
||||||
|
productData,
|
||||||
|
sharedContext
|
||||||
|
)
|
||||||
|
|
||||||
|
productOptionsMap.set(
|
||||||
|
productData.id,
|
||||||
|
(productData.options ?? []) as TProductOption[]
|
||||||
|
)
|
||||||
|
|
||||||
return productData as UpdateProductDTO
|
return productData as UpdateProductDTO
|
||||||
})
|
})
|
||||||
@@ -908,20 +913,6 @@ export default class ProductModuleService<
|
|||||||
products.map((product) => [product.id, product])
|
products.map((product) => [product.id, product])
|
||||||
)
|
)
|
||||||
|
|
||||||
const productOptionsData = [...productOptionsMap]
|
|
||||||
.map(([id, options]) =>
|
|
||||||
options.map((option) => ({
|
|
||||||
...option,
|
|
||||||
product: productByIdMap.get(id)!,
|
|
||||||
}))
|
|
||||||
)
|
|
||||||
.flat()
|
|
||||||
|
|
||||||
const productOptions = await this.productOptionService_.create(
|
|
||||||
productOptionsData,
|
|
||||||
sharedContext
|
|
||||||
)
|
|
||||||
|
|
||||||
const productVariantIdsToDelete: string[] = []
|
const productVariantIdsToDelete: string[] = []
|
||||||
const productVariantsToCreateMap = new Map<
|
const productVariantsToCreateMap = new Map<
|
||||||
string,
|
string,
|
||||||
@@ -937,6 +928,7 @@ export default class ProductModuleService<
|
|||||||
const variantsToCreate: ProductTypes.CreateProductVariantDTO[] = []
|
const variantsToCreate: ProductTypes.CreateProductVariantDTO[] = []
|
||||||
const variantsToUpdate: ProductTypes.UpdateProductVariantDTO[] = []
|
const variantsToUpdate: ProductTypes.UpdateProductVariantDTO[] = []
|
||||||
const existingVariants = existingProductVariantsMap.get(productId)
|
const existingVariants = existingProductVariantsMap.get(productId)
|
||||||
|
const productOptions = productOptionsMap.get(productId)!
|
||||||
|
|
||||||
variants.forEach((variant) => {
|
variants.forEach((variant) => {
|
||||||
const isVariantIdDefined = "id" in variant && isDefined(variant.id)
|
const isVariantIdDefined = "id" in variant && isDefined(variant.id)
|
||||||
@@ -955,7 +947,7 @@ export default class ProductModuleService<
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
if (variantOptions) {
|
if (variantOptions?.length) {
|
||||||
variant.options = variantOptions
|
variant.options = variantOptions
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -1010,6 +1002,18 @@ export default class ProductModuleService<
|
|||||||
return products
|
return products
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected async upsertAndAssignOptionsToProductData(
|
||||||
|
productData: ProductTypes.CreateProductDTO | ProductTypes.UpdateProductDTO,
|
||||||
|
sharedContext: Context = {}
|
||||||
|
) {
|
||||||
|
if (productData.options?.length) {
|
||||||
|
productData.options = await this.productOptionService_.upsert(
|
||||||
|
productData.options,
|
||||||
|
sharedContext
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected async upsertAndAssignImagesToProductData(
|
protected async upsertAndAssignImagesToProductData(
|
||||||
productData: ProductTypes.CreateProductDTO | ProductTypes.UpdateProductDTO,
|
productData: ProductTypes.CreateProductDTO | ProductTypes.UpdateProductDTO,
|
||||||
sharedContext: Context = {}
|
sharedContext: Context = {}
|
||||||
@@ -1060,7 +1064,7 @@ export default class ProductModuleService<
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async delete(
|
async delete(
|
||||||
productIds: string[],
|
productIds: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -1075,6 +1079,7 @@ export default class ProductModuleService<
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@InjectManager("baseRepository_")
|
||||||
async softDelete<
|
async softDelete<
|
||||||
TReturnableLinkableKeys extends string = Lowercase<
|
TReturnableLinkableKeys extends string = Lowercase<
|
||||||
keyof typeof LinkableKeys
|
keyof typeof LinkableKeys
|
||||||
@@ -1116,7 +1121,7 @@ export default class ProductModuleService<
|
|||||||
return mappedCascadedEntitiesMap ? mappedCascadedEntitiesMap : void 0
|
return mappedCascadedEntitiesMap ? mappedCascadedEntitiesMap : void 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
protected async softDelete_(
|
protected async softDelete_(
|
||||||
productIds: string[],
|
productIds: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -1124,6 +1129,7 @@ export default class ProductModuleService<
|
|||||||
return await this.productService_.softDelete(productIds, sharedContext)
|
return await this.productService_.softDelete(productIds, sharedContext)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@InjectManager("baseRepository_")
|
||||||
async restore<
|
async restore<
|
||||||
TReturnableLinkableKeys extends string = Lowercase<
|
TReturnableLinkableKeys extends string = Lowercase<
|
||||||
keyof typeof LinkableKeys
|
keyof typeof LinkableKeys
|
||||||
@@ -1152,7 +1158,7 @@ export default class ProductModuleService<
|
|||||||
return mappedCascadedEntitiesMap ? mappedCascadedEntitiesMap : void 0
|
return mappedCascadedEntitiesMap ? mappedCascadedEntitiesMap : void 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectManager("baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async restoreVariants<
|
async restoreVariants<
|
||||||
TReturnableLinkableKeys extends string = Lowercase<
|
TReturnableLinkableKeys extends string = Lowercase<
|
||||||
keyof typeof LinkableKeys
|
keyof typeof LinkableKeys
|
||||||
@@ -1179,7 +1185,7 @@ export default class ProductModuleService<
|
|||||||
return mappedCascadedEntitiesMap ? mappedCascadedEntitiesMap : void 0
|
return mappedCascadedEntitiesMap ? mappedCascadedEntitiesMap : void 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async restore_(
|
async restore_(
|
||||||
productIds: string[],
|
productIds: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
|||||||
@@ -2,15 +2,13 @@ import { ProductOption } from "@models"
|
|||||||
import { Context, DAL, FindConfig, ProductTypes } from "@medusajs/types"
|
import { Context, DAL, FindConfig, ProductTypes } from "@medusajs/types"
|
||||||
import { ProductOptionRepository } from "@repositories"
|
import { ProductOptionRepository } from "@repositories"
|
||||||
import {
|
import {
|
||||||
InjectTransactionManager,
|
|
||||||
InjectManager,
|
InjectManager,
|
||||||
|
InjectTransactionManager,
|
||||||
MedusaContext,
|
MedusaContext,
|
||||||
ModulesSdkUtils,
|
ModulesSdkUtils,
|
||||||
retrieveEntity,
|
retrieveEntity,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
|
|
||||||
import { doNotForceTransaction, shouldForceTransaction } from "../utils"
|
|
||||||
|
|
||||||
type InjectedDependencies = {
|
type InjectedDependencies = {
|
||||||
productOptionRepository: DAL.RepositoryService
|
productOptionRepository: DAL.RepositoryService
|
||||||
}
|
}
|
||||||
@@ -80,7 +78,7 @@ export default class ProductOptionService<
|
|||||||
return queryOptions
|
return queryOptions
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "productOptionRepository_")
|
@InjectTransactionManager("productOptionRepository_")
|
||||||
async create(
|
async create(
|
||||||
data: ProductTypes.CreateProductOptionOnlyDTO[],
|
data: ProductTypes.CreateProductOptionOnlyDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -92,7 +90,7 @@ export default class ProductOptionService<
|
|||||||
})) as TEntity[]
|
})) as TEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "productOptionRepository_")
|
@InjectTransactionManager("productOptionRepository_")
|
||||||
async update(
|
async update(
|
||||||
data: ProductTypes.UpdateProductOptionDTO[],
|
data: ProductTypes.UpdateProductOptionDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -102,11 +100,22 @@ export default class ProductOptionService<
|
|||||||
).update(data, sharedContext)) as TEntity[]
|
).update(data, sharedContext)) as TEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(doNotForceTransaction, "productOptionRepository_")
|
@InjectTransactionManager("productOptionRepository_")
|
||||||
async delete(
|
async delete(
|
||||||
ids: string[],
|
ids: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
return await this.productOptionRepository_.delete(ids, sharedContext)
|
return await this.productOptionRepository_.delete(ids, sharedContext)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@InjectTransactionManager("productOptionRepository_")
|
||||||
|
async upsert(
|
||||||
|
data:
|
||||||
|
| ProductTypes.CreateProductOptionDTO[]
|
||||||
|
| ProductTypes.UpdateProductOptionDTO[],
|
||||||
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
): Promise<TEntity[]> {
|
||||||
|
return (await (this.productOptionRepository_ as ProductOptionRepository)
|
||||||
|
.upsert!(data, sharedContext)) as TEntity[]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,22 +2,22 @@ import { ProductTag } from "@models"
|
|||||||
import {
|
import {
|
||||||
Context,
|
Context,
|
||||||
CreateProductTagDTO,
|
CreateProductTagDTO,
|
||||||
UpdateProductTagDTO,
|
|
||||||
UpsertProductTagDTO,
|
|
||||||
DAL,
|
DAL,
|
||||||
FindConfig,
|
FindConfig,
|
||||||
ProductTypes,
|
ProductTypes,
|
||||||
|
UpdateProductTagDTO,
|
||||||
|
UpsertProductTagDTO,
|
||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
import {
|
import {
|
||||||
InjectTransactionManager,
|
|
||||||
InjectManager,
|
InjectManager,
|
||||||
|
InjectTransactionManager,
|
||||||
MedusaContext,
|
MedusaContext,
|
||||||
ModulesSdkUtils,
|
ModulesSdkUtils,
|
||||||
retrieveEntity,
|
retrieveEntity,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import { ProductTagRepository } from "@repositories"
|
import { ProductTagRepository } from "@repositories"
|
||||||
|
|
||||||
import { doNotForceTransaction, shouldForceTransaction } from "../utils"
|
import { shouldForceTransaction } from "../utils"
|
||||||
|
|
||||||
type InjectedDependencies = {
|
type InjectedDependencies = {
|
||||||
productTagRepository: DAL.RepositoryService
|
productTagRepository: DAL.RepositoryService
|
||||||
@@ -84,7 +84,7 @@ export default class ProductTagService<
|
|||||||
return queryOptions
|
return queryOptions
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "productTagRepository_")
|
@InjectTransactionManager("productTagRepository_")
|
||||||
async create(
|
async create(
|
||||||
data: CreateProductTagDTO[],
|
data: CreateProductTagDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -95,7 +95,7 @@ export default class ProductTagService<
|
|||||||
)) as TEntity[]
|
)) as TEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "productTagRepository_")
|
@InjectTransactionManager("productTagRepository_")
|
||||||
async update(
|
async update(
|
||||||
data: UpdateProductTagDTO[],
|
data: UpdateProductTagDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -106,7 +106,7 @@ export default class ProductTagService<
|
|||||||
)) as TEntity[]
|
)) as TEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(doNotForceTransaction, "productTagRepository_")
|
@InjectTransactionManager("productTagRepository_")
|
||||||
async delete(
|
async delete(
|
||||||
ids: string[],
|
ids: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -114,7 +114,7 @@ export default class ProductTagService<
|
|||||||
await this.productTagRepository_.delete(ids, sharedContext)
|
await this.productTagRepository_.delete(ids, sharedContext)
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(doNotForceTransaction, "productTagRepository_")
|
@InjectTransactionManager("productTagRepository_")
|
||||||
async upsert(
|
async upsert(
|
||||||
data: UpsertProductTagDTO[],
|
data: UpsertProductTagDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
|||||||
@@ -2,11 +2,11 @@ import { ProductType } from "@models"
|
|||||||
import {
|
import {
|
||||||
Context,
|
Context,
|
||||||
CreateProductTypeDTO,
|
CreateProductTypeDTO,
|
||||||
UpsertProductTypeDTO,
|
|
||||||
UpdateProductTypeDTO,
|
|
||||||
DAL,
|
DAL,
|
||||||
FindConfig,
|
FindConfig,
|
||||||
ProductTypes,
|
ProductTypes,
|
||||||
|
UpdateProductTypeDTO,
|
||||||
|
UpsertProductTypeDTO,
|
||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
import { ProductTypeRepository } from "@repositories"
|
import { ProductTypeRepository } from "@repositories"
|
||||||
import {
|
import {
|
||||||
@@ -17,7 +17,7 @@ import {
|
|||||||
retrieveEntity,
|
retrieveEntity,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
|
|
||||||
import { doNotForceTransaction, shouldForceTransaction } from "../utils"
|
import { shouldForceTransaction } from "../utils"
|
||||||
|
|
||||||
type InjectedDependencies = {
|
type InjectedDependencies = {
|
||||||
productTypeRepository: DAL.RepositoryService
|
productTypeRepository: DAL.RepositoryService
|
||||||
@@ -87,7 +87,7 @@ export default class ProductTypeService<
|
|||||||
return queryOptions
|
return queryOptions
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(doNotForceTransaction, "productTypeRepository_")
|
@InjectTransactionManager("productTypeRepository_")
|
||||||
async upsert(
|
async upsert(
|
||||||
types: UpsertProductTypeDTO[],
|
types: UpsertProductTypeDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -96,7 +96,7 @@ export default class ProductTypeService<
|
|||||||
.upsert!(types, sharedContext)) as TEntity[]
|
.upsert!(types, sharedContext)) as TEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "productTypeRepository_")
|
@InjectTransactionManager("productTypeRepository_")
|
||||||
async create(
|
async create(
|
||||||
data: CreateProductTypeDTO[],
|
data: CreateProductTypeDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -107,7 +107,7 @@ export default class ProductTypeService<
|
|||||||
)) as TEntity[]
|
)) as TEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(shouldForceTransaction, "productTypeRepository_")
|
@InjectTransactionManager("productTypeRepository_")
|
||||||
async update(
|
async update(
|
||||||
data: UpdateProductTypeDTO[],
|
data: UpdateProductTypeDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -118,7 +118,7 @@ export default class ProductTypeService<
|
|||||||
)) as TEntity[]
|
)) as TEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(doNotForceTransaction, "productTypeRepository_")
|
@InjectTransactionManager("productTypeRepository_")
|
||||||
async delete(
|
async delete(
|
||||||
ids: string[],
|
ids: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import {
|
|||||||
|
|
||||||
import { ProductVariantServiceTypes } from "../types/services"
|
import { ProductVariantServiceTypes } from "../types/services"
|
||||||
import ProductService from "./product"
|
import ProductService from "./product"
|
||||||
import { doNotForceTransaction } from "../utils"
|
|
||||||
|
|
||||||
type InjectedDependencies = {
|
type InjectedDependencies = {
|
||||||
productVariantRepository: DAL.RepositoryService
|
productVariantRepository: DAL.RepositoryService
|
||||||
@@ -86,7 +85,7 @@ export default class ProductVariantService<
|
|||||||
)) as [TEntity[], number]
|
)) as [TEntity[], number]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(doNotForceTransaction, "productVariantRepository_")
|
@InjectTransactionManager("productVariantRepository_")
|
||||||
async create(
|
async create(
|
||||||
productOrId: TProduct | string,
|
productOrId: TProduct | string,
|
||||||
data: ProductTypes.CreateProductVariantOnlyDTO[],
|
data: ProductTypes.CreateProductVariantOnlyDTO[],
|
||||||
@@ -119,7 +118,7 @@ export default class ProductVariantService<
|
|||||||
})) as TEntity[]
|
})) as TEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(doNotForceTransaction, "productVariantRepository_")
|
@InjectTransactionManager("productVariantRepository_")
|
||||||
async update(
|
async update(
|
||||||
productOrId: TProduct | string,
|
productOrId: TProduct | string,
|
||||||
data: ProductVariantServiceTypes.UpdateProductVariantDTO[],
|
data: ProductVariantServiceTypes.UpdateProductVariantDTO[],
|
||||||
@@ -145,7 +144,7 @@ export default class ProductVariantService<
|
|||||||
})) as TEntity[]
|
})) as TEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(doNotForceTransaction, "productVariantRepository_")
|
@InjectTransactionManager("productVariantRepository_")
|
||||||
async delete(
|
async delete(
|
||||||
ids: string[],
|
ids: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -155,7 +154,7 @@ export default class ProductVariantService<
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(doNotForceTransaction, "productVariantRepository_")
|
@InjectTransactionManager("productVariantRepository_")
|
||||||
async softDelete(
|
async softDelete(
|
||||||
ids: string[],
|
ids: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -165,7 +164,7 @@ export default class ProductVariantService<
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(doNotForceTransaction, "productVariantRepository_")
|
@InjectTransactionManager("productVariantRepository_")
|
||||||
async restore(
|
async restore(
|
||||||
ids: string[],
|
ids: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
|||||||
@@ -8,17 +8,16 @@ import {
|
|||||||
import {
|
import {
|
||||||
InjectManager,
|
InjectManager,
|
||||||
InjectTransactionManager,
|
InjectTransactionManager,
|
||||||
|
isDefined,
|
||||||
MedusaContext,
|
MedusaContext,
|
||||||
MedusaError,
|
MedusaError,
|
||||||
ModulesSdkUtils,
|
ModulesSdkUtils,
|
||||||
ProductUtils,
|
ProductUtils,
|
||||||
isDefined,
|
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import { Product } from "@models"
|
import { Product } from "@models"
|
||||||
import { ProductRepository } from "@repositories"
|
import { ProductRepository } from "@repositories"
|
||||||
|
|
||||||
import { ProductServiceTypes } from "../types/services"
|
import { ProductServiceTypes } from "../types/services"
|
||||||
import { doNotForceTransaction } from "../utils"
|
|
||||||
|
|
||||||
type InjectedDependencies = {
|
type InjectedDependencies = {
|
||||||
productRepository: DAL.RepositoryService
|
productRepository: DAL.RepositoryService
|
||||||
@@ -118,7 +117,7 @@ export default class ProductService<TEntity extends Product = Product> {
|
|||||||
)) as [TEntity[], number]
|
)) as [TEntity[], number]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(doNotForceTransaction, "productRepository_")
|
@InjectTransactionManager("productRepository_")
|
||||||
async create(
|
async create(
|
||||||
data: ProductTypes.CreateProductOnlyDTO[],
|
data: ProductTypes.CreateProductOnlyDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -138,7 +137,7 @@ export default class ProductService<TEntity extends Product = Product> {
|
|||||||
)) as TEntity[]
|
)) as TEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(doNotForceTransaction, "productRepository_")
|
@InjectTransactionManager("productRepository_")
|
||||||
async update(
|
async update(
|
||||||
data: ProductServiceTypes.UpdateProductDTO[],
|
data: ProductServiceTypes.UpdateProductDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -154,7 +153,7 @@ export default class ProductService<TEntity extends Product = Product> {
|
|||||||
)) as TEntity[]
|
)) as TEntity[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(doNotForceTransaction, "productRepository_")
|
@InjectTransactionManager("productRepository_")
|
||||||
async delete(
|
async delete(
|
||||||
ids: string[],
|
ids: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -164,7 +163,7 @@ export default class ProductService<TEntity extends Product = Product> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(doNotForceTransaction, "productRepository_")
|
@InjectTransactionManager("productRepository_")
|
||||||
async softDelete(
|
async softDelete(
|
||||||
productIds: string[],
|
productIds: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -174,7 +173,7 @@ export default class ProductService<TEntity extends Product = Product> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager(doNotForceTransaction, "productRepository_")
|
@InjectTransactionManager("productRepository_")
|
||||||
async restore(
|
async restore(
|
||||||
productIds: string[],
|
productIds: string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
import { MODULE_RESOURCE_TYPE } from "@medusajs/types"
|
export function shouldForceTransaction(): boolean {
|
||||||
|
return true
|
||||||
export function shouldForceTransaction(target: any): boolean {
|
|
||||||
return target.moduleDeclaration?.resources === MODULE_RESOURCE_TYPE.ISOLATED
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function doNotForceTransaction(): boolean {
|
export function doNotForceTransaction(): boolean {
|
||||||
|
|||||||
@@ -28,9 +28,12 @@ export async function transactionWrapper<TManager = unknown>(
|
|||||||
Object.assign(options, { isolationLevel })
|
Object.assign(options, { isolationLevel })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const freshManager = this.getFreshManager
|
||||||
|
? this.getFreshManager()
|
||||||
|
: this.manager_
|
||||||
const transactionMethod =
|
const transactionMethod =
|
||||||
this.manager_.transaction ?? this.manager_.transactional
|
freshManager.transaction ?? freshManager.transactional
|
||||||
return await transactionMethod.bind(this.manager_)(task, options)
|
return await transactionMethod.bind(freshManager)(task, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -16,13 +16,12 @@ export function InjectManager(managerProperty?: string): MethodDecorator {
|
|||||||
const argIndex = target.MedusaContextIndex_[propertyKey]
|
const argIndex = target.MedusaContextIndex_[propertyKey]
|
||||||
|
|
||||||
descriptor.value = function (...args: any[]) {
|
descriptor.value = function (...args: any[]) {
|
||||||
const context: SharedContext | Context = args[argIndex] ?? {}
|
const context: SharedContext | Context = { ...(args[argIndex] ?? {}) }
|
||||||
const resourceWithManager = (!managerProperty
|
const resourceWithManager = !managerProperty
|
||||||
? this
|
? this
|
||||||
: this[managerProperty])
|
: this[managerProperty]
|
||||||
|
|
||||||
context.manager =
|
context.manager = context.manager ?? resourceWithManager.getFreshManager()
|
||||||
context.manager ?? resourceWithManager.getFreshManager()
|
|
||||||
args[argIndex] = context
|
args[argIndex] = context
|
||||||
|
|
||||||
return originalMethod.apply(this, args)
|
return originalMethod.apply(this, args)
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
import { Context, SharedContext } from "@medusajs/types"
|
import { Context, SharedContext } from "@medusajs/types"
|
||||||
|
import { isString } from "../../common"
|
||||||
|
|
||||||
export function InjectTransactionManager(
|
export function InjectTransactionManager(
|
||||||
shouldForceTransaction: (target: any) => boolean = () => false,
|
shouldForceTransactionOrManagerProperty:
|
||||||
|
| string
|
||||||
|
| ((target: any) => boolean) = () => false,
|
||||||
managerProperty?: string
|
managerProperty?: string
|
||||||
): MethodDecorator {
|
): MethodDecorator {
|
||||||
return function (
|
return function (
|
||||||
@@ -16,6 +19,14 @@ export function InjectTransactionManager(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const originalMethod = descriptor.value
|
const originalMethod = descriptor.value
|
||||||
|
const shouldForceTransaction = !isString(
|
||||||
|
shouldForceTransactionOrManagerProperty
|
||||||
|
)
|
||||||
|
? shouldForceTransactionOrManagerProperty
|
||||||
|
: () => false
|
||||||
|
managerProperty = isString(shouldForceTransactionOrManagerProperty)
|
||||||
|
? shouldForceTransactionOrManagerProperty
|
||||||
|
: managerProperty
|
||||||
|
|
||||||
const argIndex = target.MedusaContextIndex_[propertyKey]
|
const argIndex = target.MedusaContextIndex_[propertyKey]
|
||||||
descriptor.value = async function (...args: any[]) {
|
descriptor.value = async function (...args: any[]) {
|
||||||
@@ -31,7 +42,7 @@ export function InjectTransactionManager(
|
|||||||
: this[managerProperty]
|
: this[managerProperty]
|
||||||
).transaction(
|
).transaction(
|
||||||
async (transactionManager) => {
|
async (transactionManager) => {
|
||||||
args[argIndex] = args[argIndex] ?? {}
|
args[argIndex] = { ...(args[argIndex] ?? {}) }
|
||||||
args[argIndex].transactionManager = transactionManager
|
args[argIndex].transactionManager = transactionManager
|
||||||
|
|
||||||
return await originalMethod.apply(this, args)
|
return await originalMethod.apply(this, args)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { ProductTypes, WorkflowTypes } from "@medusajs/types"
|
|||||||
|
|
||||||
import { MedusaError } from "@medusajs/utils"
|
import { MedusaError } from "@medusajs/utils"
|
||||||
import { WorkflowArguments } from "../../helper"
|
import { WorkflowArguments } from "../../helper"
|
||||||
|
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||||
|
|
||||||
type ProductHandle = string
|
type ProductHandle = string
|
||||||
type VariantIndexAndPrices = {
|
type VariantIndexAndPrices = {
|
||||||
@@ -37,6 +38,8 @@ export async function updateProductsVariantsPrices({
|
|||||||
products.map((p) => [p.handle!, p])
|
products.map((p) => [p.handle!, p])
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const regionIds = new Set()
|
||||||
|
|
||||||
for (const mapData of productsHandleVariantsIndexPricesMap.entries()) {
|
for (const mapData of productsHandleVariantsIndexPricesMap.entries()) {
|
||||||
const [handle, variantData] = mapData
|
const [handle, variantData] = mapData
|
||||||
|
|
||||||
@@ -55,9 +58,10 @@ export async function updateProductsVariantsPrices({
|
|||||||
prices: item.prices,
|
prices: item.prices,
|
||||||
})
|
})
|
||||||
|
|
||||||
variantPricesMap.set(variant.id, [])
|
const prices: any[] = []
|
||||||
|
variantPricesMap.set(variant.id, prices)
|
||||||
|
|
||||||
item.prices.forEach(async (price) => {
|
item.prices.forEach((price) => {
|
||||||
const obj = {
|
const obj = {
|
||||||
amount: price.amount,
|
amount: price.amount,
|
||||||
currency_code: price.currency_code,
|
currency_code: price.currency_code,
|
||||||
@@ -65,38 +69,59 @@ export async function updateProductsVariantsPrices({
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (price.region_id) {
|
if (price.region_id) {
|
||||||
const region = await regionService.retrieve(price.region_id)
|
regionIds.add(price.region_id)
|
||||||
obj.currency_code = region.currency_code
|
;(obj as any).region_id = price.region_id
|
||||||
obj.rules = {
|
|
||||||
region_id: price.region_id,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const variantPrices = variantPricesMap.get(variant.id)
|
prices.push(obj)
|
||||||
variantPrices?.push(obj)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if (featureFlagRouter.isFeatureEnabled("isolate_pricing_domain")) {
|
if (regionIds.size) {
|
||||||
const remoteLink = container.resolve("remoteLink")
|
const regions = await regionService.list({
|
||||||
const pricingModuleService = container.resolve("pricingModuleService")
|
id: [...regionIds],
|
||||||
|
})
|
||||||
|
const regionMap = new Map<string, any>(regions.map((r) => [r.id, r]))
|
||||||
|
|
||||||
for (let { variantId } of variantIdsPricesData) {
|
for (const [, prices] of variantPricesMap.entries()) {
|
||||||
const priceSet = await pricingModuleService.create({
|
prices.forEach((price) => {
|
||||||
rules: [{ rule_attribute: "region_id" }],
|
if (price.region_id) {
|
||||||
prices: variantPricesMap.get(variantId),
|
const region = regionMap.get(price.region_id)
|
||||||
})
|
price.currency_code = region?.currency_code
|
||||||
|
price.rules = {
|
||||||
|
region_id: price.region_id,
|
||||||
|
}
|
||||||
|
|
||||||
await remoteLink.create({
|
delete price.region_id
|
||||||
productService: {
|
}
|
||||||
variant_id: variantId,
|
|
||||||
},
|
|
||||||
pricingService: {
|
|
||||||
price_set_id: priceSet.id,
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (featureFlagRouter.isFeatureEnabled("isolate_pricing_domain")) {
|
||||||
|
const remoteLink = container.resolve("remoteLink")
|
||||||
|
const pricingModuleService = container.resolve(
|
||||||
|
ModuleRegistrationName.PRICING
|
||||||
|
)
|
||||||
|
|
||||||
|
const priceSetsToCreate = variantIdsPricesData.map(({ variantId }) => ({
|
||||||
|
rules: [{ rule_attribute: "region_id" }],
|
||||||
|
prices: variantPricesMap.get(variantId),
|
||||||
|
}))
|
||||||
|
|
||||||
|
const priceSets = await pricingModuleService.create(priceSetsToCreate)
|
||||||
|
|
||||||
|
const links = priceSets.map((priceSet, index) => ({
|
||||||
|
productService: {
|
||||||
|
variant_id: variantIdsPricesData[index].variantId,
|
||||||
|
},
|
||||||
|
pricingService: {
|
||||||
|
price_set_id: priceSet.id,
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
|
||||||
|
await remoteLink.create(links)
|
||||||
} else {
|
} else {
|
||||||
await productVariantServiceTx.updateVariantPrices(variantIdsPricesData)
|
await productVariantServiceTx.updateVariantPrices(variantIdsPricesData)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user