order.e2e-spec.ts 49 KB

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