1
0

order.e2e-spec.ts 49 KB

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