瀏覽代碼

feat(core): Add isPublic flag to AddNoteToOrderInput

Relates to #180
Michael Bromley 6 年之前
父節點
當前提交
f97c3acb0d

+ 1 - 0
packages/admin-ui/src/app/common/generated-types.ts

@@ -22,6 +22,7 @@ export type Scalars = {
 export type AddNoteToOrderInput = {
   id: Scalars['ID'],
   note: Scalars['String'],
+  isPublic: Scalars['Boolean'],
 };
 
 export type Address = Node & {

+ 2 - 0
packages/common/src/generated-shop-types.ts

@@ -837,6 +837,7 @@ export type HistoryEntry = Node & {
     id: Scalars['ID'];
     createdAt: Scalars['DateTime'];
     updatedAt: Scalars['DateTime'];
+    isPublic: Scalars['Boolean'];
     type: HistoryEntryType;
     administrator?: Maybe<Administrator>;
     data: Scalars['JSON'];
@@ -845,6 +846,7 @@ export type HistoryEntry = Node & {
 export type HistoryEntryFilterParameter = {
     createdAt?: Maybe<DateOperators>;
     updatedAt?: Maybe<DateOperators>;
+    isPublic?: Maybe<BooleanOperators>;
     type?: Maybe<StringOperators>;
 };
 

+ 3 - 0
packages/common/src/generated-types.ts

@@ -21,6 +21,7 @@ export type Scalars = {
 export type AddNoteToOrderInput = {
   id: Scalars['ID'],
   note: Scalars['String'],
+  isPublic: Scalars['Boolean'],
 };
 
 export type Address = Node & {
@@ -1169,6 +1170,7 @@ export type HistoryEntry = Node & {
   id: Scalars['ID'],
   createdAt: Scalars['DateTime'],
   updatedAt: Scalars['DateTime'],
+  isPublic: Scalars['Boolean'],
   type: HistoryEntryType,
   administrator?: Maybe<Administrator>,
   data: Scalars['JSON'],
@@ -1177,6 +1179,7 @@ export type HistoryEntry = Node & {
 export type HistoryEntryFilterParameter = {
   createdAt?: Maybe<DateOperators>,
   updatedAt?: Maybe<DateOperators>,
+  isPublic?: Maybe<BooleanOperators>,
   type?: Maybe<StringOperators>,
 };
 

+ 3 - 0
packages/core/e2e/graphql/generated-e2e-admin-types.ts

@@ -21,6 +21,7 @@ export type Scalars = {
 export type AddNoteToOrderInput = {
     id: Scalars['ID'];
     note: Scalars['String'];
+    isPublic: Scalars['Boolean'];
 };
 
 export type Address = Node & {
@@ -1173,6 +1174,7 @@ export type HistoryEntry = Node & {
     id: Scalars['ID'];
     createdAt: Scalars['DateTime'];
     updatedAt: Scalars['DateTime'];
+    isPublic: Scalars['Boolean'];
     type: HistoryEntryType;
     administrator?: Maybe<Administrator>;
     data: Scalars['JSON'];
@@ -1181,6 +1183,7 @@ export type HistoryEntry = Node & {
 export type HistoryEntryFilterParameter = {
     createdAt?: Maybe<DateOperators>;
     updatedAt?: Maybe<DateOperators>;
+    isPublic?: Maybe<BooleanOperators>;
     type?: Maybe<StringOperators>;
 };
 

+ 2 - 0
packages/core/e2e/graphql/generated-e2e-shop-types.ts

@@ -837,6 +837,7 @@ export type HistoryEntry = Node & {
     id: Scalars['ID'];
     createdAt: Scalars['DateTime'];
     updatedAt: Scalars['DateTime'];
+    isPublic: Scalars['Boolean'];
     type: HistoryEntryType;
     administrator?: Maybe<Administrator>;
     data: Scalars['JSON'];
@@ -845,6 +846,7 @@ export type HistoryEntry = Node & {
 export type HistoryEntryFilterParameter = {
     createdAt?: Maybe<DateOperators>;
     updatedAt?: Maybe<DateOperators>;
+    isPublic?: Maybe<BooleanOperators>;
     type?: Maybe<StringOperators>;
 };
 

+ 93 - 28
packages/core/e2e/order.e2e-spec.ts

@@ -28,14 +28,14 @@ import {
     SettleRefund,
     UpdateProductVariants,
 } from './graphql/generated-e2e-admin-types';
-import { AddItemToOrder } from './graphql/generated-e2e-shop-types';
+import { AddItemToOrder, GetActiveOrder } from './graphql/generated-e2e-shop-types';
 import {
     GET_CUSTOMER_LIST,
     GET_PRODUCT_WITH_VARIANTS,
     GET_STOCK_MOVEMENT,
     UPDATE_PRODUCT_VARIANTS,
 } from './graphql/shared-definitions';
-import { ADD_ITEM_TO_ORDER } from './graphql/shop-definitions';
+import { ADD_ITEM_TO_ORDER, GET_ACTIVE_ORDER } from './graphql/shop-definitions';
 import { TestAdminClient, TestShopClient } from './test-client';
 import { TestServer } from './test-server';
 import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
@@ -52,7 +52,7 @@ describe('Orders resolver', () => {
         const token = await server.init(
             {
                 productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
-                customerCount: 2,
+                customerCount: 3,
             },
             {
                 paymentOptions: {
@@ -71,7 +71,7 @@ describe('Orders resolver', () => {
             GET_CUSTOMER_LIST,
             {
                 options: {
-                    take: 2,
+                    take: 3,
                 },
             },
         );
@@ -1092,37 +1092,102 @@ describe('Orders resolver', () => {
         });
     });
 
-    it('addNoteToOrder', async () => {
-        const { addNoteToOrder } = await adminClient.query<AddNoteToOrder.Mutation, AddNoteToOrder.Variables>(
-            ADD_NOTE_TO_ORDER,
-            {
+    describe('order notes', () => {
+        let orderId: string;
+
+        beforeAll(async () => {
+            const result = await createTestOrder(
+                adminClient,
+                shopClient,
+                customers[2].emailAddress,
+                password,
+            );
+
+            orderId = result.orderId;
+        });
+
+        it('private note', async () => {
+            const { addNoteToOrder } = await adminClient.query<
+                AddNoteToOrder.Mutation,
+                AddNoteToOrder.Variables
+            >(ADD_NOTE_TO_ORDER, {
                 input: {
-                    id: 'T_4',
-                    note: 'A test note',
+                    id: orderId,
+                    note: 'A private note',
+                    isPublic: false,
                 },
-            },
-        );
+            });
 
-        expect(addNoteToOrder.id).toBe('T_4');
+            expect(addNoteToOrder.id).toBe(orderId);
 
-        const { order } = await adminClient.query<GetOrderHistory.Query, GetOrderHistory.Variables>(
-            GET_ORDER_HISTORY,
-            {
-                id: 'T_4',
-                options: {
-                    skip: 2,
+            const { order } = await adminClient.query<GetOrderHistory.Query, GetOrderHistory.Variables>(
+                GET_ORDER_HISTORY,
+                {
+                    id: orderId,
+                    options: {
+                        skip: 0,
+                    },
                 },
-            },
-        );
+            );
 
-        expect(order!.history.items.map(pick(['type', 'data']))).toEqual([
-            {
-                type: HistoryEntryType.ORDER_NOTE,
-                data: {
-                    note: 'A test note',
+            expect(order!.history.items.map(pick(['type', 'data']))).toEqual([
+                {
+                    type: HistoryEntryType.ORDER_NOTE,
+                    data: {
+                        note: 'A private note',
+                    },
                 },
-            },
-        ]);
+            ]);
+
+            const { activeOrder } = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
+
+            expect(activeOrder!.history.items.map(pick(['type', 'data']))).toEqual([]);
+        });
+
+        it('public note', async () => {
+            const { addNoteToOrder } = await adminClient.query<
+                AddNoteToOrder.Mutation,
+                AddNoteToOrder.Variables
+            >(ADD_NOTE_TO_ORDER, {
+                input: {
+                    id: orderId,
+                    note: 'A public note',
+                    isPublic: true,
+                },
+            });
+
+            expect(addNoteToOrder.id).toBe(orderId);
+
+            const { order } = await adminClient.query<GetOrderHistory.Query, GetOrderHistory.Variables>(
+                GET_ORDER_HISTORY,
+                {
+                    id: orderId,
+                    options: {
+                        skip: 1,
+                    },
+                },
+            );
+
+            expect(order!.history.items.map(pick(['type', 'data']))).toEqual([
+                {
+                    type: HistoryEntryType.ORDER_NOTE,
+                    data: {
+                        note: 'A public note',
+                    },
+                },
+            ]);
+
+            const { activeOrder } = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
+
+            expect(activeOrder!.history.items.map(pick(['type', 'data']))).toEqual([
+                {
+                    type: HistoryEntryType.ORDER_NOTE,
+                    data: {
+                        note: 'A public note',
+                    },
+                },
+            ]);
+        });
     });
 });
 

+ 5 - 2
packages/core/src/api/resolvers/entity/order-entity.resolver.ts

@@ -5,6 +5,8 @@ import { Order } from '../../../entity/order/order.entity';
 import { HistoryService } from '../../../service/services/history.service';
 import { OrderService } from '../../../service/services/order.service';
 import { ShippingMethodService } from '../../../service/services/shipping-method.service';
+import { ApiType } from '../../common/get-api-type';
+import { Api } from '../../decorators/api.decorator';
 
 @Resolver('Order')
 export class OrderEntityResolver {
@@ -40,8 +42,9 @@ export class OrderEntityResolver {
     }
 
     @ResolveProperty()
-    async history(@Parent() order: Order, @Args() args: OrderHistoryArgs) {
-        return this.historyService.getHistoryForOrder(order.id, args.options || undefined);
+    async history(@Api() apiType: ApiType, @Parent() order: Order, @Args() args: OrderHistoryArgs) {
+        const publicOnly = apiType === 'shop';
+        return this.historyService.getHistoryForOrder(order.id, publicOnly, args.options || undefined);
     }
 
     @ResolveProperty()

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

@@ -50,5 +50,5 @@ input SettleRefundInput {
 input AddNoteToOrderInput {
     id: ID!
     note: String!
+    isPublic: Boolean!
 }
-

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

@@ -2,6 +2,7 @@ type HistoryEntry implements Node {
     id: ID!
     createdAt: DateTime!
     updatedAt: DateTime!
+    isPublic: Boolean!
     type: HistoryEntryType!
     administrator: Administrator
     data: JSON!

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

@@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
 import { InjectConnection } from '@nestjs/typeorm';
 import { HistoryEntryListOptions, HistoryEntryType } from '@vendure/common/lib/generated-types';
 import { ID, PaginatedList, Type } from '@vendure/common/lib/shared-types';
-import { Connection } from 'typeorm';
+import { Connection, FindConditions } from 'typeorm';
 
 import { RequestContext } from '../../api/common/request-context';
 import { HistoryEntry } from '../../entity/history-entry/history-entry.entity';
@@ -69,12 +69,14 @@ export class HistoryService {
 
     async getHistoryForOrder(
         orderId: ID,
+        publicOnly: boolean,
         options?: HistoryEntryListOptions,
     ): Promise<PaginatedList<OrderHistoryEntry>> {
         return this.listQueryBuilder
             .build((HistoryEntry as any) as Type<OrderHistoryEntry>, options, {
                 where: {
                     order: { id: orderId } as any,
+                    ...(publicOnly ? { isPublic: true } : {}),
                 },
                 relations: ['administrator'],
             })
@@ -87,6 +89,7 @@ export class HistoryService {
 
     async createHistoryEntryForOrder<T extends keyof OrderHistoryEntryData>(
         args: CreateOrderHistoryEntryArgs<T>,
+        isPublic = true,
     ): Promise<OrderHistoryEntry> {
         const { ctx, data, orderId, type } = args;
         const administrator = ctx.activeUserId
@@ -94,8 +97,7 @@ export class HistoryService {
             : undefined;
         const entry = new OrderHistoryEntry({
             type,
-            // TODO: figure out which should be public and not
-            isPublic: true,
+            isPublic,
             data: data as any,
             order: { id: orderId },
             administrator,

+ 10 - 7
packages/core/src/service/services/order.service.ts

@@ -667,14 +667,17 @@ export class OrderService {
 
     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,
+        await this.historyService.createHistoryEntryForOrder(
+            {
+                ctx,
+                orderId: order.id,
+                type: HistoryEntryType.ORDER_NOTE,
+                data: {
+                    note: input.note,
+                },
             },
-        });
+            input.isPublic,
+        );
         return order;
     }
 

文件差異過大導致無法顯示
+ 0 - 0
schema-admin.json


文件差異過大導致無法顯示
+ 0 - 0
schema-shop.json


部分文件因文件數量過多而無法顯示