diff --git a/packages/medusa/src/services/__tests__/custom-shipping-option.js b/packages/medusa/src/services/__tests__/custom-shipping-option.js index 6536495501..35290b9746 100644 --- a/packages/medusa/src/services/__tests__/custom-shipping-option.js +++ b/packages/medusa/src/services/__tests__/custom-shipping-option.js @@ -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)), }) diff --git a/packages/medusa/src/services/__tests__/discount.js b/packages/medusa/src/services/__tests__/discount.js index e598c5d5ad..c3ca2499b2 100644 --- a/packages/medusa/src/services/__tests__/discount.js +++ b/packages/medusa/src/services/__tests__/discount.js @@ -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" }) diff --git a/packages/medusa/src/services/batch-job.ts b/packages/medusa/src/services/batch-job.ts index 77ea2ca514..57163b531f 100644 --- a/packages/medusa/src/services/batch-job.ts +++ b/packages/medusa/src/services/batch-job.ts @@ -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 { batchJobId: string, config: FindConfig = {} ): Promise { - 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 = { 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 { diff --git a/packages/medusa/src/services/cart.ts b/packages/medusa/src/services/cart.ts index 2a4348b57c..9ef89f349d 100644 --- a/packages/medusa/src/services/cart.ts +++ b/packages/medusa/src/services/cart.ts @@ -260,16 +260,11 @@ class CartService extends TransactionBaseService { selector: FilterableCartProps, config: FindConfig = {} ): Promise { - 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 { options: FindConfig = {}, totalsConfig: TotalsConfig = {} ): Promise { - 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) } /** diff --git a/packages/medusa/src/services/claim.ts b/packages/medusa/src/services/claim.ts index a241a3421a..82340a5c26 100644 --- a/packages/medusa/src/services/claim.ts +++ b/packages/medusa/src/services/claim.ts @@ -814,15 +814,10 @@ export default class ClaimService extends TransactionBaseService< order: { created_at: "DESC" }, } ): Promise { - 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 = {} ): Promise { - 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 } } diff --git a/packages/medusa/src/services/custom-shipping-option.ts b/packages/medusa/src/services/custom-shipping-option.ts index 3c437b62e3..370d5d56f9 100644 --- a/packages/medusa/src/services/custom-shipping-option.ts +++ b/packages/medusa/src/services/custom-shipping-option.ts @@ -37,24 +37,23 @@ class CustomShippingOptionService extends TransactionBaseService = {} ): Promise { - 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 { - 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 { 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) } } diff --git a/packages/medusa/src/services/customer.ts b/packages/medusa/src/services/customer.ts index aa5991c15d..b2f29a70fd 100644 --- a/packages/medusa/src/services/customer.ts +++ b/packages/medusa/src/services/customer.ts @@ -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 { selector: Selector & { q?: string } = {}, config: FindConfig = { relations: [], skip: 0, take: 50 } ): Promise { - 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, Customer>(selector, config) + const query = buildQuery, 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 { 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, Customer>(selector, config) + const query = buildQuery, Customer>(selector, config) - return await customerRepo.listAndCount(query, q) - }) + return await customerRepo.listAndCount(query, q) } /** @@ -159,10 +156,9 @@ class CustomerService extends TransactionBaseService { * @return {Promise} the result of the count operation */ async count(): Promise { - 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 { email: string, config: FindConfig = {} ): Promise { - 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 { phone: string, config: FindConfig = {} ): Promise { - 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 { customerId: string, config: FindConfig = {} ): Promise { - return await this.atomicPhase_(async () => { - return this.retrieve_({ id: customerId }, config) - }) + return this.retrieve_({ id: customerId }, config) } /** diff --git a/packages/medusa/src/services/discount-condition.ts b/packages/medusa/src/services/discount-condition.ts index b6217e892b..560cdc0fad 100644 --- a/packages/medusa/src/services/discount-condition.ts +++ b/packages/medusa/src/services/discount-condition.ts @@ -50,24 +50,23 @@ class DiscountConditionService extends TransactionBaseService ): Promise { - 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): diff --git a/packages/medusa/src/services/discount.ts b/packages/medusa/src/services/discount.ts index 0fde9be4fe..5ea3d45427 100644 --- a/packages/medusa/src/services/discount.ts +++ b/packages/medusa/src/services/discount.ts @@ -115,14 +115,11 @@ class DiscountService extends TransactionBaseService { selector: FilterableDiscountProps = {}, config: FindConfig = { relations: [], skip: 0, take: 10 } ): Promise { - 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, config) - return await discountRepo.find(query) - }) + const query = buildQuery(selector as Selector, config) + return await discountRepo.find(query) } /** @@ -138,39 +135,36 @@ class DiscountService extends TransactionBaseService { 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, config) + + if (q) { + const where = query.where + + delete where.code + + query.where = (qb: SelectQueryBuilder): void => { + qb.where(where) + + qb.andWhere( + new Brackets((qb) => { + qb.where({ code: ILike(`%${q}%`) }) + }) + ) } + } - const query = buildQuery(selector as Selector, config) + const [discounts, count] = await discountRepo.findAndCount(query) - if (q) { - const where = query.where - - delete where.code - - query.where = (qb: SelectQueryBuilder): 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 { discountId: string, config: FindConfig = {} ): Promise { - 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 { discountCode: string, config: FindConfig = {} ): Promise { - 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 { 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 { discountRuleId: string, customerId: string | undefined ): Promise { - 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 { 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, diff --git a/packages/medusa/src/services/draft-order.ts b/packages/medusa/src/services/draft-order.ts index b787135e62..cffb387a0b 100644 --- a/packages/medusa/src/services/draft-order.ts +++ b/packages/medusa/src/services/draft-order.ts @@ -92,24 +92,21 @@ class DraftOrderService extends TransactionBaseService { id: string, config: FindConfig = {} ): Promise { - 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 { cartId: string, config: FindConfig = {} ): Promise { - 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 { 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 & ExtendedFindConfig - - 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 & ExtendedFindConfig + + 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 { order: { created_at: "DESC" }, } ): Promise { - 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) } /** diff --git a/packages/medusa/src/services/fulfillment.ts b/packages/medusa/src/services/fulfillment.ts index 654b03987c..22bc2506e2 100644 --- a/packages/medusa/src/services/fulfillment.ts +++ b/packages/medusa/src/services/fulfillment.ts @@ -167,23 +167,22 @@ class FulfillmentService extends TransactionBaseService { id: string, config: FindConfig = {} ): Promise { - 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 } /** diff --git a/packages/medusa/src/services/gift-card.ts b/packages/medusa/src/services/gift-card.ts index 70fd249b0e..b115b71400 100644 --- a/packages/medusa/src/services/gift-card.ts +++ b/packages/medusa/src/services/gift-card.ts @@ -85,25 +85,24 @@ class GiftCardService extends TransactionBaseService { selector: QuerySelector = {}, config: FindConfig = { 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 - > = buildQuery, GiftCard>(selector, config) + const query: ExtendedFindConfig< + GiftCard, + QuerySelector + > = buildQuery, 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 { selector: QuerySelector = {}, config: FindConfig = { relations: [], skip: 0, take: 10 } ): Promise { - 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 - > = buildQuery, GiftCard>(selector, config) + const query: ExtendedFindConfig< + GiftCard, + QuerySelector + > = buildQuery, 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 { - 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 { selector: Selector, config: FindConfig = {} ): Promise { - 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 { giftCardId: string, config: FindConfig = {} ): Promise { - 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 = {} ): Promise { - 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 { 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 { * @return the result of the delete operation */ async delete(giftCardId: string): Promise { - 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) } } diff --git a/packages/medusa/src/services/line-item.ts b/packages/medusa/src/services/line-item.ts index f11e1ca0b1..9f59ebf638 100644 --- a/packages/medusa/src/services/line-item.ts +++ b/packages/medusa/src/services/line-item.ts @@ -91,15 +91,10 @@ class LineItemService extends BaseService { selector, config = { skip: 0, take: 50, order: { created_at: "DESC" } } ): Promise { - 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} the line item */ async retrieve(id: string, config = {}): Promise { - 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 } /** diff --git a/packages/medusa/src/services/price-list.ts b/packages/medusa/src/services/price-list.ts index 97b3afd650..bac06de9c3 100644 --- a/packages/medusa/src/services/price-list.ts +++ b/packages/medusa/src/services/price-list.ts @@ -248,19 +248,18 @@ class PriceListService extends TransactionBaseService { selector: FilterablePriceListProps = {}, config: FindConfig = { skip: 0, take: 20 } ): Promise { - 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 - query.where.customer_groups = undefined + const groups = query.where.customer_groups as FindOperator + 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 { 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 - delete query.where.customer_groups + const groups = query.where.customer_groups as FindOperator + 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 { 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 { 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_) diff --git a/packages/medusa/src/services/pricing.ts b/packages/medusa/src/services/pricing.ts index 8c93527b87..c3a61a2d8e 100644 --- a/packages/medusa/src/services/pricing.ts +++ b/packages/medusa/src/services/pricing.ts @@ -63,34 +63,32 @@ class PricingService extends TransactionBaseService { async collectPricingContext( context: PriceSelectionContext ): Promise { - 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, + } + }) } /** diff --git a/packages/medusa/src/services/product.ts b/packages/medusa/src/services/product.ts index 5b424047e1..33b1b2e467 100644 --- a/packages/medusa/src/services/product.ts +++ b/packages/medusa/src/services/product.ts @@ -113,21 +113,20 @@ class ProductService extends TransactionBaseService { include_discount_prices: false, } ): Promise { - 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 { 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 { * @return {Promise} the result of the count operation */ async count(selector: Selector = {}): Promise { - 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 { include_discount_prices: false, } ): Promise { - 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 { productHandle: string, config: FindProductConfig = {} ): Promise { - 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 { externalId: string, config: FindProductConfig = {} ): Promise { - 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 { include_discount_prices: false, } ): Promise { - 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 { take: 50, } ): Promise { - 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 { - 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 { - 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) } /** diff --git a/packages/medusa/src/services/sales-channel.ts b/packages/medusa/src/services/sales-channel.ts index 90cd5ba89a..76748c30eb 100644 --- a/packages/medusa/src/services/sales-channel.ts +++ b/packages/medusa/src/services/sales-channel.ts @@ -66,29 +66,28 @@ class SalesChannelService extends TransactionBaseService { salesChannelId: string, config: FindConfig = {} ): Promise { - 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 { 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) } /** diff --git a/packages/medusa/src/services/shipping-option.ts b/packages/medusa/src/services/shipping-option.ts index 4668f0ede6..c671cd2b07 100644 --- a/packages/medusa/src/services/shipping-option.ts +++ b/packages/medusa/src/services/shipping-option.ts @@ -131,14 +131,11 @@ class ShippingOptionService extends TransactionBaseService, config: FindConfig = { skip: 0, take: 50 } ): Promise { - 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, config: FindConfig = { 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 { - 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 = { - where: { id: optionId }, - } + const query: ExtendedFindConfig = { + 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 } /** diff --git a/packages/medusa/src/services/store.ts b/packages/medusa/src/services/store.ts index b6cc337a6b..e2b9ece03e 100644 --- a/packages/medusa/src/services/store.ts +++ b/packages/medusa/src/services/store.ts @@ -89,24 +89,16 @@ class StoreService extends TransactionBaseService { * @return the store */ async retrieve(config: FindConfig = {}): Promise { - 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 { diff --git a/packages/medusa/src/services/tax-rate.ts b/packages/medusa/src/services/tax-rate.ts index c6bc2e8d97..5b7829be22 100644 --- a/packages/medusa/src/services/tax-rate.ts +++ b/packages/medusa/src/services/tax-rate.ts @@ -85,20 +85,19 @@ class TaxRateService extends BaseService { id: string, config: FindConfig = {} ): Promise { - 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 { @@ -323,17 +322,15 @@ class TaxRateService extends BaseService { config: TaxRateListByConfig ): Promise { // 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 { - 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) } } diff --git a/packages/medusa/src/services/user.ts b/packages/medusa/src/services/user.ts index d11d81a1ef..7c7a5f333d 100644 --- a/packages/medusa/src/services/user.ts +++ b/packages/medusa/src/services/user.ts @@ -71,12 +71,9 @@ class UserService extends TransactionBaseService { * @return {Promise} the result of the find operation */ async list(selector: FilterableUserProps, config = {}): Promise { - 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 { * @return {Promise} the user document. */ async retrieve(userId: string, config: FindConfig = {}): Promise { - 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 { apiToken: string, relations: string[] = [] ): Promise { - 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 { email: string, config: FindConfig = {} ): Promise { - 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 } /**