order.e2e-spec.ts 51 KB

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