order.e2e-spec.ts 49 KB

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