Browse Source

feat(core): Add `totalQuantity` field to Order type

Closes #465
Michael Bromley 5 years ago
parent
commit
829ac966c2

+ 1 - 0
packages/core/src/api/schema/type/order.type.graphql

@@ -18,6 +18,7 @@ type Order implements Node {
     promotions: [Promotion!]!
     payments: [Payment!]
     fulfillments: [Fulfillment!]
+    totalQuantity: Int!
     subTotalBeforeTax: Int!
     "The subTotal is the total of the OrderLines, before order-level promotions and shipping has been applied."
     subTotal: Int!

+ 11 - 9
packages/core/src/entity/order/order.entity.ts

@@ -3,9 +3,11 @@ import { DeepPartial, ID } from '@vendure/common/lib/shared-types';
 import { Column, Entity, JoinTable, ManyToMany, ManyToOne, OneToMany } from 'typeorm';
 
 import { Calculated } from '../../common/calculated-decorator';
+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';
 import { VendureEntity } from '../base/base.entity';
+import { Channel } from '../channel/channel.entity';
 import { CustomOrderFields } from '../custom-entity-fields';
 import { Customer } from '../customer/customer.entity';
 import { EntityId } from '../entity-id.decorator';
@@ -14,8 +16,6 @@ import { OrderLine } from '../order-line/order-line.entity';
 import { Payment } from '../payment/payment.entity';
 import { Promotion } from '../promotion/promotion.entity';
 import { ShippingMethod } from '../shipping-method/shipping-method.entity';
-import { ChannelAware } from '../../common/types/common-types';
-import { Channel } from '../channel/channel.entity';
 
 /**
  * @description
@@ -96,7 +96,7 @@ export class Order extends VendureEntity implements ChannelAware, HasCustomField
     @EntityId({ nullable: true })
     taxZoneId?: ID;
 
-    @ManyToMany((type) => Channel)
+    @ManyToMany(type => Channel)
     @JoinTable()
     channels: Channel[];
 
@@ -115,6 +115,11 @@ export class Order extends VendureEntity implements ChannelAware, HasCustomField
         return this.pendingAdjustments || [];
     }
 
+    @Calculated()
+    get totalQuantity(): number {
+        return (this.lines || []).reduce((total, line) => total + line.quantity, 0);
+    }
+
     get promotionAdjustmentsTotal(): number {
         return this.adjustments
             .filter(a => a.type === AdjustmentType.PROMOTION)
@@ -134,11 +139,8 @@ export class Order extends VendureEntity implements ChannelAware, HasCustomField
     }
 
     getOrderItems(): OrderItem[] {
-        return this.lines.reduce(
-            (items, line) => {
-                return [...items, ...line.items];
-            },
-            [] as OrderItem[],
-        );
+        return this.lines.reduce((items, line) => {
+            return [...items, ...line.items];
+        }, [] as OrderItem[]);
     }
 }