Merge branch 'hotfix/cart-retrieve' into develop
This commit is contained in:
@@ -1,5 +1,53 @@
|
|||||||
import { EntityRepository, Repository } from "typeorm"
|
import { EntityRepository, FindManyOptions, Repository } from "typeorm"
|
||||||
|
import { flatten, groupBy, map, merge } from "lodash"
|
||||||
import { Cart } from "../models/cart"
|
import { Cart } from "../models/cart"
|
||||||
|
|
||||||
@EntityRepository(Cart)
|
@EntityRepository(Cart)
|
||||||
export class CartRepository extends Repository<Cart> {}
|
export class CartRepository extends Repository<Cart> {
|
||||||
|
public async findWithRelations(
|
||||||
|
relations: Array<keyof Cart> = [],
|
||||||
|
optionsWithoutRelations: Omit<FindManyOptions<Cart>, "relations"> = {}
|
||||||
|
): Promise<Cart[]> {
|
||||||
|
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(entitiesAndRelationsById, entityAndRelations =>
|
||||||
|
merge({}, ...entityAndRelations)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
public async findOneWithRelations(
|
||||||
|
relations: Array<keyof Cart> = [],
|
||||||
|
optionsWithoutRelations: Omit<FindManyOptions<Cart>, "relations"> = {}
|
||||||
|
): Promise<Cart> {
|
||||||
|
// Limit 1
|
||||||
|
optionsWithoutRelations.take = 1
|
||||||
|
|
||||||
|
const result = await this.findWithRelations(
|
||||||
|
relations,
|
||||||
|
optionsWithoutRelations
|
||||||
|
)
|
||||||
|
return result[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -34,7 +34,8 @@ describe("CartService", () => {
|
|||||||
describe("retrieve", () => {
|
describe("retrieve", () => {
|
||||||
let result
|
let result
|
||||||
const cartRepository = MockRepository({
|
const cartRepository = MockRepository({
|
||||||
findOne: () => Promise.resolve({ id: IdMap.getId("emptyCart") }),
|
findOneWithRelations: () =>
|
||||||
|
Promise.resolve({ id: IdMap.getId("emptyCart") }),
|
||||||
})
|
})
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
jest.clearAllMocks()
|
jest.clearAllMocks()
|
||||||
@@ -47,10 +48,13 @@ describe("CartService", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("calls cart model functions", () => {
|
it("calls cart model functions", () => {
|
||||||
expect(cartRepository.findOne).toHaveBeenCalledTimes(1)
|
expect(cartRepository.findOneWithRelations).toHaveBeenCalledTimes(1)
|
||||||
expect(cartRepository.findOne).toHaveBeenCalledWith({
|
expect(cartRepository.findOneWithRelations).toHaveBeenCalledWith(
|
||||||
where: { id: IdMap.getId("emptyCart") },
|
undefined,
|
||||||
})
|
{
|
||||||
|
where: { id: IdMap.getId("emptyCart") },
|
||||||
|
}
|
||||||
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -314,7 +318,7 @@ describe("CartService", () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const cartRepository = MockRepository({
|
const cartRepository = MockRepository({
|
||||||
findOne: q => {
|
findOneWithRelations: (rels, q) => {
|
||||||
if (q.where.id === IdMap.getId("cartWithLine")) {
|
if (q.where.id === IdMap.getId("cartWithLine")) {
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
items: [
|
items: [
|
||||||
@@ -472,7 +476,7 @@ describe("CartService", () => {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
const cartRepository = MockRepository({
|
const cartRepository = MockRepository({
|
||||||
findOne: q => {
|
findOneWithRelations: (rels, q) => {
|
||||||
if (q.where.id === IdMap.getId("withShipping")) {
|
if (q.where.id === IdMap.getId("withShipping")) {
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
shipping_methods: [
|
shipping_methods: [
|
||||||
@@ -574,7 +578,7 @@ describe("CartService", () => {
|
|||||||
|
|
||||||
describe("update", () => {
|
describe("update", () => {
|
||||||
const cartRepository = MockRepository({
|
const cartRepository = MockRepository({
|
||||||
findOne: q => {
|
findOneWithRelations: (rels, q) => {
|
||||||
if (q.where.id === "withpays") {
|
if (q.where.id === "withpays") {
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
payment_sessions: [
|
payment_sessions: [
|
||||||
@@ -602,8 +606,8 @@ describe("CartService", () => {
|
|||||||
cartService.setPaymentSessions = jest.fn()
|
cartService.setPaymentSessions = jest.fn()
|
||||||
await cartService.update("withpays", {})
|
await cartService.update("withpays", {})
|
||||||
|
|
||||||
expect(cartRepository.findOne).toHaveBeenCalledWith({
|
expect(cartRepository.findOneWithRelations).toHaveBeenCalledWith(
|
||||||
relations: [
|
[
|
||||||
"items",
|
"items",
|
||||||
"shipping_methods",
|
"shipping_methods",
|
||||||
"shipping_address",
|
"shipping_address",
|
||||||
@@ -617,8 +621,10 @@ describe("CartService", () => {
|
|||||||
"discounts.rule",
|
"discounts.rule",
|
||||||
"discounts.regions",
|
"discounts.regions",
|
||||||
],
|
],
|
||||||
where: { id: "withpays" },
|
{
|
||||||
})
|
where: { id: "withpays" },
|
||||||
|
}
|
||||||
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -636,7 +642,7 @@ describe("CartService", () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const cartRepository = MockRepository({
|
const cartRepository = MockRepository({
|
||||||
findOne: q => {
|
findOneWithRelations: (rels, q) => {
|
||||||
if (q.where.id === IdMap.getId("cannot")) {
|
if (q.where.id === IdMap.getId("cannot")) {
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
items: [
|
items: [
|
||||||
@@ -719,7 +725,7 @@ describe("CartService", () => {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
const cartRepository = MockRepository({
|
const cartRepository = MockRepository({
|
||||||
findOne: () => Promise.resolve({}),
|
findOneWithRelations: () => Promise.resolve({}),
|
||||||
})
|
})
|
||||||
const cartService = new CartService({
|
const cartService = new CartService({
|
||||||
manager: MockManager,
|
manager: MockManager,
|
||||||
@@ -792,7 +798,7 @@ describe("CartService", () => {
|
|||||||
|
|
||||||
describe("updateBillingAddress", () => {
|
describe("updateBillingAddress", () => {
|
||||||
const cartRepository = MockRepository({
|
const cartRepository = MockRepository({
|
||||||
findOne: () =>
|
findOneWithRelations: () =>
|
||||||
Promise.resolve({
|
Promise.resolve({
|
||||||
region: { countries: [{ iso_2: "us" }] },
|
region: { countries: [{ iso_2: "us" }] },
|
||||||
}),
|
}),
|
||||||
@@ -854,7 +860,7 @@ describe("CartService", () => {
|
|||||||
|
|
||||||
describe("updateShippingAddress", () => {
|
describe("updateShippingAddress", () => {
|
||||||
const cartRepository = MockRepository({
|
const cartRepository = MockRepository({
|
||||||
findOne: () =>
|
findOneWithRelations: () =>
|
||||||
Promise.resolve({
|
Promise.resolve({
|
||||||
region: { countries: [{ iso_2: "us" }] },
|
region: { countries: [{ iso_2: "us" }] },
|
||||||
}),
|
}),
|
||||||
@@ -957,7 +963,7 @@ describe("CartService", () => {
|
|||||||
),
|
),
|
||||||
}
|
}
|
||||||
const cartRepository = MockRepository({
|
const cartRepository = MockRepository({
|
||||||
findOne: () =>
|
findOneWithRelations: () =>
|
||||||
Promise.resolve({
|
Promise.resolve({
|
||||||
items: [
|
items: [
|
||||||
{
|
{
|
||||||
@@ -1056,7 +1062,7 @@ describe("CartService", () => {
|
|||||||
|
|
||||||
describe("setPaymentSession", () => {
|
describe("setPaymentSession", () => {
|
||||||
const cartRepository = MockRepository({
|
const cartRepository = MockRepository({
|
||||||
findOne: () => {
|
findOneWithRelations: () => {
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
region: {
|
region: {
|
||||||
payment_providers: [
|
payment_providers: [
|
||||||
@@ -1165,7 +1171,7 @@ describe("CartService", () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const cartRepository = MockRepository({
|
const cartRepository = MockRepository({
|
||||||
findOne: q => {
|
findOneWithRelations: (rels, q) => {
|
||||||
if (q.where.id === IdMap.getId("cart-to-filter")) {
|
if (q.where.id === IdMap.getId("cart-to-filter")) {
|
||||||
return Promise.resolve(cart3)
|
return Promise.resolve(cart3)
|
||||||
}
|
}
|
||||||
@@ -1287,7 +1293,7 @@ describe("CartService", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const cartRepository = MockRepository({
|
const cartRepository = MockRepository({
|
||||||
findOne: q => {
|
findOneWithRelations: (rels, q) => {
|
||||||
switch (q.where.id) {
|
switch (q.where.id) {
|
||||||
case IdMap.getId("lines"):
|
case IdMap.getId("lines"):
|
||||||
return Promise.resolve(cart3)
|
return Promise.resolve(cart3)
|
||||||
@@ -1420,7 +1426,7 @@ describe("CartService", () => {
|
|||||||
|
|
||||||
describe("applyDiscount", () => {
|
describe("applyDiscount", () => {
|
||||||
const cartRepository = MockRepository({
|
const cartRepository = MockRepository({
|
||||||
findOne: q => {
|
findOneWithRelations: (rels, q) => {
|
||||||
if (q.where.id === IdMap.getId("with-d")) {
|
if (q.where.id === IdMap.getId("with-d")) {
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
id: IdMap.getId("cart"),
|
id: IdMap.getId("cart"),
|
||||||
@@ -1599,7 +1605,7 @@ describe("CartService", () => {
|
|||||||
|
|
||||||
describe("removeDiscount", () => {
|
describe("removeDiscount", () => {
|
||||||
const cartRepository = MockRepository({
|
const cartRepository = MockRepository({
|
||||||
findOne: q => {
|
findOneWithRelations: (rels, q) => {
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
id: IdMap.getId("cart"),
|
id: IdMap.getId("cart"),
|
||||||
discounts: [
|
discounts: [
|
||||||
|
|||||||
@@ -269,7 +269,9 @@ class CartService extends BaseService {
|
|||||||
query.select = select
|
query.select = select
|
||||||
}
|
}
|
||||||
|
|
||||||
const raw = await cartRepo.findOne(query)
|
const rels = query.relations
|
||||||
|
delete query.relations
|
||||||
|
const raw = await cartRepo.findOneWithRelations(rels, query)
|
||||||
|
|
||||||
if (!raw) {
|
if (!raw) {
|
||||||
throw new MedusaError(
|
throw new MedusaError(
|
||||||
|
|||||||
Reference in New Issue
Block a user