order-modification.e2e-spec.ts 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  1. /* tslint:disable:no-non-null-assertion */
  2. import { omit } from '@vendure/common/lib/omit';
  3. import { pick } from '@vendure/common/lib/pick';
  4. import {
  5. DefaultLogger,
  6. defaultShippingCalculator,
  7. defaultShippingEligibilityChecker,
  8. mergeConfig,
  9. ShippingCalculator,
  10. } from '@vendure/core';
  11. import { createErrorResultGuard, createTestEnvironment, ErrorResultGuard } from '@vendure/testing';
  12. import gql from 'graphql-tag';
  13. import path from 'path';
  14. import { initialData } from '../../../e2e-common/e2e-initial-data';
  15. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  16. import { manualFulfillmentHandler } from '../src/config/fulfillment/manual-fulfillment-handler';
  17. import { testSuccessfulPaymentMethod } from './fixtures/test-payment-methods';
  18. import {
  19. AddManualPayment,
  20. AdminTransition,
  21. CreateShippingMethod,
  22. ErrorCode,
  23. GetOrder,
  24. GetOrderHistory,
  25. GetOrderWithModifications,
  26. GlobalFlag,
  27. HistoryEntryType,
  28. LanguageCode,
  29. ModifyOrder,
  30. OrderFragment,
  31. OrderWithLinesFragment,
  32. OrderWithModificationsFragment,
  33. UpdateProductVariants,
  34. } from './graphql/generated-e2e-admin-types';
  35. import {
  36. AddItemToOrderMutationVariables,
  37. SetShippingAddress,
  38. SetShippingMethod,
  39. TestOrderWithPaymentsFragment,
  40. TransitionToState,
  41. UpdatedOrderFragment,
  42. } from './graphql/generated-e2e-shop-types';
  43. import {
  44. ADMIN_TRANSITION_TO_STATE,
  45. CREATE_SHIPPING_METHOD,
  46. GET_ORDER,
  47. GET_ORDER_HISTORY,
  48. UPDATE_PRODUCT_VARIANTS,
  49. } from './graphql/shared-definitions';
  50. import { SET_SHIPPING_ADDRESS, SET_SHIPPING_METHOD, TRANSITION_TO_STATE } from './graphql/shop-definitions';
  51. import { addPaymentToOrder, proceedToArrangingPayment } from './utils/test-order-utils';
  52. const SHIPPING_GB = 500;
  53. const SHIPPING_US = 1000;
  54. const SHIPPING_OTHER = 750;
  55. const testCalculator = new ShippingCalculator({
  56. code: 'test-calculator',
  57. description: [{ languageCode: LanguageCode.en, value: 'Has metadata' }],
  58. args: {},
  59. calculate: (ctx, order, args) => {
  60. let price;
  61. switch (order.shippingAddress.countryCode) {
  62. case 'GB':
  63. price = SHIPPING_GB;
  64. break;
  65. case 'US':
  66. price = SHIPPING_US;
  67. break;
  68. default:
  69. price = SHIPPING_OTHER;
  70. }
  71. return {
  72. price,
  73. priceIncludesTax: true,
  74. taxRate: 20,
  75. };
  76. },
  77. });
  78. describe('Order modification', () => {
  79. const { server, adminClient, shopClient } = createTestEnvironment(
  80. mergeConfig(testConfig, {
  81. logger: new DefaultLogger(),
  82. paymentOptions: {
  83. paymentMethodHandlers: [testSuccessfulPaymentMethod],
  84. },
  85. shippingOptions: {
  86. shippingCalculators: [defaultShippingCalculator, testCalculator],
  87. },
  88. customFields: {
  89. Order: [{ name: 'points', type: 'int', defaultValue: 0 }],
  90. OrderLine: [{ name: 'color', type: 'string', nullable: true }],
  91. },
  92. }),
  93. );
  94. let orderId: string;
  95. let testShippingMethodId: string;
  96. const orderGuard: ErrorResultGuard<
  97. UpdatedOrderFragment | OrderWithModificationsFragment | OrderFragment
  98. > = createErrorResultGuard(input => !!input.id);
  99. beforeAll(async () => {
  100. await server.init({
  101. initialData,
  102. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  103. customerCount: 2,
  104. });
  105. await adminClient.asSuperAdmin();
  106. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  107. UPDATE_PRODUCT_VARIANTS,
  108. {
  109. input: [
  110. {
  111. id: 'T_1',
  112. trackInventory: GlobalFlag.TRUE,
  113. },
  114. {
  115. id: 'T_2',
  116. trackInventory: GlobalFlag.TRUE,
  117. },
  118. {
  119. id: 'T_3',
  120. trackInventory: GlobalFlag.TRUE,
  121. },
  122. ],
  123. },
  124. );
  125. const { createShippingMethod } = await adminClient.query<
  126. CreateShippingMethod.Mutation,
  127. CreateShippingMethod.Variables
  128. >(CREATE_SHIPPING_METHOD, {
  129. input: {
  130. code: 'new-method',
  131. fulfillmentHandler: manualFulfillmentHandler.code,
  132. checker: {
  133. code: defaultShippingEligibilityChecker.code,
  134. arguments: [
  135. {
  136. name: 'orderMinimum',
  137. value: '0',
  138. },
  139. ],
  140. },
  141. calculator: {
  142. code: testCalculator.code,
  143. arguments: [],
  144. },
  145. translations: [{ languageCode: LanguageCode.en, name: 'test method', description: '' }],
  146. },
  147. });
  148. testShippingMethodId = createShippingMethod.id;
  149. // create an order and check out
  150. await shopClient.asUserWithCredentials('hayden.zieme12@hotmail.com', 'test');
  151. await shopClient.query(gql(ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS), {
  152. productVariantId: 'T_1',
  153. quantity: 1,
  154. customFields: {
  155. color: 'green',
  156. },
  157. } as any);
  158. await shopClient.query(gql(ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS), {
  159. productVariantId: 'T_4',
  160. quantity: 2,
  161. });
  162. await proceedToArrangingPayment(shopClient);
  163. const result = await addPaymentToOrder(shopClient, testSuccessfulPaymentMethod);
  164. orderGuard.assertSuccess(result);
  165. orderId = result.id;
  166. }, TEST_SETUP_TIMEOUT_MS);
  167. afterAll(async () => {
  168. await server.destroy();
  169. });
  170. it('modifyOrder returns error result when not in Modifying state', async () => {
  171. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  172. id: orderId,
  173. });
  174. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  175. MODIFY_ORDER,
  176. {
  177. input: {
  178. dryRun: false,
  179. orderId,
  180. adjustOrderLines: order!.lines.map(l => ({ orderLineId: l.id, quantity: 3 })),
  181. },
  182. },
  183. );
  184. orderGuard.assertErrorResult(modifyOrder);
  185. expect(modifyOrder.errorCode).toBe(ErrorCode.ORDER_MODIFICATION_STATE_ERROR);
  186. });
  187. it('transition to Modifying state', async () => {
  188. const { transitionOrderToState } = await adminClient.query<
  189. AdminTransition.Mutation,
  190. AdminTransition.Variables
  191. >(ADMIN_TRANSITION_TO_STATE, {
  192. id: orderId,
  193. state: 'Modifying',
  194. });
  195. orderGuard.assertSuccess(transitionOrderToState);
  196. expect(transitionOrderToState.state).toBe('Modifying');
  197. });
  198. describe('error cases', () => {
  199. it('no changes specified error', async () => {
  200. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  201. MODIFY_ORDER,
  202. {
  203. input: {
  204. dryRun: false,
  205. orderId,
  206. },
  207. },
  208. );
  209. orderGuard.assertErrorResult(modifyOrder);
  210. expect(modifyOrder.errorCode).toBe(ErrorCode.NO_CHANGES_SPECIFIED_ERROR);
  211. });
  212. it('no refund paymentId specified', async () => {
  213. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  214. id: orderId,
  215. });
  216. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  217. MODIFY_ORDER,
  218. {
  219. input: {
  220. dryRun: false,
  221. orderId,
  222. surcharges: [{ price: -500, priceIncludesTax: true, description: 'Discount' }],
  223. },
  224. },
  225. );
  226. orderGuard.assertErrorResult(modifyOrder);
  227. expect(modifyOrder.errorCode).toBe(ErrorCode.REFUND_PAYMENT_ID_MISSING_ERROR);
  228. await assertOrderIsUnchanged(order!);
  229. });
  230. it('addItems negative quantity', async () => {
  231. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  232. id: orderId,
  233. });
  234. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  235. MODIFY_ORDER,
  236. {
  237. input: {
  238. dryRun: false,
  239. orderId,
  240. addItems: [{ productVariantId: 'T_3', quantity: -1 }],
  241. },
  242. },
  243. );
  244. orderGuard.assertErrorResult(modifyOrder);
  245. expect(modifyOrder.errorCode).toBe(ErrorCode.NEGATIVE_QUANTITY_ERROR);
  246. await assertOrderIsUnchanged(order!);
  247. });
  248. it('adjustOrderLines negative quantity', async () => {
  249. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  250. id: orderId,
  251. });
  252. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  253. MODIFY_ORDER,
  254. {
  255. input: {
  256. dryRun: false,
  257. orderId,
  258. adjustOrderLines: [{ orderLineId: order!.lines[0].id, quantity: -1 }],
  259. },
  260. },
  261. );
  262. orderGuard.assertErrorResult(modifyOrder);
  263. expect(modifyOrder.errorCode).toBe(ErrorCode.NEGATIVE_QUANTITY_ERROR);
  264. await assertOrderIsUnchanged(order!);
  265. });
  266. it('addItems insufficient stock', async () => {
  267. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  268. id: orderId,
  269. });
  270. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  271. MODIFY_ORDER,
  272. {
  273. input: {
  274. dryRun: false,
  275. orderId,
  276. addItems: [{ productVariantId: 'T_3', quantity: 500 }],
  277. },
  278. },
  279. );
  280. orderGuard.assertErrorResult(modifyOrder);
  281. expect(modifyOrder.errorCode).toBe(ErrorCode.INSUFFICIENT_STOCK_ERROR);
  282. await assertOrderIsUnchanged(order!);
  283. });
  284. it('adjustOrderLines insufficient stock', async () => {
  285. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  286. id: orderId,
  287. });
  288. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  289. MODIFY_ORDER,
  290. {
  291. input: {
  292. dryRun: false,
  293. orderId,
  294. adjustOrderLines: [{ orderLineId: order!.lines[0].id, quantity: 500 }],
  295. },
  296. },
  297. );
  298. orderGuard.assertErrorResult(modifyOrder);
  299. expect(modifyOrder.errorCode).toBe(ErrorCode.INSUFFICIENT_STOCK_ERROR);
  300. await assertOrderIsUnchanged(order!);
  301. });
  302. it('addItems order limit', async () => {
  303. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  304. id: orderId,
  305. });
  306. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  307. MODIFY_ORDER,
  308. {
  309. input: {
  310. dryRun: false,
  311. orderId,
  312. addItems: [{ productVariantId: 'T_4', quantity: 9999 }],
  313. },
  314. },
  315. );
  316. orderGuard.assertErrorResult(modifyOrder);
  317. expect(modifyOrder.errorCode).toBe(ErrorCode.ORDER_LIMIT_ERROR);
  318. await assertOrderIsUnchanged(order!);
  319. });
  320. it('adjustOrderLines order limit', async () => {
  321. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  322. id: orderId,
  323. });
  324. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  325. MODIFY_ORDER,
  326. {
  327. input: {
  328. dryRun: false,
  329. orderId,
  330. adjustOrderLines: [{ orderLineId: order!.lines[1].id, quantity: 9999 }],
  331. },
  332. },
  333. );
  334. orderGuard.assertErrorResult(modifyOrder);
  335. expect(modifyOrder.errorCode).toBe(ErrorCode.ORDER_LIMIT_ERROR);
  336. await assertOrderIsUnchanged(order!);
  337. });
  338. });
  339. describe('dry run', () => {
  340. it('addItems', async () => {
  341. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  342. id: orderId,
  343. });
  344. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  345. MODIFY_ORDER,
  346. {
  347. input: {
  348. dryRun: true,
  349. orderId,
  350. addItems: [{ productVariantId: 'T_5', quantity: 1 }],
  351. },
  352. },
  353. );
  354. orderGuard.assertSuccess(modifyOrder);
  355. const expectedTotal = order!.totalWithTax + Math.round(14374 * 1.2); // price of variant T_5
  356. expect(modifyOrder.totalWithTax).toBe(expectedTotal);
  357. expect(modifyOrder.lines.length).toBe(order!.lines.length + 1);
  358. await assertOrderIsUnchanged(order!);
  359. });
  360. it('addItems with existing variant id increments existing OrderLine', async () => {
  361. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  362. id: orderId,
  363. });
  364. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  365. MODIFY_ORDER,
  366. {
  367. input: {
  368. dryRun: true,
  369. orderId,
  370. addItems: [
  371. { productVariantId: 'T_1', quantity: 1, customFields: { color: 'green' } } as any,
  372. ],
  373. },
  374. },
  375. );
  376. orderGuard.assertSuccess(modifyOrder);
  377. const lineT1 = modifyOrder.lines.find(l => l.productVariant.id === 'T_1');
  378. expect(modifyOrder.lines.length).toBe(2);
  379. expect(lineT1?.quantity).toBe(2);
  380. await assertOrderIsUnchanged(order!);
  381. });
  382. it('addItems with existing variant id but different customFields adds new OrderLine', async () => {
  383. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  384. id: orderId,
  385. });
  386. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  387. MODIFY_ORDER,
  388. {
  389. input: {
  390. dryRun: true,
  391. orderId,
  392. addItems: [
  393. { productVariantId: 'T_1', quantity: 1, customFields: { color: 'blue' } } as any,
  394. ],
  395. },
  396. },
  397. );
  398. orderGuard.assertSuccess(modifyOrder);
  399. const lineT1 = modifyOrder.lines.find(l => l.productVariant.id === 'T_1');
  400. expect(modifyOrder.lines.length).toBe(3);
  401. expect(
  402. modifyOrder.lines.map(l => ({ variantId: l.productVariant.id, quantity: l.quantity })),
  403. ).toEqual([
  404. { variantId: 'T_1', quantity: 1 },
  405. { variantId: 'T_4', quantity: 2 },
  406. { variantId: 'T_1', quantity: 1 },
  407. ]);
  408. await assertOrderIsUnchanged(order!);
  409. });
  410. it('adjustOrderLines up', async () => {
  411. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  412. id: orderId,
  413. });
  414. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  415. MODIFY_ORDER,
  416. {
  417. input: {
  418. dryRun: true,
  419. orderId,
  420. adjustOrderLines: [{ orderLineId: order!.lines[0].id, quantity: 3 }],
  421. },
  422. },
  423. );
  424. orderGuard.assertSuccess(modifyOrder);
  425. const expectedTotal = order!.totalWithTax + order!.lines[0].unitPriceWithTax * 2;
  426. expect(modifyOrder.lines[0].items.length).toBe(3);
  427. expect(modifyOrder.totalWithTax).toBe(expectedTotal);
  428. await assertOrderIsUnchanged(order!);
  429. });
  430. it('adjustOrderLines down', async () => {
  431. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  432. id: orderId,
  433. });
  434. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  435. MODIFY_ORDER,
  436. {
  437. input: {
  438. dryRun: true,
  439. orderId,
  440. adjustOrderLines: [{ orderLineId: order!.lines[1].id, quantity: 1 }],
  441. },
  442. },
  443. );
  444. orderGuard.assertSuccess(modifyOrder);
  445. const expectedTotal = order!.totalWithTax - order!.lines[1].unitPriceWithTax;
  446. expect(modifyOrder.lines[1].items.filter(i => i.cancelled).length).toBe(1);
  447. expect(modifyOrder.lines[1].items.filter(i => !i.cancelled).length).toBe(1);
  448. expect(modifyOrder.totalWithTax).toBe(expectedTotal);
  449. await assertOrderIsUnchanged(order!);
  450. });
  451. it('adjustOrderLines to zero', async () => {
  452. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  453. id: orderId,
  454. });
  455. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  456. MODIFY_ORDER,
  457. {
  458. input: {
  459. dryRun: true,
  460. orderId,
  461. adjustOrderLines: [{ orderLineId: order!.lines[0].id, quantity: 0 }],
  462. },
  463. },
  464. );
  465. orderGuard.assertSuccess(modifyOrder);
  466. const expectedTotal = order!.totalWithTax - order!.lines[0].linePriceWithTax;
  467. expect(modifyOrder.totalWithTax).toBe(expectedTotal);
  468. expect(modifyOrder.lines[0].items.every(i => i.cancelled)).toBe(true);
  469. await assertOrderIsUnchanged(order!);
  470. });
  471. it('surcharge positive', async () => {
  472. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  473. id: orderId,
  474. });
  475. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  476. MODIFY_ORDER,
  477. {
  478. input: {
  479. dryRun: true,
  480. orderId,
  481. surcharges: [
  482. {
  483. description: 'extra fee',
  484. sku: '123',
  485. price: 300,
  486. priceIncludesTax: true,
  487. taxRate: 20,
  488. taxDescription: 'VAT',
  489. },
  490. ],
  491. },
  492. },
  493. );
  494. orderGuard.assertSuccess(modifyOrder);
  495. const expectedTotal = order!.totalWithTax + 300;
  496. expect(modifyOrder.totalWithTax).toBe(expectedTotal);
  497. expect(modifyOrder.surcharges.map(s => omit(s, ['id']))).toEqual([
  498. {
  499. description: 'extra fee',
  500. sku: '123',
  501. price: 250,
  502. priceWithTax: 300,
  503. taxRate: 20,
  504. },
  505. ]);
  506. await assertOrderIsUnchanged(order!);
  507. });
  508. it('surcharge negative', async () => {
  509. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  510. id: orderId,
  511. });
  512. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  513. MODIFY_ORDER,
  514. {
  515. input: {
  516. dryRun: true,
  517. orderId,
  518. surcharges: [
  519. {
  520. description: 'special discount',
  521. sku: '123',
  522. price: -300,
  523. priceIncludesTax: true,
  524. taxRate: 20,
  525. taxDescription: 'VAT',
  526. },
  527. ],
  528. },
  529. },
  530. );
  531. orderGuard.assertSuccess(modifyOrder);
  532. const expectedTotal = order!.totalWithTax + -300;
  533. expect(modifyOrder.totalWithTax).toBe(expectedTotal);
  534. expect(modifyOrder.surcharges.map(s => omit(s, ['id']))).toEqual([
  535. {
  536. description: 'special discount',
  537. sku: '123',
  538. price: -250,
  539. priceWithTax: -300,
  540. taxRate: 20,
  541. },
  542. ]);
  543. await assertOrderIsUnchanged(order!);
  544. });
  545. it('does not add a history entry', async () => {
  546. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  547. id: orderId,
  548. });
  549. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  550. MODIFY_ORDER,
  551. {
  552. input: {
  553. dryRun: true,
  554. orderId,
  555. addItems: [{ productVariantId: 'T_5', quantity: 1 }],
  556. },
  557. },
  558. );
  559. orderGuard.assertSuccess(modifyOrder);
  560. const { order: history } = await adminClient.query<
  561. GetOrderHistory.Query,
  562. GetOrderHistory.Variables
  563. >(GET_ORDER_HISTORY, {
  564. id: orderId,
  565. options: { filter: { type: { eq: HistoryEntryType.ORDER_MODIFIED } } },
  566. });
  567. orderGuard.assertSuccess(history);
  568. expect(history.history.totalItems).toBe(0);
  569. });
  570. });
  571. describe('wet run', () => {
  572. async function assertModifiedOrderIsPersisted(order: OrderWithModificationsFragment) {
  573. const { order: order2 } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  574. id: order.id,
  575. });
  576. expect(order2!.totalWithTax).toBe(order!.totalWithTax);
  577. expect(order2!.lines.length).toBe(order!.lines.length);
  578. expect(order2!.surcharges.length).toBe(order!.surcharges.length);
  579. expect(order2!.payments!.length).toBe(order!.payments!.length);
  580. expect(order2!.payments!.map(p => pick(p, ['id', 'amount', 'method']))).toEqual(
  581. order!.payments!.map(p => pick(p, ['id', 'amount', 'method'])),
  582. );
  583. }
  584. it('addItems', async () => {
  585. const order = await createOrderAndTransitionToModifyingState([
  586. {
  587. productVariantId: 'T_1',
  588. quantity: 1,
  589. },
  590. ]);
  591. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  592. MODIFY_ORDER,
  593. {
  594. input: {
  595. dryRun: false,
  596. orderId: order.id,
  597. addItems: [{ productVariantId: 'T_5', quantity: 1 }],
  598. },
  599. },
  600. );
  601. orderGuard.assertSuccess(modifyOrder);
  602. const priceDelta = Math.round(14374 * 1.2); // price of variant T_5
  603. const expectedTotal = order!.totalWithTax + priceDelta;
  604. expect(modifyOrder.totalWithTax).toBe(expectedTotal);
  605. expect(modifyOrder.lines.length).toBe(order!.lines.length + 1);
  606. expect(modifyOrder.modifications.length).toBe(1);
  607. expect(modifyOrder.modifications[0].priceChange).toBe(priceDelta);
  608. expect(modifyOrder.modifications[0].orderItems?.length).toBe(1);
  609. expect(modifyOrder.modifications[0].orderItems?.map(i => i.id)).toEqual([
  610. modifyOrder.lines[1].items[0].id,
  611. ]);
  612. await assertModifiedOrderIsPersisted(modifyOrder);
  613. });
  614. it('adjustOrderLines up', async () => {
  615. const order = await createOrderAndTransitionToModifyingState([
  616. {
  617. productVariantId: 'T_1',
  618. quantity: 1,
  619. },
  620. ]);
  621. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  622. MODIFY_ORDER,
  623. {
  624. input: {
  625. dryRun: false,
  626. orderId: order.id,
  627. adjustOrderLines: [{ orderLineId: order!.lines[0].id, quantity: 2 }],
  628. },
  629. },
  630. );
  631. orderGuard.assertSuccess(modifyOrder);
  632. const priceDelta = order!.lines[0].unitPriceWithTax;
  633. const expectedTotal = order!.totalWithTax + priceDelta;
  634. expect(modifyOrder.totalWithTax).toBe(expectedTotal);
  635. expect(modifyOrder.lines[0].quantity).toBe(2);
  636. expect(modifyOrder.modifications.length).toBe(1);
  637. expect(modifyOrder.modifications[0].priceChange).toBe(priceDelta);
  638. expect(modifyOrder.modifications[0].orderItems?.length).toBe(1);
  639. expect(
  640. modifyOrder.lines[0].items
  641. .map(i => i.id)
  642. .includes(modifyOrder.modifications?.[0].orderItems?.[0].id as string),
  643. ).toBe(true);
  644. await assertModifiedOrderIsPersisted(modifyOrder);
  645. });
  646. it('adjustOrderLines down', async () => {
  647. const order = await createOrderAndTransitionToModifyingState([
  648. {
  649. productVariantId: 'T_1',
  650. quantity: 2,
  651. },
  652. ]);
  653. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  654. MODIFY_ORDER,
  655. {
  656. input: {
  657. dryRun: false,
  658. orderId: order.id,
  659. adjustOrderLines: [{ orderLineId: order!.lines[0].id, quantity: 1 }],
  660. refund: { paymentId: order!.payments![0].id },
  661. },
  662. },
  663. );
  664. orderGuard.assertSuccess(modifyOrder);
  665. const priceDelta = -order!.lines[0].unitPriceWithTax;
  666. const expectedTotal = order!.totalWithTax + priceDelta;
  667. expect(modifyOrder.totalWithTax).toBe(expectedTotal);
  668. expect(modifyOrder.lines[0].quantity).toBe(1);
  669. expect(modifyOrder.payments?.length).toBe(1);
  670. expect(modifyOrder.payments?.[0].refunds.length).toBe(1);
  671. expect(modifyOrder.payments?.[0].refunds[0]).toEqual({
  672. id: 'T_1',
  673. state: 'Pending',
  674. total: -priceDelta,
  675. paymentId: modifyOrder.payments?.[0].id,
  676. });
  677. expect(modifyOrder.modifications.length).toBe(1);
  678. expect(modifyOrder.modifications[0].priceChange).toBe(priceDelta);
  679. expect(modifyOrder.modifications[0].surcharges).toEqual(modifyOrder.surcharges.map(pick(['id'])));
  680. expect(modifyOrder.modifications[0].orderItems?.length).toBe(1);
  681. expect(
  682. modifyOrder.lines[0].items
  683. .map(i => i.id)
  684. .includes(modifyOrder.modifications?.[0].orderItems?.[0].id as string),
  685. ).toBe(true);
  686. await assertModifiedOrderIsPersisted(modifyOrder);
  687. });
  688. it('adjustOrderLines with changed customField value', async () => {
  689. const order = await createOrderAndTransitionToModifyingState([
  690. {
  691. productVariantId: 'T_1',
  692. quantity: 1,
  693. customFields: {
  694. color: 'green',
  695. },
  696. },
  697. ]);
  698. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  699. MODIFY_ORDER,
  700. {
  701. input: {
  702. dryRun: false,
  703. orderId: order.id,
  704. adjustOrderLines: [
  705. {
  706. orderLineId: order!.lines[0].id,
  707. quantity: 1,
  708. customFields: { color: 'black' },
  709. } as any,
  710. ],
  711. },
  712. },
  713. );
  714. orderGuard.assertSuccess(modifyOrder);
  715. expect(modifyOrder.lines.length).toBe(1);
  716. const { order: orderWithLines } = await adminClient.query(gql(GET_ORDER_WITH_CUSTOM_FIELDS), {
  717. id: order.id,
  718. });
  719. expect(orderWithLines.lines[0]).toEqual({
  720. id: order!.lines[0].id,
  721. customFields: { color: 'black' },
  722. });
  723. });
  724. it('adjustOrderLines handles quantity correctly', async () => {
  725. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  726. UPDATE_PRODUCT_VARIANTS,
  727. {
  728. input: [
  729. {
  730. id: 'T_6',
  731. stockOnHand: 1,
  732. trackInventory: GlobalFlag.TRUE,
  733. },
  734. ],
  735. },
  736. );
  737. const order = await createOrderAndTransitionToModifyingState([
  738. {
  739. productVariantId: 'T_6',
  740. quantity: 1,
  741. },
  742. ]);
  743. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  744. MODIFY_ORDER,
  745. {
  746. input: {
  747. dryRun: false,
  748. orderId: order.id,
  749. adjustOrderLines: [
  750. {
  751. orderLineId: order.lines[0].id,
  752. quantity: 1,
  753. },
  754. ],
  755. updateShippingAddress: {
  756. fullName: 'Jim',
  757. },
  758. },
  759. },
  760. );
  761. orderGuard.assertSuccess(modifyOrder);
  762. });
  763. it('surcharge positive', async () => {
  764. const order = await createOrderAndTransitionToModifyingState([
  765. {
  766. productVariantId: 'T_1',
  767. quantity: 1,
  768. },
  769. ]);
  770. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  771. MODIFY_ORDER,
  772. {
  773. input: {
  774. dryRun: false,
  775. orderId: order.id,
  776. surcharges: [
  777. {
  778. description: 'extra fee',
  779. sku: '123',
  780. price: 300,
  781. priceIncludesTax: true,
  782. taxRate: 20,
  783. taxDescription: 'VAT',
  784. },
  785. ],
  786. },
  787. },
  788. );
  789. orderGuard.assertSuccess(modifyOrder);
  790. const priceDelta = 300;
  791. const expectedTotal = order!.totalWithTax + priceDelta;
  792. expect(modifyOrder.totalWithTax).toBe(expectedTotal);
  793. expect(modifyOrder.surcharges.map(s => omit(s, ['id']))).toEqual([
  794. {
  795. description: 'extra fee',
  796. sku: '123',
  797. price: 250,
  798. priceWithTax: 300,
  799. taxRate: 20,
  800. },
  801. ]);
  802. expect(modifyOrder.modifications.length).toBe(1);
  803. expect(modifyOrder.modifications[0].priceChange).toBe(priceDelta);
  804. expect(modifyOrder.modifications[0].surcharges).toEqual(modifyOrder.surcharges.map(pick(['id'])));
  805. await assertModifiedOrderIsPersisted(modifyOrder);
  806. });
  807. it('surcharge negative', async () => {
  808. const order = await createOrderAndTransitionToModifyingState([
  809. {
  810. productVariantId: 'T_1',
  811. quantity: 1,
  812. },
  813. ]);
  814. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  815. MODIFY_ORDER,
  816. {
  817. input: {
  818. dryRun: false,
  819. orderId: order!.id,
  820. surcharges: [
  821. {
  822. description: 'special discount',
  823. sku: '123',
  824. price: -300,
  825. priceIncludesTax: true,
  826. taxRate: 20,
  827. taxDescription: 'VAT',
  828. },
  829. ],
  830. refund: {
  831. paymentId: order!.payments![0].id,
  832. },
  833. },
  834. },
  835. );
  836. orderGuard.assertSuccess(modifyOrder);
  837. const expectedTotal = order!.totalWithTax + -300;
  838. expect(modifyOrder.totalWithTax).toBe(expectedTotal);
  839. expect(modifyOrder.surcharges.map(s => omit(s, ['id']))).toEqual([
  840. {
  841. description: 'special discount',
  842. sku: '123',
  843. price: -250,
  844. priceWithTax: -300,
  845. taxRate: 20,
  846. },
  847. ]);
  848. expect(modifyOrder.modifications.length).toBe(1);
  849. expect(modifyOrder.modifications[0].priceChange).toBe(-300);
  850. await assertModifiedOrderIsPersisted(modifyOrder);
  851. });
  852. it('update updateShippingAddress, recalculate shipping', async () => {
  853. const order = await createOrderAndTransitionToModifyingState([
  854. {
  855. productVariantId: 'T_1',
  856. quantity: 1,
  857. },
  858. ]);
  859. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  860. MODIFY_ORDER,
  861. {
  862. input: {
  863. dryRun: false,
  864. orderId: order!.id,
  865. updateShippingAddress: {
  866. countryCode: 'US',
  867. },
  868. options: {
  869. recalculateShipping: true,
  870. },
  871. },
  872. },
  873. );
  874. orderGuard.assertSuccess(modifyOrder);
  875. const priceDelta = SHIPPING_US - SHIPPING_OTHER;
  876. const expectedTotal = order!.totalWithTax + priceDelta;
  877. expect(modifyOrder.totalWithTax).toBe(expectedTotal);
  878. expect(modifyOrder.shippingAddress?.countryCode).toBe('US');
  879. expect(modifyOrder.modifications.length).toBe(1);
  880. expect(modifyOrder.modifications[0].priceChange).toBe(priceDelta);
  881. await assertModifiedOrderIsPersisted(modifyOrder);
  882. });
  883. it('update updateShippingAddress, do not recalculate shipping', async () => {
  884. const order = await createOrderAndTransitionToModifyingState([
  885. {
  886. productVariantId: 'T_1',
  887. quantity: 1,
  888. },
  889. ]);
  890. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  891. MODIFY_ORDER,
  892. {
  893. input: {
  894. dryRun: false,
  895. orderId: order!.id,
  896. updateShippingAddress: {
  897. countryCode: 'US',
  898. },
  899. options: {
  900. recalculateShipping: false,
  901. },
  902. },
  903. },
  904. );
  905. orderGuard.assertSuccess(modifyOrder);
  906. const priceDelta = 0;
  907. const expectedTotal = order!.totalWithTax + priceDelta;
  908. expect(modifyOrder.totalWithTax).toBe(expectedTotal);
  909. expect(modifyOrder.shippingAddress?.countryCode).toBe('US');
  910. expect(modifyOrder.modifications.length).toBe(1);
  911. expect(modifyOrder.modifications[0].priceChange).toBe(priceDelta);
  912. await assertModifiedOrderIsPersisted(modifyOrder);
  913. });
  914. it('update Order customFields', async () => {
  915. const order = await createOrderAndTransitionToModifyingState([
  916. {
  917. productVariantId: 'T_1',
  918. quantity: 1,
  919. },
  920. ]);
  921. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  922. MODIFY_ORDER,
  923. {
  924. input: {
  925. dryRun: false,
  926. orderId: order.id,
  927. customFields: {
  928. points: 42,
  929. },
  930. } as any,
  931. },
  932. );
  933. orderGuard.assertSuccess(modifyOrder);
  934. const { order: orderWithCustomFields } = await adminClient.query(
  935. gql(GET_ORDER_WITH_CUSTOM_FIELDS),
  936. { id: order.id },
  937. );
  938. expect(orderWithCustomFields.customFields).toEqual({
  939. points: 42,
  940. });
  941. });
  942. it('adds a history entry', async () => {
  943. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  944. id: orderId,
  945. });
  946. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  947. MODIFY_ORDER,
  948. {
  949. input: {
  950. dryRun: false,
  951. orderId: order!.id,
  952. addItems: [{ productVariantId: 'T_5', quantity: 1 }],
  953. },
  954. },
  955. );
  956. orderGuard.assertSuccess(modifyOrder);
  957. const { order: history } = await adminClient.query<
  958. GetOrderHistory.Query,
  959. GetOrderHistory.Variables
  960. >(GET_ORDER_HISTORY, {
  961. id: orderId,
  962. options: { filter: { type: { eq: HistoryEntryType.ORDER_MODIFIED } } },
  963. });
  964. orderGuard.assertSuccess(history);
  965. expect(history.history.totalItems).toBe(1);
  966. expect(history.history.items[0].data).toEqual({
  967. modificationId: modifyOrder.modifications[0].id,
  968. });
  969. });
  970. });
  971. describe('additional payment handling', () => {
  972. let orderId2: string;
  973. beforeAll(async () => {
  974. const order = await createOrderAndTransitionToModifyingState([
  975. {
  976. productVariantId: 'T_1',
  977. quantity: 1,
  978. },
  979. ]);
  980. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  981. MODIFY_ORDER,
  982. {
  983. input: {
  984. dryRun: false,
  985. orderId: order.id,
  986. surcharges: [
  987. {
  988. description: 'extra fee',
  989. sku: '123',
  990. price: 300,
  991. priceIncludesTax: true,
  992. taxRate: 20,
  993. taxDescription: 'VAT',
  994. },
  995. ],
  996. },
  997. },
  998. );
  999. orderGuard.assertSuccess(modifyOrder);
  1000. orderId2 = modifyOrder.id;
  1001. });
  1002. it('cannot transition back to original state if no payment is set', async () => {
  1003. const { transitionOrderToState } = await adminClient.query<
  1004. AdminTransition.Mutation,
  1005. AdminTransition.Variables
  1006. >(ADMIN_TRANSITION_TO_STATE, {
  1007. id: orderId2,
  1008. state: 'PaymentSettled',
  1009. });
  1010. orderGuard.assertErrorResult(transitionOrderToState);
  1011. expect(transitionOrderToState!.errorCode).toBe(ErrorCode.ORDER_STATE_TRANSITION_ERROR);
  1012. expect(transitionOrderToState!.transitionError).toBe(
  1013. `Can only transition to the "ArrangingAdditionalPayment" state`,
  1014. );
  1015. });
  1016. it('can transition to ArrangingAdditionalPayment state', async () => {
  1017. const { transitionOrderToState } = await adminClient.query<
  1018. AdminTransition.Mutation,
  1019. AdminTransition.Variables
  1020. >(ADMIN_TRANSITION_TO_STATE, {
  1021. id: orderId2,
  1022. state: 'ArrangingAdditionalPayment',
  1023. });
  1024. orderGuard.assertSuccess(transitionOrderToState);
  1025. expect(transitionOrderToState!.state).toBe('ArrangingAdditionalPayment');
  1026. });
  1027. it('cannot transition from ArrangingAdditionalPayment when total not covered by Payments', async () => {
  1028. const { transitionOrderToState } = await adminClient.query<
  1029. AdminTransition.Mutation,
  1030. AdminTransition.Variables
  1031. >(ADMIN_TRANSITION_TO_STATE, {
  1032. id: orderId2,
  1033. state: 'PaymentSettled',
  1034. });
  1035. orderGuard.assertErrorResult(transitionOrderToState);
  1036. expect(transitionOrderToState!.errorCode).toBe(ErrorCode.ORDER_STATE_TRANSITION_ERROR);
  1037. expect(transitionOrderToState!.transitionError).toBe(
  1038. `Cannot transition away from "ArrangingAdditionalPayment" unless Order total is covered by Payments`,
  1039. );
  1040. });
  1041. it('addManualPaymentToOrder', async () => {
  1042. const { addManualPaymentToOrder } = await adminClient.query<
  1043. AddManualPayment.Mutation,
  1044. AddManualPayment.Variables
  1045. >(ADD_MANUAL_PAYMENT, {
  1046. input: {
  1047. orderId: orderId2,
  1048. method: 'test',
  1049. transactionId: 'ABC123',
  1050. metadata: {
  1051. foo: 'bar',
  1052. },
  1053. },
  1054. });
  1055. orderGuard.assertSuccess(addManualPaymentToOrder);
  1056. expect(addManualPaymentToOrder.payments?.length).toBe(2);
  1057. expect(omit(addManualPaymentToOrder.payments![1], ['id'])).toEqual({
  1058. transactionId: 'ABC123',
  1059. state: 'Settled',
  1060. amount: 300,
  1061. method: 'test',
  1062. metadata: {
  1063. foo: 'bar',
  1064. },
  1065. refunds: [],
  1066. });
  1067. expect(addManualPaymentToOrder.modifications[0].isSettled).toBe(true);
  1068. expect(addManualPaymentToOrder.modifications[0].payment?.id).toBe(
  1069. addManualPaymentToOrder.payments![1].id,
  1070. );
  1071. });
  1072. it('transition back to original state', async () => {
  1073. const { transitionOrderToState } = await adminClient.query<
  1074. AdminTransition.Mutation,
  1075. AdminTransition.Variables
  1076. >(ADMIN_TRANSITION_TO_STATE, {
  1077. id: orderId2,
  1078. state: 'PaymentSettled',
  1079. });
  1080. orderGuard.assertSuccess(transitionOrderToState);
  1081. expect(transitionOrderToState.state).toBe('PaymentSettled');
  1082. });
  1083. });
  1084. describe('refund handling', () => {
  1085. let orderId3: string;
  1086. beforeAll(async () => {
  1087. const order = await createOrderAndTransitionToModifyingState([
  1088. {
  1089. productVariantId: 'T_1',
  1090. quantity: 1,
  1091. },
  1092. ]);
  1093. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  1094. MODIFY_ORDER,
  1095. {
  1096. input: {
  1097. dryRun: false,
  1098. orderId: order.id,
  1099. surcharges: [
  1100. {
  1101. description: 'discount',
  1102. sku: '123',
  1103. price: -300,
  1104. priceIncludesTax: true,
  1105. taxRate: 20,
  1106. taxDescription: 'VAT',
  1107. },
  1108. ],
  1109. refund: {
  1110. paymentId: order.payments![0].id,
  1111. reason: 'discount',
  1112. },
  1113. },
  1114. },
  1115. );
  1116. orderGuard.assertSuccess(modifyOrder);
  1117. orderId3 = modifyOrder.id;
  1118. });
  1119. it('modification is settled', async () => {
  1120. const { order } = await adminClient.query<
  1121. GetOrderWithModifications.Query,
  1122. GetOrderWithModifications.Variables
  1123. >(GET_ORDER_WITH_MODIFICATIONS, { id: orderId3 });
  1124. expect(order?.modifications.length).toBe(1);
  1125. expect(order?.modifications[0].isSettled).toBe(true);
  1126. });
  1127. it('cannot transition to ArrangingAdditionalPayment state if no payment is needed', async () => {
  1128. const { transitionOrderToState } = await adminClient.query<
  1129. AdminTransition.Mutation,
  1130. AdminTransition.Variables
  1131. >(ADMIN_TRANSITION_TO_STATE, {
  1132. id: orderId3,
  1133. state: 'ArrangingAdditionalPayment',
  1134. });
  1135. orderGuard.assertErrorResult(transitionOrderToState);
  1136. expect(transitionOrderToState!.errorCode).toBe(ErrorCode.ORDER_STATE_TRANSITION_ERROR);
  1137. expect(transitionOrderToState!.transitionError).toBe(
  1138. `Cannot transition Order to the \"ArrangingAdditionalPayment\" state as no additional payments are needed`,
  1139. );
  1140. });
  1141. it('can transition to original state', async () => {
  1142. const { transitionOrderToState } = await adminClient.query<
  1143. AdminTransition.Mutation,
  1144. AdminTransition.Variables
  1145. >(ADMIN_TRANSITION_TO_STATE, {
  1146. id: orderId3,
  1147. state: 'PaymentSettled',
  1148. });
  1149. orderGuard.assertSuccess(transitionOrderToState);
  1150. expect(transitionOrderToState!.state).toBe('PaymentSettled');
  1151. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  1152. id: orderId3,
  1153. });
  1154. expect(order?.payments![0].refunds.length).toBe(1);
  1155. expect(order?.payments![0].refunds[0].total).toBe(300);
  1156. expect(order?.payments![0].refunds[0].reason).toBe('discount');
  1157. });
  1158. });
  1159. async function assertOrderIsUnchanged(order: OrderWithLinesFragment) {
  1160. const { order: order2 } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  1161. id: order.id,
  1162. });
  1163. expect(order2!.totalWithTax).toBe(order!.totalWithTax);
  1164. expect(order2!.lines.length).toBe(order!.lines.length);
  1165. expect(order2!.surcharges.length).toBe(order!.surcharges.length);
  1166. expect(order2!.totalQuantity).toBe(order!.totalQuantity);
  1167. }
  1168. async function createOrderAndTransitionToModifyingState(
  1169. items: Array<AddItemToOrderMutationVariables & { customFields?: any }>,
  1170. ): Promise<TestOrderWithPaymentsFragment> {
  1171. await shopClient.asUserWithCredentials('hayden.zieme12@hotmail.com', 'test');
  1172. for (const itemInput of items) {
  1173. await shopClient.query(gql(ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS), itemInput);
  1174. }
  1175. await shopClient.query<SetShippingAddress.Mutation, SetShippingAddress.Variables>(
  1176. SET_SHIPPING_ADDRESS,
  1177. {
  1178. input: {
  1179. fullName: 'name',
  1180. streetLine1: '12 the street',
  1181. city: 'foo',
  1182. postalCode: '123456',
  1183. countryCode: 'AT',
  1184. },
  1185. },
  1186. );
  1187. await shopClient.query<SetShippingMethod.Mutation, SetShippingMethod.Variables>(SET_SHIPPING_METHOD, {
  1188. id: testShippingMethodId,
  1189. });
  1190. await shopClient.query<TransitionToState.Mutation, TransitionToState.Variables>(TRANSITION_TO_STATE, {
  1191. state: 'ArrangingPayment',
  1192. });
  1193. const order = await addPaymentToOrder(shopClient, testSuccessfulPaymentMethod);
  1194. orderGuard.assertSuccess(order);
  1195. const { transitionOrderToState } = await adminClient.query<
  1196. AdminTransition.Mutation,
  1197. AdminTransition.Variables
  1198. >(ADMIN_TRANSITION_TO_STATE, {
  1199. id: order.id,
  1200. state: 'Modifying',
  1201. });
  1202. return order;
  1203. }
  1204. });
  1205. export const ORDER_WITH_MODIFICATION_FRAGMENT = gql`
  1206. fragment OrderWithModifications on Order {
  1207. id
  1208. state
  1209. total
  1210. totalWithTax
  1211. lines {
  1212. id
  1213. quantity
  1214. linePrice
  1215. linePriceWithTax
  1216. productVariant {
  1217. id
  1218. name
  1219. }
  1220. items {
  1221. id
  1222. createdAt
  1223. updatedAt
  1224. cancelled
  1225. unitPrice
  1226. }
  1227. }
  1228. surcharges {
  1229. id
  1230. description
  1231. sku
  1232. price
  1233. priceWithTax
  1234. taxRate
  1235. }
  1236. payments {
  1237. id
  1238. transactionId
  1239. state
  1240. amount
  1241. method
  1242. metadata
  1243. refunds {
  1244. id
  1245. state
  1246. total
  1247. paymentId
  1248. }
  1249. }
  1250. modifications {
  1251. id
  1252. note
  1253. priceChange
  1254. isSettled
  1255. orderItems {
  1256. id
  1257. }
  1258. surcharges {
  1259. id
  1260. }
  1261. payment {
  1262. id
  1263. state
  1264. amount
  1265. method
  1266. }
  1267. refund {
  1268. id
  1269. state
  1270. total
  1271. paymentId
  1272. }
  1273. }
  1274. shippingAddress {
  1275. streetLine1
  1276. city
  1277. postalCode
  1278. province
  1279. countryCode
  1280. country
  1281. }
  1282. billingAddress {
  1283. streetLine1
  1284. city
  1285. postalCode
  1286. province
  1287. countryCode
  1288. country
  1289. }
  1290. }
  1291. `;
  1292. export const GET_ORDER_WITH_MODIFICATIONS = gql`
  1293. query GetOrderWithModifications($id: ID!) {
  1294. order(id: $id) {
  1295. ...OrderWithModifications
  1296. }
  1297. }
  1298. ${ORDER_WITH_MODIFICATION_FRAGMENT}
  1299. `;
  1300. export const MODIFY_ORDER = gql`
  1301. mutation ModifyOrder($input: ModifyOrderInput!) {
  1302. modifyOrder(input: $input) {
  1303. ...OrderWithModifications
  1304. ... on ErrorResult {
  1305. errorCode
  1306. message
  1307. }
  1308. }
  1309. }
  1310. ${ORDER_WITH_MODIFICATION_FRAGMENT}
  1311. `;
  1312. export const ADD_MANUAL_PAYMENT = gql`
  1313. mutation AddManualPayment($input: ManualPaymentInput!) {
  1314. addManualPaymentToOrder(input: $input) {
  1315. ...OrderWithModifications
  1316. ... on ErrorResult {
  1317. errorCode
  1318. message
  1319. }
  1320. }
  1321. }
  1322. ${ORDER_WITH_MODIFICATION_FRAGMENT}
  1323. `;
  1324. // Note, we don't use the gql tag around these due to the customFields which
  1325. // would cause a codegen error.
  1326. const ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS = `
  1327. mutation AddItemToOrder($productVariantId: ID!, $quantity: Int!, $customFields: OrderLineCustomFieldsInput) {
  1328. addItemToOrder(productVariantId: $productVariantId, quantity: $quantity, customFields: $customFields) {
  1329. ...on Order { id }
  1330. }
  1331. }
  1332. `;
  1333. const GET_ORDER_WITH_CUSTOM_FIELDS = `
  1334. query GetOrderCustomFields($id: ID!) {
  1335. order(id: $id) {
  1336. customFields { points }
  1337. lines { id, customFields { color } }
  1338. }
  1339. }
  1340. `;