From 0a33e5c49a0a9b40573680460f76adf269691fb0 Mon Sep 17 00:00:00 2001 From: Michael Bromley Date: Tue, 19 Oct 2021 16:08:57 +0200 Subject: [PATCH] fix(core): Improved error message when Order.lines not joined --- packages/core/src/entity/order/order.entity.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packages/core/src/entity/order/order.entity.ts b/packages/core/src/entity/order/order.entity.ts index 9e27a3329d..85883ccfa5 100644 --- a/packages/core/src/entity/order/order.entity.ts +++ b/packages/core/src/entity/order/order.entity.ts @@ -11,6 +11,7 @@ import { summate } from '@vendure/common/lib/shared-utils'; import { Column, Entity, JoinTable, ManyToMany, ManyToOne, OneToMany } from 'typeorm'; import { Calculated } from '../../common/calculated-decorator'; +import { InternalServerError } from '../../common/error/errors'; import { ChannelAware } from '../../common/types/common-types'; import { HasCustomFields } from '../../config/custom-field/custom-field-types'; import { OrderState } from '../../service/helpers/order-state-machine/order-state'; @@ -164,6 +165,7 @@ export class Order extends VendureEntity implements ChannelAware, HasCustomField @Calculated() get discounts(): Discount[] { + this.throwIfLinesNotJoined('discounts'); const groupedAdjustments = new Map(); for (const line of this.lines) { for (const discount of line.discounts) { @@ -257,6 +259,7 @@ export class Order extends VendureEntity implements ChannelAware, HasCustomField expression: 'qty', }) get totalQuantity(): number { + this.throwIfLinesNotJoined('totalQuantity'); return summate(this.lines, 'quantity'); } @@ -266,6 +269,7 @@ export class Order extends VendureEntity implements ChannelAware, HasCustomField */ @Calculated() get taxSummary(): OrderTaxSummary[] { + this.throwIfLinesNotJoined('taxSummary'); const taxRateMap = new Map< string, { rate: number; base: number; tax: number; description: string } @@ -305,8 +309,20 @@ export class Order extends VendureEntity implements ChannelAware, HasCustomField } getOrderItems(): OrderItem[] { + this.throwIfLinesNotJoined('getOrderItems'); return this.lines.reduce((items, line) => { return [...items, ...line.items]; }, [] as OrderItem[]); } + + private throwIfLinesNotJoined(propertyName: keyof Order) { + if (this.lines == null) { + const errorMessage = [ + `The property "${propertyName}" on the Order entity requires the Order.lines relation to be joined.`, + `This can be done with the EntityHydratorService: \`await entityHydratorService.hydrate(ctx, order, { relations: ['lines'] })\``, + ]; + + throw new InternalServerError(errorMessage.join('\n')); + } + } }