order.e2e-spec.ts 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  1. /* tslint:disable:no-non-null-assertion */
  2. import { pick } from '@vendure/common/lib/pick';
  3. import { createTestEnvironment, SimpleGraphQLClient } from '@vendure/testing';
  4. import gql from 'graphql-tag';
  5. import path from 'path';
  6. import { initialData } from '../../../e2e-common/e2e-initial-data';
  7. import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  8. import {
  9. failsToSettlePaymentMethod,
  10. onTransitionSpy,
  11. singleStageRefundablePaymentMethod,
  12. twoStagePaymentMethod,
  13. } from './fixtures/test-payment-methods';
  14. import { ORDER_FRAGMENT } from './graphql/fragments';
  15. import {
  16. AddNoteToOrder,
  17. CancelOrder,
  18. CreateFulfillment,
  19. DeleteOrderNote,
  20. GetCustomerList,
  21. GetOrder,
  22. GetOrderFulfillmentItems,
  23. GetOrderFulfillments,
  24. GetOrderHistory,
  25. GetOrderList,
  26. GetOrderListFulfillments,
  27. GetProductWithVariants,
  28. GetStockMovement,
  29. HistoryEntryType,
  30. OrderItemFragment,
  31. RefundOrder,
  32. SettlePayment,
  33. SettleRefund,
  34. StockMovementType,
  35. UpdateOrderNote,
  36. UpdateProductVariants,
  37. } from './graphql/generated-e2e-admin-types';
  38. import { AddItemToOrder, DeletionResult, GetActiveOrder } from './graphql/generated-e2e-shop-types';
  39. import {
  40. GET_CUSTOMER_LIST,
  41. GET_ORDER,
  42. GET_PRODUCT_WITH_VARIANTS,
  43. GET_STOCK_MOVEMENT,
  44. UPDATE_PRODUCT_VARIANTS,
  45. } from './graphql/shared-definitions';
  46. import { ADD_ITEM_TO_ORDER, GET_ACTIVE_ORDER } from './graphql/shop-definitions';
  47. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  48. import { addPaymentToOrder, proceedToArrangingPayment } from './utils/test-order-utils';
  49. describe('Orders resolver', () => {
  50. const { server, adminClient, shopClient } = createTestEnvironment({
  51. ...testConfig,
  52. paymentOptions: {
  53. paymentMethodHandlers: [
  54. twoStagePaymentMethod,
  55. failsToSettlePaymentMethod,
  56. singleStageRefundablePaymentMethod,
  57. ],
  58. },
  59. });
  60. let customers: GetCustomerList.Items[];
  61. const password = 'test';
  62. beforeAll(async () => {
  63. await server.init({
  64. initialData,
  65. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  66. customerCount: 3,
  67. });
  68. await adminClient.asSuperAdmin();
  69. // Create a couple of orders to be queried
  70. const result = await adminClient.query<GetCustomerList.Query, GetCustomerList.Variables>(
  71. GET_CUSTOMER_LIST,
  72. {
  73. options: {
  74. take: 3,
  75. },
  76. },
  77. );
  78. customers = result.customers.items;
  79. await shopClient.asUserWithCredentials(customers[0].emailAddress, password);
  80. await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
  81. productVariantId: 'T_1',
  82. quantity: 1,
  83. });
  84. await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
  85. productVariantId: 'T_2',
  86. quantity: 1,
  87. });
  88. await shopClient.asUserWithCredentials(customers[1].emailAddress, password);
  89. await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
  90. productVariantId: 'T_2',
  91. quantity: 1,
  92. });
  93. await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
  94. productVariantId: 'T_3',
  95. quantity: 3,
  96. });
  97. }, TEST_SETUP_TIMEOUT_MS);
  98. afterAll(async () => {
  99. await server.destroy();
  100. });
  101. it('orders', async () => {
  102. const result = await adminClient.query<GetOrderList.Query>(GET_ORDERS_LIST);
  103. expect(result.orders.items.map((o) => o.id)).toEqual(['T_1', 'T_2']);
  104. });
  105. it('order', async () => {
  106. const result = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, { id: 'T_2' });
  107. expect(result.order!.id).toBe('T_2');
  108. });
  109. it('order history initially empty', async () => {
  110. const { order } = await adminClient.query<GetOrderHistory.Query, GetOrderHistory.Variables>(
  111. GET_ORDER_HISTORY,
  112. { id: 'T_1' },
  113. );
  114. expect(order!.history.totalItems).toBe(0);
  115. expect(order!.history.items).toEqual([]);
  116. });
  117. describe('payments', () => {
  118. it('settlePayment fails', async () => {
  119. await shopClient.asUserWithCredentials(customers[0].emailAddress, password);
  120. await proceedToArrangingPayment(shopClient);
  121. const order = await addPaymentToOrder(shopClient, failsToSettlePaymentMethod);
  122. expect(order.state).toBe('PaymentAuthorized');
  123. const payment = order.payments![0];
  124. const { settlePayment } = await adminClient.query<
  125. SettlePayment.Mutation,
  126. SettlePayment.Variables
  127. >(SETTLE_PAYMENT, {
  128. id: payment.id,
  129. });
  130. expect(settlePayment!.id).toBe(payment.id);
  131. expect(settlePayment!.state).toBe('Authorized');
  132. const result = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  133. id: order.id,
  134. });
  135. expect(result.order!.state).toBe('PaymentAuthorized');
  136. });
  137. it('settlePayment succeeds, onStateTransitionStart called', async () => {
  138. onTransitionSpy.mockClear();
  139. await shopClient.asUserWithCredentials(customers[1].emailAddress, password);
  140. await proceedToArrangingPayment(shopClient);
  141. const order = await addPaymentToOrder(shopClient, twoStagePaymentMethod);
  142. expect(order.state).toBe('PaymentAuthorized');
  143. expect(onTransitionSpy).toHaveBeenCalledTimes(1);
  144. expect(onTransitionSpy.mock.calls[0][0]).toBe('Created');
  145. expect(onTransitionSpy.mock.calls[0][1]).toBe('Authorized');
  146. const payment = order.payments![0];
  147. const { settlePayment } = await adminClient.query<
  148. SettlePayment.Mutation,
  149. SettlePayment.Variables
  150. >(SETTLE_PAYMENT, {
  151. id: payment.id,
  152. });
  153. expect(settlePayment!.id).toBe(payment.id);
  154. expect(settlePayment!.state).toBe('Settled');
  155. // further metadata is combined into existing object
  156. expect(settlePayment!.metadata).toEqual({
  157. baz: 'quux',
  158. moreData: 42,
  159. });
  160. expect(onTransitionSpy).toHaveBeenCalledTimes(2);
  161. expect(onTransitionSpy.mock.calls[1][0]).toBe('Authorized');
  162. expect(onTransitionSpy.mock.calls[1][1]).toBe('Settled');
  163. const result = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  164. id: order.id,
  165. });
  166. expect(result.order!.state).toBe('PaymentSettled');
  167. expect(result.order!.payments![0].state).toBe('Settled');
  168. });
  169. it('order history contains expected entries', async () => {
  170. const { order } = await adminClient.query<GetOrderHistory.Query, GetOrderHistory.Variables>(
  171. GET_ORDER_HISTORY,
  172. { id: 'T_2' },
  173. );
  174. expect(order!.history.items.map(pick(['type', 'data']))).toEqual([
  175. {
  176. type: HistoryEntryType.ORDER_STATE_TRANSITION,
  177. data: {
  178. from: 'AddingItems',
  179. to: 'ArrangingPayment',
  180. },
  181. },
  182. {
  183. type: HistoryEntryType.ORDER_PAYMENT_TRANSITION,
  184. data: {
  185. paymentId: 'T_2',
  186. from: 'Created',
  187. to: 'Authorized',
  188. },
  189. },
  190. {
  191. type: HistoryEntryType.ORDER_STATE_TRANSITION,
  192. data: {
  193. from: 'ArrangingPayment',
  194. to: 'PaymentAuthorized',
  195. },
  196. },
  197. {
  198. type: HistoryEntryType.ORDER_PAYMENT_TRANSITION,
  199. data: {
  200. paymentId: 'T_2',
  201. from: 'Authorized',
  202. to: 'Settled',
  203. },
  204. },
  205. {
  206. type: HistoryEntryType.ORDER_STATE_TRANSITION,
  207. data: {
  208. from: 'PaymentAuthorized',
  209. to: 'PaymentSettled',
  210. },
  211. },
  212. ]);
  213. });
  214. });
  215. describe('fulfillment', () => {
  216. it(
  217. 'throws if Order is not in "PaymentSettled" state',
  218. assertThrowsWithMessage(async () => {
  219. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  220. id: 'T_1',
  221. });
  222. expect(order!.state).toBe('PaymentAuthorized');
  223. await adminClient.query<CreateFulfillment.Mutation, CreateFulfillment.Variables>(
  224. CREATE_FULFILLMENT,
  225. {
  226. input: {
  227. lines: order!.lines.map((l) => ({ orderLineId: l.id, quantity: l.quantity })),
  228. method: 'Test',
  229. },
  230. },
  231. );
  232. }, 'One or more OrderItems belong to an Order which is in an invalid state'),
  233. );
  234. it(
  235. 'throws if lines is empty',
  236. assertThrowsWithMessage(async () => {
  237. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  238. id: 'T_2',
  239. });
  240. expect(order!.state).toBe('PaymentSettled');
  241. await adminClient.query<CreateFulfillment.Mutation, CreateFulfillment.Variables>(
  242. CREATE_FULFILLMENT,
  243. {
  244. input: {
  245. lines: [],
  246. method: 'Test',
  247. },
  248. },
  249. );
  250. }, 'Nothing to fulfill'),
  251. );
  252. it(
  253. 'throws if all quantities are zero',
  254. assertThrowsWithMessage(async () => {
  255. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  256. id: 'T_2',
  257. });
  258. expect(order!.state).toBe('PaymentSettled');
  259. await adminClient.query<CreateFulfillment.Mutation, CreateFulfillment.Variables>(
  260. CREATE_FULFILLMENT,
  261. {
  262. input: {
  263. lines: order!.lines.map((l) => ({ orderLineId: l.id, quantity: 0 })),
  264. method: 'Test',
  265. },
  266. },
  267. );
  268. }, 'Nothing to fulfill'),
  269. );
  270. it('creates a partial fulfillment', async () => {
  271. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  272. id: 'T_2',
  273. });
  274. expect(order!.state).toBe('PaymentSettled');
  275. const lines = order!.lines;
  276. const { fulfillOrder } = await adminClient.query<
  277. CreateFulfillment.Mutation,
  278. CreateFulfillment.Variables
  279. >(CREATE_FULFILLMENT, {
  280. input: {
  281. lines: lines.map((l) => ({ orderLineId: l.id, quantity: 1 })),
  282. method: 'Test1',
  283. trackingCode: '111',
  284. },
  285. });
  286. expect(fulfillOrder!.method).toBe('Test1');
  287. expect(fulfillOrder!.trackingCode).toBe('111');
  288. expect(fulfillOrder!.orderItems).toEqual([
  289. { id: lines[0].items[0].id },
  290. { id: lines[1].items[0].id },
  291. ]);
  292. const result = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  293. id: 'T_2',
  294. });
  295. expect(result.order!.state).toBe('PartiallyFulfilled');
  296. expect(result.order!.lines[0].items[0].fulfillment!.id).toBe(fulfillOrder!.id);
  297. expect(
  298. result.order!.lines[1].items.filter(
  299. (i) => i.fulfillment && i.fulfillment.id === fulfillOrder.id,
  300. ).length,
  301. ).toBe(1);
  302. expect(result.order!.lines[1].items.filter((i) => i.fulfillment == null).length).toBe(2);
  303. });
  304. it('creates a second partial fulfillment', async () => {
  305. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  306. id: 'T_2',
  307. });
  308. expect(order!.state).toBe('PartiallyFulfilled');
  309. const lines = order!.lines;
  310. const { fulfillOrder } = await adminClient.query<
  311. CreateFulfillment.Mutation,
  312. CreateFulfillment.Variables
  313. >(CREATE_FULFILLMENT, {
  314. input: {
  315. lines: [{ orderLineId: lines[1].id, quantity: 1 }],
  316. method: 'Test2',
  317. trackingCode: '222',
  318. },
  319. });
  320. const result = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  321. id: 'T_2',
  322. });
  323. expect(result.order!.state).toBe('PartiallyFulfilled');
  324. expect(result.order!.lines[1].items.filter((i) => i.fulfillment != null).length).toBe(2);
  325. expect(result.order!.lines[1].items.filter((i) => i.fulfillment == null).length).toBe(1);
  326. });
  327. it(
  328. 'throws if an OrderItem already part of a Fulfillment',
  329. assertThrowsWithMessage(async () => {
  330. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  331. id: 'T_2',
  332. });
  333. expect(order!.state).toBe('PartiallyFulfilled');
  334. await adminClient.query<CreateFulfillment.Mutation, CreateFulfillment.Variables>(
  335. CREATE_FULFILLMENT,
  336. {
  337. input: {
  338. method: 'Test',
  339. lines: [
  340. {
  341. orderLineId: order!.lines[0].id,
  342. quantity: 1,
  343. },
  344. ],
  345. },
  346. },
  347. );
  348. }, 'One or more OrderItems have already been fulfilled'),
  349. );
  350. it('completes fulfillment', async () => {
  351. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  352. id: 'T_2',
  353. });
  354. expect(order!.state).toBe('PartiallyFulfilled');
  355. const orderItems = order!.lines.reduce(
  356. (items, line) => [...items, ...line.items],
  357. [] as OrderItemFragment[],
  358. );
  359. const unfulfilledItem = order!.lines[1].items.find((i) => i.fulfillment == null)!;
  360. const { fulfillOrder } = await adminClient.query<
  361. CreateFulfillment.Mutation,
  362. CreateFulfillment.Variables
  363. >(CREATE_FULFILLMENT, {
  364. input: {
  365. lines: [
  366. {
  367. orderLineId: order!.lines[1].id,
  368. quantity: 1,
  369. },
  370. ],
  371. method: 'Test3',
  372. trackingCode: '333',
  373. },
  374. });
  375. expect(fulfillOrder!.method).toBe('Test3');
  376. expect(fulfillOrder!.trackingCode).toBe('333');
  377. expect(fulfillOrder!.orderItems).toEqual([{ id: unfulfilledItem.id }]);
  378. const result = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  379. id: 'T_2',
  380. });
  381. expect(result.order!.state).toBe('Fulfilled');
  382. });
  383. it('order history contains expected entries', async () => {
  384. const { order } = await adminClient.query<GetOrderHistory.Query, GetOrderHistory.Variables>(
  385. GET_ORDER_HISTORY,
  386. {
  387. id: 'T_2',
  388. options: {
  389. skip: 5,
  390. },
  391. },
  392. );
  393. expect(order!.history.items.map(pick(['type', 'data']))).toEqual([
  394. {
  395. type: HistoryEntryType.ORDER_FULLFILLMENT,
  396. data: {
  397. fulfillmentId: 'T_1',
  398. },
  399. },
  400. {
  401. type: HistoryEntryType.ORDER_STATE_TRANSITION,
  402. data: {
  403. from: 'PaymentSettled',
  404. to: 'PartiallyFulfilled',
  405. },
  406. },
  407. {
  408. type: HistoryEntryType.ORDER_FULLFILLMENT,
  409. data: {
  410. fulfillmentId: 'T_2',
  411. },
  412. },
  413. {
  414. type: HistoryEntryType.ORDER_STATE_TRANSITION,
  415. data: {
  416. from: 'PartiallyFulfilled',
  417. to: 'PartiallyFulfilled',
  418. },
  419. },
  420. {
  421. type: HistoryEntryType.ORDER_FULLFILLMENT,
  422. data: {
  423. fulfillmentId: 'T_3',
  424. },
  425. },
  426. {
  427. type: HistoryEntryType.ORDER_STATE_TRANSITION,
  428. data: {
  429. from: 'PartiallyFulfilled',
  430. to: 'Fulfilled',
  431. },
  432. },
  433. ]);
  434. });
  435. it('order.fullfillments resolver for single order', async () => {
  436. const { order } = await adminClient.query<
  437. GetOrderFulfillments.Query,
  438. GetOrderFulfillments.Variables
  439. >(GET_ORDER_FULFILLMENTS, {
  440. id: 'T_2',
  441. });
  442. expect(order!.fulfillments).toEqual([
  443. { id: 'T_1', method: 'Test1' },
  444. { id: 'T_2', method: 'Test2' },
  445. { id: 'T_3', method: 'Test3' },
  446. ]);
  447. });
  448. it('order.fullfillments resolver for order list', async () => {
  449. const { orders } = await adminClient.query<GetOrderListFulfillments.Query>(
  450. GET_ORDER_LIST_FULFILLMENTS,
  451. );
  452. expect(orders.items[0].fulfillments).toEqual([]);
  453. expect(orders.items[1].fulfillments).toEqual([
  454. { id: 'T_1', method: 'Test1' },
  455. { id: 'T_2', method: 'Test2' },
  456. { id: 'T_3', method: 'Test3' },
  457. ]);
  458. });
  459. it('order.fullfillments.orderItems resolver', async () => {
  460. const { order } = await adminClient.query<
  461. GetOrderFulfillmentItems.Query,
  462. GetOrderFulfillmentItems.Variables
  463. >(GET_ORDER_FULFILLMENT_ITEMS, {
  464. id: 'T_2',
  465. });
  466. expect(order!.fulfillments![0].orderItems).toEqual([{ id: 'T_3' }, { id: 'T_4' }]);
  467. expect(order!.fulfillments![1].orderItems).toEqual([{ id: 'T_5' }]);
  468. });
  469. });
  470. describe('cancellation by orderId', () => {
  471. it('cancel from AddingItems state', async () => {
  472. const testOrder = await createTestOrder(
  473. adminClient,
  474. shopClient,
  475. customers[0].emailAddress,
  476. password,
  477. );
  478. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  479. id: testOrder.orderId,
  480. });
  481. expect(order!.state).toBe('AddingItems');
  482. const { cancelOrder } = await adminClient.query<CancelOrder.Mutation, CancelOrder.Variables>(
  483. CANCEL_ORDER,
  484. {
  485. input: {
  486. orderId: testOrder.orderId,
  487. },
  488. },
  489. );
  490. const { order: order2 } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  491. id: testOrder.orderId,
  492. });
  493. expect(order2!.state).toBe('Cancelled');
  494. expect(order2!.active).toBe(false);
  495. await assertNoStockMovementsCreated(testOrder.product.id);
  496. });
  497. it('cancel from ArrangingPayment state', async () => {
  498. const testOrder = await createTestOrder(
  499. adminClient,
  500. shopClient,
  501. customers[0].emailAddress,
  502. password,
  503. );
  504. await proceedToArrangingPayment(shopClient);
  505. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  506. id: testOrder.orderId,
  507. });
  508. expect(order!.state).toBe('ArrangingPayment');
  509. await adminClient.query<CancelOrder.Mutation, CancelOrder.Variables>(CANCEL_ORDER, {
  510. input: {
  511. orderId: testOrder.orderId,
  512. },
  513. });
  514. const { order: order2 } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  515. id: testOrder.orderId,
  516. });
  517. expect(order2!.state).toBe('Cancelled');
  518. expect(order2!.active).toBe(false);
  519. await assertNoStockMovementsCreated(testOrder.product.id);
  520. });
  521. it('cancel from PaymentAuthorized state', async () => {
  522. const testOrder = await createTestOrder(
  523. adminClient,
  524. shopClient,
  525. customers[0].emailAddress,
  526. password,
  527. );
  528. await proceedToArrangingPayment(shopClient);
  529. const order = await addPaymentToOrder(shopClient, failsToSettlePaymentMethod);
  530. expect(order.state).toBe('PaymentAuthorized');
  531. const result1 = await adminClient.query<GetStockMovement.Query, GetStockMovement.Variables>(
  532. GET_STOCK_MOVEMENT,
  533. {
  534. id: 'T_3',
  535. },
  536. );
  537. let variant1 = result1.product!.variants[0];
  538. expect(variant1.stockOnHand).toBe(98);
  539. expect(variant1.stockMovements.items.map(pick(['type', 'quantity']))).toEqual([
  540. { type: StockMovementType.ADJUSTMENT, quantity: 100 },
  541. { type: StockMovementType.SALE, quantity: -2 },
  542. ]);
  543. const { cancelOrder } = await adminClient.query<CancelOrder.Mutation, CancelOrder.Variables>(
  544. CANCEL_ORDER,
  545. {
  546. input: {
  547. orderId: testOrder.orderId,
  548. },
  549. },
  550. );
  551. expect(cancelOrder.lines.map((l) => l.items.map(pick(['id', 'cancelled'])))).toEqual([
  552. [
  553. { id: 'T_11', cancelled: true },
  554. { id: 'T_12', cancelled: true },
  555. ],
  556. ]);
  557. const { order: order2 } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  558. id: testOrder.orderId,
  559. });
  560. expect(order2!.active).toBe(false);
  561. expect(order2!.state).toBe('Cancelled');
  562. const result2 = await adminClient.query<GetStockMovement.Query, GetStockMovement.Variables>(
  563. GET_STOCK_MOVEMENT,
  564. {
  565. id: 'T_3',
  566. },
  567. );
  568. variant1 = result2.product!.variants[0];
  569. expect(variant1.stockOnHand).toBe(100);
  570. expect(variant1.stockMovements.items.map(pick(['type', 'quantity']))).toEqual([
  571. { type: StockMovementType.ADJUSTMENT, quantity: 100 },
  572. { type: StockMovementType.SALE, quantity: -2 },
  573. { type: StockMovementType.CANCELLATION, quantity: 1 },
  574. { type: StockMovementType.CANCELLATION, quantity: 1 },
  575. ]);
  576. });
  577. async function assertNoStockMovementsCreated(productId: string) {
  578. const result = await adminClient.query<GetStockMovement.Query, GetStockMovement.Variables>(
  579. GET_STOCK_MOVEMENT,
  580. {
  581. id: productId,
  582. },
  583. );
  584. const variant2 = result.product!.variants[0];
  585. expect(variant2.stockOnHand).toBe(100);
  586. expect(variant2.stockMovements.items.map(pick(['type', 'quantity']))).toEqual([
  587. { type: StockMovementType.ADJUSTMENT, quantity: 100 },
  588. ]);
  589. }
  590. });
  591. describe('cancellation by OrderLine', () => {
  592. let orderId: string;
  593. let product: GetProductWithVariants.Product;
  594. let productVariantId: string;
  595. beforeAll(async () => {
  596. const result = await createTestOrder(
  597. adminClient,
  598. shopClient,
  599. customers[0].emailAddress,
  600. password,
  601. );
  602. orderId = result.orderId;
  603. product = result.product;
  604. productVariantId = result.productVariantId;
  605. });
  606. it(
  607. 'cannot cancel from AddingItems state',
  608. assertThrowsWithMessage(async () => {
  609. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  610. id: orderId,
  611. });
  612. expect(order!.state).toBe('AddingItems');
  613. await adminClient.query<CancelOrder.Mutation, CancelOrder.Variables>(CANCEL_ORDER, {
  614. input: {
  615. orderId,
  616. lines: order!.lines.map((l) => ({ orderLineId: l.id, quantity: 1 })),
  617. },
  618. });
  619. }, 'Cannot cancel OrderLines from an Order in the "AddingItems" state'),
  620. );
  621. it(
  622. 'cannot cancel from ArrangingPayment state',
  623. assertThrowsWithMessage(async () => {
  624. await proceedToArrangingPayment(shopClient);
  625. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  626. id: orderId,
  627. });
  628. expect(order!.state).toBe('ArrangingPayment');
  629. await adminClient.query<CancelOrder.Mutation, CancelOrder.Variables>(CANCEL_ORDER, {
  630. input: {
  631. orderId,
  632. lines: order!.lines.map((l) => ({ orderLineId: l.id, quantity: 1 })),
  633. },
  634. });
  635. }, 'Cannot cancel OrderLines from an Order in the "ArrangingPayment" state'),
  636. );
  637. it(
  638. 'throws if lines are empty',
  639. assertThrowsWithMessage(async () => {
  640. const order = await addPaymentToOrder(shopClient, twoStagePaymentMethod);
  641. expect(order.state).toBe('PaymentAuthorized');
  642. await adminClient.query<CancelOrder.Mutation, CancelOrder.Variables>(CANCEL_ORDER, {
  643. input: {
  644. orderId,
  645. lines: [],
  646. },
  647. });
  648. }, 'Nothing to cancel'),
  649. );
  650. it(
  651. 'throws if all quantities zero',
  652. assertThrowsWithMessage(async () => {
  653. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  654. id: orderId,
  655. });
  656. await adminClient.query<CancelOrder.Mutation, CancelOrder.Variables>(CANCEL_ORDER, {
  657. input: {
  658. orderId,
  659. lines: order!.lines.map((l) => ({ orderLineId: l.id, quantity: 0 })),
  660. },
  661. });
  662. }, 'Nothing to cancel'),
  663. );
  664. it('partial cancellation', async () => {
  665. const result1 = await adminClient.query<GetStockMovement.Query, GetStockMovement.Variables>(
  666. GET_STOCK_MOVEMENT,
  667. {
  668. id: product.id,
  669. },
  670. );
  671. const variant1 = result1.product!.variants[0];
  672. expect(variant1.stockOnHand).toBe(98);
  673. expect(variant1.stockMovements.items.map(pick(['type', 'quantity']))).toEqual([
  674. { type: StockMovementType.ADJUSTMENT, quantity: 100 },
  675. { type: StockMovementType.SALE, quantity: -2 },
  676. { type: StockMovementType.CANCELLATION, quantity: 1 },
  677. { type: StockMovementType.CANCELLATION, quantity: 1 },
  678. { type: StockMovementType.SALE, quantity: -2 },
  679. ]);
  680. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  681. id: orderId,
  682. });
  683. const { cancelOrder } = await adminClient.query<CancelOrder.Mutation, CancelOrder.Variables>(
  684. CANCEL_ORDER,
  685. {
  686. input: {
  687. orderId,
  688. lines: order!.lines.map((l) => ({ orderLineId: l.id, quantity: 1 })),
  689. reason: 'cancel reason 1',
  690. },
  691. },
  692. );
  693. expect(cancelOrder.lines[0].quantity).toBe(1);
  694. expect(cancelOrder.lines[0].items.sort((a, b) => (a.id < b.id ? -1 : 1))).toEqual([
  695. { id: 'T_13', cancelled: true },
  696. { id: 'T_14', cancelled: false },
  697. ]);
  698. const { order: order2 } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  699. id: orderId,
  700. });
  701. expect(order2!.state).toBe('PaymentAuthorized');
  702. expect(order2!.lines[0].quantity).toBe(1);
  703. const result2 = await adminClient.query<GetStockMovement.Query, GetStockMovement.Variables>(
  704. GET_STOCK_MOVEMENT,
  705. {
  706. id: product.id,
  707. },
  708. );
  709. const variant2 = result2.product!.variants[0];
  710. expect(variant2.stockOnHand).toBe(99);
  711. expect(variant2.stockMovements.items.map(pick(['type', 'quantity']))).toEqual([
  712. { type: StockMovementType.ADJUSTMENT, quantity: 100 },
  713. { type: StockMovementType.SALE, quantity: -2 },
  714. { type: StockMovementType.CANCELLATION, quantity: 1 },
  715. { type: StockMovementType.CANCELLATION, quantity: 1 },
  716. { type: StockMovementType.SALE, quantity: -2 },
  717. { type: StockMovementType.CANCELLATION, quantity: 1 },
  718. ]);
  719. });
  720. it('complete cancellation', async () => {
  721. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  722. id: orderId,
  723. });
  724. await adminClient.query<CancelOrder.Mutation, CancelOrder.Variables>(CANCEL_ORDER, {
  725. input: {
  726. orderId,
  727. lines: order!.lines.map((l) => ({ orderLineId: l.id, quantity: 1 })),
  728. reason: 'cancel reason 2',
  729. },
  730. });
  731. const { order: order2 } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  732. id: orderId,
  733. });
  734. expect(order2!.state).toBe('Cancelled');
  735. const result = await adminClient.query<GetStockMovement.Query, GetStockMovement.Variables>(
  736. GET_STOCK_MOVEMENT,
  737. {
  738. id: product.id,
  739. },
  740. );
  741. const variant2 = result.product!.variants[0];
  742. expect(variant2.stockOnHand).toBe(100);
  743. expect(variant2.stockMovements.items.map(pick(['type', 'quantity']))).toEqual([
  744. { type: StockMovementType.ADJUSTMENT, quantity: 100 },
  745. { type: StockMovementType.SALE, quantity: -2 },
  746. { type: StockMovementType.CANCELLATION, quantity: 1 },
  747. { type: StockMovementType.CANCELLATION, quantity: 1 },
  748. { type: StockMovementType.SALE, quantity: -2 },
  749. { type: StockMovementType.CANCELLATION, quantity: 1 },
  750. { type: StockMovementType.CANCELLATION, quantity: 1 },
  751. ]);
  752. });
  753. it('order history contains expected entries', async () => {
  754. const { order } = await adminClient.query<GetOrderHistory.Query, GetOrderHistory.Variables>(
  755. GET_ORDER_HISTORY,
  756. {
  757. id: orderId,
  758. options: {
  759. skip: 0,
  760. },
  761. },
  762. );
  763. expect(order!.history.items.map(pick(['type', 'data']))).toEqual([
  764. {
  765. type: HistoryEntryType.ORDER_STATE_TRANSITION,
  766. data: {
  767. from: 'AddingItems',
  768. to: 'ArrangingPayment',
  769. },
  770. },
  771. {
  772. type: HistoryEntryType.ORDER_PAYMENT_TRANSITION,
  773. data: {
  774. paymentId: 'T_4',
  775. from: 'Created',
  776. to: 'Authorized',
  777. },
  778. },
  779. {
  780. type: HistoryEntryType.ORDER_STATE_TRANSITION,
  781. data: {
  782. from: 'ArrangingPayment',
  783. to: 'PaymentAuthorized',
  784. },
  785. },
  786. {
  787. type: HistoryEntryType.ORDER_CANCELLATION,
  788. data: {
  789. orderItemIds: ['T_13'],
  790. reason: 'cancel reason 1',
  791. },
  792. },
  793. {
  794. type: HistoryEntryType.ORDER_CANCELLATION,
  795. data: {
  796. orderItemIds: ['T_14'],
  797. reason: 'cancel reason 2',
  798. },
  799. },
  800. {
  801. type: HistoryEntryType.ORDER_STATE_TRANSITION,
  802. data: {
  803. from: 'PaymentAuthorized',
  804. to: 'Cancelled',
  805. },
  806. },
  807. ]);
  808. });
  809. });
  810. describe('refunds', () => {
  811. let orderId: string;
  812. let product: GetProductWithVariants.Product;
  813. let productVariantId: string;
  814. let paymentId: string;
  815. let refundId: string;
  816. beforeAll(async () => {
  817. const result = await createTestOrder(
  818. adminClient,
  819. shopClient,
  820. customers[0].emailAddress,
  821. password,
  822. );
  823. orderId = result.orderId;
  824. product = result.product;
  825. productVariantId = result.productVariantId;
  826. });
  827. it(
  828. 'cannot refund from PaymentAuthorized state',
  829. assertThrowsWithMessage(async () => {
  830. await proceedToArrangingPayment(shopClient);
  831. const order = await addPaymentToOrder(shopClient, twoStagePaymentMethod);
  832. expect(order.state).toBe('PaymentAuthorized');
  833. paymentId = order.payments![0].id;
  834. await adminClient.query<RefundOrder.Mutation, RefundOrder.Variables>(REFUND_ORDER, {
  835. input: {
  836. lines: order.lines.map((l) => ({ orderLineId: l.id, quantity: 1 })),
  837. shipping: 0,
  838. adjustment: 0,
  839. paymentId,
  840. },
  841. });
  842. }, 'Cannot refund an Order in the "PaymentAuthorized" state'),
  843. );
  844. it(
  845. 'throws if no lines and no shipping',
  846. assertThrowsWithMessage(async () => {
  847. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  848. id: orderId,
  849. });
  850. const { settlePayment } = await adminClient.query<
  851. SettlePayment.Mutation,
  852. SettlePayment.Variables
  853. >(SETTLE_PAYMENT, {
  854. id: order!.payments![0].id,
  855. });
  856. expect(settlePayment!.state).toBe('Settled');
  857. await adminClient.query<RefundOrder.Mutation, RefundOrder.Variables>(REFUND_ORDER, {
  858. input: {
  859. lines: order!.lines.map((l) => ({ orderLineId: l.id, quantity: 0 })),
  860. shipping: 0,
  861. adjustment: 0,
  862. paymentId,
  863. },
  864. });
  865. }, 'Nothing to refund'),
  866. );
  867. it(
  868. 'throws if paymentId not valid',
  869. assertThrowsWithMessage(async () => {
  870. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  871. id: orderId,
  872. });
  873. const { refundOrder } = await adminClient.query<RefundOrder.Mutation, RefundOrder.Variables>(
  874. REFUND_ORDER,
  875. {
  876. input: {
  877. lines: [],
  878. shipping: 100,
  879. adjustment: 0,
  880. paymentId: 'T_999',
  881. },
  882. },
  883. );
  884. }, "No Payment with the id '999' could be found"),
  885. );
  886. it(
  887. 'throws if payment and order lines do not belong to the same Order',
  888. assertThrowsWithMessage(async () => {
  889. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  890. id: orderId,
  891. });
  892. const { refundOrder } = await adminClient.query<RefundOrder.Mutation, RefundOrder.Variables>(
  893. REFUND_ORDER,
  894. {
  895. input: {
  896. lines: order!.lines.map((l) => ({ orderLineId: l.id, quantity: l.quantity })),
  897. shipping: 100,
  898. adjustment: 0,
  899. paymentId: 'T_1',
  900. },
  901. },
  902. );
  903. }, 'The Payment and OrderLines do not belong to the same Order'),
  904. );
  905. it('creates a Refund to be manually settled', async () => {
  906. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  907. id: orderId,
  908. });
  909. const { refundOrder } = await adminClient.query<RefundOrder.Mutation, RefundOrder.Variables>(
  910. REFUND_ORDER,
  911. {
  912. input: {
  913. lines: order!.lines.map((l) => ({ orderLineId: l.id, quantity: l.quantity })),
  914. shipping: order!.shipping,
  915. adjustment: 0,
  916. reason: 'foo',
  917. paymentId,
  918. },
  919. },
  920. );
  921. expect(refundOrder.shipping).toBe(order!.shipping);
  922. expect(refundOrder.items).toBe(order!.subTotal);
  923. expect(refundOrder.total).toBe(order!.total);
  924. expect(refundOrder.transactionId).toBe(null);
  925. expect(refundOrder.state).toBe('Pending');
  926. refundId = refundOrder.id;
  927. });
  928. it(
  929. 'throws if attempting to refund the same item more than once',
  930. assertThrowsWithMessage(async () => {
  931. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  932. id: orderId,
  933. });
  934. const { refundOrder } = await adminClient.query<RefundOrder.Mutation, RefundOrder.Variables>(
  935. REFUND_ORDER,
  936. {
  937. input: {
  938. lines: order!.lines.map((l) => ({ orderLineId: l.id, quantity: l.quantity })),
  939. shipping: order!.shipping,
  940. adjustment: 0,
  941. paymentId,
  942. },
  943. },
  944. );
  945. }, 'Cannot refund an OrderItem which has already been refunded'),
  946. );
  947. it('manually settle a Refund', async () => {
  948. const { settleRefund } = await adminClient.query<SettleRefund.Mutation, SettleRefund.Variables>(
  949. SETTLE_REFUND,
  950. {
  951. input: {
  952. id: refundId,
  953. transactionId: 'aaabbb',
  954. },
  955. },
  956. );
  957. expect(settleRefund.state).toBe('Settled');
  958. expect(settleRefund.transactionId).toBe('aaabbb');
  959. });
  960. it('order history contains expected entries', async () => {
  961. const { order } = await adminClient.query<GetOrderHistory.Query, GetOrderHistory.Variables>(
  962. GET_ORDER_HISTORY,
  963. {
  964. id: orderId,
  965. options: {
  966. skip: 0,
  967. },
  968. },
  969. );
  970. expect(order!.history.items.map(pick(['type', 'data']))).toEqual([
  971. {
  972. type: HistoryEntryType.ORDER_STATE_TRANSITION,
  973. data: {
  974. from: 'AddingItems',
  975. to: 'ArrangingPayment',
  976. },
  977. },
  978. {
  979. type: HistoryEntryType.ORDER_PAYMENT_TRANSITION,
  980. data: {
  981. paymentId: 'T_5',
  982. from: 'Created',
  983. to: 'Authorized',
  984. },
  985. },
  986. {
  987. type: HistoryEntryType.ORDER_STATE_TRANSITION,
  988. data: {
  989. from: 'ArrangingPayment',
  990. to: 'PaymentAuthorized',
  991. },
  992. },
  993. {
  994. type: HistoryEntryType.ORDER_PAYMENT_TRANSITION,
  995. data: {
  996. paymentId: 'T_5',
  997. from: 'Authorized',
  998. to: 'Settled',
  999. },
  1000. },
  1001. {
  1002. type: HistoryEntryType.ORDER_STATE_TRANSITION,
  1003. data: {
  1004. from: 'PaymentAuthorized',
  1005. to: 'PaymentSettled',
  1006. },
  1007. },
  1008. {
  1009. type: HistoryEntryType.ORDER_REFUND_TRANSITION,
  1010. data: {
  1011. refundId: 'T_1',
  1012. reason: 'foo',
  1013. from: 'Pending',
  1014. to: 'Settled',
  1015. },
  1016. },
  1017. ]);
  1018. });
  1019. });
  1020. describe('order notes', () => {
  1021. let orderId: string;
  1022. let firstNoteId: string;
  1023. beforeAll(async () => {
  1024. const result = await createTestOrder(
  1025. adminClient,
  1026. shopClient,
  1027. customers[2].emailAddress,
  1028. password,
  1029. );
  1030. orderId = result.orderId;
  1031. });
  1032. it('private note', async () => {
  1033. const { addNoteToOrder } = await adminClient.query<
  1034. AddNoteToOrder.Mutation,
  1035. AddNoteToOrder.Variables
  1036. >(ADD_NOTE_TO_ORDER, {
  1037. input: {
  1038. id: orderId,
  1039. note: 'A private note',
  1040. isPublic: false,
  1041. },
  1042. });
  1043. expect(addNoteToOrder.id).toBe(orderId);
  1044. const { order } = await adminClient.query<GetOrderHistory.Query, GetOrderHistory.Variables>(
  1045. GET_ORDER_HISTORY,
  1046. {
  1047. id: orderId,
  1048. options: {
  1049. skip: 0,
  1050. },
  1051. },
  1052. );
  1053. expect(order!.history.items.map(pick(['type', 'data']))).toEqual([
  1054. {
  1055. type: HistoryEntryType.ORDER_NOTE,
  1056. data: {
  1057. note: 'A private note',
  1058. },
  1059. },
  1060. ]);
  1061. firstNoteId = order!.history.items[0].id;
  1062. const { activeOrder } = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
  1063. expect(activeOrder!.history.items.map(pick(['type', 'data']))).toEqual([]);
  1064. });
  1065. it('public note', async () => {
  1066. const { addNoteToOrder } = await adminClient.query<
  1067. AddNoteToOrder.Mutation,
  1068. AddNoteToOrder.Variables
  1069. >(ADD_NOTE_TO_ORDER, {
  1070. input: {
  1071. id: orderId,
  1072. note: 'A public note',
  1073. isPublic: true,
  1074. },
  1075. });
  1076. expect(addNoteToOrder.id).toBe(orderId);
  1077. const { order } = await adminClient.query<GetOrderHistory.Query, GetOrderHistory.Variables>(
  1078. GET_ORDER_HISTORY,
  1079. {
  1080. id: orderId,
  1081. options: {
  1082. skip: 1,
  1083. },
  1084. },
  1085. );
  1086. expect(order!.history.items.map(pick(['type', 'data']))).toEqual([
  1087. {
  1088. type: HistoryEntryType.ORDER_NOTE,
  1089. data: {
  1090. note: 'A public note',
  1091. },
  1092. },
  1093. ]);
  1094. const { activeOrder } = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
  1095. expect(activeOrder!.history.items.map(pick(['type', 'data']))).toEqual([
  1096. {
  1097. type: HistoryEntryType.ORDER_NOTE,
  1098. data: {
  1099. note: 'A public note',
  1100. },
  1101. },
  1102. ]);
  1103. });
  1104. it('update note', async () => {
  1105. const { updateOrderNote } = await adminClient.query<
  1106. UpdateOrderNote.Mutation,
  1107. UpdateOrderNote.Variables
  1108. >(UPDATE_ORDER_NOTE, {
  1109. input: {
  1110. noteId: firstNoteId,
  1111. note: 'An updated note',
  1112. },
  1113. });
  1114. expect(updateOrderNote.data).toEqual({
  1115. note: 'An updated note',
  1116. });
  1117. });
  1118. it('delete note', async () => {
  1119. const { order: before } = await adminClient.query<
  1120. GetOrderHistory.Query,
  1121. GetOrderHistory.Variables
  1122. >(GET_ORDER_HISTORY, { id: orderId });
  1123. expect(before?.history.totalItems).toBe(2);
  1124. const { deleteOrderNote } = await adminClient.query<
  1125. DeleteOrderNote.Mutation,
  1126. DeleteOrderNote.Variables
  1127. >(DELETE_ORDER_NOTE, {
  1128. id: firstNoteId,
  1129. });
  1130. expect(deleteOrderNote.result).toBe(DeletionResult.DELETED);
  1131. const { order: after } = await adminClient.query<
  1132. GetOrderHistory.Query,
  1133. GetOrderHistory.Variables
  1134. >(GET_ORDER_HISTORY, { id: orderId });
  1135. expect(after?.history.totalItems).toBe(1);
  1136. });
  1137. });
  1138. });
  1139. async function createTestOrder(
  1140. adminClient: SimpleGraphQLClient,
  1141. shopClient: SimpleGraphQLClient,
  1142. emailAddress: string,
  1143. password: string,
  1144. ): Promise<{
  1145. orderId: string;
  1146. product: GetProductWithVariants.Product;
  1147. productVariantId: string;
  1148. }> {
  1149. const result = await adminClient.query<GetProductWithVariants.Query, GetProductWithVariants.Variables>(
  1150. GET_PRODUCT_WITH_VARIANTS,
  1151. {
  1152. id: 'T_3',
  1153. },
  1154. );
  1155. const product = result.product!;
  1156. const productVariantId = product.variants[0].id;
  1157. // Set the ProductVariant to trackInventory
  1158. const { updateProductVariants } = await adminClient.query<
  1159. UpdateProductVariants.Mutation,
  1160. UpdateProductVariants.Variables
  1161. >(UPDATE_PRODUCT_VARIANTS, {
  1162. input: [
  1163. {
  1164. id: productVariantId,
  1165. trackInventory: true,
  1166. },
  1167. ],
  1168. });
  1169. // Add the ProductVariant to the Order
  1170. await shopClient.asUserWithCredentials(emailAddress, password);
  1171. const { addItemToOrder } = await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(
  1172. ADD_ITEM_TO_ORDER,
  1173. {
  1174. productVariantId,
  1175. quantity: 2,
  1176. },
  1177. );
  1178. const orderId = addItemToOrder!.id;
  1179. return { product, productVariantId, orderId };
  1180. }
  1181. export const GET_ORDERS_LIST = gql`
  1182. query GetOrderList($options: OrderListOptions) {
  1183. orders(options: $options) {
  1184. items {
  1185. ...Order
  1186. }
  1187. totalItems
  1188. }
  1189. }
  1190. ${ORDER_FRAGMENT}
  1191. `;
  1192. export const SETTLE_PAYMENT = gql`
  1193. mutation SettlePayment($id: ID!) {
  1194. settlePayment(id: $id) {
  1195. id
  1196. state
  1197. metadata
  1198. }
  1199. }
  1200. `;
  1201. export const CREATE_FULFILLMENT = gql`
  1202. mutation CreateFulfillment($input: FulfillOrderInput!) {
  1203. fulfillOrder(input: $input) {
  1204. id
  1205. method
  1206. trackingCode
  1207. orderItems {
  1208. id
  1209. }
  1210. }
  1211. }
  1212. `;
  1213. export const GET_ORDER_FULFILLMENTS = gql`
  1214. query GetOrderFulfillments($id: ID!) {
  1215. order(id: $id) {
  1216. id
  1217. fulfillments {
  1218. id
  1219. method
  1220. }
  1221. }
  1222. }
  1223. `;
  1224. export const GET_ORDER_LIST_FULFILLMENTS = gql`
  1225. query GetOrderListFulfillments {
  1226. orders {
  1227. items {
  1228. id
  1229. fulfillments {
  1230. id
  1231. method
  1232. }
  1233. }
  1234. }
  1235. }
  1236. `;
  1237. export const GET_ORDER_FULFILLMENT_ITEMS = gql`
  1238. query GetOrderFulfillmentItems($id: ID!) {
  1239. order(id: $id) {
  1240. id
  1241. fulfillments {
  1242. id
  1243. orderItems {
  1244. id
  1245. }
  1246. }
  1247. }
  1248. }
  1249. `;
  1250. export const CANCEL_ORDER = gql`
  1251. mutation CancelOrder($input: CancelOrderInput!) {
  1252. cancelOrder(input: $input) {
  1253. id
  1254. lines {
  1255. quantity
  1256. items {
  1257. id
  1258. cancelled
  1259. }
  1260. }
  1261. }
  1262. }
  1263. `;
  1264. export const REFUND_ORDER = gql`
  1265. mutation RefundOrder($input: RefundOrderInput!) {
  1266. refundOrder(input: $input) {
  1267. id
  1268. state
  1269. items
  1270. transactionId
  1271. shipping
  1272. total
  1273. metadata
  1274. }
  1275. }
  1276. `;
  1277. export const SETTLE_REFUND = gql`
  1278. mutation SettleRefund($input: SettleRefundInput!) {
  1279. settleRefund(input: $input) {
  1280. id
  1281. state
  1282. items
  1283. transactionId
  1284. shipping
  1285. total
  1286. metadata
  1287. }
  1288. }
  1289. `;
  1290. export const GET_ORDER_HISTORY = gql`
  1291. query GetOrderHistory($id: ID!, $options: HistoryEntryListOptions) {
  1292. order(id: $id) {
  1293. id
  1294. history(options: $options) {
  1295. totalItems
  1296. items {
  1297. id
  1298. type
  1299. administrator {
  1300. id
  1301. }
  1302. data
  1303. }
  1304. }
  1305. }
  1306. }
  1307. `;
  1308. export const ADD_NOTE_TO_ORDER = gql`
  1309. mutation AddNoteToOrder($input: AddNoteToOrderInput!) {
  1310. addNoteToOrder(input: $input) {
  1311. id
  1312. }
  1313. }
  1314. `;
  1315. export const UPDATE_ORDER_NOTE = gql`
  1316. mutation UpdateOrderNote($input: UpdateOrderNoteInput!) {
  1317. updateOrderNote(input: $input) {
  1318. id
  1319. data
  1320. isPublic
  1321. }
  1322. }
  1323. `;
  1324. export const DELETE_ORDER_NOTE = gql`
  1325. mutation DeleteOrderNote($id: ID!) {
  1326. deleteOrderNote(id: $id) {
  1327. result
  1328. message
  1329. }
  1330. }
  1331. `;