Skip to content

Commit

Permalink
feat(core): Add totalQuantity field to Order type
Browse files Browse the repository at this point in the history
Closes #465
  • Loading branch information
michaelbromley committed Sep 29, 2020
1 parent 55304c5 commit 829ac96
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/core/src/api/schema/type/order.type.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
20 changes: 11 additions & 9 deletions packages/core/src/entity/order/order.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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
Expand Down Expand Up @@ -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[];

Expand All @@ -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)
Expand All @@ -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[]);
}
}

0 comments on commit 829ac96

Please sign in to comment.