order.e2e-spec.ts 49 KB

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