chore(medusa): Clean up atomicPhase usage (#1850)
This commit is contained in:
1 parent
f5091c6370
commit
4dd7fdf61c
21 files changed
+107
-237
No files matched your search
@@ -86,9 +86,7 @@ describe("CustomShippingOptionService", () => {
|
|||||||
|
|
||||||
describe("create", () => {
|
describe("create", () => {
|
||||||
const customShippingOptionRepository = MockRepository({
|
const customShippingOptionRepository = MockRepository({
|
||||||
create: jest
|
create: jest.fn().mockImplementation((f) => ({ id: "test-cso", ...f })),
|
||||||
.fn()
|
|
||||||
.mockImplementation((f) => Promise.resolve({ id: "test-cso", ...f })),
|
|
||||||
save: jest.fn().mockImplementation((f) => Promise.resolve(f)),
|
save: jest.fn().mockImplementation((f) => Promise.resolve(f)),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ describe("DiscountService", () => {
|
|||||||
id: IdMap.getId("france"),
|
id: IdMap.getId("france"),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
withTransaction: function() {
|
withTransaction: function () {
|
||||||
return this
|
return this
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -762,7 +762,7 @@ describe("DiscountService", () => {
|
|||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
discountService = new DiscountService({
|
discountService = new DiscountService({
|
||||||
manager: MockManager
|
manager: MockManager,
|
||||||
})
|
})
|
||||||
const hasReachedLimitMock = jest.fn().mockImplementation(() => false)
|
const hasReachedLimitMock = jest.fn().mockImplementation(() => false)
|
||||||
const isDisabledMock = jest.fn().mockImplementation(() => false)
|
const isDisabledMock = jest.fn().mockImplementation(() => false)
|
||||||
@@ -1067,7 +1067,7 @@ describe("DiscountService", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const discountService = new DiscountService({
|
const discountService = new DiscountService({
|
||||||
manager: MockManager
|
manager: MockManager,
|
||||||
})
|
})
|
||||||
discountService.retrieve = retrieveMock
|
discountService.retrieve = retrieveMock
|
||||||
|
|
||||||
@@ -1161,6 +1161,9 @@ describe("DiscountService", () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const customerService = {
|
const customerService = {
|
||||||
|
withTransaction: function () {
|
||||||
|
return this
|
||||||
|
},
|
||||||
retrieve: jest.fn().mockImplementation((id) => {
|
retrieve: jest.fn().mockImplementation((id) => {
|
||||||
if (id === "customer-no-groups") {
|
if (id === "customer-no-groups") {
|
||||||
return Promise.resolve({ id: "customer-no-groups" })
|
return Promise.resolve({ id: "customer-no-groups" })
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import {
|
|||||||
FilterableBatchJobProps,
|
FilterableBatchJobProps,
|
||||||
} from "../types/batch-job"
|
} from "../types/batch-job"
|
||||||
import { FindConfig } from "../types/common"
|
import { FindConfig } from "../types/common"
|
||||||
import { AbstractBatchJobStrategy, TransactionBaseService } from "../interfaces"
|
import { TransactionBaseService } from "../interfaces"
|
||||||
import { buildQuery } from "../utils"
|
import { buildQuery } from "../utils"
|
||||||
import { MedusaError } from "medusa-core-utils"
|
import { MedusaError } from "medusa-core-utils"
|
||||||
import { EventBusService, StrategyResolverService } from "./index"
|
import { EventBusService, StrategyResolverService } from "./index"
|
||||||
@@ -113,11 +113,8 @@ class BatchJobService extends TransactionBaseService<BatchJobService> {
|
|||||||
batchJobId: string,
|
batchJobId: string,
|
||||||
config: FindConfig<BatchJob> = {}
|
config: FindConfig<BatchJob> = {}
|
||||||
): Promise<BatchJob | never> {
|
): Promise<BatchJob | never> {
|
||||||
return await this.atomicPhase_(
|
const manager = this.manager_
|
||||||
async (transactionManager: EntityManager) => {
|
const batchJobRepo = manager.getCustomRepository(this.batchJobRepository_)
|
||||||
const batchJobRepo = transactionManager.getCustomRepository(
|
|
||||||
this.batchJobRepository_
|
|
||||||
)
|
|
||||||
|
|
||||||
const query = buildQuery({ id: batchJobId }, config)
|
const query = buildQuery({ id: batchJobId }, config)
|
||||||
const batchJob = await batchJobRepo.findOne(query)
|
const batchJob = await batchJobRepo.findOne(query)
|
||||||
@@ -131,24 +128,17 @@ class BatchJobService extends TransactionBaseService<BatchJobService> {
|
|||||||
|
|
||||||
return batchJob
|
return batchJob
|
||||||
}
|
}
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
async listAndCount(
|
async listAndCount(
|
||||||
selector: FilterableBatchJobProps = {},
|
selector: FilterableBatchJobProps = {},
|
||||||
config: FindConfig<BatchJob> = { skip: 0, take: 20 }
|
config: FindConfig<BatchJob> = { skip: 0, take: 20 }
|
||||||
): Promise<[BatchJob[], number]> {
|
): Promise<[BatchJob[], number]> {
|
||||||
return await this.atomicPhase_(
|
const manager = this.manager_
|
||||||
async (manager: EntityManager): Promise<[BatchJob[], number]> => {
|
const batchJobRepo = manager.getCustomRepository(this.batchJobRepository_)
|
||||||
const batchJobRepo = manager.getCustomRepository(
|
|
||||||
this.batchJobRepository_
|
|
||||||
)
|
|
||||||
|
|
||||||
const query = buildQuery(selector, config)
|
const query = buildQuery(selector, config)
|
||||||
return await batchJobRepo.findAndCount(query)
|
return await batchJobRepo.findAndCount(query)
|
||||||
}
|
}
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
async create(data: BatchJobCreateProps): Promise<BatchJob> {
|
async create(data: BatchJobCreateProps): Promise<BatchJob> {
|
||||||
return await this.atomicPhase_(async (manager) => {
|
return await this.atomicPhase_(async (manager) => {
|
||||||
|
|||||||
@@ -260,17 +260,12 @@ class CartService extends TransactionBaseService<CartService> {
|
|||||||
selector: FilterableCartProps,
|
selector: FilterableCartProps,
|
||||||
config: FindConfig<Cart> = {}
|
config: FindConfig<Cart> = {}
|
||||||
): Promise<Cart[]> {
|
): Promise<Cart[]> {
|
||||||
return await this.atomicPhase_(
|
const manager = this.manager_
|
||||||
async (transactionManager: EntityManager) => {
|
const cartRepo = manager.getCustomRepository(this.cartRepository_)
|
||||||
const cartRepo = transactionManager.getCustomRepository(
|
|
||||||
this.cartRepository_
|
|
||||||
)
|
|
||||||
|
|
||||||
const query = buildQuery(selector, config)
|
const query = buildQuery(selector, config)
|
||||||
return await cartRepo.find(query)
|
return await cartRepo.find(query)
|
||||||
}
|
}
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a cart by id.
|
* Gets a cart by id.
|
||||||
@@ -284,11 +279,8 @@ class CartService extends TransactionBaseService<CartService> {
|
|||||||
options: FindConfig<Cart> = {},
|
options: FindConfig<Cart> = {},
|
||||||
totalsConfig: TotalsConfig = {}
|
totalsConfig: TotalsConfig = {}
|
||||||
): Promise<Cart> {
|
): Promise<Cart> {
|
||||||
return await this.atomicPhase_(
|
const manager = this.manager_
|
||||||
async (transactionManager: EntityManager) => {
|
const cartRepo = manager.getCustomRepository(this.cartRepository_)
|
||||||
const cartRepo = transactionManager.getCustomRepository(
|
|
||||||
this.cartRepository_
|
|
||||||
)
|
|
||||||
const validatedId = validateId(cartId)
|
const validatedId = validateId(cartId)
|
||||||
|
|
||||||
const { select, relations, totalsToSelect } =
|
const { select, relations, totalsToSelect } =
|
||||||
@@ -323,8 +315,6 @@ class CartService extends TransactionBaseService<CartService> {
|
|||||||
|
|
||||||
return await this.decorateTotals_(raw, totalsToSelect, totalsConfig)
|
return await this.decorateTotals_(raw, totalsToSelect, totalsConfig)
|
||||||
}
|
}
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a cart.
|
* Creates a cart.
|
||||||
|
|||||||
@@ -814,16 +814,11 @@ export default class ClaimService extends TransactionBaseService<
|
|||||||
order: { created_at: "DESC" },
|
order: { created_at: "DESC" },
|
||||||
}
|
}
|
||||||
): Promise<ClaimOrder[]> {
|
): Promise<ClaimOrder[]> {
|
||||||
return await this.atomicPhase_(
|
const manager = this.manager_
|
||||||
async (transactionManager: EntityManager) => {
|
const claimRepo = manager.getCustomRepository(this.claimRepository_)
|
||||||
const claimRepo = transactionManager.getCustomRepository(
|
|
||||||
this.claimRepository_
|
|
||||||
)
|
|
||||||
const query = buildQuery(selector, config)
|
const query = buildQuery(selector, config)
|
||||||
return await claimRepo.find(query)
|
return await claimRepo.find(query)
|
||||||
}
|
}
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets an order by id.
|
* Gets an order by id.
|
||||||
@@ -835,11 +830,8 @@ export default class ClaimService extends TransactionBaseService<
|
|||||||
id: string,
|
id: string,
|
||||||
config: FindConfig<ClaimOrder> = {}
|
config: FindConfig<ClaimOrder> = {}
|
||||||
): Promise<ClaimOrder> {
|
): Promise<ClaimOrder> {
|
||||||
return await this.atomicPhase_(
|
const manager = this.manager_
|
||||||
async (transactionManager: EntityManager) => {
|
const claimRepo = manager.getCustomRepository(this.claimRepository_)
|
||||||
const claimRepo = transactionManager.getCustomRepository(
|
|
||||||
this.claimRepository_
|
|
||||||
)
|
|
||||||
|
|
||||||
const query = buildQuery({ id }, config)
|
const query = buildQuery({ id }, config)
|
||||||
const claim = await claimRepo.findOne(query)
|
const claim = await claimRepo.findOne(query)
|
||||||
@@ -853,6 +845,4 @@ export default class ClaimService extends TransactionBaseService<
|
|||||||
|
|
||||||
return claim
|
return claim
|
||||||
}
|
}
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -37,7 +37,7 @@ class CustomShippingOptionService extends TransactionBaseService<CustomShippingO
|
|||||||
id: string,
|
id: string,
|
||||||
config: FindConfig<CustomShippingOption> = {}
|
config: FindConfig<CustomShippingOption> = {}
|
||||||
): Promise<CustomShippingOption> {
|
): Promise<CustomShippingOption> {
|
||||||
return await this.atomicPhase_(async (manager) => {
|
const manager = this.manager_
|
||||||
const customShippingOptionRepo = manager.getCustomRepository(
|
const customShippingOptionRepo = manager.getCustomRepository(
|
||||||
this.customShippingOptionRepository_
|
this.customShippingOptionRepository_
|
||||||
)
|
)
|
||||||
@@ -54,7 +54,6 @@ class CustomShippingOptionService extends TransactionBaseService<CustomShippingO
|
|||||||
}
|
}
|
||||||
|
|
||||||
return customShippingOption
|
return customShippingOption
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Fetches all custom shipping options based on the given selector
|
/** Fetches all custom shipping options based on the given selector
|
||||||
@@ -70,7 +69,7 @@ class CustomShippingOptionService extends TransactionBaseService<CustomShippingO
|
|||||||
relations: [],
|
relations: [],
|
||||||
}
|
}
|
||||||
): Promise<CustomShippingOption[]> {
|
): Promise<CustomShippingOption[]> {
|
||||||
return await this.atomicPhase_(async (manager) => {
|
const manager = this.manager_
|
||||||
const customShippingOptionRepo = manager.getCustomRepository(
|
const customShippingOptionRepo = manager.getCustomRepository(
|
||||||
this.customShippingOptionRepository_
|
this.customShippingOptionRepository_
|
||||||
)
|
)
|
||||||
@@ -78,7 +77,6 @@ class CustomShippingOptionService extends TransactionBaseService<CustomShippingO
|
|||||||
const query = buildQuery(selector, config)
|
const query = buildQuery(selector, config)
|
||||||
|
|
||||||
return await customShippingOptionRepo.find(query)
|
return await customShippingOptionRepo.find(query)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -92,19 +90,18 @@ class CustomShippingOptionService extends TransactionBaseService<CustomShippingO
|
|||||||
): Promise<CustomShippingOption> {
|
): Promise<CustomShippingOption> {
|
||||||
const { cart_id, shipping_option_id, price, metadata } = data
|
const { cart_id, shipping_option_id, price, metadata } = data
|
||||||
|
|
||||||
return await this.atomicPhase_(async (manager) => {
|
const manager = this.manager_
|
||||||
const customShippingOptionRepo = manager.getCustomRepository(
|
const customShippingOptionRepo = manager.getCustomRepository(
|
||||||
this.customShippingOptionRepository_
|
this.customShippingOptionRepository_
|
||||||
)
|
)
|
||||||
|
|
||||||
const customShippingOption = await customShippingOptionRepo.create({
|
const customShippingOption = customShippingOptionRepo.create({
|
||||||
cart_id,
|
cart_id,
|
||||||
shipping_option_id,
|
shipping_option_id,
|
||||||
price,
|
price,
|
||||||
metadata,
|
metadata,
|
||||||
})
|
})
|
||||||
return await customShippingOptionRepo.save(customShippingOption)
|
return await customShippingOptionRepo.save(customShippingOption)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import jwt from "jsonwebtoken"
|
import jwt from "jsonwebtoken"
|
||||||
import _ from "lodash"
|
|
||||||
import { MedusaError } from "medusa-core-utils"
|
import { MedusaError } from "medusa-core-utils"
|
||||||
import Scrypt from "scrypt-kdf"
|
import Scrypt from "scrypt-kdf"
|
||||||
import { DeepPartial, EntityManager } from "typeorm"
|
import { DeepPartial, EntityManager } from "typeorm"
|
||||||
@@ -109,7 +108,7 @@ class CustomerService extends TransactionBaseService<CustomerService> {
|
|||||||
selector: Selector<Customer> & { q?: string } = {},
|
selector: Selector<Customer> & { q?: string } = {},
|
||||||
config: FindConfig<Customer> = { relations: [], skip: 0, take: 50 }
|
config: FindConfig<Customer> = { relations: [], skip: 0, take: 50 }
|
||||||
): Promise<Customer[]> {
|
): Promise<Customer[]> {
|
||||||
return await this.atomicPhase_(async (manager) => {
|
const manager = this.manager_
|
||||||
const customerRepo = manager.getCustomRepository(this.customerRepository_)
|
const customerRepo = manager.getCustomRepository(this.customerRepository_)
|
||||||
|
|
||||||
let q
|
let q
|
||||||
@@ -122,7 +121,6 @@ class CustomerService extends TransactionBaseService<CustomerService> {
|
|||||||
|
|
||||||
const [customers] = await customerRepo.listAndCount(query, q)
|
const [customers] = await customerRepo.listAndCount(query, q)
|
||||||
return customers
|
return customers
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -139,7 +137,7 @@ class CustomerService extends TransactionBaseService<CustomerService> {
|
|||||||
order: { created_at: "DESC" },
|
order: { created_at: "DESC" },
|
||||||
}
|
}
|
||||||
): Promise<[Customer[], number]> {
|
): Promise<[Customer[], number]> {
|
||||||
return await this.atomicPhase_(async (manager) => {
|
const manager = this.manager_
|
||||||
const customerRepo = manager.getCustomRepository(this.customerRepository_)
|
const customerRepo = manager.getCustomRepository(this.customerRepository_)
|
||||||
|
|
||||||
let q
|
let q
|
||||||
@@ -151,7 +149,6 @@ class CustomerService extends TransactionBaseService<CustomerService> {
|
|||||||
const query = buildQuery<Selector<Customer>, Customer>(selector, config)
|
const query = buildQuery<Selector<Customer>, Customer>(selector, config)
|
||||||
|
|
||||||
return await customerRepo.listAndCount(query, q)
|
return await customerRepo.listAndCount(query, q)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -159,10 +156,9 @@ class CustomerService extends TransactionBaseService<CustomerService> {
|
|||||||
* @return {Promise} the result of the count operation
|
* @return {Promise} the result of the count operation
|
||||||
*/
|
*/
|
||||||
async count(): Promise<number> {
|
async count(): Promise<number> {
|
||||||
return await this.atomicPhase_(async (manager) => {
|
const manager = this.manager_
|
||||||
const customerRepo = manager.getCustomRepository(this.customerRepository_)
|
const customerRepo = manager.getCustomRepository(this.customerRepository_)
|
||||||
return await customerRepo.count({})
|
return await customerRepo.count({})
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async retrieve_(
|
private async retrieve_(
|
||||||
@@ -199,9 +195,7 @@ class CustomerService extends TransactionBaseService<CustomerService> {
|
|||||||
email: string,
|
email: string,
|
||||||
config: FindConfig<Customer> = {}
|
config: FindConfig<Customer> = {}
|
||||||
): Promise<Customer | never> {
|
): Promise<Customer | never> {
|
||||||
return await this.atomicPhase_(async () => {
|
|
||||||
return await this.retrieve_({ email: email.toLowerCase() }, config)
|
return await this.retrieve_({ email: email.toLowerCase() }, config)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -214,9 +208,7 @@ class CustomerService extends TransactionBaseService<CustomerService> {
|
|||||||
phone: string,
|
phone: string,
|
||||||
config: FindConfig<Customer> = {}
|
config: FindConfig<Customer> = {}
|
||||||
): Promise<Customer | never> {
|
): Promise<Customer | never> {
|
||||||
return await this.atomicPhase_(async () => {
|
|
||||||
return await this.retrieve_({ phone }, config)
|
return await this.retrieve_({ phone }, config)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -229,9 +221,7 @@ class CustomerService extends TransactionBaseService<CustomerService> {
|
|||||||
customerId: string,
|
customerId: string,
|
||||||
config: FindConfig<Customer> = {}
|
config: FindConfig<Customer> = {}
|
||||||
): Promise<Customer> {
|
): Promise<Customer> {
|
||||||
return await this.atomicPhase_(async () => {
|
|
||||||
return this.retrieve_({ id: customerId }, config)
|
return this.retrieve_({ id: customerId }, config)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ class DiscountConditionService extends TransactionBaseService<DiscountConditionS
|
|||||||
conditionId: string,
|
conditionId: string,
|
||||||
config?: FindConfig<DiscountCondition>
|
config?: FindConfig<DiscountCondition>
|
||||||
): Promise<DiscountCondition | never> {
|
): Promise<DiscountCondition | never> {
|
||||||
return await this.atomicPhase_(async (manager: EntityManager) => {
|
const manager = this.manager_
|
||||||
const conditionRepo = manager.getCustomRepository(
|
const conditionRepo = manager.getCustomRepository(
|
||||||
this.discountConditionRepository_
|
this.discountConditionRepository_
|
||||||
)
|
)
|
||||||
@@ -67,7 +67,6 @@ class DiscountConditionService extends TransactionBaseService<DiscountConditionS
|
|||||||
}
|
}
|
||||||
|
|
||||||
return condition
|
return condition
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static resolveConditionType_(data: UpsertDiscountConditionInput):
|
protected static resolveConditionType_(data: UpsertDiscountConditionInput):
|
||||||
|
|||||||
@@ -115,14 +115,11 @@ class DiscountService extends TransactionBaseService<DiscountService> {
|
|||||||
selector: FilterableDiscountProps = {},
|
selector: FilterableDiscountProps = {},
|
||||||
config: FindConfig<Discount> = { relations: [], skip: 0, take: 10 }
|
config: FindConfig<Discount> = { relations: [], skip: 0, take: 10 }
|
||||||
): Promise<Discount[]> {
|
): Promise<Discount[]> {
|
||||||
return await this.atomicPhase_(async (transactionManager) => {
|
const manager = this.manager_
|
||||||
const discountRepo = transactionManager.getCustomRepository(
|
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
||||||
this.discountRepository_
|
|
||||||
)
|
|
||||||
|
|
||||||
const query = buildQuery(selector as Selector<Discount>, config)
|
const query = buildQuery(selector as Selector<Discount>, config)
|
||||||
return await discountRepo.find(query)
|
return await discountRepo.find(query)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -138,10 +135,8 @@ class DiscountService extends TransactionBaseService<DiscountService> {
|
|||||||
order: { created_at: "DESC" },
|
order: { created_at: "DESC" },
|
||||||
}
|
}
|
||||||
): Promise<[Discount[], number]> {
|
): Promise<[Discount[], number]> {
|
||||||
return await this.atomicPhase_(async (transactionManager) => {
|
const manager = this.manager_
|
||||||
const discountRepo = transactionManager.getCustomRepository(
|
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
||||||
this.discountRepository_
|
|
||||||
)
|
|
||||||
|
|
||||||
let q
|
let q
|
||||||
if ("q" in selector) {
|
if ("q" in selector) {
|
||||||
@@ -170,7 +165,6 @@ class DiscountService extends TransactionBaseService<DiscountService> {
|
|||||||
const [discounts, count] = await discountRepo.findAndCount(query)
|
const [discounts, count] = await discountRepo.findAndCount(query)
|
||||||
|
|
||||||
return [discounts, count]
|
return [discounts, count]
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -248,10 +242,8 @@ class DiscountService extends TransactionBaseService<DiscountService> {
|
|||||||
discountId: string,
|
discountId: string,
|
||||||
config: FindConfig<Discount> = {}
|
config: FindConfig<Discount> = {}
|
||||||
): Promise<Discount> {
|
): Promise<Discount> {
|
||||||
return await this.atomicPhase_(async (transactionManager) => {
|
const manager = this.manager_
|
||||||
const discountRepo = transactionManager.getCustomRepository(
|
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
||||||
this.discountRepository_
|
|
||||||
)
|
|
||||||
|
|
||||||
const query = buildQuery({ id: discountId }, config)
|
const query = buildQuery({ id: discountId }, config)
|
||||||
const discount = await discountRepo.findOne(query)
|
const discount = await discountRepo.findOne(query)
|
||||||
@@ -264,7 +256,6 @@ class DiscountService extends TransactionBaseService<DiscountService> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return discount
|
return discount
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -277,10 +268,8 @@ class DiscountService extends TransactionBaseService<DiscountService> {
|
|||||||
discountCode: string,
|
discountCode: string,
|
||||||
config: FindConfig<Discount> = {}
|
config: FindConfig<Discount> = {}
|
||||||
): Promise<Discount> {
|
): Promise<Discount> {
|
||||||
return await this.atomicPhase_(async (transactionManager) => {
|
const manager = this.manager_
|
||||||
const discountRepo = transactionManager.getCustomRepository(
|
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
||||||
this.discountRepository_
|
|
||||||
)
|
|
||||||
|
|
||||||
let query = buildQuery({ code: discountCode, is_dynamic: false }, config)
|
let query = buildQuery({ code: discountCode, is_dynamic: false }, config)
|
||||||
let discount = await discountRepo.findOne(query)
|
let discount = await discountRepo.findOne(query)
|
||||||
@@ -298,7 +287,6 @@ class DiscountService extends TransactionBaseService<DiscountService> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return discount
|
return discount
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -562,7 +550,9 @@ class DiscountService extends TransactionBaseService<DiscountService> {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
const product = await this.productService_.retrieve(productId, {
|
const product = await this.productService_
|
||||||
|
.withTransaction(manager)
|
||||||
|
.retrieve(productId, {
|
||||||
relations: ["tags"],
|
relations: ["tags"],
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -721,7 +711,7 @@ class DiscountService extends TransactionBaseService<DiscountService> {
|
|||||||
discountRuleId: string,
|
discountRuleId: string,
|
||||||
customerId: string | undefined
|
customerId: string | undefined
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
return await this.atomicPhase_(async (manager) => {
|
return await this.atomicPhase_(async (manager: EntityManager) => {
|
||||||
const discountConditionRepo: DiscountConditionRepository =
|
const discountConditionRepo: DiscountConditionRepository =
|
||||||
manager.getCustomRepository(this.discountConditionRepository_)
|
manager.getCustomRepository(this.discountConditionRepository_)
|
||||||
|
|
||||||
@@ -730,7 +720,9 @@ class DiscountService extends TransactionBaseService<DiscountService> {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
const customer = await this.customerService_.retrieve(customerId, {
|
const customer = await this.customerService_
|
||||||
|
.withTransaction(manager)
|
||||||
|
.retrieve(customerId, {
|
||||||
relations: ["groups"],
|
relations: ["groups"],
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -92,9 +92,8 @@ class DraftOrderService extends TransactionBaseService<DraftOrderService> {
|
|||||||
id: string,
|
id: string,
|
||||||
config: FindConfig<DraftOrder> = {}
|
config: FindConfig<DraftOrder> = {}
|
||||||
): Promise<DraftOrder | never> {
|
): Promise<DraftOrder | never> {
|
||||||
return await this.atomicPhase_(
|
const manager = this.manager_
|
||||||
async (transactionManager: EntityManager) => {
|
const draftOrderRepo = manager.getCustomRepository(
|
||||||
const draftOrderRepo = transactionManager.getCustomRepository(
|
|
||||||
this.draftOrderRepository_
|
this.draftOrderRepository_
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -109,8 +108,6 @@ class DraftOrderService extends TransactionBaseService<DraftOrderService> {
|
|||||||
|
|
||||||
return draftOrder
|
return draftOrder
|
||||||
}
|
}
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves a draft order based on its associated cart id
|
* Retrieves a draft order based on its associated cart id
|
||||||
@@ -122,9 +119,8 @@ class DraftOrderService extends TransactionBaseService<DraftOrderService> {
|
|||||||
cartId: string,
|
cartId: string,
|
||||||
config: FindConfig<DraftOrder> = {}
|
config: FindConfig<DraftOrder> = {}
|
||||||
): Promise<DraftOrder | never> {
|
): Promise<DraftOrder | never> {
|
||||||
return await this.atomicPhase_(
|
const manager = this.manager_
|
||||||
async (transactionManager: EntityManager) => {
|
const draftOrderRepo = manager.getCustomRepository(
|
||||||
const draftOrderRepo = transactionManager.getCustomRepository(
|
|
||||||
this.draftOrderRepository_
|
this.draftOrderRepository_
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -139,8 +135,6 @@ class DraftOrderService extends TransactionBaseService<DraftOrderService> {
|
|||||||
|
|
||||||
return draftOrder
|
return draftOrder
|
||||||
}
|
}
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes draft order idempotently.
|
* Deletes draft order idempotently.
|
||||||
@@ -179,9 +173,8 @@ class DraftOrderService extends TransactionBaseService<DraftOrderService> {
|
|||||||
order: { created_at: "DESC" },
|
order: { created_at: "DESC" },
|
||||||
}
|
}
|
||||||
): Promise<[DraftOrder[], number]> {
|
): Promise<[DraftOrder[], number]> {
|
||||||
return await this.atomicPhase_(
|
const manager = this.manager_
|
||||||
async (transactionManager: EntityManager) => {
|
const draftOrderRepository = manager.getCustomRepository(
|
||||||
const draftOrderRepository = transactionManager.getCustomRepository(
|
|
||||||
this.draftOrderRepository_
|
this.draftOrderRepository_
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -219,8 +212,6 @@ class DraftOrderService extends TransactionBaseService<DraftOrderService> {
|
|||||||
|
|
||||||
return await draftOrderRepository.findAndCount(query)
|
return await draftOrderRepository.findAndCount(query)
|
||||||
}
|
}
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lists draft orders
|
* Lists draft orders
|
||||||
@@ -236,9 +227,8 @@ class DraftOrderService extends TransactionBaseService<DraftOrderService> {
|
|||||||
order: { created_at: "DESC" },
|
order: { created_at: "DESC" },
|
||||||
}
|
}
|
||||||
): Promise<DraftOrder[]> {
|
): Promise<DraftOrder[]> {
|
||||||
return await this.atomicPhase_(
|
const manager = this.manager_
|
||||||
async (transactionManager: EntityManager) => {
|
const draftOrderRepo = manager.getCustomRepository(
|
||||||
const draftOrderRepo = transactionManager.getCustomRepository(
|
|
||||||
this.draftOrderRepository_
|
this.draftOrderRepository_
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -246,8 +236,6 @@ class DraftOrderService extends TransactionBaseService<DraftOrderService> {
|
|||||||
|
|
||||||
return await draftOrderRepo.find(query)
|
return await draftOrderRepo.find(query)
|
||||||
}
|
}
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a draft order.
|
* Creates a draft order.
|
||||||
|
|||||||
@@ -167,7 +167,7 @@ class FulfillmentService extends TransactionBaseService<FulfillmentService> {
|
|||||||
id: string,
|
id: string,
|
||||||
config: FindConfig<Fulfillment> = {}
|
config: FindConfig<Fulfillment> = {}
|
||||||
): Promise<Fulfillment> {
|
): Promise<Fulfillment> {
|
||||||
return await this.atomicPhase_(async (manager) => {
|
const manager = this.manager_
|
||||||
const fulfillmentRepository = manager.getCustomRepository(
|
const fulfillmentRepository = manager.getCustomRepository(
|
||||||
this.fulfillmentRepository_
|
this.fulfillmentRepository_
|
||||||
)
|
)
|
||||||
@@ -183,7 +183,6 @@ class FulfillmentService extends TransactionBaseService<FulfillmentService> {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
return fulfillment
|
return fulfillment
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ class GiftCardService extends TransactionBaseService<GiftCardService> {
|
|||||||
selector: QuerySelector<GiftCard> = {},
|
selector: QuerySelector<GiftCard> = {},
|
||||||
config: FindConfig<GiftCard> = { relations: [], skip: 0, take: 10 }
|
config: FindConfig<GiftCard> = { relations: [], skip: 0, take: 10 }
|
||||||
): Promise<[GiftCard[], number]> {
|
): Promise<[GiftCard[], number]> {
|
||||||
return await this.atomicPhase_(async (manager) => {
|
const manager = this.manager_
|
||||||
const giftCardRepo = manager.getCustomRepository(this.giftCardRepository_)
|
const giftCardRepo = manager.getCustomRepository(this.giftCardRepository_)
|
||||||
|
|
||||||
let q: string | undefined
|
let q: string | undefined
|
||||||
@@ -103,7 +103,6 @@ class GiftCardService extends TransactionBaseService<GiftCardService> {
|
|||||||
delete query.relations
|
delete query.relations
|
||||||
|
|
||||||
return await giftCardRepo.listGiftCardsAndCount(query, rels, q)
|
return await giftCardRepo.listGiftCardsAndCount(query, rels, q)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -115,7 +114,7 @@ class GiftCardService extends TransactionBaseService<GiftCardService> {
|
|||||||
selector: QuerySelector<GiftCard> = {},
|
selector: QuerySelector<GiftCard> = {},
|
||||||
config: FindConfig<GiftCard> = { relations: [], skip: 0, take: 10 }
|
config: FindConfig<GiftCard> = { relations: [], skip: 0, take: 10 }
|
||||||
): Promise<GiftCard[]> {
|
): Promise<GiftCard[]> {
|
||||||
return await this.atomicPhase_(async (manager) => {
|
const manager = this.manager_
|
||||||
const giftCardRepo = manager.getCustomRepository(this.giftCardRepository_)
|
const giftCardRepo = manager.getCustomRepository(this.giftCardRepository_)
|
||||||
|
|
||||||
let q: string | undefined
|
let q: string | undefined
|
||||||
@@ -133,18 +132,16 @@ class GiftCardService extends TransactionBaseService<GiftCardService> {
|
|||||||
delete query.relations
|
delete query.relations
|
||||||
|
|
||||||
return await giftCardRepo.listGiftCards(query, rels, q)
|
return await giftCardRepo.listGiftCards(query, rels, q)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async createTransaction(
|
async createTransaction(
|
||||||
data: CreateGiftCardTransactionInput
|
data: CreateGiftCardTransactionInput
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
return await this.atomicPhase_(async (manager) => {
|
const manager = this.manager_
|
||||||
const gctRepo = manager.getCustomRepository(this.giftCardTransactionRepo_)
|
const gctRepo = manager.getCustomRepository(this.giftCardTransactionRepo_)
|
||||||
const created = gctRepo.create(data)
|
const created = gctRepo.create(data)
|
||||||
const saved = await gctRepo.save(created)
|
const saved = await gctRepo.save(created)
|
||||||
return saved.id
|
return saved.id
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -186,7 +183,7 @@ class GiftCardService extends TransactionBaseService<GiftCardService> {
|
|||||||
selector: Selector<GiftCard>,
|
selector: Selector<GiftCard>,
|
||||||
config: FindConfig<GiftCard> = {}
|
config: FindConfig<GiftCard> = {}
|
||||||
): Promise<GiftCard> {
|
): Promise<GiftCard> {
|
||||||
return await this.atomicPhase_(async (manager) => {
|
const manager = this.manager_
|
||||||
const giftCardRepo = manager.getCustomRepository(this.giftCardRepository_)
|
const giftCardRepo = manager.getCustomRepository(this.giftCardRepository_)
|
||||||
|
|
||||||
const { relations, ...query } = buildQuery(selector, config)
|
const { relations, ...query } = buildQuery(selector, config)
|
||||||
@@ -208,7 +205,6 @@ class GiftCardService extends TransactionBaseService<GiftCardService> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return giftCard
|
return giftCard
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -221,18 +217,14 @@ class GiftCardService extends TransactionBaseService<GiftCardService> {
|
|||||||
giftCardId: string,
|
giftCardId: string,
|
||||||
config: FindConfig<GiftCard> = {}
|
config: FindConfig<GiftCard> = {}
|
||||||
): Promise<GiftCard> {
|
): Promise<GiftCard> {
|
||||||
return await this.atomicPhase_(async () => {
|
|
||||||
return await this.retrieve_({ id: giftCardId }, config)
|
return await this.retrieve_({ id: giftCardId }, config)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async retrieveByCode(
|
async retrieveByCode(
|
||||||
code: string,
|
code: string,
|
||||||
config: FindConfig<GiftCard> = {}
|
config: FindConfig<GiftCard> = {}
|
||||||
): Promise<GiftCard> {
|
): Promise<GiftCard> {
|
||||||
return await this.atomicPhase_(async () => {
|
|
||||||
return await this.retrieve_({ code }, config)
|
return await this.retrieve_({ code }, config)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -253,7 +245,9 @@ class GiftCardService extends TransactionBaseService<GiftCardService> {
|
|||||||
const { region_id, metadata, balance, ...rest } = update
|
const { region_id, metadata, balance, ...rest } = update
|
||||||
|
|
||||||
if (region_id && region_id !== giftCard.region_id) {
|
if (region_id && region_id !== giftCard.region_id) {
|
||||||
const region = await this.regionService_.retrieve(region_id)
|
const region = await this.regionService_
|
||||||
|
.withTransaction(manager)
|
||||||
|
.retrieve(region_id)
|
||||||
giftCard.region_id = region.id
|
giftCard.region_id = region.id
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -286,7 +280,7 @@ class GiftCardService extends TransactionBaseService<GiftCardService> {
|
|||||||
* @return the result of the delete operation
|
* @return the result of the delete operation
|
||||||
*/
|
*/
|
||||||
async delete(giftCardId: string): Promise<GiftCard | void> {
|
async delete(giftCardId: string): Promise<GiftCard | void> {
|
||||||
return await this.atomicPhase_(async (manager) => {
|
const manager = this.manager_
|
||||||
const giftCardRepo = manager.getCustomRepository(this.giftCardRepository_)
|
const giftCardRepo = manager.getCustomRepository(this.giftCardRepository_)
|
||||||
|
|
||||||
const giftCard = await giftCardRepo.findOne({ where: { id: giftCardId } })
|
const giftCard = await giftCardRepo.findOne({ where: { id: giftCardId } })
|
||||||
@@ -296,7 +290,6 @@ class GiftCardService extends TransactionBaseService<GiftCardService> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return await giftCardRepo.softRemove(giftCard)
|
return await giftCardRepo.softRemove(giftCard)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -91,16 +91,11 @@ class LineItemService extends BaseService {
|
|||||||
selector,
|
selector,
|
||||||
config = { skip: 0, take: 50, order: { created_at: "DESC" } }
|
config = { skip: 0, take: 50, order: { created_at: "DESC" } }
|
||||||
): Promise<LineItem[]> {
|
): Promise<LineItem[]> {
|
||||||
return await this.atomicPhase_(
|
const manager = this.manager_
|
||||||
async (transactionManager: EntityManager) => {
|
const lineItemRepo = manager.getCustomRepository(this.lineItemRepository_)
|
||||||
const lineItemRepo = transactionManager.getCustomRepository(
|
|
||||||
this.lineItemRepository_
|
|
||||||
)
|
|
||||||
const query = this.buildQuery_(selector, config)
|
const query = this.buildQuery_(selector, config)
|
||||||
return await lineItemRepo.find(query)
|
return await lineItemRepo.find(query)
|
||||||
}
|
}
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves a line item by its id.
|
* Retrieves a line item by its id.
|
||||||
@@ -109,9 +104,8 @@ class LineItemService extends BaseService {
|
|||||||
* @return {Promise<LineItem | never>} the line item
|
* @return {Promise<LineItem | never>} the line item
|
||||||
*/
|
*/
|
||||||
async retrieve(id: string, config = {}): Promise<LineItem | never> {
|
async retrieve(id: string, config = {}): Promise<LineItem | never> {
|
||||||
return await this.atomicPhase_(
|
const manager = this.manager_
|
||||||
async (transactionManager: EntityManager) => {
|
const lineItemRepository = manager.getCustomRepository(
|
||||||
const lineItemRepository = transactionManager.getCustomRepository(
|
|
||||||
this.lineItemRepository_
|
this.lineItemRepository_
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -129,8 +123,6 @@ class LineItemService extends BaseService {
|
|||||||
|
|
||||||
return lineItem
|
return lineItem
|
||||||
}
|
}
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates return line items for a given cart based on the return items in a
|
* Creates return line items for a given cart based on the return items in a
|
||||||
|
|||||||
@@ -248,7 +248,7 @@ class PriceListService extends TransactionBaseService<PriceListService> {
|
|||||||
selector: FilterablePriceListProps = {},
|
selector: FilterablePriceListProps = {},
|
||||||
config: FindConfig<FilterablePriceListProps> = { skip: 0, take: 20 }
|
config: FindConfig<FilterablePriceListProps> = { skip: 0, take: 20 }
|
||||||
): Promise<PriceList[]> {
|
): Promise<PriceList[]> {
|
||||||
return await this.atomicPhase_(async (manager: EntityManager) => {
|
const manager = this.manager_
|
||||||
const priceListRepo = manager.getCustomRepository(this.priceListRepo_)
|
const priceListRepo = manager.getCustomRepository(this.priceListRepo_)
|
||||||
|
|
||||||
const { q, ...priceListSelector } = selector
|
const { q, ...priceListSelector } = selector
|
||||||
@@ -260,7 +260,6 @@ class PriceListService extends TransactionBaseService<PriceListService> {
|
|||||||
const [priceLists] = await priceListRepo.listAndCount(query, groups)
|
const [priceLists] = await priceListRepo.listAndCount(query, groups)
|
||||||
|
|
||||||
return priceLists
|
return priceLists
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -276,7 +275,7 @@ class PriceListService extends TransactionBaseService<PriceListService> {
|
|||||||
take: 20,
|
take: 20,
|
||||||
}
|
}
|
||||||
): Promise<[PriceList[], number]> {
|
): Promise<[PriceList[], number]> {
|
||||||
return await this.atomicPhase_(async (manager: EntityManager) => {
|
const manager = this.manager_
|
||||||
const priceListRepo = manager.getCustomRepository(this.priceListRepo_)
|
const priceListRepo = manager.getCustomRepository(this.priceListRepo_)
|
||||||
const { q, ...priceListSelector } = selector
|
const { q, ...priceListSelector } = selector
|
||||||
const { relations, ...query } = buildQuery<
|
const { relations, ...query } = buildQuery<
|
||||||
@@ -296,7 +295,6 @@ class PriceListService extends TransactionBaseService<PriceListService> {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
return await priceListRepo.listAndCount({ ...query, relations }, groups)
|
return await priceListRepo.listAndCount({ ...query, relations }, groups)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async upsertCustomerGroups_(
|
protected async upsertCustomerGroups_(
|
||||||
@@ -332,10 +330,9 @@ class PriceListService extends TransactionBaseService<PriceListService> {
|
|||||||
const productVariantRepo = manager.getCustomRepository(
|
const productVariantRepo = manager.getCustomRepository(
|
||||||
this.productVariantRepo_
|
this.productVariantRepo_
|
||||||
)
|
)
|
||||||
const [products, count] = await this.productService_.listAndCount(
|
const [products, count] = await this.productService_
|
||||||
selector,
|
.withTransaction(manager)
|
||||||
config
|
.listAndCount(selector, config)
|
||||||
)
|
|
||||||
|
|
||||||
const moneyAmountRepo = manager.getCustomRepository(this.moneyAmountRepo_)
|
const moneyAmountRepo = manager.getCustomRepository(this.moneyAmountRepo_)
|
||||||
|
|
||||||
@@ -378,10 +375,9 @@ class PriceListService extends TransactionBaseService<PriceListService> {
|
|||||||
requiresPriceList = false
|
requiresPriceList = false
|
||||||
): Promise<[ProductVariant[], number]> {
|
): Promise<[ProductVariant[], number]> {
|
||||||
return await this.atomicPhase_(async (manager: EntityManager) => {
|
return await this.atomicPhase_(async (manager: EntityManager) => {
|
||||||
const [variants, count] = await this.variantService_.listAndCount(
|
const [variants, count] = await this.variantService_
|
||||||
selector,
|
.withTransaction(manager)
|
||||||
config
|
.listAndCount(selector, config)
|
||||||
)
|
|
||||||
|
|
||||||
const moneyAmountRepo = manager.getCustomRepository(this.moneyAmountRepo_)
|
const moneyAmountRepo = manager.getCustomRepository(this.moneyAmountRepo_)
|
||||||
|
|
||||||
|
|||||||
@@ -63,15 +63,14 @@ class PricingService extends TransactionBaseService<PricingService> {
|
|||||||
async collectPricingContext(
|
async collectPricingContext(
|
||||||
context: PriceSelectionContext
|
context: PriceSelectionContext
|
||||||
): Promise<PricingContext> {
|
): Promise<PricingContext> {
|
||||||
return await this.atomicPhase_(
|
return await this.atomicPhase_(async (manager: EntityManager) => {
|
||||||
async (transactionManager: EntityManager) => {
|
|
||||||
let automaticTaxes = false
|
let automaticTaxes = false
|
||||||
let taxRate = null
|
let taxRate = null
|
||||||
let currencyCode = context.currency_code
|
let currencyCode = context.currency_code
|
||||||
|
|
||||||
if (context.region_id) {
|
if (context.region_id) {
|
||||||
const region = await this.regionService
|
const region = await this.regionService
|
||||||
.withTransaction(transactionManager)
|
.withTransaction(manager)
|
||||||
.retrieve(context.region_id, {
|
.retrieve(context.region_id, {
|
||||||
select: ["id", "currency_code", "automatic_taxes", "tax_rate"],
|
select: ["id", "currency_code", "automatic_taxes", "tax_rate"],
|
||||||
})
|
})
|
||||||
@@ -89,8 +88,7 @@ class PricingService extends TransactionBaseService<PricingService> {
|
|||||||
automatic_taxes: automaticTaxes,
|
automatic_taxes: automaticTaxes,
|
||||||
tax_rate: taxRate,
|
tax_rate: taxRate,
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -113,7 +113,7 @@ class ProductService extends TransactionBaseService<ProductService> {
|
|||||||
include_discount_prices: false,
|
include_discount_prices: false,
|
||||||
}
|
}
|
||||||
): Promise<Product[]> {
|
): Promise<Product[]> {
|
||||||
return await this.atomicPhase_(async (manager) => {
|
const manager = this.manager_
|
||||||
const productRepo = manager.getCustomRepository(this.productRepository_)
|
const productRepo = manager.getCustomRepository(this.productRepository_)
|
||||||
|
|
||||||
const { q, query, relations } = this.prepareListQuery_(selector, config)
|
const { q, query, relations } = this.prepareListQuery_(selector, config)
|
||||||
@@ -127,7 +127,6 @@ class ProductService extends TransactionBaseService<ProductService> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return await productRepo.findWithRelations(relations, query)
|
return await productRepo.findWithRelations(relations, query)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -150,7 +149,7 @@ class ProductService extends TransactionBaseService<ProductService> {
|
|||||||
include_discount_prices: false,
|
include_discount_prices: false,
|
||||||
}
|
}
|
||||||
): Promise<[Product[], number]> {
|
): Promise<[Product[], number]> {
|
||||||
return await this.atomicPhase_(async (manager) => {
|
const manager = this.manager_
|
||||||
const productRepo = manager.getCustomRepository(this.productRepository_)
|
const productRepo = manager.getCustomRepository(this.productRepository_)
|
||||||
|
|
||||||
const { q, query, relations } = this.prepareListQuery_(selector, config)
|
const { q, query, relations } = this.prepareListQuery_(selector, config)
|
||||||
@@ -164,7 +163,6 @@ class ProductService extends TransactionBaseService<ProductService> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return await productRepo.findWithRelationsAndCount(relations, query)
|
return await productRepo.findWithRelationsAndCount(relations, query)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -173,11 +171,10 @@ class ProductService extends TransactionBaseService<ProductService> {
|
|||||||
* @return {Promise} the result of the count operation
|
* @return {Promise} the result of the count operation
|
||||||
*/
|
*/
|
||||||
async count(selector: Selector<Product> = {}): Promise<number> {
|
async count(selector: Selector<Product> = {}): Promise<number> {
|
||||||
return await this.atomicPhase_(async (manager) => {
|
const manager = this.manager_
|
||||||
const productRepo = manager.getCustomRepository(this.productRepository_)
|
const productRepo = manager.getCustomRepository(this.productRepository_)
|
||||||
const query = buildQuery(selector)
|
const query = buildQuery(selector)
|
||||||
return await productRepo.count(query)
|
return await productRepo.count(query)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -194,9 +191,7 @@ class ProductService extends TransactionBaseService<ProductService> {
|
|||||||
include_discount_prices: false,
|
include_discount_prices: false,
|
||||||
}
|
}
|
||||||
): Promise<Product> {
|
): Promise<Product> {
|
||||||
return await this.atomicPhase_(async () => {
|
|
||||||
return await this.retrieve_({ id: productId }, config)
|
return await this.retrieve_({ id: productId }, config)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -210,9 +205,7 @@ class ProductService extends TransactionBaseService<ProductService> {
|
|||||||
productHandle: string,
|
productHandle: string,
|
||||||
config: FindProductConfig = {}
|
config: FindProductConfig = {}
|
||||||
): Promise<Product> {
|
): Promise<Product> {
|
||||||
return await this.atomicPhase_(async () => {
|
|
||||||
return await this.retrieve_({ handle: productHandle }, config)
|
return await this.retrieve_({ handle: productHandle }, config)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -226,9 +219,7 @@ class ProductService extends TransactionBaseService<ProductService> {
|
|||||||
externalId: string,
|
externalId: string,
|
||||||
config: FindProductConfig = {}
|
config: FindProductConfig = {}
|
||||||
): Promise<Product> {
|
): Promise<Product> {
|
||||||
return await this.atomicPhase_(async () => {
|
|
||||||
return await this.retrieve_({ external_id: externalId }, config)
|
return await this.retrieve_({ external_id: externalId }, config)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -245,7 +236,7 @@ class ProductService extends TransactionBaseService<ProductService> {
|
|||||||
include_discount_prices: false,
|
include_discount_prices: false,
|
||||||
}
|
}
|
||||||
): Promise<Product> {
|
): Promise<Product> {
|
||||||
return await this.atomicPhase_(async (manager) => {
|
const manager = this.manager_
|
||||||
const productRepo = manager.getCustomRepository(this.productRepository_)
|
const productRepo = manager.getCustomRepository(this.productRepository_)
|
||||||
|
|
||||||
const { relations, ...query } = buildQuery(selector, config)
|
const { relations, ...query } = buildQuery(selector, config)
|
||||||
@@ -267,7 +258,6 @@ class ProductService extends TransactionBaseService<ProductService> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return product
|
return product
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -283,7 +273,6 @@ class ProductService extends TransactionBaseService<ProductService> {
|
|||||||
take: 50,
|
take: 50,
|
||||||
}
|
}
|
||||||
): Promise<ProductVariant[]> {
|
): Promise<ProductVariant[]> {
|
||||||
return await this.atomicPhase_(async () => {
|
|
||||||
const givenRelations = config.relations ?? []
|
const givenRelations = config.relations ?? []
|
||||||
const requiredRelations = ["variants"]
|
const requiredRelations = ["variants"]
|
||||||
const relationsSet = new Set([...givenRelations, ...requiredRelations])
|
const relationsSet = new Set([...givenRelations, ...requiredRelations])
|
||||||
@@ -293,27 +282,24 @@ class ProductService extends TransactionBaseService<ProductService> {
|
|||||||
relations: [...relationsSet],
|
relations: [...relationsSet],
|
||||||
})
|
})
|
||||||
return product.variants
|
return product.variants
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async listTypes(): Promise<ProductType[]> {
|
async listTypes(): Promise<ProductType[]> {
|
||||||
return await this.atomicPhase_(async (manager) => {
|
const manager = this.manager_
|
||||||
const productTypeRepository = manager.getCustomRepository(
|
const productTypeRepository = manager.getCustomRepository(
|
||||||
this.productTypeRepository_
|
this.productTypeRepository_
|
||||||
)
|
)
|
||||||
|
|
||||||
return await productTypeRepository.find({})
|
return await productTypeRepository.find({})
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async listTagsByUsage(count = 10): Promise<ProductTag[]> {
|
async listTagsByUsage(count = 10): Promise<ProductTag[]> {
|
||||||
return await this.atomicPhase_(async (manager) => {
|
const manager = this.manager_
|
||||||
const productTagRepo = manager.getCustomRepository(
|
const productTagRepo = manager.getCustomRepository(
|
||||||
this.productTagRepository_
|
this.productTagRepository_
|
||||||
)
|
)
|
||||||
|
|
||||||
return await productTagRepo.listTagsByUsage(count)
|
return await productTagRepo.listTagsByUsage(count)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -66,8 +66,8 @@ class SalesChannelService extends TransactionBaseService<SalesChannelService> {
|
|||||||
salesChannelId: string,
|
salesChannelId: string,
|
||||||
config: FindConfig<SalesChannel> = {}
|
config: FindConfig<SalesChannel> = {}
|
||||||
): Promise<SalesChannel | never> {
|
): Promise<SalesChannel | never> {
|
||||||
return await this.atomicPhase_(async (transactionManager) => {
|
const manager = this.manager_
|
||||||
const salesChannelRepo = transactionManager.getCustomRepository(
|
const salesChannelRepo = manager.getCustomRepository(
|
||||||
this.salesChannelRepository_
|
this.salesChannelRepository_
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -88,7 +88,6 @@ class SalesChannelService extends TransactionBaseService<SalesChannelService> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return salesChannel
|
return salesChannel
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -105,8 +104,8 @@ class SalesChannelService extends TransactionBaseService<SalesChannelService> {
|
|||||||
take: 20,
|
take: 20,
|
||||||
}
|
}
|
||||||
): Promise<[SalesChannel[], number]> {
|
): Promise<[SalesChannel[], number]> {
|
||||||
return await this.atomicPhase_(async (transactionManager) => {
|
const manager = this.manager_
|
||||||
const salesChannelRepo = transactionManager.getCustomRepository(
|
const salesChannelRepo = manager.getCustomRepository(
|
||||||
this.salesChannelRepository_
|
this.salesChannelRepository_
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -124,7 +123,6 @@ class SalesChannelService extends TransactionBaseService<SalesChannelService> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return await salesChannelRepo.findAndCount(query)
|
return await salesChannelRepo.findAndCount(query)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -131,14 +131,11 @@ class ShippingOptionService extends TransactionBaseService<ShippingOptionService
|
|||||||
selector: Selector<ShippingMethod>,
|
selector: Selector<ShippingMethod>,
|
||||||
config: FindConfig<ShippingOption> = { skip: 0, take: 50 }
|
config: FindConfig<ShippingOption> = { skip: 0, take: 50 }
|
||||||
): Promise<ShippingOption[]> {
|
): Promise<ShippingOption[]> {
|
||||||
return await this.atomicPhase_(async (transactionManager) => {
|
const manager = this.manager_
|
||||||
const optRepo = transactionManager.getCustomRepository(
|
const optRepo = manager.getCustomRepository(this.optionRepository_)
|
||||||
this.optionRepository_
|
|
||||||
)
|
|
||||||
|
|
||||||
const query = buildQuery(selector, config)
|
const query = buildQuery(selector, config)
|
||||||
return optRepo.find(query)
|
return optRepo.find(query)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -150,14 +147,11 @@ class ShippingOptionService extends TransactionBaseService<ShippingOptionService
|
|||||||
selector: Selector<ShippingMethod>,
|
selector: Selector<ShippingMethod>,
|
||||||
config: FindConfig<ShippingOption> = { skip: 0, take: 50 }
|
config: FindConfig<ShippingOption> = { skip: 0, take: 50 }
|
||||||
): Promise<[ShippingOption[], number]> {
|
): Promise<[ShippingOption[], number]> {
|
||||||
return await this.atomicPhase_(async (transactionManager) => {
|
const manager = this.manager_
|
||||||
const optRepo = transactionManager.getCustomRepository(
|
const optRepo = manager.getCustomRepository(this.optionRepository_)
|
||||||
this.optionRepository_
|
|
||||||
)
|
|
||||||
|
|
||||||
const query = buildQuery(selector, config)
|
const query = buildQuery(selector, config)
|
||||||
return await optRepo.findAndCount(query)
|
return await optRepo.findAndCount(query)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -171,9 +165,10 @@ class ShippingOptionService extends TransactionBaseService<ShippingOptionService
|
|||||||
optionId,
|
optionId,
|
||||||
options: { select?: (keyof ShippingOption)[]; relations?: string[] } = {}
|
options: { select?: (keyof ShippingOption)[]; relations?: string[] } = {}
|
||||||
): Promise<ShippingOption> {
|
): Promise<ShippingOption> {
|
||||||
return await this.atomicPhase_(async (transactionManager) => {
|
const manager = this.manager_
|
||||||
const soRepo: ShippingOptionRepository =
|
const soRepo: ShippingOptionRepository = manager.getCustomRepository(
|
||||||
transactionManager.getCustomRepository(this.optionRepository_)
|
this.optionRepository_
|
||||||
|
)
|
||||||
|
|
||||||
const query: ExtendedFindConfig<ShippingOption> = {
|
const query: ExtendedFindConfig<ShippingOption> = {
|
||||||
where: { id: optionId },
|
where: { id: optionId },
|
||||||
@@ -197,7 +192,6 @@ class ShippingOptionService extends TransactionBaseService<ShippingOptionService
|
|||||||
}
|
}
|
||||||
|
|
||||||
return option
|
return option
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -89,25 +89,17 @@ class StoreService extends TransactionBaseService<StoreService> {
|
|||||||
* @return the store
|
* @return the store
|
||||||
*/
|
*/
|
||||||
async retrieve(config: FindConfig<Store> = {}): Promise<Store> {
|
async retrieve(config: FindConfig<Store> = {}): Promise<Store> {
|
||||||
return await this.atomicPhase_(
|
const manager = this.manager_
|
||||||
async (transactionManager: EntityManager) => {
|
const storeRepo = manager.getCustomRepository(this.storeRepository_)
|
||||||
const storeRepo = transactionManager.getCustomRepository(
|
|
||||||
this.storeRepository_
|
|
||||||
)
|
|
||||||
const query = buildQuery({}, config)
|
const query = buildQuery({}, config)
|
||||||
const store = await storeRepo.findOne(query)
|
const store = await storeRepo.findOne(query)
|
||||||
|
|
||||||
if (!store) {
|
if (!store) {
|
||||||
throw new MedusaError(
|
throw new MedusaError(MedusaError.Types.NOT_FOUND, "Store does not exist")
|
||||||
MedusaError.Types.NOT_FOUND,
|
|
||||||
"Store does not exist"
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return store
|
return store
|
||||||
}
|
}
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
protected getDefaultCurrency_(code: string): Partial<Currency> {
|
protected getDefaultCurrency_(code: string): Partial<Currency> {
|
||||||
const currencyObject = currencies[code.toUpperCase()]
|
const currencyObject = currencies[code.toUpperCase()]
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ class TaxRateService extends BaseService {
|
|||||||
id: string,
|
id: string,
|
||||||
config: FindConfig<TaxRate> = {}
|
config: FindConfig<TaxRate> = {}
|
||||||
): Promise<TaxRate> {
|
): Promise<TaxRate> {
|
||||||
return await this.atomicPhase_(async (manager: EntityManager) => {
|
const manager = this.manager_
|
||||||
const taxRateRepo = manager.getCustomRepository(this.taxRateRepository_)
|
const taxRateRepo = manager.getCustomRepository(this.taxRateRepository_)
|
||||||
const query = this.buildQuery_({ id }, config)
|
const query = this.buildQuery_({ id }, config)
|
||||||
|
|
||||||
@@ -98,7 +98,6 @@ class TaxRateService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return taxRate
|
return taxRate
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(data: CreateTaxRateInput): Promise<TaxRate> {
|
async create(data: CreateTaxRateInput): Promise<TaxRate> {
|
||||||
@@ -323,17 +322,15 @@ class TaxRateService extends BaseService {
|
|||||||
config: TaxRateListByConfig
|
config: TaxRateListByConfig
|
||||||
): Promise<TaxRate[]> {
|
): Promise<TaxRate[]> {
|
||||||
// Check both ProductTaxRate + ProductTypeTaxRate
|
// Check both ProductTaxRate + ProductTypeTaxRate
|
||||||
return await this.atomicPhase_(async (manager: EntityManager) => {
|
const manager = this.manager_
|
||||||
const taxRateRepo = manager.getCustomRepository(this.taxRateRepository_)
|
const taxRateRepo = manager.getCustomRepository(this.taxRateRepository_)
|
||||||
return await taxRateRepo.listByProduct(productId, config)
|
return await taxRateRepo.listByProduct(productId, config)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async listByShippingOption(shippingOptionId: string): Promise<TaxRate[]> {
|
async listByShippingOption(shippingOptionId: string): Promise<TaxRate[]> {
|
||||||
return await this.atomicPhase_(async (manager: EntityManager) => {
|
const manager = this.manager_
|
||||||
const taxRateRepo = manager.getCustomRepository(this.taxRateRepository_)
|
const taxRateRepo = manager.getCustomRepository(this.taxRateRepository_)
|
||||||
return await taxRateRepo.listByShippingOption(shippingOptionId)
|
return await taxRateRepo.listByShippingOption(shippingOptionId)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -71,12 +71,9 @@ class UserService extends TransactionBaseService<UserService> {
|
|||||||
* @return {Promise} the result of the find operation
|
* @return {Promise} the result of the find operation
|
||||||
*/
|
*/
|
||||||
async list(selector: FilterableUserProps, config = {}): Promise<User[]> {
|
async list(selector: FilterableUserProps, config = {}): Promise<User[]> {
|
||||||
return await this.atomicPhase_(async (transactionManager) => {
|
const manager = this.manager_
|
||||||
const userRepo = transactionManager.getCustomRepository(
|
const userRepo = manager.getCustomRepository(this.userRepository_)
|
||||||
this.userRepository_
|
|
||||||
)
|
|
||||||
return await userRepo.find(buildQuery(selector, config))
|
return await userRepo.find(buildQuery(selector, config))
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -87,10 +84,8 @@ class UserService extends TransactionBaseService<UserService> {
|
|||||||
* @return {Promise<User>} the user document.
|
* @return {Promise<User>} the user document.
|
||||||
*/
|
*/
|
||||||
async retrieve(userId: string, config: FindConfig<User> = {}): Promise<User> {
|
async retrieve(userId: string, config: FindConfig<User> = {}): Promise<User> {
|
||||||
return await this.atomicPhase_(async (transactionManager) => {
|
const manager = this.manager_
|
||||||
const userRepo = transactionManager.getCustomRepository(
|
const userRepo = manager.getCustomRepository(this.userRepository_)
|
||||||
this.userRepository_
|
|
||||||
)
|
|
||||||
const query = buildQuery({ id: userId }, config)
|
const query = buildQuery({ id: userId }, config)
|
||||||
|
|
||||||
const user = await userRepo.findOne(query)
|
const user = await userRepo.findOne(query)
|
||||||
@@ -103,7 +98,6 @@ class UserService extends TransactionBaseService<UserService> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return user
|
return user
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -117,10 +111,8 @@ class UserService extends TransactionBaseService<UserService> {
|
|||||||
apiToken: string,
|
apiToken: string,
|
||||||
relations: string[] = []
|
relations: string[] = []
|
||||||
): Promise<User> {
|
): Promise<User> {
|
||||||
return await this.atomicPhase_(async (transactionManager) => {
|
const manager = this.manager_
|
||||||
const userRepo = transactionManager.getCustomRepository(
|
const userRepo = manager.getCustomRepository(this.userRepository_)
|
||||||
this.userRepository_
|
|
||||||
)
|
|
||||||
|
|
||||||
const user = await userRepo.findOne({
|
const user = await userRepo.findOne({
|
||||||
where: { api_token: apiToken },
|
where: { api_token: apiToken },
|
||||||
@@ -135,7 +127,6 @@ class UserService extends TransactionBaseService<UserService> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return user
|
return user
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -149,10 +140,8 @@ class UserService extends TransactionBaseService<UserService> {
|
|||||||
email: string,
|
email: string,
|
||||||
config: FindConfig<User> = {}
|
config: FindConfig<User> = {}
|
||||||
): Promise<User> {
|
): Promise<User> {
|
||||||
return await this.atomicPhase_(async (transactionManager) => {
|
const manager = this.manager_
|
||||||
const userRepo = transactionManager.getCustomRepository(
|
const userRepo = manager.getCustomRepository(this.userRepository_)
|
||||||
this.userRepository_
|
|
||||||
)
|
|
||||||
|
|
||||||
const query = buildQuery({ email: email.toLowerCase() }, config)
|
const query = buildQuery({ email: email.toLowerCase() }, config)
|
||||||
const user = await userRepo.findOne(query)
|
const user = await userRepo.findOne(query)
|
||||||
@@ -165,7 +154,6 @@ class UserService extends TransactionBaseService<UserService> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return user
|
return user
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in new issue
Block a user