| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466 |
- /* tslint:disable:no-non-null-assertion */
- import { LanguageCode } from '@vendure/common/lib/generated-shop-types';
- import gql from 'graphql-tag';
- import path from 'path';
- import { HistoryEntryType, StockMovementType } from '../../common/lib/generated-types';
- import { pick } from '../../common/lib/pick';
- import { PaymentMethodHandler } from '../src/config/payment-method/payment-method-handler';
- import { TEST_SETUP_TIMEOUT_MS } from './config/test-config';
- import { ORDER_FRAGMENT, ORDER_WITH_LINES_FRAGMENT } from './graphql/fragments';
- import {
- AddNoteToOrder,
- CancelOrder,
- CreateFulfillment,
- GetCustomerList,
- GetOrder,
- GetOrderFulfillmentItems,
- GetOrderFulfillments,
- GetOrderHistory,
- GetOrderList,
- GetOrderListFulfillments,
- GetProductWithVariants,
- GetStockMovement,
- OrderItemFragment,
- RefundOrder,
- SettlePayment,
- SettleRefund,
- UpdateProductVariants,
- } from './graphql/generated-e2e-admin-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, 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';
- import { addPaymentToOrder, proceedToArrangingPayment } from './utils/test-order-utils';
- describe('Orders resolver', () => {
- const adminClient = new TestAdminClient();
- const shopClient = new TestShopClient();
- const server = new TestServer();
- let customers: GetCustomerList.Items[];
- const password = 'test';
- beforeAll(async () => {
- const token = await server.init(
- {
- productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
- customerCount: 3,
- },
- {
- paymentOptions: {
- paymentMethodHandlers: [
- twoStagePaymentMethod,
- failsToSettlePaymentMethod,
- singleStageRefundablePaymentMethod,
- ],
- },
- },
- );
- await adminClient.init();
- // Create a couple of orders to be queried
- const result = await adminClient.query<GetCustomerList.Query, GetCustomerList.Variables>(
- GET_CUSTOMER_LIST,
- {
- options: {
- take: 3,
- },
- },
- );
- customers = result.customers.items;
- await shopClient.asUserWithCredentials(customers[0].emailAddress, password);
- await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_1',
- quantity: 1,
- });
- await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_2',
- quantity: 1,
- });
- await shopClient.asUserWithCredentials(customers[1].emailAddress, password);
- await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_2',
- quantity: 1,
- });
- await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
- productVariantId: 'T_3',
- quantity: 3,
- });
- }, TEST_SETUP_TIMEOUT_MS);
- afterAll(async () => {
- await server.destroy();
- });
- it('orders', async () => {
- const result = await adminClient.query<GetOrderList.Query>(GET_ORDERS_LIST);
- expect(result.orders.items.map(o => o.id)).toEqual(['T_1', 'T_2']);
- });
- it('order', async () => {
- const result = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, { id: 'T_2' });
- expect(result.order!.id).toBe('T_2');
- });
- it('order history initially empty', async () => {
- const { order } = await adminClient.query<GetOrderHistory.Query, GetOrderHistory.Variables>(
- GET_ORDER_HISTORY,
- { id: 'T_1' },
- );
- expect(order!.history.totalItems).toBe(0);
- expect(order!.history.items).toEqual([]);
- });
- describe('payments', () => {
- it('settlePayment fails', async () => {
- await shopClient.asUserWithCredentials(customers[0].emailAddress, password);
- await proceedToArrangingPayment(shopClient);
- const order = await addPaymentToOrder(shopClient, failsToSettlePaymentMethod);
- expect(order.state).toBe('PaymentAuthorized');
- const payment = order.payments![0];
- const { settlePayment } = await adminClient.query<
- SettlePayment.Mutation,
- SettlePayment.Variables
- >(SETTLE_PAYMENT, {
- id: payment.id,
- });
- expect(settlePayment!.id).toBe(payment.id);
- expect(settlePayment!.state).toBe('Authorized');
- const result = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: order.id,
- });
- expect(result.order!.state).toBe('PaymentAuthorized');
- });
- it('settlePayment succeeds', async () => {
- await shopClient.asUserWithCredentials(customers[1].emailAddress, password);
- await proceedToArrangingPayment(shopClient);
- const order = await addPaymentToOrder(shopClient, twoStagePaymentMethod);
- expect(order.state).toBe('PaymentAuthorized');
- const payment = order.payments![0];
- const { settlePayment } = await adminClient.query<
- SettlePayment.Mutation,
- SettlePayment.Variables
- >(SETTLE_PAYMENT, {
- id: payment.id,
- });
- expect(settlePayment!.id).toBe(payment.id);
- expect(settlePayment!.state).toBe('Settled');
- // further metadata is combined into existing object
- expect(settlePayment!.metadata).toEqual({
- baz: 'quux',
- moreData: 42,
- });
- const result = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: order.id,
- });
- expect(result.order!.state).toBe('PaymentSettled');
- expect(result.order!.payments![0].state).toBe('Settled');
- });
- it('order history contains expected entries', async () => {
- const { order } = await adminClient.query<GetOrderHistory.Query, GetOrderHistory.Variables>(
- GET_ORDER_HISTORY,
- { id: 'T_2' },
- );
- expect(order!.history.items.map(pick(['type', 'data']))).toEqual([
- {
- type: HistoryEntryType.ORDER_STATE_TRANSITION,
- data: {
- from: 'AddingItems',
- to: 'ArrangingPayment',
- },
- },
- {
- type: HistoryEntryType.ORDER_PAYMENT_TRANSITION,
- data: {
- paymentId: 'T_2',
- from: 'Created',
- to: 'Authorized',
- },
- },
- {
- type: HistoryEntryType.ORDER_STATE_TRANSITION,
- data: {
- from: 'ArrangingPayment',
- to: 'PaymentAuthorized',
- },
- },
- {
- type: HistoryEntryType.ORDER_PAYMENT_TRANSITION,
- data: {
- paymentId: 'T_2',
- from: 'Authorized',
- to: 'Settled',
- },
- },
- {
- type: HistoryEntryType.ORDER_STATE_TRANSITION,
- data: {
- from: 'PaymentAuthorized',
- to: 'PaymentSettled',
- },
- },
- ]);
- });
- });
- describe('fulfillment', () => {
- it(
- 'throws if Order is not in "PaymentSettled" state',
- assertThrowsWithMessage(async () => {
- const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: 'T_1',
- });
- expect(order!.state).toBe('PaymentAuthorized');
- await adminClient.query<CreateFulfillment.Mutation, CreateFulfillment.Variables>(
- CREATE_FULFILLMENT,
- {
- input: {
- lines: order!.lines.map(l => ({ orderLineId: l.id, quantity: l.quantity })),
- method: 'Test',
- },
- },
- );
- }, 'One or more OrderItems belong to an Order which is in an invalid state'),
- );
- it(
- 'throws if lines is empty',
- assertThrowsWithMessage(async () => {
- const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: 'T_2',
- });
- expect(order!.state).toBe('PaymentSettled');
- await adminClient.query<CreateFulfillment.Mutation, CreateFulfillment.Variables>(
- CREATE_FULFILLMENT,
- {
- input: {
- lines: [],
- method: 'Test',
- },
- },
- );
- }, 'Nothing to fulfill'),
- );
- it(
- 'throws if all quantities are zero',
- assertThrowsWithMessage(async () => {
- const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: 'T_2',
- });
- expect(order!.state).toBe('PaymentSettled');
- await adminClient.query<CreateFulfillment.Mutation, CreateFulfillment.Variables>(
- CREATE_FULFILLMENT,
- {
- input: {
- lines: order!.lines.map(l => ({ orderLineId: l.id, quantity: 0 })),
- method: 'Test',
- },
- },
- );
- }, 'Nothing to fulfill'),
- );
- it('creates a partial fulfillment', async () => {
- const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: 'T_2',
- });
- expect(order!.state).toBe('PaymentSettled');
- const lines = order!.lines;
- const { fulfillOrder } = await adminClient.query<
- CreateFulfillment.Mutation,
- CreateFulfillment.Variables
- >(CREATE_FULFILLMENT, {
- input: {
- lines: lines.map(l => ({ orderLineId: l.id, quantity: 1 })),
- method: 'Test1',
- trackingCode: '111',
- },
- });
- expect(fulfillOrder!.method).toBe('Test1');
- expect(fulfillOrder!.trackingCode).toBe('111');
- expect(fulfillOrder!.orderItems).toEqual([
- { id: lines[0].items[0].id },
- { id: lines[1].items[0].id },
- ]);
- const result = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: 'T_2',
- });
- expect(result.order!.state).toBe('PartiallyFulfilled');
- expect(result.order!.lines[0].items[0].fulfillment!.id).toBe(fulfillOrder!.id);
- expect(result.order!.lines[1].items[2].fulfillment!.id).toBe(fulfillOrder!.id);
- expect(result.order!.lines[1].items[1].fulfillment).toBeNull();
- expect(result.order!.lines[1].items[0].fulfillment).toBeNull();
- });
- it('creates a second partial fulfillment', async () => {
- const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: 'T_2',
- });
- expect(order!.state).toBe('PartiallyFulfilled');
- const lines = order!.lines;
- const { fulfillOrder } = await adminClient.query<
- CreateFulfillment.Mutation,
- CreateFulfillment.Variables
- >(CREATE_FULFILLMENT, {
- input: {
- lines: [{ orderLineId: lines[1].id, quantity: 1 }],
- method: 'Test2',
- trackingCode: '222',
- },
- });
- const result = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: 'T_2',
- });
- // expect(result.order!.lines).toEqual({});
- expect(result.order!.state).toBe('PartiallyFulfilled');
- expect(result.order!.lines[1].items[2].fulfillment).not.toBeNull();
- expect(result.order!.lines[1].items[1].fulfillment).not.toBeNull();
- expect(result.order!.lines[1].items[0].fulfillment).toBeNull();
- });
- it(
- 'throws if an OrderItem already part of a Fulfillment',
- assertThrowsWithMessage(async () => {
- const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: 'T_2',
- });
- expect(order!.state).toBe('PartiallyFulfilled');
- await adminClient.query<CreateFulfillment.Mutation, CreateFulfillment.Variables>(
- CREATE_FULFILLMENT,
- {
- input: {
- method: 'Test',
- lines: [
- {
- orderLineId: order!.lines[0].id,
- quantity: 1,
- },
- ],
- },
- },
- );
- }, 'One or more OrderItems have already been fulfilled'),
- );
- it('completes fulfillment', async () => {
- const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: 'T_2',
- });
- expect(order!.state).toBe('PartiallyFulfilled');
- const orderItems = order!.lines.reduce(
- (items, line) => [...items, ...line.items],
- [] as OrderItemFragment[],
- );
- const { fulfillOrder } = await adminClient.query<
- CreateFulfillment.Mutation,
- CreateFulfillment.Variables
- >(CREATE_FULFILLMENT, {
- input: {
- lines: [
- {
- orderLineId: order!.lines[1].id,
- quantity: 1,
- },
- ],
- method: 'Test3',
- trackingCode: '333',
- },
- });
- expect(fulfillOrder!.method).toBe('Test3');
- expect(fulfillOrder!.trackingCode).toBe('333');
- expect(fulfillOrder!.orderItems).toEqual([{ id: orderItems[1].id }]);
- const result = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: 'T_2',
- });
- expect(result.order!.state).toBe('Fulfilled');
- });
- it('order history contains expected entries', async () => {
- const { order } = await adminClient.query<GetOrderHistory.Query, GetOrderHistory.Variables>(
- GET_ORDER_HISTORY,
- {
- id: 'T_2',
- options: {
- skip: 5,
- },
- },
- );
- expect(order!.history.items.map(pick(['type', 'data']))).toEqual([
- {
- type: HistoryEntryType.ORDER_FULLFILLMENT,
- data: {
- fulfillmentId: 'T_1',
- },
- },
- {
- type: HistoryEntryType.ORDER_STATE_TRANSITION,
- data: {
- from: 'PaymentSettled',
- to: 'PartiallyFulfilled',
- },
- },
- {
- type: HistoryEntryType.ORDER_FULLFILLMENT,
- data: {
- fulfillmentId: 'T_2',
- },
- },
- {
- type: HistoryEntryType.ORDER_STATE_TRANSITION,
- data: {
- from: 'PartiallyFulfilled',
- to: 'PartiallyFulfilled',
- },
- },
- {
- type: HistoryEntryType.ORDER_FULLFILLMENT,
- data: {
- fulfillmentId: 'T_3',
- },
- },
- {
- type: HistoryEntryType.ORDER_STATE_TRANSITION,
- data: {
- from: 'PartiallyFulfilled',
- to: 'Fulfilled',
- },
- },
- ]);
- });
- it('order.fullfillments resolver for single order', async () => {
- const { order } = await adminClient.query<
- GetOrderFulfillments.Query,
- GetOrderFulfillments.Variables
- >(GET_ORDER_FULFILLMENTS, {
- id: 'T_2',
- });
- expect(order!.fulfillments).toEqual([
- { id: 'T_1', method: 'Test1' },
- { id: 'T_2', method: 'Test2' },
- { id: 'T_3', method: 'Test3' },
- ]);
- });
- it('order.fullfillments resolver for order list', async () => {
- const { orders } = await adminClient.query<GetOrderListFulfillments.Query>(
- GET_ORDER_LIST_FULFILLMENTS,
- );
- expect(orders.items[0].fulfillments).toEqual([]);
- expect(orders.items[1].fulfillments).toEqual([
- { id: 'T_1', method: 'Test1' },
- { id: 'T_2', method: 'Test2' },
- { id: 'T_3', method: 'Test3' },
- ]);
- });
- it('order.fullfillments.orderItems resolver', async () => {
- const { order } = await adminClient.query<
- GetOrderFulfillmentItems.Query,
- GetOrderFulfillmentItems.Variables
- >(GET_ORDER_FULFILLMENT_ITEMS, {
- id: 'T_2',
- });
- expect(order!.fulfillments![0].orderItems).toEqual([{ id: 'T_3' }, { id: 'T_4' }]);
- expect(order!.fulfillments![1].orderItems).toEqual([{ id: 'T_5' }]);
- });
- });
- describe('cancellation by orderId', () => {
- it('cancel from AddingItems state', async () => {
- const testOrder = await createTestOrder(
- adminClient,
- shopClient,
- customers[0].emailAddress,
- password,
- );
- const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: testOrder.orderId,
- });
- expect(order!.state).toBe('AddingItems');
- const { cancelOrder } = await adminClient.query<CancelOrder.Mutation, CancelOrder.Variables>(
- CANCEL_ORDER,
- {
- input: {
- orderId: testOrder.orderId,
- },
- },
- );
- const { order: order2 } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: testOrder.orderId,
- });
- expect(order2!.state).toBe('Cancelled');
- expect(order2!.active).toBe(false);
- await assertNoStockMovementsCreated(testOrder.product.id);
- });
- it('cancel from ArrangingPayment state', async () => {
- const testOrder = await createTestOrder(
- adminClient,
- shopClient,
- customers[0].emailAddress,
- password,
- );
- await proceedToArrangingPayment(shopClient);
- const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: testOrder.orderId,
- });
- expect(order!.state).toBe('ArrangingPayment');
- await adminClient.query<CancelOrder.Mutation, CancelOrder.Variables>(CANCEL_ORDER, {
- input: {
- orderId: testOrder.orderId,
- },
- });
- const { order: order2 } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: testOrder.orderId,
- });
- expect(order2!.state).toBe('Cancelled');
- expect(order2!.active).toBe(false);
- await assertNoStockMovementsCreated(testOrder.product.id);
- });
- it('cancel from PaymentAuthorized state', async () => {
- const testOrder = await createTestOrder(
- adminClient,
- shopClient,
- customers[0].emailAddress,
- password,
- );
- await proceedToArrangingPayment(shopClient);
- const order = await addPaymentToOrder(shopClient, failsToSettlePaymentMethod);
- expect(order.state).toBe('PaymentAuthorized');
- const result1 = await adminClient.query<GetStockMovement.Query, GetStockMovement.Variables>(
- GET_STOCK_MOVEMENT,
- {
- id: 'T_3',
- },
- );
- let variant1 = result1.product!.variants[0];
- expect(variant1.stockOnHand).toBe(98);
- expect(variant1.stockMovements.items.map(pick(['type', 'quantity']))).toEqual([
- { type: StockMovementType.ADJUSTMENT, quantity: 100 },
- { type: StockMovementType.SALE, quantity: -2 },
- ]);
- const { cancelOrder } = await adminClient.query<CancelOrder.Mutation, CancelOrder.Variables>(
- CANCEL_ORDER,
- {
- input: {
- orderId: testOrder.orderId,
- },
- },
- );
- expect(cancelOrder.lines.map(l => l.items.map(pick(['id', 'cancelled'])))).toEqual([
- [{ id: 'T_11', cancelled: true }, { id: 'T_12', cancelled: true }],
- ]);
- const { order: order2 } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: testOrder.orderId,
- });
- expect(order2!.active).toBe(false);
- expect(order2!.state).toBe('Cancelled');
- const result2 = await adminClient.query<GetStockMovement.Query, GetStockMovement.Variables>(
- GET_STOCK_MOVEMENT,
- {
- id: 'T_3',
- },
- );
- variant1 = result2.product!.variants[0];
- expect(variant1.stockOnHand).toBe(100);
- expect(variant1.stockMovements.items.map(pick(['type', 'quantity']))).toEqual([
- { type: StockMovementType.ADJUSTMENT, quantity: 100 },
- { type: StockMovementType.SALE, quantity: -2 },
- { type: StockMovementType.CANCELLATION, quantity: 1 },
- { type: StockMovementType.CANCELLATION, quantity: 1 },
- ]);
- });
- async function assertNoStockMovementsCreated(productId: string) {
- const result = await adminClient.query<GetStockMovement.Query, GetStockMovement.Variables>(
- GET_STOCK_MOVEMENT,
- {
- id: productId,
- },
- );
- const variant2 = result.product!.variants[0];
- expect(variant2.stockOnHand).toBe(100);
- expect(variant2.stockMovements.items.map(pick(['type', 'quantity']))).toEqual([
- { type: StockMovementType.ADJUSTMENT, quantity: 100 },
- ]);
- }
- });
- describe('cancellation by OrderLine', () => {
- let orderId: string;
- let product: GetProductWithVariants.Product;
- let productVariantId: string;
- beforeAll(async () => {
- const result = await createTestOrder(
- adminClient,
- shopClient,
- customers[0].emailAddress,
- password,
- );
- orderId = result.orderId;
- product = result.product;
- productVariantId = result.productVariantId;
- });
- it(
- 'cannot cancel from AddingItems state',
- assertThrowsWithMessage(async () => {
- const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: orderId,
- });
- expect(order!.state).toBe('AddingItems');
- await adminClient.query<CancelOrder.Mutation, CancelOrder.Variables>(CANCEL_ORDER, {
- input: {
- orderId,
- lines: order!.lines.map(l => ({ orderLineId: l.id, quantity: 1 })),
- },
- });
- }, 'Cannot cancel OrderLines from an Order in the "AddingItems" state'),
- );
- it(
- 'cannot cancel from ArrangingPayment state',
- assertThrowsWithMessage(async () => {
- await proceedToArrangingPayment(shopClient);
- const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: orderId,
- });
- expect(order!.state).toBe('ArrangingPayment');
- await adminClient.query<CancelOrder.Mutation, CancelOrder.Variables>(CANCEL_ORDER, {
- input: {
- orderId,
- lines: order!.lines.map(l => ({ orderLineId: l.id, quantity: 1 })),
- },
- });
- }, 'Cannot cancel OrderLines from an Order in the "ArrangingPayment" state'),
- );
- it(
- 'throws if lines are ampty',
- assertThrowsWithMessage(async () => {
- const order = await addPaymentToOrder(shopClient, twoStagePaymentMethod);
- expect(order.state).toBe('PaymentAuthorized');
- await adminClient.query<CancelOrder.Mutation, CancelOrder.Variables>(CANCEL_ORDER, {
- input: {
- orderId,
- lines: [],
- },
- });
- }, 'Nothing to cancel'),
- );
- it(
- 'throws if all quantities zero',
- assertThrowsWithMessage(async () => {
- const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: orderId,
- });
- await adminClient.query<CancelOrder.Mutation, CancelOrder.Variables>(CANCEL_ORDER, {
- input: {
- orderId,
- lines: order!.lines.map(l => ({ orderLineId: l.id, quantity: 0 })),
- },
- });
- }, 'Nothing to cancel'),
- );
- it('partial cancellation', async () => {
- const result1 = await adminClient.query<GetStockMovement.Query, GetStockMovement.Variables>(
- GET_STOCK_MOVEMENT,
- {
- id: product.id,
- },
- );
- const variant1 = result1.product!.variants[0];
- expect(variant1.stockOnHand).toBe(98);
- expect(variant1.stockMovements.items.map(pick(['type', 'quantity']))).toEqual([
- { type: StockMovementType.ADJUSTMENT, quantity: 100 },
- { type: StockMovementType.SALE, quantity: -2 },
- { type: StockMovementType.CANCELLATION, quantity: 1 },
- { type: StockMovementType.CANCELLATION, quantity: 1 },
- { type: StockMovementType.SALE, quantity: -2 },
- ]);
- const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: orderId,
- });
- const { cancelOrder } = await adminClient.query<CancelOrder.Mutation, CancelOrder.Variables>(
- CANCEL_ORDER,
- {
- input: {
- orderId,
- lines: order!.lines.map(l => ({ orderLineId: l.id, quantity: 1 })),
- reason: 'cancel reason 1',
- },
- },
- );
- expect(cancelOrder.lines[0].quantity).toBe(1);
- expect(cancelOrder.lines[0].items).toEqual([
- { id: 'T_13', cancelled: true },
- { id: 'T_14', cancelled: false },
- ]);
- const { order: order2 } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: orderId,
- });
- expect(order2!.state).toBe('PaymentAuthorized');
- expect(order2!.lines[0].quantity).toBe(1);
- const result2 = await adminClient.query<GetStockMovement.Query, GetStockMovement.Variables>(
- GET_STOCK_MOVEMENT,
- {
- id: product.id,
- },
- );
- const variant2 = result2.product!.variants[0];
- expect(variant2.stockOnHand).toBe(99);
- expect(variant2.stockMovements.items.map(pick(['type', 'quantity']))).toEqual([
- { type: StockMovementType.ADJUSTMENT, quantity: 100 },
- { type: StockMovementType.SALE, quantity: -2 },
- { type: StockMovementType.CANCELLATION, quantity: 1 },
- { type: StockMovementType.CANCELLATION, quantity: 1 },
- { type: StockMovementType.SALE, quantity: -2 },
- { type: StockMovementType.CANCELLATION, quantity: 1 },
- ]);
- });
- it('complete cancellation', async () => {
- const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: orderId,
- });
- await adminClient.query<CancelOrder.Mutation, CancelOrder.Variables>(CANCEL_ORDER, {
- input: {
- orderId,
- lines: order!.lines.map(l => ({ orderLineId: l.id, quantity: 1 })),
- reason: 'cancel reason 2',
- },
- });
- const { order: order2 } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: orderId,
- });
- expect(order2!.state).toBe('Cancelled');
- const result = await adminClient.query<GetStockMovement.Query, GetStockMovement.Variables>(
- GET_STOCK_MOVEMENT,
- {
- id: product.id,
- },
- );
- const variant2 = result.product!.variants[0];
- expect(variant2.stockOnHand).toBe(100);
- expect(variant2.stockMovements.items.map(pick(['type', 'quantity']))).toEqual([
- { type: StockMovementType.ADJUSTMENT, quantity: 100 },
- { type: StockMovementType.SALE, quantity: -2 },
- { type: StockMovementType.CANCELLATION, quantity: 1 },
- { type: StockMovementType.CANCELLATION, quantity: 1 },
- { type: StockMovementType.SALE, quantity: -2 },
- { type: StockMovementType.CANCELLATION, quantity: 1 },
- { type: StockMovementType.CANCELLATION, quantity: 1 },
- ]);
- });
- it('order history contains expected entries', async () => {
- 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_STATE_TRANSITION,
- data: {
- from: 'AddingItems',
- to: 'ArrangingPayment',
- },
- },
- {
- type: HistoryEntryType.ORDER_PAYMENT_TRANSITION,
- data: {
- paymentId: 'T_4',
- from: 'Created',
- to: 'Authorized',
- },
- },
- {
- type: HistoryEntryType.ORDER_STATE_TRANSITION,
- data: {
- from: 'ArrangingPayment',
- to: 'PaymentAuthorized',
- },
- },
- {
- type: HistoryEntryType.ORDER_CANCELLATION,
- data: {
- orderItemIds: ['T_13'],
- reason: 'cancel reason 1',
- },
- },
- {
- type: HistoryEntryType.ORDER_CANCELLATION,
- data: {
- orderItemIds: ['T_14'],
- reason: 'cancel reason 2',
- },
- },
- {
- type: HistoryEntryType.ORDER_STATE_TRANSITION,
- data: {
- from: 'PaymentAuthorized',
- to: 'Cancelled',
- },
- },
- ]);
- });
- });
- describe('refunds', () => {
- let orderId: string;
- let product: GetProductWithVariants.Product;
- let productVariantId: string;
- let paymentId: string;
- let refundId: string;
- beforeAll(async () => {
- const result = await createTestOrder(
- adminClient,
- shopClient,
- customers[0].emailAddress,
- password,
- );
- orderId = result.orderId;
- product = result.product;
- productVariantId = result.productVariantId;
- });
- it(
- 'cannot refund from PaymentAuthorized state',
- assertThrowsWithMessage(async () => {
- await proceedToArrangingPayment(shopClient);
- const order = await addPaymentToOrder(shopClient, twoStagePaymentMethod);
- expect(order.state).toBe('PaymentAuthorized');
- paymentId = order.payments![0].id;
- await adminClient.query<RefundOrder.Mutation, RefundOrder.Variables>(REFUND_ORDER, {
- input: {
- lines: order.lines.map(l => ({ orderLineId: l.id, quantity: 1 })),
- shipping: 0,
- adjustment: 0,
- paymentId,
- },
- });
- }, 'Cannot refund an Order in the "PaymentAuthorized" state'),
- );
- it(
- 'throws if no lines and no shipping',
- assertThrowsWithMessage(async () => {
- const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: orderId,
- });
- const { settlePayment } = await adminClient.query<
- SettlePayment.Mutation,
- SettlePayment.Variables
- >(SETTLE_PAYMENT, {
- id: order!.payments![0].id,
- });
- expect(settlePayment!.state).toBe('Settled');
- await adminClient.query<RefundOrder.Mutation, RefundOrder.Variables>(REFUND_ORDER, {
- input: {
- lines: order!.lines.map(l => ({ orderLineId: l.id, quantity: 0 })),
- shipping: 0,
- adjustment: 0,
- paymentId,
- },
- });
- }, 'Nothing to refund'),
- );
- it(
- 'throws if paymentId not valid',
- assertThrowsWithMessage(async () => {
- const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: orderId,
- });
- const { refundOrder } = await adminClient.query<RefundOrder.Mutation, RefundOrder.Variables>(
- REFUND_ORDER,
- {
- input: {
- lines: [],
- shipping: 100,
- adjustment: 0,
- paymentId: 'T_999',
- },
- },
- );
- }, "No Payment with the id '999' could be found"),
- );
- it(
- 'throws if payment and order lines do not belong to the same Order',
- assertThrowsWithMessage(async () => {
- const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: orderId,
- });
- const { refundOrder } = await adminClient.query<RefundOrder.Mutation, RefundOrder.Variables>(
- REFUND_ORDER,
- {
- input: {
- lines: order!.lines.map(l => ({ orderLineId: l.id, quantity: l.quantity })),
- shipping: 100,
- adjustment: 0,
- paymentId: 'T_1',
- },
- },
- );
- }, 'The Payment and OrderLines do not belong to the same Order'),
- );
- it('creates a Refund to be manually settled', async () => {
- const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: orderId,
- });
- const { refundOrder } = await adminClient.query<RefundOrder.Mutation, RefundOrder.Variables>(
- REFUND_ORDER,
- {
- input: {
- lines: order!.lines.map(l => ({ orderLineId: l.id, quantity: l.quantity })),
- shipping: order!.shipping,
- adjustment: 0,
- reason: 'foo',
- paymentId,
- },
- },
- );
- expect(refundOrder.shipping).toBe(order!.shipping);
- expect(refundOrder.items).toBe(order!.subTotal);
- expect(refundOrder.total).toBe(order!.total);
- expect(refundOrder.transactionId).toBe(null);
- expect(refundOrder.state).toBe('Pending');
- refundId = refundOrder.id;
- });
- it(
- 'throws if attempting to refund the same item more than once',
- assertThrowsWithMessage(async () => {
- const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
- id: orderId,
- });
- const { refundOrder } = await adminClient.query<RefundOrder.Mutation, RefundOrder.Variables>(
- REFUND_ORDER,
- {
- input: {
- lines: order!.lines.map(l => ({ orderLineId: l.id, quantity: l.quantity })),
- shipping: order!.shipping,
- adjustment: 0,
- paymentId,
- },
- },
- );
- }, 'Cannot refund an OrderItem which has already been refunded'),
- );
- it('manually settle a Refund', async () => {
- const { settleRefund } = await adminClient.query<SettleRefund.Mutation, SettleRefund.Variables>(
- SETTLE_REFUND,
- {
- input: {
- id: refundId,
- transactionId: 'aaabbb',
- },
- },
- );
- expect(settleRefund.state).toBe('Settled');
- expect(settleRefund.transactionId).toBe('aaabbb');
- });
- it('order history contains expected entries', async () => {
- 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_STATE_TRANSITION,
- data: {
- from: 'AddingItems',
- to: 'ArrangingPayment',
- },
- },
- {
- type: HistoryEntryType.ORDER_PAYMENT_TRANSITION,
- data: {
- paymentId: 'T_5',
- from: 'Created',
- to: 'Authorized',
- },
- },
- {
- type: HistoryEntryType.ORDER_STATE_TRANSITION,
- data: {
- from: 'ArrangingPayment',
- to: 'PaymentAuthorized',
- },
- },
- {
- type: HistoryEntryType.ORDER_PAYMENT_TRANSITION,
- data: {
- paymentId: 'T_5',
- from: 'Authorized',
- to: 'Settled',
- },
- },
- {
- type: HistoryEntryType.ORDER_STATE_TRANSITION,
- data: {
- from: 'PaymentAuthorized',
- to: 'PaymentSettled',
- },
- },
- {
- type: HistoryEntryType.ORDER_REFUND_TRANSITION,
- data: {
- refundId: 'T_1',
- reason: 'foo',
- from: 'Pending',
- to: 'Settled',
- },
- },
- ]);
- });
- });
- 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: orderId,
- note: 'A private note',
- isPublic: false,
- },
- });
- expect(addNoteToOrder.id).toBe(orderId);
- 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 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',
- },
- },
- ]);
- });
- });
- });
- /**
- * A two-stage (authorize, capture) payment method, with no createRefund method.
- */
- const twoStagePaymentMethod = new PaymentMethodHandler({
- code: 'authorize-only-payment-method',
- description: [{ languageCode: LanguageCode.en, value: 'Test Payment Method' }],
- args: {},
- createPayment: (order, args, metadata) => {
- return {
- amount: order.total,
- state: 'Authorized',
- transactionId: '12345',
- metadata,
- };
- },
- settlePayment: () => {
- return {
- success: true,
- metadata: {
- moreData: 42,
- },
- };
- },
- });
- /**
- * A payment method which includes a createRefund method.
- */
- const singleStageRefundablePaymentMethod = new PaymentMethodHandler({
- code: 'single-stage-refundable-payment-method',
- description: [{ languageCode: LanguageCode.en, value: 'Test Payment Method' }],
- args: {},
- createPayment: (order, args, metadata) => {
- return {
- amount: order.total,
- state: 'Settled',
- transactionId: '12345',
- metadata,
- };
- },
- settlePayment: () => {
- return { success: true };
- },
- createRefund: (input, total, order, payment, args) => {
- return {
- amount: total,
- state: 'Settled',
- transactionId: 'abc123',
- };
- },
- });
- /**
- * A payment method where calling `settlePayment` always fails.
- */
- const failsToSettlePaymentMethod = new PaymentMethodHandler({
- code: 'fails-to-settle-payment-method',
- description: [{ languageCode: LanguageCode.en, value: 'Test Payment Method' }],
- args: {},
- createPayment: (order, args, metadata) => {
- return {
- amount: order.total,
- state: 'Authorized',
- transactionId: '12345',
- metadata,
- };
- },
- settlePayment: () => {
- return {
- success: false,
- errorMessage: 'Something went horribly wrong',
- };
- },
- });
- async function createTestOrder(
- adminClient: TestAdminClient,
- shopClient: TestShopClient,
- emailAddress: string,
- password: string,
- ): Promise<{
- orderId: string;
- product: GetProductWithVariants.Product;
- productVariantId: string;
- }> {
- const result = await adminClient.query<GetProductWithVariants.Query, GetProductWithVariants.Variables>(
- GET_PRODUCT_WITH_VARIANTS,
- {
- id: 'T_3',
- },
- );
- const product = result.product!;
- const productVariantId = product.variants[0].id;
- // Set the ProductVariant to trackInventory
- const { updateProductVariants } = await adminClient.query<
- UpdateProductVariants.Mutation,
- UpdateProductVariants.Variables
- >(UPDATE_PRODUCT_VARIANTS, {
- input: [
- {
- id: productVariantId,
- trackInventory: true,
- },
- ],
- });
- // Add the ProductVariant to the Order
- await shopClient.asUserWithCredentials(emailAddress, password);
- const { addItemToOrder } = await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(
- ADD_ITEM_TO_ORDER,
- {
- productVariantId,
- quantity: 2,
- },
- );
- const orderId = addItemToOrder!.id;
- return { product, productVariantId, orderId };
- }
- export const GET_ORDERS_LIST = gql`
- query GetOrderList($options: OrderListOptions) {
- orders(options: $options) {
- items {
- ...Order
- }
- totalItems
- }
- }
- ${ORDER_FRAGMENT}
- `;
- export const GET_ORDER = gql`
- query GetOrder($id: ID!) {
- order(id: $id) {
- ...OrderWithLines
- }
- }
- ${ORDER_WITH_LINES_FRAGMENT}
- `;
- export const SETTLE_PAYMENT = gql`
- mutation SettlePayment($id: ID!) {
- settlePayment(id: $id) {
- id
- state
- metadata
- }
- }
- `;
- export const CREATE_FULFILLMENT = gql`
- mutation CreateFulfillment($input: FulfillOrderInput!) {
- fulfillOrder(input: $input) {
- id
- method
- trackingCode
- orderItems {
- id
- }
- }
- }
- `;
- export const GET_ORDER_FULFILLMENTS = gql`
- query GetOrderFulfillments($id: ID!) {
- order(id: $id) {
- id
- fulfillments {
- id
- method
- }
- }
- }
- `;
- export const GET_ORDER_LIST_FULFILLMENTS = gql`
- query GetOrderListFulfillments {
- orders {
- items {
- id
- fulfillments {
- id
- method
- }
- }
- }
- }
- `;
- export const GET_ORDER_FULFILLMENT_ITEMS = gql`
- query GetOrderFulfillmentItems($id: ID!) {
- order(id: $id) {
- id
- fulfillments {
- id
- orderItems {
- id
- }
- }
- }
- }
- `;
- export const CANCEL_ORDER = gql`
- mutation CancelOrder($input: CancelOrderInput!) {
- cancelOrder(input: $input) {
- id
- lines {
- quantity
- items {
- id
- cancelled
- }
- }
- }
- }
- `;
- export const REFUND_ORDER = gql`
- mutation RefundOrder($input: RefundOrderInput!) {
- refundOrder(input: $input) {
- id
- state
- items
- transactionId
- shipping
- total
- metadata
- }
- }
- `;
- export const SETTLE_REFUND = gql`
- mutation SettleRefund($input: SettleRefundInput!) {
- settleRefund(input: $input) {
- id
- state
- items
- transactionId
- shipping
- total
- metadata
- }
- }
- `;
- export const GET_ORDER_HISTORY = gql`
- query GetOrderHistory($id: ID!, $options: HistoryEntryListOptions) {
- order(id: $id) {
- id
- history(options: $options) {
- totalItems
- items {
- id
- type
- administrator {
- id
- }
- data
- }
- }
- }
- }
- `;
- export const ADD_NOTE_TO_ORDER = gql`
- mutation AddNoteToOrder($input: AddNoteToOrderInput!) {
- addNoteToOrder(input: $input) {
- id
- }
- }
- `;
|