Skip to content

Commit

Permalink
feat(core): Add isPublic flag to AddNoteToOrderInput
Browse files Browse the repository at this point in the history
Relates to #180
  • Loading branch information
michaelbromley committed Oct 11, 2019
1 parent 9f269fe commit f97c3ac
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 43 deletions.
1 change: 1 addition & 0 deletions packages/admin-ui/src/app/common/generated-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type Scalars = {
export type AddNoteToOrderInput = {
id: Scalars['ID'],
note: Scalars['String'],
isPublic: Scalars['Boolean'],
};

export type Address = Node & {
Expand Down
2 changes: 2 additions & 0 deletions packages/common/src/generated-shop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -845,6 +846,7 @@ export type HistoryEntry = Node & {
export type HistoryEntryFilterParameter = {
createdAt?: Maybe<DateOperators>;
updatedAt?: Maybe<DateOperators>;
isPublic?: Maybe<BooleanOperators>;
type?: Maybe<StringOperators>;
};

Expand Down
3 changes: 3 additions & 0 deletions packages/common/src/generated-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type Scalars = {
export type AddNoteToOrderInput = {
id: Scalars['ID'],
note: Scalars['String'],
isPublic: Scalars['Boolean'],
};

export type Address = Node & {
Expand Down Expand Up @@ -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'],
Expand All @@ -1177,6 +1179,7 @@ export type HistoryEntry = Node & {
export type HistoryEntryFilterParameter = {
createdAt?: Maybe<DateOperators>,
updatedAt?: Maybe<DateOperators>,
isPublic?: Maybe<BooleanOperators>,
type?: Maybe<StringOperators>,
};

Expand Down
3 changes: 3 additions & 0 deletions packages/core/e2e/graphql/generated-e2e-admin-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type Scalars = {
export type AddNoteToOrderInput = {
id: Scalars['ID'];
note: Scalars['String'];
isPublic: Scalars['Boolean'];
};

export type Address = Node & {
Expand Down Expand Up @@ -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'];
Expand All @@ -1181,6 +1183,7 @@ export type HistoryEntry = Node & {
export type HistoryEntryFilterParameter = {
createdAt?: Maybe<DateOperators>;
updatedAt?: Maybe<DateOperators>;
isPublic?: Maybe<BooleanOperators>;
type?: Maybe<StringOperators>;
};

Expand Down
2 changes: 2 additions & 0 deletions packages/core/e2e/graphql/generated-e2e-shop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -845,6 +846,7 @@ export type HistoryEntry = Node & {
export type HistoryEntryFilterParameter = {
createdAt?: Maybe<DateOperators>;
updatedAt?: Maybe<DateOperators>;
isPublic?: Maybe<BooleanOperators>;
type?: Maybe<StringOperators>;
};

Expand Down
121 changes: 93 additions & 28 deletions packages/core/e2e/order.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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: {
Expand All @@ -71,7 +71,7 @@ describe('Orders resolver', () => {
GET_CUSTOMER_LIST,
{
options: {
take: 2,
take: 3,
},
},
);
Expand Down Expand Up @@ -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',
},
},
]);
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/api/schema/admin-api/order.api.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ input SettleRefundInput {
input AddNoteToOrderInput {
id: ID!
note: String!
isPublic: Boolean!
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ type HistoryEntry implements Node {
id: ID!
createdAt: DateTime!
updatedAt: DateTime!
isPublic: Boolean!
type: HistoryEntryType!
administrator: Administrator
data: JSON!
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/service/services/history.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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'],
})
Expand All @@ -87,15 +89,15 @@ 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
? await this.administratorService.findOneByUserId(ctx.activeUserId)
: 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,
Expand Down
17 changes: 10 additions & 7 deletions packages/core/src/service/services/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion schema-admin.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion schema-shop.json

Large diffs are not rendered by default.

0 comments on commit f97c3ac

Please sign in to comment.