Browse Source

feat(core): Implement adding notes to an Order

Relates to #118
Michael Bromley 6 years ago
parent
commit
3682cbf455

+ 58 - 47
packages/common/src/generated-types.ts

@@ -18,6 +18,11 @@ export type Scalars = {
   Upload: any,
 };
 
+export type AddNoteToOrderInput = {
+  id: Scalars['ID'],
+  note: Scalars['String'],
+};
+
 export type Address = Node & {
   __typename?: 'Address',
   id: Scalars['ID'],
@@ -1546,12 +1551,6 @@ export type Mutation = {
   updateCollection: Collection,
   /** Move a Collection to a different parent or index */
   moveCollection: Collection,
-  /** Create a new Country */
-  createCountry: Country,
-  /** Update an existing Country */
-  updateCountry: Country,
-  /** Delete a Country */
-  deleteCountry: DeletionResponse,
   /** Create a new CustomerGroup */
   createCustomerGroup: CustomerGroup,
   /** Update an existing CustomerGroup */
@@ -1560,6 +1559,12 @@ export type Mutation = {
   addCustomersToGroup: CustomerGroup,
   /** Remove Customers from a CustomerGroup */
   removeCustomersFromGroup: CustomerGroup,
+  /** Create a new Country */
+  createCountry: Country,
+  /** Update an existing Country */
+  updateCountry: Country,
+  /** Delete a Country */
+  deleteCountry: DeletionResponse,
   /** Create a new Customer. If a password is provided, a new User will also be created an linked to the Customer. */
   createCustomer: Customer,
   /** Update an existing Customer */
@@ -1591,6 +1596,7 @@ export type Mutation = {
   cancelOrder: Order,
   refundOrder: Refund,
   settleRefund: Refund,
+  addNoteToOrder: Order,
   /** Update an existing PaymentMethod */
   updatePaymentMethod: PaymentMethod,
   /** Create a new ProductOptionGroup */
@@ -1623,14 +1629,14 @@ export type Mutation = {
   createShippingMethod: ShippingMethod,
   /** Update an existing ShippingMethod */
   updateShippingMethod: ShippingMethod,
-  /** Create a new TaxRate */
-  createTaxRate: TaxRate,
-  /** Update an existing TaxRate */
-  updateTaxRate: TaxRate,
   /** Create a new TaxCategory */
   createTaxCategory: TaxCategory,
   /** Update an existing TaxCategory */
   updateTaxCategory: TaxCategory,
+  /** Create a new TaxRate */
+  createTaxRate: TaxRate,
+  /** Update an existing TaxRate */
+  updateTaxRate: TaxRate,
   /** Create a new Zone */
   createZone: Zone,
   /** Update an existing Zone */
@@ -1697,21 +1703,6 @@ export type MutationMoveCollectionArgs = {
 };
 
 
-export type MutationCreateCountryArgs = {
-  input: CreateCountryInput
-};
-
-
-export type MutationUpdateCountryArgs = {
-  input: UpdateCountryInput
-};
-
-
-export type MutationDeleteCountryArgs = {
-  id: Scalars['ID']
-};
-
-
 export type MutationCreateCustomerGroupArgs = {
   input: CreateCustomerGroupInput
 };
@@ -1734,6 +1725,21 @@ export type MutationRemoveCustomersFromGroupArgs = {
 };
 
 
+export type MutationCreateCountryArgs = {
+  input: CreateCountryInput
+};
+
+
+export type MutationUpdateCountryArgs = {
+  input: UpdateCountryInput
+};
+
+
+export type MutationDeleteCountryArgs = {
+  id: Scalars['ID']
+};
+
+
 export type MutationCreateCustomerArgs = {
   input: CreateCustomerInput,
   password?: Maybe<Scalars['String']>
@@ -1833,6 +1839,11 @@ export type MutationSettleRefundArgs = {
 };
 
 
+export type MutationAddNoteToOrderArgs = {
+  input: AddNoteToOrderInput
+};
+
+
 export type MutationUpdatePaymentMethodArgs = {
   input: UpdatePaymentMethodInput
 };
@@ -1923,23 +1934,23 @@ export type MutationUpdateShippingMethodArgs = {
 };
 
 
-export type MutationCreateTaxRateArgs = {
-  input: CreateTaxRateInput
+export type MutationCreateTaxCategoryArgs = {
+  input: CreateTaxCategoryInput
 };
 
 
-export type MutationUpdateTaxRateArgs = {
-  input: UpdateTaxRateInput
+export type MutationUpdateTaxCategoryArgs = {
+  input: UpdateTaxCategoryInput
 };
 
 
-export type MutationCreateTaxCategoryArgs = {
-  input: CreateTaxCategoryInput
+export type MutationCreateTaxRateArgs = {
+  input: CreateTaxRateInput
 };
 
 
-export type MutationUpdateTaxCategoryArgs = {
-  input: UpdateTaxCategoryInput
+export type MutationUpdateTaxRateArgs = {
+  input: UpdateTaxRateInput
 };
 
 
@@ -2475,10 +2486,10 @@ export type Query = {
   collections: CollectionList,
   collection?: Maybe<Collection>,
   collectionFilters: Array<ConfigurableOperation>,
-  countries: CountryList,
-  country?: Maybe<Country>,
   customerGroups: Array<CustomerGroup>,
   customerGroup?: Maybe<CustomerGroup>,
+  countries: CountryList,
+  country?: Maybe<Country>,
   customers: CustomerList,
   customer?: Maybe<Customer>,
   facets: FacetList,
@@ -2505,10 +2516,10 @@ export type Query = {
   shippingMethod?: Maybe<ShippingMethod>,
   shippingEligibilityCheckers: Array<ConfigurableOperation>,
   shippingCalculators: Array<ConfigurableOperation>,
-  taxRates: TaxRateList,
-  taxRate?: Maybe<TaxRate>,
   taxCategories: Array<TaxCategory>,
   taxCategory?: Maybe<TaxCategory>,
+  taxRates: TaxRateList,
+  taxRate?: Maybe<TaxRate>,
   zones: Array<Zone>,
   zone?: Maybe<Zone>,
 };
@@ -2551,17 +2562,17 @@ export type QueryCollectionArgs = {
 };
 
 
-export type QueryCountriesArgs = {
-  options?: Maybe<CountryListOptions>
+export type QueryCustomerGroupArgs = {
+  id: Scalars['ID']
 };
 
 
-export type QueryCountryArgs = {
-  id: Scalars['ID']
+export type QueryCountriesArgs = {
+  options?: Maybe<CountryListOptions>
 };
 
 
-export type QueryCustomerGroupArgs = {
+export type QueryCountryArgs = {
   id: Scalars['ID']
 };
 
@@ -2678,17 +2689,17 @@ export type QueryShippingMethodArgs = {
 };
 
 
-export type QueryTaxRatesArgs = {
-  options?: Maybe<TaxRateListOptions>
+export type QueryTaxCategoryArgs = {
+  id: Scalars['ID']
 };
 
 
-export type QueryTaxRateArgs = {
-  id: Scalars['ID']
+export type QueryTaxRatesArgs = {
+  options?: Maybe<TaxRateListOptions>
 };
 
 
-export type QueryTaxCategoryArgs = {
+export type QueryTaxRateArgs = {
   id: Scalars['ID']
 };
 

+ 66 - 42
packages/core/e2e/graphql/generated-e2e-admin-types.ts

@@ -18,6 +18,11 @@ export type Scalars = {
     Upload: any;
 };
 
+export type AddNoteToOrderInput = {
+    id: Scalars['ID'];
+    note: Scalars['String'];
+};
+
 export type Address = Node & {
     __typename?: 'Address';
     id: Scalars['ID'];
@@ -1543,12 +1548,6 @@ export type Mutation = {
     updateCollection: Collection;
     /** Move a Collection to a different parent or index */
     moveCollection: Collection;
-    /** Create a new Country */
-    createCountry: Country;
-    /** Update an existing Country */
-    updateCountry: Country;
-    /** Delete a Country */
-    deleteCountry: DeletionResponse;
     /** Create a new CustomerGroup */
     createCustomerGroup: CustomerGroup;
     /** Update an existing CustomerGroup */
@@ -1557,6 +1556,12 @@ export type Mutation = {
     addCustomersToGroup: CustomerGroup;
     /** Remove Customers from a CustomerGroup */
     removeCustomersFromGroup: CustomerGroup;
+    /** Create a new Country */
+    createCountry: Country;
+    /** Update an existing Country */
+    updateCountry: Country;
+    /** Delete a Country */
+    deleteCountry: DeletionResponse;
     /** Create a new Customer. If a password is provided, a new User will also be created an linked to the Customer. */
     createCustomer: Customer;
     /** Update an existing Customer */
@@ -1588,6 +1593,7 @@ export type Mutation = {
     cancelOrder: Order;
     refundOrder: Refund;
     settleRefund: Refund;
+    addNoteToOrder: Order;
     /** Update an existing PaymentMethod */
     updatePaymentMethod: PaymentMethod;
     /** Create a new ProductOptionGroup */
@@ -1620,14 +1626,14 @@ export type Mutation = {
     createShippingMethod: ShippingMethod;
     /** Update an existing ShippingMethod */
     updateShippingMethod: ShippingMethod;
-    /** Create a new TaxRate */
-    createTaxRate: TaxRate;
-    /** Update an existing TaxRate */
-    updateTaxRate: TaxRate;
     /** Create a new TaxCategory */
     createTaxCategory: TaxCategory;
     /** Update an existing TaxCategory */
     updateTaxCategory: TaxCategory;
+    /** Create a new TaxRate */
+    createTaxRate: TaxRate;
+    /** Update an existing TaxRate */
+    updateTaxRate: TaxRate;
     /** Create a new Zone */
     createZone: Zone;
     /** Update an existing Zone */
@@ -1683,18 +1689,6 @@ export type MutationMoveCollectionArgs = {
     input: MoveCollectionInput;
 };
 
-export type MutationCreateCountryArgs = {
-    input: CreateCountryInput;
-};
-
-export type MutationUpdateCountryArgs = {
-    input: UpdateCountryInput;
-};
-
-export type MutationDeleteCountryArgs = {
-    id: Scalars['ID'];
-};
-
 export type MutationCreateCustomerGroupArgs = {
     input: CreateCustomerGroupInput;
 };
@@ -1713,6 +1707,18 @@ export type MutationRemoveCustomersFromGroupArgs = {
     customerIds: Array<Scalars['ID']>;
 };
 
+export type MutationCreateCountryArgs = {
+    input: CreateCountryInput;
+};
+
+export type MutationUpdateCountryArgs = {
+    input: UpdateCountryInput;
+};
+
+export type MutationDeleteCountryArgs = {
+    id: Scalars['ID'];
+};
+
 export type MutationCreateCustomerArgs = {
     input: CreateCustomerInput;
     password?: Maybe<Scalars['String']>;
@@ -1793,6 +1799,10 @@ export type MutationSettleRefundArgs = {
     input: SettleRefundInput;
 };
 
+export type MutationAddNoteToOrderArgs = {
+    input: AddNoteToOrderInput;
+};
+
 export type MutationUpdatePaymentMethodArgs = {
     input: UpdatePaymentMethodInput;
 };
@@ -1866,14 +1876,6 @@ export type MutationUpdateShippingMethodArgs = {
     input: UpdateShippingMethodInput;
 };
 
-export type MutationCreateTaxRateArgs = {
-    input: CreateTaxRateInput;
-};
-
-export type MutationUpdateTaxRateArgs = {
-    input: UpdateTaxRateInput;
-};
-
 export type MutationCreateTaxCategoryArgs = {
     input: CreateTaxCategoryInput;
 };
@@ -1882,6 +1884,14 @@ export type MutationUpdateTaxCategoryArgs = {
     input: UpdateTaxCategoryInput;
 };
 
+export type MutationCreateTaxRateArgs = {
+    input: CreateTaxRateInput;
+};
+
+export type MutationUpdateTaxRateArgs = {
+    input: UpdateTaxRateInput;
+};
+
 export type MutationCreateZoneArgs = {
     input: CreateZoneInput;
 };
@@ -2408,10 +2418,10 @@ export type Query = {
     collections: CollectionList;
     collection?: Maybe<Collection>;
     collectionFilters: Array<ConfigurableOperation>;
-    countries: CountryList;
-    country?: Maybe<Country>;
     customerGroups: Array<CustomerGroup>;
     customerGroup?: Maybe<CustomerGroup>;
+    countries: CountryList;
+    country?: Maybe<Country>;
     customers: CustomerList;
     customer?: Maybe<Customer>;
     facets: FacetList;
@@ -2438,10 +2448,10 @@ export type Query = {
     shippingMethod?: Maybe<ShippingMethod>;
     shippingEligibilityCheckers: Array<ConfigurableOperation>;
     shippingCalculators: Array<ConfigurableOperation>;
-    taxRates: TaxRateList;
-    taxRate?: Maybe<TaxRate>;
     taxCategories: Array<TaxCategory>;
     taxCategory?: Maybe<TaxCategory>;
+    taxRates: TaxRateList;
+    taxRate?: Maybe<TaxRate>;
     zones: Array<Zone>;
     zone?: Maybe<Zone>;
 };
@@ -2476,6 +2486,10 @@ export type QueryCollectionArgs = {
     languageCode?: Maybe<LanguageCode>;
 };
 
+export type QueryCustomerGroupArgs = {
+    id: Scalars['ID'];
+};
+
 export type QueryCountriesArgs = {
     options?: Maybe<CountryListOptions>;
 };
@@ -2484,10 +2498,6 @@ export type QueryCountryArgs = {
     id: Scalars['ID'];
 };
 
-export type QueryCustomerGroupArgs = {
-    id: Scalars['ID'];
-};
-
 export type QueryCustomersArgs = {
     options?: Maybe<CustomerListOptions>;
 };
@@ -2579,6 +2589,10 @@ export type QueryShippingMethodArgs = {
     id: Scalars['ID'];
 };
 
+export type QueryTaxCategoryArgs = {
+    id: Scalars['ID'];
+};
+
 export type QueryTaxRatesArgs = {
     options?: Maybe<TaxRateListOptions>;
 };
@@ -2587,10 +2601,6 @@ export type QueryTaxRateArgs = {
     id: Scalars['ID'];
 };
 
-export type QueryTaxCategoryArgs = {
-    id: Scalars['ID'];
-};
-
 export type QueryZoneArgs = {
     id: Scalars['ID'];
 };
@@ -4167,6 +4177,14 @@ export type GetOrderHistoryQuery = { __typename?: 'Query' } & {
     >;
 };
 
+export type AddNoteToOrderMutationVariables = {
+    input: AddNoteToOrderInput;
+};
+
+export type AddNoteToOrderMutation = { __typename?: 'Mutation' } & {
+    addNoteToOrder: { __typename?: 'Order' } & Pick<Order, 'id'>;
+};
+
 export type AddOptionGroupToProductMutationVariables = {
     productId: Scalars['ID'];
     optionGroupId: Scalars['ID'];
@@ -5167,6 +5185,12 @@ export namespace GetOrderHistory {
     >;
 }
 
+export namespace AddNoteToOrder {
+    export type Variables = AddNoteToOrderMutationVariables;
+    export type Mutation = AddNoteToOrderMutation;
+    export type AddNoteToOrder = AddNoteToOrderMutation['addNoteToOrder'];
+}
+
 export namespace AddOptionGroupToProduct {
     export type Variables = AddOptionGroupToProductMutationVariables;
     export type Mutation = AddOptionGroupToProductMutation;

+ 35 - 0
packages/core/e2e/order.e2e-spec.ts

@@ -10,6 +10,7 @@ import { PaymentMethodHandler } from '../src/config/payment-method/payment-metho
 import { TEST_SETUP_TIMEOUT_MS } from './config/test-config';
 import { ORDER_FRAGMENT, ORDER_WITH_LINES_FRAGMENT } from './graphql/fragments';
 import {
+    AddNoteToOrder,
     CancelOrder,
     CreateFulfillment,
     GetCustomerList,
@@ -995,6 +996,32 @@ describe('Orders resolver', () => {
             ]);
         });
     });
+
+    it('addNoteToOrder', async () => {
+        const { addNoteToOrder } = await adminClient.query<AddNoteToOrder.Mutation, AddNoteToOrder.Variables>(ADD_NOTE_TO_ORDER, {
+            input: {
+                id: 'T_4',
+                note: 'A test note',
+            },
+        });
+
+        expect(addNoteToOrder.id).toBe('T_4');
+
+        const { order } = await adminClient.query<GetOrderHistory.Query, GetOrderHistory.Variables>(GET_ORDER_HISTORY, {
+            id: 'T_4',
+            options: {
+                skip: 6,
+            },
+        });
+
+        expect(order!.history.items.map(pick(['type', 'data']))).toEqual([
+            {
+                type: HistoryEntryType.ORDER_NOTE, data: {
+                    note: 'A test note',
+                },
+            },
+        ]);
+    });
 });
 
 /**
@@ -1244,3 +1271,11 @@ export const GET_ORDER_HISTORY = gql`
         }
     }
 `;
+
+export const ADD_NOTE_TO_ORDER = gql`
+    mutation AddNoteToOrder($input: AddNoteToOrderInput!) {
+        addNoteToOrder(input: $input) {
+            id
+        }
+    }
+`;

+ 9 - 1
packages/core/src/api/resolvers/admin/order.resolver.ts

@@ -1,9 +1,11 @@
 import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
 import {
+    MutationAddNoteToOrderArgs,
     MutationCancelOrderArgs,
     MutationFulfillOrderArgs,
     MutationRefundOrderArgs,
-    MutationSettlePaymentArgs, MutationSettleRefundArgs,
+    MutationSettlePaymentArgs,
+    MutationSettleRefundArgs,
     Permission,
     QueryOrderArgs,
     QueryOrdersArgs,
@@ -66,4 +68,10 @@ export class OrderResolver {
     async settleRefund(@Ctx() ctx: RequestContext, @Args() args: MutationSettleRefundArgs) {
         return this.orderService.settleRefund(ctx, args.input);
     }
+
+    @Mutation()
+    @Allow(Permission.UpdateOrder)
+    async addNoteToOrder(@Ctx() ctx: RequestContext, @Args() args: MutationAddNoteToOrderArgs) {
+        return this.orderService.addNoteToOrder(ctx, args.input);
+    }
 }

+ 6 - 0
packages/core/src/api/schema/admin-api/order.api.graphql

@@ -9,6 +9,7 @@ type Mutation {
     cancelOrder(input: CancelOrderInput!): Order!
     refundOrder(input: RefundOrderInput!): Refund!
     settleRefund(input: SettleRefundInput!): Refund!
+    addNoteToOrder(input: AddNoteToOrderInput!): Order!
 }
 
 # generated by generateListOptions function
@@ -43,3 +44,8 @@ input SettleRefundInput {
     transactionId: String!
 }
 
+input AddNoteToOrderInput {
+    id: ID!
+    note: String!
+}
+

+ 3 - 0
packages/core/src/service/services/history.service.ts

@@ -37,6 +37,9 @@ export type OrderHistoryEntryData = {
         to: RefundState;
         reason?: string;
     };
+    [HistoryEntryType.ORDER_NOTE]: {
+        note: string;
+    };
 };
 
 export interface CreateOrderHistoryEntryArgs<T extends keyof OrderHistoryEntryData> {

+ 14 - 0
packages/core/src/service/services/order.service.ts

@@ -1,6 +1,7 @@
 import { InjectConnection } from '@nestjs/typeorm';
 import { PaymentInput } from '@vendure/common/lib/generated-shop-types';
 import {
+    AddNoteToOrderInput,
     CancelOrderInput,
     CreateAddressInput,
     FulfillOrderInput,
@@ -532,6 +533,19 @@ export class OrderService {
         return this.connection.getRepository(Order).save(order);
     }
 
+    async addNoteToOrder(ctx: RequestContext, input: AddNoteToOrderInput): Promise<Order> {
+        const order = await this.getOrderOrThrow(ctx, input.id);
+        await this.historyService.createHistoryEntryForOrder({
+            ctx,
+            orderId: order.id,
+            type: HistoryEntryType.ORDER_NOTE,
+            data: {
+                note: input.note,
+            },
+        });
+        return order;
+    }
+
     /**
      * When a guest user with an anonymous Order signs in and has an existing Order associated with that Customer,
      * we need to reconcile the contents of the two orders.

File diff suppressed because it is too large
+ 0 - 0
schema-admin.json


Some files were not shown because too many files changed in this diff