diff --git a/packages/medusa/src/repositories/swap.ts b/packages/medusa/src/repositories/swap.ts index 015fc56bba..3ee40c4d21 100644 --- a/packages/medusa/src/repositories/swap.ts +++ b/packages/medusa/src/repositories/swap.ts @@ -1,5 +1,53 @@ import { EntityRepository, Repository } from "typeorm" import { Swap } from "../models/swap" +import { flatten, groupBy, map, merge } from "lodash" @EntityRepository(Swap) -export class SwapRepository extends Repository {} +export class SwapRepository extends Repository { + public async findWithRelations( + relations: Array = [], + optionsWithoutRelations: Omit, "relations"> = {} + ): Promise { + const entities = await this.find(optionsWithoutRelations) + const entitiesIds = entities.map(({ id }) => id) + + const groupedRelations = {} + for (const rel of relations) { + const [topLevel] = rel.split(".") + if (groupedRelations[topLevel]) { + groupedRelations[topLevel].push(rel) + } else { + groupedRelations[topLevel] = [rel] + } + } + + const entitiesIdsWithRelations = await Promise.all( + Object.entries(groupedRelations).map(([_, rels]) => { + return this.findByIds(entitiesIds, { + select: ["id"], + relations: rels as string[], + }) + }) + ).then(flatten) + + const entitiesAndRelations = entitiesIdsWithRelations.concat(entities) + + const entitiesAndRelationsById = groupBy(entitiesAndRelations, "id") + + return map(entities, e => merge({}, ...entitiesAndRelationsById[e.id])) + } + + public async findOneWithRelations( + relations: Array = [], + optionsWithoutRelations: Omit, "relations"> = {} + ): Promise { + // Limit 1 + optionsWithoutRelations.take = 1 + + const result = await this.findWithRelations( + relations, + optionsWithoutRelations + ) + return result[0] + } +} diff --git a/packages/medusa/src/services/__tests__/swap.js b/packages/medusa/src/services/__tests__/swap.js index cbbc61fc85..cce77b2d7c 100644 --- a/packages/medusa/src/services/__tests__/swap.js +++ b/packages/medusa/src/services/__tests__/swap.js @@ -164,7 +164,7 @@ describe("SwapService", () => { } const swapRepo = MockRepository({ - findOne: () => Promise.resolve(existing), + findOneWithRelations: () => Promise.resolve(existing), }) const lineItemService = { @@ -186,10 +186,9 @@ describe("SwapService", () => { it("finds swap and calls return create cart", async () => { await swapService.createCart(IdMap.getId("swap-1")) - expect(swapRepo.findOne).toHaveBeenCalledTimes(1) - expect(swapRepo.findOne).toHaveBeenCalledWith({ - where: { id: IdMap.getId("swap-1") }, - relations: [ + expect(swapRepo.findOneWithRelations).toHaveBeenCalledTimes(1) + expect(swapRepo.findOneWithRelations).toHaveBeenCalledWith( + [ "order", "order.items", "order.swaps", @@ -200,7 +199,10 @@ describe("SwapService", () => { "return_order.items", "return_order.shipping_method", ], - }) + { + where: { id: IdMap.getId("swap-1") }, + } + ) expect(cartService.create).toHaveBeenCalledTimes(1) expect(cartService.create).toHaveBeenCalledWith({ @@ -239,7 +241,7 @@ describe("SwapService", () => { it("fails if cart already created", async () => { const swapRepo = MockRepository({ - findOne: () => + findOneWithRelations: () => Promise.resolve({ ...existing, order_id: IdMap.getId("test"), @@ -368,7 +370,7 @@ describe("SwapService", () => { } const swapRepo = MockRepository({ - findOne: () => Promise.resolve(existing), + findOneWithRelations: () => Promise.resolve(existing), }) const swapService = new SwapService({ manager: MockManager, @@ -411,7 +413,7 @@ describe("SwapService", () => { } const swapRepo = MockRepository({ - findOne: q => + findOneWithRelations: (rels, q) => Promise.resolve(q.where.id === IdMap.getId("empty") ? {} : existing), }) const swapService = new SwapService({ @@ -487,7 +489,7 @@ describe("SwapService", () => { } const swapRepo = MockRepository({ - findOne: () => Promise.resolve({ ...existing }), + findOneWithRelations: () => Promise.resolve({ ...existing }), }) const swapService = new SwapService({ manager: MockManager, @@ -607,7 +609,7 @@ describe("SwapService", () => { } const swapRepo = MockRepository({ - findOne: () => Promise.resolve(existing), + findOneWithRelations: () => Promise.resolve(existing), }) const swapService = new SwapService({ @@ -704,7 +706,7 @@ describe("SwapService", () => { } const swapRepo = MockRepository({ - findOne: () => Promise.resolve(existing), + findOneWithRelations: () => Promise.resolve(existing), }) const swapService = new SwapService({ @@ -769,7 +771,7 @@ describe("SwapService", () => { }) const swapRepo = MockRepository({ - findOne: q => { + findOneWithRelations: (rels, q) => { switch (q.where.id) { case "refund": return Promise.resolve(existing(-1, false)) @@ -880,7 +882,7 @@ describe("SwapService", () => { } const swapRepo = MockRepository({ - findOne: q => { + findOneWithRelations: (rels, q) => { switch (q.where.id) { case "requested": return Promise.resolve({ diff --git a/packages/medusa/src/services/swap.js b/packages/medusa/src/services/swap.js index fef1599ceb..ce149eb325 100644 --- a/packages/medusa/src/services/swap.js +++ b/packages/medusa/src/services/swap.js @@ -103,7 +103,10 @@ class SwapService extends BaseService { const query = this.buildQuery_({ id: validatedId }, config) - const swap = await swapRepo.findOne(query) + const rels = query.relations + delete query.relations + const swap = await swapRepo.findOneWithRelations(rels, query) + if (!swap) { throw new MedusaError(MedusaError.Types.NOT_FOUND, "Swap was not found") } @@ -143,7 +146,10 @@ class SwapService extends BaseService { ) { const swapRepo = this.manager_.getCustomRepository(this.swapRepository_) const query = this.buildQuery_(selector, config) - return swapRepo.find(query) + + let rels = query.relations + delete query.relations + return swapRepo.findWithRelations(rels, query) } /**