chore(medusa): Clean up atomicPhase usage (#1850)
This commit is contained in:
@@ -86,9 +86,7 @@ describe("CustomShippingOptionService", () => {
|
||||
|
||||
describe("create", () => {
|
||||
const customShippingOptionRepository = MockRepository({
|
||||
create: jest
|
||||
.fn()
|
||||
.mockImplementation((f) => Promise.resolve({ id: "test-cso", ...f })),
|
||||
create: jest.fn().mockImplementation((f) => ({ id: "test-cso", ...f })),
|
||||
save: jest.fn().mockImplementation((f) => Promise.resolve(f)),
|
||||
})
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ describe("DiscountService", () => {
|
||||
id: IdMap.getId("france"),
|
||||
}
|
||||
},
|
||||
withTransaction: function() {
|
||||
withTransaction: function () {
|
||||
return this
|
||||
},
|
||||
}
|
||||
@@ -762,7 +762,7 @@ describe("DiscountService", () => {
|
||||
|
||||
beforeEach(async () => {
|
||||
discountService = new DiscountService({
|
||||
manager: MockManager
|
||||
manager: MockManager,
|
||||
})
|
||||
const hasReachedLimitMock = jest.fn().mockImplementation(() => false)
|
||||
const isDisabledMock = jest.fn().mockImplementation(() => false)
|
||||
@@ -1067,7 +1067,7 @@ describe("DiscountService", () => {
|
||||
})
|
||||
|
||||
const discountService = new DiscountService({
|
||||
manager: MockManager
|
||||
manager: MockManager,
|
||||
})
|
||||
discountService.retrieve = retrieveMock
|
||||
|
||||
@@ -1161,6 +1161,9 @@ describe("DiscountService", () => {
|
||||
}
|
||||
|
||||
const customerService = {
|
||||
withTransaction: function () {
|
||||
return this
|
||||
},
|
||||
retrieve: jest.fn().mockImplementation((id) => {
|
||||
if (id === "customer-no-groups") {
|
||||
return Promise.resolve({ id: "customer-no-groups" })
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
FilterableBatchJobProps,
|
||||
} from "../types/batch-job"
|
||||
import { FindConfig } from "../types/common"
|
||||
import { AbstractBatchJobStrategy, TransactionBaseService } from "../interfaces"
|
||||
import { TransactionBaseService } from "../interfaces"
|
||||
import { buildQuery } from "../utils"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import { EventBusService, StrategyResolverService } from "./index"
|
||||
@@ -113,41 +113,31 @@ class BatchJobService extends TransactionBaseService<BatchJobService> {
|
||||
batchJobId: string,
|
||||
config: FindConfig<BatchJob> = {}
|
||||
): Promise<BatchJob | never> {
|
||||
return await this.atomicPhase_(
|
||||
async (transactionManager: EntityManager) => {
|
||||
const batchJobRepo = transactionManager.getCustomRepository(
|
||||
this.batchJobRepository_
|
||||
)
|
||||
const manager = this.manager_
|
||||
const batchJobRepo = manager.getCustomRepository(this.batchJobRepository_)
|
||||
|
||||
const query = buildQuery({ id: batchJobId }, config)
|
||||
const batchJob = await batchJobRepo.findOne(query)
|
||||
const query = buildQuery({ id: batchJobId }, config)
|
||||
const batchJob = await batchJobRepo.findOne(query)
|
||||
|
||||
if (!batchJob) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Batch job with id ${batchJobId} was not found`
|
||||
)
|
||||
}
|
||||
if (!batchJob) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Batch job with id ${batchJobId} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
return batchJob
|
||||
}
|
||||
)
|
||||
return batchJob
|
||||
}
|
||||
|
||||
async listAndCount(
|
||||
selector: FilterableBatchJobProps = {},
|
||||
config: FindConfig<BatchJob> = { skip: 0, take: 20 }
|
||||
): Promise<[BatchJob[], number]> {
|
||||
return await this.atomicPhase_(
|
||||
async (manager: EntityManager): Promise<[BatchJob[], number]> => {
|
||||
const batchJobRepo = manager.getCustomRepository(
|
||||
this.batchJobRepository_
|
||||
)
|
||||
const manager = this.manager_
|
||||
const batchJobRepo = manager.getCustomRepository(this.batchJobRepository_)
|
||||
|
||||
const query = buildQuery(selector, config)
|
||||
return await batchJobRepo.findAndCount(query)
|
||||
}
|
||||
)
|
||||
const query = buildQuery(selector, config)
|
||||
return await batchJobRepo.findAndCount(query)
|
||||
}
|
||||
|
||||
async create(data: BatchJobCreateProps): Promise<BatchJob> {
|
||||
|
||||
@@ -260,16 +260,11 @@ class CartService extends TransactionBaseService<CartService> {
|
||||
selector: FilterableCartProps,
|
||||
config: FindConfig<Cart> = {}
|
||||
): Promise<Cart[]> {
|
||||
return await this.atomicPhase_(
|
||||
async (transactionManager: EntityManager) => {
|
||||
const cartRepo = transactionManager.getCustomRepository(
|
||||
this.cartRepository_
|
||||
)
|
||||
const manager = this.manager_
|
||||
const cartRepo = manager.getCustomRepository(this.cartRepository_)
|
||||
|
||||
const query = buildQuery(selector, config)
|
||||
return await cartRepo.find(query)
|
||||
}
|
||||
)
|
||||
const query = buildQuery(selector, config)
|
||||
return await cartRepo.find(query)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -284,46 +279,41 @@ class CartService extends TransactionBaseService<CartService> {
|
||||
options: FindConfig<Cart> = {},
|
||||
totalsConfig: TotalsConfig = {}
|
||||
): Promise<Cart> {
|
||||
return await this.atomicPhase_(
|
||||
async (transactionManager: EntityManager) => {
|
||||
const cartRepo = transactionManager.getCustomRepository(
|
||||
this.cartRepository_
|
||||
)
|
||||
const validatedId = validateId(cartId)
|
||||
const manager = this.manager_
|
||||
const cartRepo = manager.getCustomRepository(this.cartRepository_)
|
||||
const validatedId = validateId(cartId)
|
||||
|
||||
const { select, relations, totalsToSelect } =
|
||||
this.transformQueryForTotals_(options)
|
||||
const { select, relations, totalsToSelect } =
|
||||
this.transformQueryForTotals_(options)
|
||||
|
||||
const query = buildQuery(
|
||||
{ id: validatedId },
|
||||
{ ...options, select, relations }
|
||||
)
|
||||
|
||||
if (relations && relations.length > 0) {
|
||||
query.relations = relations
|
||||
}
|
||||
|
||||
if (select && select.length > 0) {
|
||||
query.select = select
|
||||
} else {
|
||||
query.select = undefined
|
||||
}
|
||||
|
||||
const queryRelations = query.relations
|
||||
query.relations = undefined
|
||||
|
||||
const raw = await cartRepo.findOneWithRelations(queryRelations, query)
|
||||
|
||||
if (!raw) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Cart with ${cartId} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
return await this.decorateTotals_(raw, totalsToSelect, totalsConfig)
|
||||
}
|
||||
const query = buildQuery(
|
||||
{ id: validatedId },
|
||||
{ ...options, select, relations }
|
||||
)
|
||||
|
||||
if (relations && relations.length > 0) {
|
||||
query.relations = relations
|
||||
}
|
||||
|
||||
if (select && select.length > 0) {
|
||||
query.select = select
|
||||
} else {
|
||||
query.select = undefined
|
||||
}
|
||||
|
||||
const queryRelations = query.relations
|
||||
query.relations = undefined
|
||||
|
||||
const raw = await cartRepo.findOneWithRelations(queryRelations, query)
|
||||
|
||||
if (!raw) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Cart with ${cartId} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
return await this.decorateTotals_(raw, totalsToSelect, totalsConfig)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -814,15 +814,10 @@ export default class ClaimService extends TransactionBaseService<
|
||||
order: { created_at: "DESC" },
|
||||
}
|
||||
): Promise<ClaimOrder[]> {
|
||||
return await this.atomicPhase_(
|
||||
async (transactionManager: EntityManager) => {
|
||||
const claimRepo = transactionManager.getCustomRepository(
|
||||
this.claimRepository_
|
||||
)
|
||||
const query = buildQuery(selector, config)
|
||||
return await claimRepo.find(query)
|
||||
}
|
||||
)
|
||||
const manager = this.manager_
|
||||
const claimRepo = manager.getCustomRepository(this.claimRepository_)
|
||||
const query = buildQuery(selector, config)
|
||||
return await claimRepo.find(query)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -835,24 +830,19 @@ export default class ClaimService extends TransactionBaseService<
|
||||
id: string,
|
||||
config: FindConfig<ClaimOrder> = {}
|
||||
): Promise<ClaimOrder> {
|
||||
return await this.atomicPhase_(
|
||||
async (transactionManager: EntityManager) => {
|
||||
const claimRepo = transactionManager.getCustomRepository(
|
||||
this.claimRepository_
|
||||
)
|
||||
const manager = this.manager_
|
||||
const claimRepo = manager.getCustomRepository(this.claimRepository_)
|
||||
|
||||
const query = buildQuery({ id }, config)
|
||||
const claim = await claimRepo.findOne(query)
|
||||
const query = buildQuery({ id }, config)
|
||||
const claim = await claimRepo.findOne(query)
|
||||
|
||||
if (!claim) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Claim with ${id} was not found`
|
||||
)
|
||||
}
|
||||
if (!claim) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Claim with ${id} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
return claim
|
||||
}
|
||||
)
|
||||
return claim
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,24 +37,23 @@ class CustomShippingOptionService extends TransactionBaseService<CustomShippingO
|
||||
id: string,
|
||||
config: FindConfig<CustomShippingOption> = {}
|
||||
): Promise<CustomShippingOption> {
|
||||
return await this.atomicPhase_(async (manager) => {
|
||||
const customShippingOptionRepo = manager.getCustomRepository(
|
||||
this.customShippingOptionRepository_
|
||||
const manager = this.manager_
|
||||
const customShippingOptionRepo = manager.getCustomRepository(
|
||||
this.customShippingOptionRepository_
|
||||
)
|
||||
|
||||
const query = buildQuery({ id }, config)
|
||||
|
||||
const customShippingOption = await customShippingOptionRepo.findOne(query)
|
||||
|
||||
if (!customShippingOption) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Custom shipping option with id: ${id} was not found.`
|
||||
)
|
||||
}
|
||||
|
||||
const query = buildQuery({ id }, config)
|
||||
|
||||
const customShippingOption = await customShippingOptionRepo.findOne(query)
|
||||
|
||||
if (!customShippingOption) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Custom shipping option with id: ${id} was not found.`
|
||||
)
|
||||
}
|
||||
|
||||
return customShippingOption
|
||||
})
|
||||
return customShippingOption
|
||||
}
|
||||
|
||||
/** Fetches all custom shipping options based on the given selector
|
||||
@@ -70,15 +69,14 @@ class CustomShippingOptionService extends TransactionBaseService<CustomShippingO
|
||||
relations: [],
|
||||
}
|
||||
): Promise<CustomShippingOption[]> {
|
||||
return await this.atomicPhase_(async (manager) => {
|
||||
const customShippingOptionRepo = manager.getCustomRepository(
|
||||
this.customShippingOptionRepository_
|
||||
)
|
||||
const manager = this.manager_
|
||||
const customShippingOptionRepo = manager.getCustomRepository(
|
||||
this.customShippingOptionRepository_
|
||||
)
|
||||
|
||||
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> {
|
||||
const { cart_id, shipping_option_id, price, metadata } = data
|
||||
|
||||
return await this.atomicPhase_(async (manager) => {
|
||||
const customShippingOptionRepo = manager.getCustomRepository(
|
||||
this.customShippingOptionRepository_
|
||||
)
|
||||
const manager = this.manager_
|
||||
const customShippingOptionRepo = manager.getCustomRepository(
|
||||
this.customShippingOptionRepository_
|
||||
)
|
||||
|
||||
const customShippingOption = await customShippingOptionRepo.create({
|
||||
cart_id,
|
||||
shipping_option_id,
|
||||
price,
|
||||
metadata,
|
||||
})
|
||||
return await customShippingOptionRepo.save(customShippingOption)
|
||||
const customShippingOption = customShippingOptionRepo.create({
|
||||
cart_id,
|
||||
shipping_option_id,
|
||||
price,
|
||||
metadata,
|
||||
})
|
||||
return await customShippingOptionRepo.save(customShippingOption)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import jwt from "jsonwebtoken"
|
||||
import _ from "lodash"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import Scrypt from "scrypt-kdf"
|
||||
import { DeepPartial, EntityManager } from "typeorm"
|
||||
@@ -109,20 +108,19 @@ class CustomerService extends TransactionBaseService<CustomerService> {
|
||||
selector: Selector<Customer> & { q?: string } = {},
|
||||
config: FindConfig<Customer> = { relations: [], skip: 0, take: 50 }
|
||||
): Promise<Customer[]> {
|
||||
return await this.atomicPhase_(async (manager) => {
|
||||
const customerRepo = manager.getCustomRepository(this.customerRepository_)
|
||||
const manager = this.manager_
|
||||
const customerRepo = manager.getCustomRepository(this.customerRepository_)
|
||||
|
||||
let q
|
||||
if ("q" in selector) {
|
||||
q = selector.q
|
||||
delete selector.q
|
||||
}
|
||||
let q
|
||||
if ("q" in selector) {
|
||||
q = selector.q
|
||||
delete selector.q
|
||||
}
|
||||
|
||||
const query = buildQuery<Selector<Customer>, Customer>(selector, config)
|
||||
const query = buildQuery<Selector<Customer>, Customer>(selector, config)
|
||||
|
||||
const [customers] = await customerRepo.listAndCount(query, q)
|
||||
return customers
|
||||
})
|
||||
const [customers] = await customerRepo.listAndCount(query, q)
|
||||
return customers
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -139,19 +137,18 @@ class CustomerService extends TransactionBaseService<CustomerService> {
|
||||
order: { created_at: "DESC" },
|
||||
}
|
||||
): Promise<[Customer[], number]> {
|
||||
return await this.atomicPhase_(async (manager) => {
|
||||
const customerRepo = manager.getCustomRepository(this.customerRepository_)
|
||||
const manager = this.manager_
|
||||
const customerRepo = manager.getCustomRepository(this.customerRepository_)
|
||||
|
||||
let q
|
||||
if ("q" in selector) {
|
||||
q = selector.q
|
||||
delete selector.q
|
||||
}
|
||||
let q
|
||||
if ("q" in selector) {
|
||||
q = selector.q
|
||||
delete selector.q
|
||||
}
|
||||
|
||||
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
|
||||
*/
|
||||
async count(): Promise<number> {
|
||||
return await this.atomicPhase_(async (manager) => {
|
||||
const customerRepo = manager.getCustomRepository(this.customerRepository_)
|
||||
return await customerRepo.count({})
|
||||
})
|
||||
const manager = this.manager_
|
||||
const customerRepo = manager.getCustomRepository(this.customerRepository_)
|
||||
return await customerRepo.count({})
|
||||
}
|
||||
|
||||
private async retrieve_(
|
||||
@@ -199,9 +195,7 @@ class CustomerService extends TransactionBaseService<CustomerService> {
|
||||
email: string,
|
||||
config: FindConfig<Customer> = {}
|
||||
): 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,
|
||||
config: FindConfig<Customer> = {}
|
||||
): 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,
|
||||
config: FindConfig<Customer> = {}
|
||||
): Promise<Customer> {
|
||||
return await this.atomicPhase_(async () => {
|
||||
return this.retrieve_({ id: customerId }, config)
|
||||
})
|
||||
return this.retrieve_({ id: customerId }, config)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -50,24 +50,23 @@ class DiscountConditionService extends TransactionBaseService<DiscountConditionS
|
||||
conditionId: string,
|
||||
config?: FindConfig<DiscountCondition>
|
||||
): Promise<DiscountCondition | never> {
|
||||
return await this.atomicPhase_(async (manager: EntityManager) => {
|
||||
const conditionRepo = manager.getCustomRepository(
|
||||
this.discountConditionRepository_
|
||||
const manager = this.manager_
|
||||
const conditionRepo = manager.getCustomRepository(
|
||||
this.discountConditionRepository_
|
||||
)
|
||||
|
||||
const query = buildQuery({ id: conditionId }, config)
|
||||
|
||||
const condition = await conditionRepo.findOne(query)
|
||||
|
||||
if (!condition) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`DiscountCondition with id ${conditionId} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
const query = buildQuery({ id: conditionId }, config)
|
||||
|
||||
const condition = await conditionRepo.findOne(query)
|
||||
|
||||
if (!condition) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`DiscountCondition with id ${conditionId} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
return condition
|
||||
})
|
||||
return condition
|
||||
}
|
||||
|
||||
protected static resolveConditionType_(data: UpsertDiscountConditionInput):
|
||||
|
||||
@@ -115,14 +115,11 @@ class DiscountService extends TransactionBaseService<DiscountService> {
|
||||
selector: FilterableDiscountProps = {},
|
||||
config: FindConfig<Discount> = { relations: [], skip: 0, take: 10 }
|
||||
): Promise<Discount[]> {
|
||||
return await this.atomicPhase_(async (transactionManager) => {
|
||||
const discountRepo = transactionManager.getCustomRepository(
|
||||
this.discountRepository_
|
||||
)
|
||||
const manager = this.manager_
|
||||
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
||||
|
||||
const query = buildQuery(selector as Selector<Discount>, config)
|
||||
return await discountRepo.find(query)
|
||||
})
|
||||
const query = buildQuery(selector as Selector<Discount>, config)
|
||||
return await discountRepo.find(query)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -138,39 +135,36 @@ class DiscountService extends TransactionBaseService<DiscountService> {
|
||||
order: { created_at: "DESC" },
|
||||
}
|
||||
): Promise<[Discount[], number]> {
|
||||
return await this.atomicPhase_(async (transactionManager) => {
|
||||
const discountRepo = transactionManager.getCustomRepository(
|
||||
this.discountRepository_
|
||||
)
|
||||
const manager = this.manager_
|
||||
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
||||
|
||||
let q
|
||||
if ("q" in selector) {
|
||||
q = selector.q
|
||||
delete selector.q
|
||||
let q
|
||||
if ("q" in selector) {
|
||||
q = selector.q
|
||||
delete selector.q
|
||||
}
|
||||
|
||||
const query = buildQuery(selector as Selector<Discount>, config)
|
||||
|
||||
if (q) {
|
||||
const where = query.where
|
||||
|
||||
delete where.code
|
||||
|
||||
query.where = (qb: SelectQueryBuilder<Discount>): void => {
|
||||
qb.where(where)
|
||||
|
||||
qb.andWhere(
|
||||
new Brackets((qb) => {
|
||||
qb.where({ code: ILike(`%${q}%`) })
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const query = buildQuery(selector as Selector<Discount>, config)
|
||||
const [discounts, count] = await discountRepo.findAndCount(query)
|
||||
|
||||
if (q) {
|
||||
const where = query.where
|
||||
|
||||
delete where.code
|
||||
|
||||
query.where = (qb: SelectQueryBuilder<Discount>): void => {
|
||||
qb.where(where)
|
||||
|
||||
qb.andWhere(
|
||||
new Brackets((qb) => {
|
||||
qb.where({ code: ILike(`%${q}%`) })
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const [discounts, count] = await discountRepo.findAndCount(query)
|
||||
|
||||
return [discounts, count]
|
||||
})
|
||||
return [discounts, count]
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -248,23 +242,20 @@ class DiscountService extends TransactionBaseService<DiscountService> {
|
||||
discountId: string,
|
||||
config: FindConfig<Discount> = {}
|
||||
): Promise<Discount> {
|
||||
return await this.atomicPhase_(async (transactionManager) => {
|
||||
const discountRepo = transactionManager.getCustomRepository(
|
||||
this.discountRepository_
|
||||
const manager = this.manager_
|
||||
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
||||
|
||||
const query = buildQuery({ id: discountId }, config)
|
||||
const discount = await discountRepo.findOne(query)
|
||||
|
||||
if (!discount) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Discount with ${discountId} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
const query = buildQuery({ id: discountId }, config)
|
||||
const discount = await discountRepo.findOne(query)
|
||||
|
||||
if (!discount) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Discount with ${discountId} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
return discount
|
||||
})
|
||||
return discount
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -277,28 +268,25 @@ class DiscountService extends TransactionBaseService<DiscountService> {
|
||||
discountCode: string,
|
||||
config: FindConfig<Discount> = {}
|
||||
): Promise<Discount> {
|
||||
return await this.atomicPhase_(async (transactionManager) => {
|
||||
const discountRepo = transactionManager.getCustomRepository(
|
||||
this.discountRepository_
|
||||
)
|
||||
const manager = this.manager_
|
||||
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
||||
|
||||
let query = buildQuery({ code: discountCode, is_dynamic: false }, config)
|
||||
let discount = await discountRepo.findOne(query)
|
||||
let query = buildQuery({ code: discountCode, is_dynamic: false }, config)
|
||||
let discount = await discountRepo.findOne(query)
|
||||
|
||||
if (!discount) {
|
||||
query = buildQuery({ code: discountCode, is_dynamic: true }, config)
|
||||
discount = await discountRepo.findOne(query)
|
||||
|
||||
if (!discount) {
|
||||
query = buildQuery({ code: discountCode, is_dynamic: true }, config)
|
||||
discount = await discountRepo.findOne(query)
|
||||
|
||||
if (!discount) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Discount with code ${discountCode} was not found`
|
||||
)
|
||||
}
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Discount with code ${discountCode} was not found`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return discount
|
||||
})
|
||||
return discount
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -562,9 +550,11 @@ class DiscountService extends TransactionBaseService<DiscountService> {
|
||||
return false
|
||||
}
|
||||
|
||||
const product = await this.productService_.retrieve(productId, {
|
||||
relations: ["tags"],
|
||||
})
|
||||
const product = await this.productService_
|
||||
.withTransaction(manager)
|
||||
.retrieve(productId, {
|
||||
relations: ["tags"],
|
||||
})
|
||||
|
||||
return await discountConditionRepo.isValidForProduct(
|
||||
discountRuleId,
|
||||
@@ -721,7 +711,7 @@ class DiscountService extends TransactionBaseService<DiscountService> {
|
||||
discountRuleId: string,
|
||||
customerId: string | undefined
|
||||
): Promise<boolean> {
|
||||
return await this.atomicPhase_(async (manager) => {
|
||||
return await this.atomicPhase_(async (manager: EntityManager) => {
|
||||
const discountConditionRepo: DiscountConditionRepository =
|
||||
manager.getCustomRepository(this.discountConditionRepository_)
|
||||
|
||||
@@ -730,9 +720,11 @@ class DiscountService extends TransactionBaseService<DiscountService> {
|
||||
return false
|
||||
}
|
||||
|
||||
const customer = await this.customerService_.retrieve(customerId, {
|
||||
relations: ["groups"],
|
||||
})
|
||||
const customer = await this.customerService_
|
||||
.withTransaction(manager)
|
||||
.retrieve(customerId, {
|
||||
relations: ["groups"],
|
||||
})
|
||||
|
||||
return await discountConditionRepo.canApplyForCustomer(
|
||||
discountRuleId,
|
||||
|
||||
@@ -92,24 +92,21 @@ class DraftOrderService extends TransactionBaseService<DraftOrderService> {
|
||||
id: string,
|
||||
config: FindConfig<DraftOrder> = {}
|
||||
): Promise<DraftOrder | never> {
|
||||
return await this.atomicPhase_(
|
||||
async (transactionManager: EntityManager) => {
|
||||
const draftOrderRepo = transactionManager.getCustomRepository(
|
||||
this.draftOrderRepository_
|
||||
)
|
||||
|
||||
const query = buildQuery({ id }, config)
|
||||
const draftOrder = await draftOrderRepo.findOne(query)
|
||||
if (!draftOrder) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Draft order with ${id} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
return draftOrder
|
||||
}
|
||||
const manager = this.manager_
|
||||
const draftOrderRepo = manager.getCustomRepository(
|
||||
this.draftOrderRepository_
|
||||
)
|
||||
|
||||
const query = buildQuery({ id }, config)
|
||||
const draftOrder = await draftOrderRepo.findOne(query)
|
||||
if (!draftOrder) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Draft order with ${id} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
return draftOrder
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -122,24 +119,21 @@ class DraftOrderService extends TransactionBaseService<DraftOrderService> {
|
||||
cartId: string,
|
||||
config: FindConfig<DraftOrder> = {}
|
||||
): Promise<DraftOrder | never> {
|
||||
return await this.atomicPhase_(
|
||||
async (transactionManager: EntityManager) => {
|
||||
const draftOrderRepo = transactionManager.getCustomRepository(
|
||||
this.draftOrderRepository_
|
||||
)
|
||||
|
||||
const query = buildQuery({ cart_id: cartId }, config)
|
||||
const draftOrder = await draftOrderRepo.findOne(query)
|
||||
if (!draftOrder) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Draft order was not found`
|
||||
)
|
||||
}
|
||||
|
||||
return draftOrder
|
||||
}
|
||||
const manager = this.manager_
|
||||
const draftOrderRepo = manager.getCustomRepository(
|
||||
this.draftOrderRepository_
|
||||
)
|
||||
|
||||
const query = buildQuery({ cart_id: cartId }, config)
|
||||
const draftOrder = await draftOrderRepo.findOne(query)
|
||||
if (!draftOrder) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Draft order was not found`
|
||||
)
|
||||
}
|
||||
|
||||
return draftOrder
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -179,47 +173,44 @@ class DraftOrderService extends TransactionBaseService<DraftOrderService> {
|
||||
order: { created_at: "DESC" },
|
||||
}
|
||||
): Promise<[DraftOrder[], number]> {
|
||||
return await this.atomicPhase_(
|
||||
async (transactionManager: EntityManager) => {
|
||||
const draftOrderRepository = transactionManager.getCustomRepository(
|
||||
this.draftOrderRepository_
|
||||
)
|
||||
|
||||
const { q, ...restSelector } = selector
|
||||
const query = buildQuery(
|
||||
restSelector,
|
||||
config
|
||||
) as FindManyOptions<DraftOrder> & ExtendedFindConfig<DraftOrder>
|
||||
|
||||
if (q) {
|
||||
const where = query.where
|
||||
delete where?.display_id
|
||||
|
||||
query.join = {
|
||||
alias: "draft_order",
|
||||
innerJoin: {
|
||||
cart: "draft_order.cart",
|
||||
},
|
||||
}
|
||||
|
||||
query.where = (qb): void => {
|
||||
qb.where(where)
|
||||
|
||||
qb.andWhere(
|
||||
new Brackets((qb) => {
|
||||
qb.where(`cart.email ILIKE :q`, {
|
||||
q: `%${q}%`,
|
||||
}).orWhere(`draft_order.display_id::TEXT ILIKE :displayId`, {
|
||||
displayId: `${q}`,
|
||||
})
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return await draftOrderRepository.findAndCount(query)
|
||||
}
|
||||
const manager = this.manager_
|
||||
const draftOrderRepository = manager.getCustomRepository(
|
||||
this.draftOrderRepository_
|
||||
)
|
||||
|
||||
const { q, ...restSelector } = selector
|
||||
const query = buildQuery(
|
||||
restSelector,
|
||||
config
|
||||
) as FindManyOptions<DraftOrder> & ExtendedFindConfig<DraftOrder>
|
||||
|
||||
if (q) {
|
||||
const where = query.where
|
||||
delete where?.display_id
|
||||
|
||||
query.join = {
|
||||
alias: "draft_order",
|
||||
innerJoin: {
|
||||
cart: "draft_order.cart",
|
||||
},
|
||||
}
|
||||
|
||||
query.where = (qb): void => {
|
||||
qb.where(where)
|
||||
|
||||
qb.andWhere(
|
||||
new Brackets((qb) => {
|
||||
qb.where(`cart.email ILIKE :q`, {
|
||||
q: `%${q}%`,
|
||||
}).orWhere(`draft_order.display_id::TEXT ILIKE :displayId`, {
|
||||
displayId: `${q}`,
|
||||
})
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return await draftOrderRepository.findAndCount(query)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -236,17 +227,14 @@ class DraftOrderService extends TransactionBaseService<DraftOrderService> {
|
||||
order: { created_at: "DESC" },
|
||||
}
|
||||
): Promise<DraftOrder[]> {
|
||||
return await this.atomicPhase_(
|
||||
async (transactionManager: EntityManager) => {
|
||||
const draftOrderRepo = transactionManager.getCustomRepository(
|
||||
this.draftOrderRepository_
|
||||
)
|
||||
|
||||
const query = buildQuery(selector, config)
|
||||
|
||||
return await draftOrderRepo.find(query)
|
||||
}
|
||||
const manager = this.manager_
|
||||
const draftOrderRepo = manager.getCustomRepository(
|
||||
this.draftOrderRepository_
|
||||
)
|
||||
|
||||
const query = buildQuery(selector, config)
|
||||
|
||||
return await draftOrderRepo.find(query)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -167,23 +167,22 @@ class FulfillmentService extends TransactionBaseService<FulfillmentService> {
|
||||
id: string,
|
||||
config: FindConfig<Fulfillment> = {}
|
||||
): Promise<Fulfillment> {
|
||||
return await this.atomicPhase_(async (manager) => {
|
||||
const fulfillmentRepository = manager.getCustomRepository(
|
||||
this.fulfillmentRepository_
|
||||
const manager = this.manager_
|
||||
const fulfillmentRepository = manager.getCustomRepository(
|
||||
this.fulfillmentRepository_
|
||||
)
|
||||
|
||||
const query = buildQuery({ id }, config)
|
||||
|
||||
const fulfillment = await fulfillmentRepository.findOne(query)
|
||||
|
||||
if (!fulfillment) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Fulfillment with id: ${id} was not found`
|
||||
)
|
||||
|
||||
const query = buildQuery({ id }, config)
|
||||
|
||||
const fulfillment = await fulfillmentRepository.findOne(query)
|
||||
|
||||
if (!fulfillment) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Fulfillment with id: ${id} was not found`
|
||||
)
|
||||
}
|
||||
return fulfillment
|
||||
})
|
||||
}
|
||||
return fulfillment
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -85,25 +85,24 @@ class GiftCardService extends TransactionBaseService<GiftCardService> {
|
||||
selector: QuerySelector<GiftCard> = {},
|
||||
config: FindConfig<GiftCard> = { relations: [], skip: 0, take: 10 }
|
||||
): Promise<[GiftCard[], number]> {
|
||||
return await this.atomicPhase_(async (manager) => {
|
||||
const giftCardRepo = manager.getCustomRepository(this.giftCardRepository_)
|
||||
const manager = this.manager_
|
||||
const giftCardRepo = manager.getCustomRepository(this.giftCardRepository_)
|
||||
|
||||
let q: string | undefined
|
||||
if (typeof selector.q !== "undefined") {
|
||||
q = selector.q
|
||||
delete selector.q
|
||||
}
|
||||
let q: string | undefined
|
||||
if (typeof selector.q !== "undefined") {
|
||||
q = selector.q
|
||||
delete selector.q
|
||||
}
|
||||
|
||||
const query: ExtendedFindConfig<
|
||||
GiftCard,
|
||||
QuerySelector<GiftCard>
|
||||
> = buildQuery<QuerySelector<GiftCard>, GiftCard>(selector, config)
|
||||
const query: ExtendedFindConfig<
|
||||
GiftCard,
|
||||
QuerySelector<GiftCard>
|
||||
> = buildQuery<QuerySelector<GiftCard>, GiftCard>(selector, config)
|
||||
|
||||
const rels = query.relations
|
||||
delete query.relations
|
||||
const rels = query.relations
|
||||
delete query.relations
|
||||
|
||||
return await giftCardRepo.listGiftCardsAndCount(query, rels, q)
|
||||
})
|
||||
return await giftCardRepo.listGiftCardsAndCount(query, rels, q)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -115,36 +114,34 @@ class GiftCardService extends TransactionBaseService<GiftCardService> {
|
||||
selector: QuerySelector<GiftCard> = {},
|
||||
config: FindConfig<GiftCard> = { relations: [], skip: 0, take: 10 }
|
||||
): Promise<GiftCard[]> {
|
||||
return await this.atomicPhase_(async (manager) => {
|
||||
const giftCardRepo = manager.getCustomRepository(this.giftCardRepository_)
|
||||
const manager = this.manager_
|
||||
const giftCardRepo = manager.getCustomRepository(this.giftCardRepository_)
|
||||
|
||||
let q: string | undefined
|
||||
if (typeof selector.q !== "undefined") {
|
||||
q = selector.q
|
||||
delete selector.q
|
||||
}
|
||||
let q: string | undefined
|
||||
if (typeof selector.q !== "undefined") {
|
||||
q = selector.q
|
||||
delete selector.q
|
||||
}
|
||||
|
||||
const query: ExtendedFindConfig<
|
||||
GiftCard,
|
||||
QuerySelector<GiftCard>
|
||||
> = buildQuery<QuerySelector<GiftCard>, GiftCard>(selector, config)
|
||||
const query: ExtendedFindConfig<
|
||||
GiftCard,
|
||||
QuerySelector<GiftCard>
|
||||
> = buildQuery<QuerySelector<GiftCard>, GiftCard>(selector, config)
|
||||
|
||||
const rels = query.relations
|
||||
delete query.relations
|
||||
const rels = query.relations
|
||||
delete query.relations
|
||||
|
||||
return await giftCardRepo.listGiftCards(query, rels, q)
|
||||
})
|
||||
return await giftCardRepo.listGiftCards(query, rels, q)
|
||||
}
|
||||
|
||||
async createTransaction(
|
||||
data: CreateGiftCardTransactionInput
|
||||
): Promise<string> {
|
||||
return await this.atomicPhase_(async (manager) => {
|
||||
const gctRepo = manager.getCustomRepository(this.giftCardTransactionRepo_)
|
||||
const created = gctRepo.create(data)
|
||||
const saved = await gctRepo.save(created)
|
||||
return saved.id
|
||||
})
|
||||
const manager = this.manager_
|
||||
const gctRepo = manager.getCustomRepository(this.giftCardTransactionRepo_)
|
||||
const created = gctRepo.create(data)
|
||||
const saved = await gctRepo.save(created)
|
||||
return saved.id
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -186,29 +183,28 @@ class GiftCardService extends TransactionBaseService<GiftCardService> {
|
||||
selector: Selector<GiftCard>,
|
||||
config: FindConfig<GiftCard> = {}
|
||||
): Promise<GiftCard> {
|
||||
return await this.atomicPhase_(async (manager) => {
|
||||
const giftCardRepo = manager.getCustomRepository(this.giftCardRepository_)
|
||||
const manager = this.manager_
|
||||
const giftCardRepo = manager.getCustomRepository(this.giftCardRepository_)
|
||||
|
||||
const { relations, ...query } = buildQuery(selector, config)
|
||||
const { relations, ...query } = buildQuery(selector, config)
|
||||
|
||||
const giftCard = await giftCardRepo.findOneWithRelations(
|
||||
relations as (keyof GiftCard)[],
|
||||
query
|
||||
const giftCard = await giftCardRepo.findOneWithRelations(
|
||||
relations as (keyof GiftCard)[],
|
||||
query
|
||||
)
|
||||
|
||||
if (!giftCard) {
|
||||
const selectorConstraints = Object.entries(selector)
|
||||
.map((key, value) => `${key}: ${value}`)
|
||||
.join(", ")
|
||||
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Gift card with ${selectorConstraints} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
if (!giftCard) {
|
||||
const selectorConstraints = Object.entries(selector)
|
||||
.map((key, value) => `${key}: ${value}`)
|
||||
.join(", ")
|
||||
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Gift card with ${selectorConstraints} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
return giftCard
|
||||
})
|
||||
return giftCard
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -221,18 +217,14 @@ class GiftCardService extends TransactionBaseService<GiftCardService> {
|
||||
giftCardId: string,
|
||||
config: FindConfig<GiftCard> = {}
|
||||
): Promise<GiftCard> {
|
||||
return await this.atomicPhase_(async () => {
|
||||
return await this.retrieve_({ id: giftCardId }, config)
|
||||
})
|
||||
return await this.retrieve_({ id: giftCardId }, config)
|
||||
}
|
||||
|
||||
async retrieveByCode(
|
||||
code: string,
|
||||
config: FindConfig<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
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -286,17 +280,16 @@ class GiftCardService extends TransactionBaseService<GiftCardService> {
|
||||
* @return the result of the delete operation
|
||||
*/
|
||||
async delete(giftCardId: string): Promise<GiftCard | void> {
|
||||
return await this.atomicPhase_(async (manager) => {
|
||||
const giftCardRepo = manager.getCustomRepository(this.giftCardRepository_)
|
||||
const manager = this.manager_
|
||||
const giftCardRepo = manager.getCustomRepository(this.giftCardRepository_)
|
||||
|
||||
const giftCard = await giftCardRepo.findOne({ where: { id: giftCardId } })
|
||||
const giftCard = await giftCardRepo.findOne({ where: { id: giftCardId } })
|
||||
|
||||
if (!giftCard) {
|
||||
return
|
||||
}
|
||||
if (!giftCard) {
|
||||
return
|
||||
}
|
||||
|
||||
return await giftCardRepo.softRemove(giftCard)
|
||||
})
|
||||
return await giftCardRepo.softRemove(giftCard)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -91,15 +91,10 @@ class LineItemService extends BaseService {
|
||||
selector,
|
||||
config = { skip: 0, take: 50, order: { created_at: "DESC" } }
|
||||
): Promise<LineItem[]> {
|
||||
return await this.atomicPhase_(
|
||||
async (transactionManager: EntityManager) => {
|
||||
const lineItemRepo = transactionManager.getCustomRepository(
|
||||
this.lineItemRepository_
|
||||
)
|
||||
const query = this.buildQuery_(selector, config)
|
||||
return await lineItemRepo.find(query)
|
||||
}
|
||||
)
|
||||
const manager = this.manager_
|
||||
const lineItemRepo = manager.getCustomRepository(this.lineItemRepository_)
|
||||
const query = this.buildQuery_(selector, config)
|
||||
return await lineItemRepo.find(query)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -109,27 +104,24 @@ class LineItemService extends BaseService {
|
||||
* @return {Promise<LineItem | never>} the line item
|
||||
*/
|
||||
async retrieve(id: string, config = {}): Promise<LineItem | never> {
|
||||
return await this.atomicPhase_(
|
||||
async (transactionManager: EntityManager) => {
|
||||
const lineItemRepository = transactionManager.getCustomRepository(
|
||||
this.lineItemRepository_
|
||||
)
|
||||
|
||||
const validatedId = this.validateId_(id)
|
||||
const query = this.buildQuery_({ id: validatedId }, config)
|
||||
|
||||
const lineItem = await lineItemRepository.findOne(query)
|
||||
|
||||
if (!lineItem) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Line item with ${id} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
return lineItem
|
||||
}
|
||||
const manager = this.manager_
|
||||
const lineItemRepository = manager.getCustomRepository(
|
||||
this.lineItemRepository_
|
||||
)
|
||||
|
||||
const validatedId = this.validateId_(id)
|
||||
const query = this.buildQuery_({ id: validatedId }, config)
|
||||
|
||||
const lineItem = await lineItemRepository.findOne(query)
|
||||
|
||||
if (!lineItem) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Line item with ${id} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
return lineItem
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -248,19 +248,18 @@ class PriceListService extends TransactionBaseService<PriceListService> {
|
||||
selector: FilterablePriceListProps = {},
|
||||
config: FindConfig<FilterablePriceListProps> = { skip: 0, take: 20 }
|
||||
): Promise<PriceList[]> {
|
||||
return await this.atomicPhase_(async (manager: EntityManager) => {
|
||||
const priceListRepo = manager.getCustomRepository(this.priceListRepo_)
|
||||
const manager = this.manager_
|
||||
const priceListRepo = manager.getCustomRepository(this.priceListRepo_)
|
||||
|
||||
const { q, ...priceListSelector } = selector
|
||||
const query = buildQuery(priceListSelector, config)
|
||||
const { q, ...priceListSelector } = selector
|
||||
const query = buildQuery(priceListSelector, config)
|
||||
|
||||
const groups = query.where.customer_groups as FindOperator<string[]>
|
||||
query.where.customer_groups = undefined
|
||||
const groups = query.where.customer_groups as FindOperator<string[]>
|
||||
query.where.customer_groups = undefined
|
||||
|
||||
const [priceLists] = await priceListRepo.listAndCount(query, groups)
|
||||
const [priceLists] = await priceListRepo.listAndCount(query, groups)
|
||||
|
||||
return priceLists
|
||||
})
|
||||
return priceLists
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -276,27 +275,26 @@ class PriceListService extends TransactionBaseService<PriceListService> {
|
||||
take: 20,
|
||||
}
|
||||
): Promise<[PriceList[], number]> {
|
||||
return await this.atomicPhase_(async (manager: EntityManager) => {
|
||||
const priceListRepo = manager.getCustomRepository(this.priceListRepo_)
|
||||
const { q, ...priceListSelector } = selector
|
||||
const { relations, ...query } = buildQuery<
|
||||
FilterablePriceListProps,
|
||||
FilterablePriceListProps
|
||||
>(priceListSelector, config)
|
||||
const manager = this.manager_
|
||||
const priceListRepo = manager.getCustomRepository(this.priceListRepo_)
|
||||
const { q, ...priceListSelector } = selector
|
||||
const { relations, ...query } = buildQuery<
|
||||
FilterablePriceListProps,
|
||||
FilterablePriceListProps
|
||||
>(priceListSelector, config)
|
||||
|
||||
const groups = query.where.customer_groups as FindOperator<string[]>
|
||||
delete query.where.customer_groups
|
||||
const groups = query.where.customer_groups as FindOperator<string[]>
|
||||
delete query.where.customer_groups
|
||||
|
||||
if (q) {
|
||||
return await priceListRepo.getFreeTextSearchResultsAndCount(
|
||||
q,
|
||||
query as PriceListFindOptions,
|
||||
groups,
|
||||
relations
|
||||
)
|
||||
}
|
||||
return await priceListRepo.listAndCount({ ...query, relations }, groups)
|
||||
})
|
||||
if (q) {
|
||||
return await priceListRepo.getFreeTextSearchResultsAndCount(
|
||||
q,
|
||||
query as PriceListFindOptions,
|
||||
groups,
|
||||
relations
|
||||
)
|
||||
}
|
||||
return await priceListRepo.listAndCount({ ...query, relations }, groups)
|
||||
}
|
||||
|
||||
protected async upsertCustomerGroups_(
|
||||
@@ -332,10 +330,9 @@ class PriceListService extends TransactionBaseService<PriceListService> {
|
||||
const productVariantRepo = manager.getCustomRepository(
|
||||
this.productVariantRepo_
|
||||
)
|
||||
const [products, count] = await this.productService_.listAndCount(
|
||||
selector,
|
||||
config
|
||||
)
|
||||
const [products, count] = await this.productService_
|
||||
.withTransaction(manager)
|
||||
.listAndCount(selector, config)
|
||||
|
||||
const moneyAmountRepo = manager.getCustomRepository(this.moneyAmountRepo_)
|
||||
|
||||
@@ -378,10 +375,9 @@ class PriceListService extends TransactionBaseService<PriceListService> {
|
||||
requiresPriceList = false
|
||||
): Promise<[ProductVariant[], number]> {
|
||||
return await this.atomicPhase_(async (manager: EntityManager) => {
|
||||
const [variants, count] = await this.variantService_.listAndCount(
|
||||
selector,
|
||||
config
|
||||
)
|
||||
const [variants, count] = await this.variantService_
|
||||
.withTransaction(manager)
|
||||
.listAndCount(selector, config)
|
||||
|
||||
const moneyAmountRepo = manager.getCustomRepository(this.moneyAmountRepo_)
|
||||
|
||||
|
||||
@@ -63,34 +63,32 @@ class PricingService extends TransactionBaseService<PricingService> {
|
||||
async collectPricingContext(
|
||||
context: PriceSelectionContext
|
||||
): Promise<PricingContext> {
|
||||
return await this.atomicPhase_(
|
||||
async (transactionManager: EntityManager) => {
|
||||
let automaticTaxes = false
|
||||
let taxRate = null
|
||||
let currencyCode = context.currency_code
|
||||
return await this.atomicPhase_(async (manager: EntityManager) => {
|
||||
let automaticTaxes = false
|
||||
let taxRate = null
|
||||
let currencyCode = context.currency_code
|
||||
|
||||
if (context.region_id) {
|
||||
const region = await this.regionService
|
||||
.withTransaction(transactionManager)
|
||||
.retrieve(context.region_id, {
|
||||
select: ["id", "currency_code", "automatic_taxes", "tax_rate"],
|
||||
})
|
||||
if (context.region_id) {
|
||||
const region = await this.regionService
|
||||
.withTransaction(manager)
|
||||
.retrieve(context.region_id, {
|
||||
select: ["id", "currency_code", "automatic_taxes", "tax_rate"],
|
||||
})
|
||||
|
||||
currencyCode = region.currency_code
|
||||
automaticTaxes = region.automatic_taxes
|
||||
taxRate = region.tax_rate
|
||||
}
|
||||
|
||||
return {
|
||||
price_selection: {
|
||||
...context,
|
||||
currency_code: currencyCode,
|
||||
},
|
||||
automatic_taxes: automaticTaxes,
|
||||
tax_rate: taxRate,
|
||||
}
|
||||
currencyCode = region.currency_code
|
||||
automaticTaxes = region.automatic_taxes
|
||||
taxRate = region.tax_rate
|
||||
}
|
||||
)
|
||||
|
||||
return {
|
||||
price_selection: {
|
||||
...context,
|
||||
currency_code: currencyCode,
|
||||
},
|
||||
automatic_taxes: automaticTaxes,
|
||||
tax_rate: taxRate,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -113,21 +113,20 @@ class ProductService extends TransactionBaseService<ProductService> {
|
||||
include_discount_prices: false,
|
||||
}
|
||||
): Promise<Product[]> {
|
||||
return await this.atomicPhase_(async (manager) => {
|
||||
const productRepo = manager.getCustomRepository(this.productRepository_)
|
||||
const manager = this.manager_
|
||||
const productRepo = manager.getCustomRepository(this.productRepository_)
|
||||
|
||||
const { q, query, relations } = this.prepareListQuery_(selector, config)
|
||||
if (q) {
|
||||
const [products] = await productRepo.getFreeTextSearchResultsAndCount(
|
||||
q,
|
||||
query,
|
||||
relations
|
||||
)
|
||||
return products
|
||||
}
|
||||
const { q, query, relations } = this.prepareListQuery_(selector, config)
|
||||
if (q) {
|
||||
const [products] = await productRepo.getFreeTextSearchResultsAndCount(
|
||||
q,
|
||||
query,
|
||||
relations
|
||||
)
|
||||
return products
|
||||
}
|
||||
|
||||
return await productRepo.findWithRelations(relations, query)
|
||||
})
|
||||
return await productRepo.findWithRelations(relations, query)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -150,21 +149,20 @@ class ProductService extends TransactionBaseService<ProductService> {
|
||||
include_discount_prices: false,
|
||||
}
|
||||
): Promise<[Product[], number]> {
|
||||
return await this.atomicPhase_(async (manager) => {
|
||||
const productRepo = manager.getCustomRepository(this.productRepository_)
|
||||
const manager = this.manager_
|
||||
const productRepo = manager.getCustomRepository(this.productRepository_)
|
||||
|
||||
const { q, query, relations } = this.prepareListQuery_(selector, config)
|
||||
const { q, query, relations } = this.prepareListQuery_(selector, config)
|
||||
|
||||
if (q) {
|
||||
return await productRepo.getFreeTextSearchResultsAndCount(
|
||||
q,
|
||||
query,
|
||||
relations
|
||||
)
|
||||
}
|
||||
if (q) {
|
||||
return await productRepo.getFreeTextSearchResultsAndCount(
|
||||
q,
|
||||
query,
|
||||
relations
|
||||
)
|
||||
}
|
||||
|
||||
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
|
||||
*/
|
||||
async count(selector: Selector<Product> = {}): Promise<number> {
|
||||
return await this.atomicPhase_(async (manager) => {
|
||||
const productRepo = manager.getCustomRepository(this.productRepository_)
|
||||
const query = buildQuery(selector)
|
||||
return await productRepo.count(query)
|
||||
})
|
||||
const manager = this.manager_
|
||||
const productRepo = manager.getCustomRepository(this.productRepository_)
|
||||
const query = buildQuery(selector)
|
||||
return await productRepo.count(query)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -194,9 +191,7 @@ class ProductService extends TransactionBaseService<ProductService> {
|
||||
include_discount_prices: false,
|
||||
}
|
||||
): 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,
|
||||
config: FindProductConfig = {}
|
||||
): 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,
|
||||
config: FindProductConfig = {}
|
||||
): Promise<Product> {
|
||||
return await this.atomicPhase_(async () => {
|
||||
return await this.retrieve_({ external_id: externalId }, config)
|
||||
})
|
||||
return await this.retrieve_({ external_id: externalId }, config)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -245,29 +236,28 @@ class ProductService extends TransactionBaseService<ProductService> {
|
||||
include_discount_prices: false,
|
||||
}
|
||||
): Promise<Product> {
|
||||
return await this.atomicPhase_(async (manager) => {
|
||||
const productRepo = manager.getCustomRepository(this.productRepository_)
|
||||
const manager = this.manager_
|
||||
const productRepo = manager.getCustomRepository(this.productRepository_)
|
||||
|
||||
const { relations, ...query } = buildQuery(selector, config)
|
||||
const { relations, ...query } = buildQuery(selector, config)
|
||||
|
||||
const product = await productRepo.findOneWithRelations(
|
||||
relations,
|
||||
query as FindWithoutRelationsOptions
|
||||
const product = await productRepo.findOneWithRelations(
|
||||
relations,
|
||||
query as FindWithoutRelationsOptions
|
||||
)
|
||||
|
||||
if (!product) {
|
||||
const selectorConstraints = Object.entries(selector)
|
||||
.map(([key, value]) => `${key}: ${value}`)
|
||||
.join(", ")
|
||||
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Product with ${selectorConstraints} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
if (!product) {
|
||||
const selectorConstraints = Object.entries(selector)
|
||||
.map(([key, value]) => `${key}: ${value}`)
|
||||
.join(", ")
|
||||
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Product with ${selectorConstraints} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
return product
|
||||
})
|
||||
return product
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -283,37 +273,33 @@ class ProductService extends TransactionBaseService<ProductService> {
|
||||
take: 50,
|
||||
}
|
||||
): Promise<ProductVariant[]> {
|
||||
return await this.atomicPhase_(async () => {
|
||||
const givenRelations = config.relations ?? []
|
||||
const requiredRelations = ["variants"]
|
||||
const relationsSet = new Set([...givenRelations, ...requiredRelations])
|
||||
const givenRelations = config.relations ?? []
|
||||
const requiredRelations = ["variants"]
|
||||
const relationsSet = new Set([...givenRelations, ...requiredRelations])
|
||||
|
||||
const product = await this.retrieve(productId, {
|
||||
...config,
|
||||
relations: [...relationsSet],
|
||||
})
|
||||
return product.variants
|
||||
const product = await this.retrieve(productId, {
|
||||
...config,
|
||||
relations: [...relationsSet],
|
||||
})
|
||||
return product.variants
|
||||
}
|
||||
|
||||
async listTypes(): Promise<ProductType[]> {
|
||||
return await this.atomicPhase_(async (manager) => {
|
||||
const productTypeRepository = manager.getCustomRepository(
|
||||
this.productTypeRepository_
|
||||
)
|
||||
const manager = this.manager_
|
||||
const productTypeRepository = manager.getCustomRepository(
|
||||
this.productTypeRepository_
|
||||
)
|
||||
|
||||
return await productTypeRepository.find({})
|
||||
})
|
||||
return await productTypeRepository.find({})
|
||||
}
|
||||
|
||||
async listTagsByUsage(count = 10): Promise<ProductTag[]> {
|
||||
return await this.atomicPhase_(async (manager) => {
|
||||
const productTagRepo = manager.getCustomRepository(
|
||||
this.productTagRepository_
|
||||
)
|
||||
const manager = this.manager_
|
||||
const productTagRepo = manager.getCustomRepository(
|
||||
this.productTagRepository_
|
||||
)
|
||||
|
||||
return await productTagRepo.listTagsByUsage(count)
|
||||
})
|
||||
return await productTagRepo.listTagsByUsage(count)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -66,29 +66,28 @@ class SalesChannelService extends TransactionBaseService<SalesChannelService> {
|
||||
salesChannelId: string,
|
||||
config: FindConfig<SalesChannel> = {}
|
||||
): Promise<SalesChannel | never> {
|
||||
return await this.atomicPhase_(async (transactionManager) => {
|
||||
const salesChannelRepo = transactionManager.getCustomRepository(
|
||||
this.salesChannelRepository_
|
||||
const manager = this.manager_
|
||||
const salesChannelRepo = manager.getCustomRepository(
|
||||
this.salesChannelRepository_
|
||||
)
|
||||
|
||||
const query = buildQuery(
|
||||
{
|
||||
id: salesChannelId,
|
||||
},
|
||||
config
|
||||
)
|
||||
|
||||
const salesChannel = await salesChannelRepo.findOne(query)
|
||||
|
||||
if (!salesChannel) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Sales channel with id ${salesChannelId} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
const query = buildQuery(
|
||||
{
|
||||
id: salesChannelId,
|
||||
},
|
||||
config
|
||||
)
|
||||
|
||||
const salesChannel = await salesChannelRepo.findOne(query)
|
||||
|
||||
if (!salesChannel) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Sales channel with id ${salesChannelId} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
return salesChannel
|
||||
})
|
||||
return salesChannel
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -105,26 +104,25 @@ class SalesChannelService extends TransactionBaseService<SalesChannelService> {
|
||||
take: 20,
|
||||
}
|
||||
): Promise<[SalesChannel[], number]> {
|
||||
return await this.atomicPhase_(async (transactionManager) => {
|
||||
const salesChannelRepo = transactionManager.getCustomRepository(
|
||||
this.salesChannelRepository_
|
||||
)
|
||||
const manager = this.manager_
|
||||
const salesChannelRepo = manager.getCustomRepository(
|
||||
this.salesChannelRepository_
|
||||
)
|
||||
|
||||
const selector_ = { ...selector }
|
||||
let q: string | undefined
|
||||
if ("q" in selector_) {
|
||||
q = selector_.q
|
||||
delete selector_.q
|
||||
}
|
||||
const selector_ = { ...selector }
|
||||
let q: string | undefined
|
||||
if ("q" in selector_) {
|
||||
q = selector_.q
|
||||
delete selector_.q
|
||||
}
|
||||
|
||||
const query = buildQuery(selector_, config)
|
||||
const query = buildQuery(selector_, config)
|
||||
|
||||
if (q) {
|
||||
return await salesChannelRepo.getFreeTextSearchResultsAndCount(q, query)
|
||||
}
|
||||
if (q) {
|
||||
return await salesChannelRepo.getFreeTextSearchResultsAndCount(q, query)
|
||||
}
|
||||
|
||||
return await salesChannelRepo.findAndCount(query)
|
||||
})
|
||||
return await salesChannelRepo.findAndCount(query)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -131,14 +131,11 @@ class ShippingOptionService extends TransactionBaseService<ShippingOptionService
|
||||
selector: Selector<ShippingMethod>,
|
||||
config: FindConfig<ShippingOption> = { skip: 0, take: 50 }
|
||||
): Promise<ShippingOption[]> {
|
||||
return await this.atomicPhase_(async (transactionManager) => {
|
||||
const optRepo = transactionManager.getCustomRepository(
|
||||
this.optionRepository_
|
||||
)
|
||||
const manager = this.manager_
|
||||
const optRepo = manager.getCustomRepository(this.optionRepository_)
|
||||
|
||||
const query = buildQuery(selector, config)
|
||||
return optRepo.find(query)
|
||||
})
|
||||
const query = buildQuery(selector, config)
|
||||
return optRepo.find(query)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -150,14 +147,11 @@ class ShippingOptionService extends TransactionBaseService<ShippingOptionService
|
||||
selector: Selector<ShippingMethod>,
|
||||
config: FindConfig<ShippingOption> = { skip: 0, take: 50 }
|
||||
): Promise<[ShippingOption[], number]> {
|
||||
return await this.atomicPhase_(async (transactionManager) => {
|
||||
const optRepo = transactionManager.getCustomRepository(
|
||||
this.optionRepository_
|
||||
)
|
||||
const manager = this.manager_
|
||||
const optRepo = manager.getCustomRepository(this.optionRepository_)
|
||||
|
||||
const query = buildQuery(selector, config)
|
||||
return await optRepo.findAndCount(query)
|
||||
})
|
||||
const query = buildQuery(selector, config)
|
||||
return await optRepo.findAndCount(query)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -171,33 +165,33 @@ class ShippingOptionService extends TransactionBaseService<ShippingOptionService
|
||||
optionId,
|
||||
options: { select?: (keyof ShippingOption)[]; relations?: string[] } = {}
|
||||
): Promise<ShippingOption> {
|
||||
return await this.atomicPhase_(async (transactionManager) => {
|
||||
const soRepo: ShippingOptionRepository =
|
||||
transactionManager.getCustomRepository(this.optionRepository_)
|
||||
const manager = this.manager_
|
||||
const soRepo: ShippingOptionRepository = manager.getCustomRepository(
|
||||
this.optionRepository_
|
||||
)
|
||||
|
||||
const query: ExtendedFindConfig<ShippingOption> = {
|
||||
where: { id: optionId },
|
||||
}
|
||||
const query: ExtendedFindConfig<ShippingOption> = {
|
||||
where: { id: optionId },
|
||||
}
|
||||
|
||||
if (options.select) {
|
||||
query.select = options.select
|
||||
}
|
||||
if (options.select) {
|
||||
query.select = options.select
|
||||
}
|
||||
|
||||
if (options.relations) {
|
||||
query.relations = options.relations
|
||||
}
|
||||
if (options.relations) {
|
||||
query.relations = options.relations
|
||||
}
|
||||
|
||||
const option = await soRepo.findOne(query)
|
||||
const option = await soRepo.findOne(query)
|
||||
|
||||
if (!option) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Shipping Option with ${optionId} was not found`
|
||||
)
|
||||
}
|
||||
if (!option) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Shipping Option with ${optionId} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
return option
|
||||
})
|
||||
return option
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -89,24 +89,16 @@ class StoreService extends TransactionBaseService<StoreService> {
|
||||
* @return the store
|
||||
*/
|
||||
async retrieve(config: FindConfig<Store> = {}): Promise<Store> {
|
||||
return await this.atomicPhase_(
|
||||
async (transactionManager: EntityManager) => {
|
||||
const storeRepo = transactionManager.getCustomRepository(
|
||||
this.storeRepository_
|
||||
)
|
||||
const query = buildQuery({}, config)
|
||||
const store = await storeRepo.findOne(query)
|
||||
const manager = this.manager_
|
||||
const storeRepo = manager.getCustomRepository(this.storeRepository_)
|
||||
const query = buildQuery({}, config)
|
||||
const store = await storeRepo.findOne(query)
|
||||
|
||||
if (!store) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
"Store does not exist"
|
||||
)
|
||||
}
|
||||
if (!store) {
|
||||
throw new MedusaError(MedusaError.Types.NOT_FOUND, "Store does not exist")
|
||||
}
|
||||
|
||||
return store
|
||||
}
|
||||
)
|
||||
return store
|
||||
}
|
||||
|
||||
protected getDefaultCurrency_(code: string): Partial<Currency> {
|
||||
|
||||
@@ -85,20 +85,19 @@ class TaxRateService extends BaseService {
|
||||
id: string,
|
||||
config: FindConfig<TaxRate> = {}
|
||||
): Promise<TaxRate> {
|
||||
return await this.atomicPhase_(async (manager: EntityManager) => {
|
||||
const taxRateRepo = manager.getCustomRepository(this.taxRateRepository_)
|
||||
const query = this.buildQuery_({ id }, config)
|
||||
const manager = this.manager_
|
||||
const taxRateRepo = manager.getCustomRepository(this.taxRateRepository_)
|
||||
const query = this.buildQuery_({ id }, config)
|
||||
|
||||
const taxRate = await taxRateRepo.findOneWithResolution(query)
|
||||
if (!taxRate) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`TaxRate with ${id} was not found`
|
||||
)
|
||||
}
|
||||
const taxRate = await taxRateRepo.findOneWithResolution(query)
|
||||
if (!taxRate) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`TaxRate with ${id} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
return taxRate
|
||||
})
|
||||
return taxRate
|
||||
}
|
||||
|
||||
async create(data: CreateTaxRateInput): Promise<TaxRate> {
|
||||
@@ -323,17 +322,15 @@ class TaxRateService extends BaseService {
|
||||
config: TaxRateListByConfig
|
||||
): Promise<TaxRate[]> {
|
||||
// Check both ProductTaxRate + ProductTypeTaxRate
|
||||
return await this.atomicPhase_(async (manager: EntityManager) => {
|
||||
const taxRateRepo = manager.getCustomRepository(this.taxRateRepository_)
|
||||
return await taxRateRepo.listByProduct(productId, config)
|
||||
})
|
||||
const manager = this.manager_
|
||||
const taxRateRepo = manager.getCustomRepository(this.taxRateRepository_)
|
||||
return await taxRateRepo.listByProduct(productId, config)
|
||||
}
|
||||
|
||||
async listByShippingOption(shippingOptionId: string): Promise<TaxRate[]> {
|
||||
return await this.atomicPhase_(async (manager: EntityManager) => {
|
||||
const taxRateRepo = manager.getCustomRepository(this.taxRateRepository_)
|
||||
return await taxRateRepo.listByShippingOption(shippingOptionId)
|
||||
})
|
||||
const manager = this.manager_
|
||||
const taxRateRepo = manager.getCustomRepository(this.taxRateRepository_)
|
||||
return await taxRateRepo.listByShippingOption(shippingOptionId)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -71,12 +71,9 @@ class UserService extends TransactionBaseService<UserService> {
|
||||
* @return {Promise} the result of the find operation
|
||||
*/
|
||||
async list(selector: FilterableUserProps, config = {}): Promise<User[]> {
|
||||
return await this.atomicPhase_(async (transactionManager) => {
|
||||
const userRepo = transactionManager.getCustomRepository(
|
||||
this.userRepository_
|
||||
)
|
||||
return await userRepo.find(buildQuery(selector, config))
|
||||
})
|
||||
const manager = this.manager_
|
||||
const userRepo = manager.getCustomRepository(this.userRepository_)
|
||||
return await userRepo.find(buildQuery(selector, config))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,23 +84,20 @@ class UserService extends TransactionBaseService<UserService> {
|
||||
* @return {Promise<User>} the user document.
|
||||
*/
|
||||
async retrieve(userId: string, config: FindConfig<User> = {}): Promise<User> {
|
||||
return await this.atomicPhase_(async (transactionManager) => {
|
||||
const userRepo = transactionManager.getCustomRepository(
|
||||
this.userRepository_
|
||||
const manager = this.manager_
|
||||
const userRepo = manager.getCustomRepository(this.userRepository_)
|
||||
const query = buildQuery({ id: userId }, config)
|
||||
|
||||
const user = await userRepo.findOne(query)
|
||||
|
||||
if (!user) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`User with id: ${userId} was not found`
|
||||
)
|
||||
const query = buildQuery({ id: userId }, config)
|
||||
}
|
||||
|
||||
const user = await userRepo.findOne(query)
|
||||
|
||||
if (!user) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`User with id: ${userId} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
return user
|
||||
})
|
||||
return user
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -117,25 +111,22 @@ class UserService extends TransactionBaseService<UserService> {
|
||||
apiToken: string,
|
||||
relations: string[] = []
|
||||
): Promise<User> {
|
||||
return await this.atomicPhase_(async (transactionManager) => {
|
||||
const userRepo = transactionManager.getCustomRepository(
|
||||
this.userRepository_
|
||||
)
|
||||
const manager = this.manager_
|
||||
const userRepo = manager.getCustomRepository(this.userRepository_)
|
||||
|
||||
const user = await userRepo.findOne({
|
||||
where: { api_token: apiToken },
|
||||
relations,
|
||||
})
|
||||
|
||||
if (!user) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`User with api token: ${apiToken} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
return user
|
||||
const user = await userRepo.findOne({
|
||||
where: { api_token: apiToken },
|
||||
relations,
|
||||
})
|
||||
|
||||
if (!user) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`User with api token: ${apiToken} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
return user
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -149,23 +140,20 @@ class UserService extends TransactionBaseService<UserService> {
|
||||
email: string,
|
||||
config: FindConfig<User> = {}
|
||||
): Promise<User> {
|
||||
return await this.atomicPhase_(async (transactionManager) => {
|
||||
const userRepo = transactionManager.getCustomRepository(
|
||||
this.userRepository_
|
||||
const manager = this.manager_
|
||||
const userRepo = manager.getCustomRepository(this.userRepository_)
|
||||
|
||||
const query = buildQuery({ email: email.toLowerCase() }, config)
|
||||
const user = await userRepo.findOne(query)
|
||||
|
||||
if (!user) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`User with email: ${email} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
const query = buildQuery({ email: email.toLowerCase() }, config)
|
||||
const user = await userRepo.findOne(query)
|
||||
|
||||
if (!user) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`User with email: ${email} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
return user
|
||||
})
|
||||
return user
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user