order.e2e-spec.ts 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  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, GetActiveOrder } 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, GET_ACTIVE_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: 3,
  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: 3,
  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. describe('order notes', () => {
  1004. let orderId: string;
  1005. beforeAll(async () => {
  1006. const result = await createTestOrder(
  1007. adminClient,
  1008. shopClient,
  1009. customers[2].emailAddress,
  1010. password,
  1011. );
  1012. orderId = result.orderId;
  1013. });
  1014. it('private note', async () => {
  1015. const { addNoteToOrder } = await adminClient.query<
  1016. AddNoteToOrder.Mutation,
  1017. AddNoteToOrder.Variables
  1018. >(ADD_NOTE_TO_ORDER, {
  1019. input: {
  1020. id: orderId,
  1021. note: 'A private note',
  1022. isPublic: false,
  1023. },
  1024. });
  1025. expect(addNoteToOrder.id).toBe(orderId);
  1026. const { order } = await adminClient.query<GetOrderHistory.Query, GetOrderHistory.Variables>(
  1027. GET_ORDER_HISTORY,
  1028. {
  1029. id: orderId,
  1030. options: {
  1031. skip: 0,
  1032. },
  1033. },
  1034. );
  1035. expect(order!.history.items.map(pick(['type', 'data']))).toEqual([
  1036. {
  1037. type: HistoryEntryType.ORDER_NOTE,
  1038. data: {
  1039. note: 'A private note',
  1040. },
  1041. },
  1042. ]);
  1043. const { activeOrder } = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
  1044. expect(activeOrder!.history.items.map(pick(['type', 'data']))).toEqual([]);
  1045. });
  1046. it('public note', async () => {
  1047. const { addNoteToOrder } = await adminClient.query<
  1048. AddNoteToOrder.Mutation,
  1049. AddNoteToOrder.Variables
  1050. >(ADD_NOTE_TO_ORDER, {
  1051. input: {
  1052. id: orderId,
  1053. note: 'A public note',
  1054. isPublic: true,
  1055. },
  1056. });
  1057. expect(addNoteToOrder.id).toBe(orderId);
  1058. const { order } = await adminClient.query<GetOrderHistory.Query, GetOrderHistory.Variables>(
  1059. GET_ORDER_HISTORY,
  1060. {
  1061. id: orderId,
  1062. options: {
  1063. skip: 1,
  1064. },
  1065. },
  1066. );
  1067. expect(order!.history.items.map(pick(['type', 'data']))).toEqual([
  1068. {
  1069. type: HistoryEntryType.ORDER_NOTE,
  1070. data: {
  1071. note: 'A public note',
  1072. },
  1073. },
  1074. ]);
  1075. const { activeOrder } = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
  1076. expect(activeOrder!.history.items.map(pick(['type', 'data']))).toEqual([
  1077. {
  1078. type: HistoryEntryType.ORDER_NOTE,
  1079. data: {
  1080. note: 'A public note',
  1081. },
  1082. },
  1083. ]);
  1084. });
  1085. });
  1086. });
  1087. /**
  1088. * A two-stage (authorize, capture) payment method, with no createRefund method.
  1089. */
  1090. const twoStagePaymentMethod = new PaymentMethodHandler({
  1091. code: 'authorize-only-payment-method',
  1092. description: [{ languageCode: LanguageCode.en, value: 'Test Payment Method' }],
  1093. args: {},
  1094. createPayment: (order, args, metadata) => {
  1095. return {
  1096. amount: order.total,
  1097. state: 'Authorized',
  1098. transactionId: '12345',
  1099. metadata,
  1100. };
  1101. },
  1102. settlePayment: () => {
  1103. return {
  1104. success: true,
  1105. metadata: {
  1106. moreData: 42,
  1107. },
  1108. };
  1109. },
  1110. });
  1111. /**
  1112. * A payment method which includes a createRefund method.
  1113. */
  1114. const singleStageRefundablePaymentMethod = new PaymentMethodHandler({
  1115. code: 'single-stage-refundable-payment-method',
  1116. description: [{ languageCode: LanguageCode.en, value: 'Test Payment Method' }],
  1117. args: {},
  1118. createPayment: (order, args, metadata) => {
  1119. return {
  1120. amount: order.total,
  1121. state: 'Settled',
  1122. transactionId: '12345',
  1123. metadata,
  1124. };
  1125. },
  1126. settlePayment: () => {
  1127. return { success: true };
  1128. },
  1129. createRefund: (input, total, order, payment, args) => {
  1130. return {
  1131. amount: total,
  1132. state: 'Settled',
  1133. transactionId: 'abc123',
  1134. };
  1135. },
  1136. });
  1137. /**
  1138. * A payment method where calling `settlePayment` always fails.
  1139. */
  1140. const failsToSettlePaymentMethod = new PaymentMethodHandler({
  1141. code: 'fails-to-settle-payment-method',
  1142. description: [{ languageCode: LanguageCode.en, value: 'Test Payment Method' }],
  1143. args: {},
  1144. createPayment: (order, args, metadata) => {
  1145. return {
  1146. amount: order.total,
  1147. state: 'Authorized',
  1148. transactionId: '12345',
  1149. metadata,
  1150. };
  1151. },
  1152. settlePayment: () => {
  1153. return {
  1154. success: false,
  1155. errorMessage: 'Something went horribly wrong',
  1156. };
  1157. },
  1158. });
  1159. async function createTestOrder(
  1160. adminClient: TestAdminClient,
  1161. shopClient: TestShopClient,
  1162. emailAddress: string,
  1163. password: string,
  1164. ): Promise<{
  1165. orderId: string;
  1166. product: GetProductWithVariants.Product;
  1167. productVariantId: string;
  1168. }> {
  1169. const result = await adminClient.query<GetProductWithVariants.Query, GetProductWithVariants.Variables>(
  1170. GET_PRODUCT_WITH_VARIANTS,
  1171. {
  1172. id: 'T_3',
  1173. },
  1174. );
  1175. const product = result.product!;
  1176. const productVariantId = product.variants[0].id;
  1177. // Set the ProductVariant to trackInventory
  1178. const { updateProductVariants } = await adminClient.query<
  1179. UpdateProductVariants.Mutation,
  1180. UpdateProductVariants.Variables
  1181. >(UPDATE_PRODUCT_VARIANTS, {
  1182. input: [
  1183. {
  1184. id: productVariantId,
  1185. trackInventory: true,
  1186. },
  1187. ],
  1188. });
  1189. // Add the ProductVariant to the Order
  1190. await shopClient.asUserWithCredentials(emailAddress, password);
  1191. const { addItemToOrder } = await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(
  1192. ADD_ITEM_TO_ORDER,
  1193. {
  1194. productVariantId,
  1195. quantity: 2,
  1196. },
  1197. );
  1198. const orderId = addItemToOrder!.id;
  1199. return { product, productVariantId, orderId };
  1200. }
  1201. export const GET_ORDERS_LIST = gql`
  1202. query GetOrderList($options: OrderListOptions) {
  1203. orders(options: $options) {
  1204. items {
  1205. ...Order
  1206. }
  1207. totalItems
  1208. }
  1209. }
  1210. ${ORDER_FRAGMENT}
  1211. `;
  1212. export const GET_ORDER = gql`
  1213. query GetOrder($id: ID!) {
  1214. order(id: $id) {
  1215. ...OrderWithLines
  1216. }
  1217. }
  1218. ${ORDER_WITH_LINES_FRAGMENT}
  1219. `;
  1220. export const SETTLE_PAYMENT = gql`
  1221. mutation SettlePayment($id: ID!) {
  1222. settlePayment(id: $id) {
  1223. id
  1224. state
  1225. metadata
  1226. }
  1227. }
  1228. `;
  1229. export const CREATE_FULFILLMENT = gql`
  1230. mutation CreateFulfillment($input: FulfillOrderInput!) {
  1231. fulfillOrder(input: $input) {
  1232. id
  1233. method
  1234. trackingCode
  1235. orderItems {
  1236. id
  1237. }
  1238. }
  1239. }
  1240. `;
  1241. export const GET_ORDER_FULFILLMENTS = gql`
  1242. query GetOrderFulfillments($id: ID!) {
  1243. order(id: $id) {
  1244. id
  1245. fulfillments {
  1246. id
  1247. method
  1248. }
  1249. }
  1250. }
  1251. `;
  1252. export const GET_ORDER_LIST_FULFILLMENTS = gql`
  1253. query GetOrderListFulfillments {
  1254. orders {
  1255. items {
  1256. id
  1257. fulfillments {
  1258. id
  1259. method
  1260. }
  1261. }
  1262. }
  1263. }
  1264. `;
  1265. export const GET_ORDER_FULFILLMENT_ITEMS = gql`
  1266. query GetOrderFulfillmentItems($id: ID!) {
  1267. order(id: $id) {
  1268. id
  1269. fulfillments {
  1270. id
  1271. orderItems {
  1272. id
  1273. }
  1274. }
  1275. }
  1276. }
  1277. `;
  1278. export const CANCEL_ORDER = gql`
  1279. mutation CancelOrder($input: CancelOrderInput!) {
  1280. cancelOrder(input: $input) {
  1281. id
  1282. lines {
  1283. quantity
  1284. items {
  1285. id
  1286. cancelled
  1287. }
  1288. }
  1289. }
  1290. }
  1291. `;
  1292. export const REFUND_ORDER = gql`
  1293. mutation RefundOrder($input: RefundOrderInput!) {
  1294. refundOrder(input: $input) {
  1295. id
  1296. state
  1297. items
  1298. transactionId
  1299. shipping
  1300. total
  1301. metadata
  1302. }
  1303. }
  1304. `;
  1305. export const SETTLE_REFUND = gql`
  1306. mutation SettleRefund($input: SettleRefundInput!) {
  1307. settleRefund(input: $input) {
  1308. id
  1309. state
  1310. items
  1311. transactionId
  1312. shipping
  1313. total
  1314. metadata
  1315. }
  1316. }
  1317. `;
  1318. export const GET_ORDER_HISTORY = gql`
  1319. query GetOrderHistory($id: ID!, $options: HistoryEntryListOptions) {
  1320. order(id: $id) {
  1321. id
  1322. history(options: $options) {
  1323. totalItems
  1324. items {
  1325. id
  1326. type
  1327. administrator {
  1328. id
  1329. }
  1330. data
  1331. }
  1332. }
  1333. }
  1334. }
  1335. `;
  1336. export const ADD_NOTE_TO_ORDER = gql`
  1337. mutation AddNoteToOrder($input: AddNoteToOrderInput!) {
  1338. addNoteToOrder(input: $input) {
  1339. id
  1340. }
  1341. }
  1342. `;