order-modification.e2e-spec.ts 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943
  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. defaultShippingCalculator,
  6. defaultShippingEligibilityChecker,
  7. mergeConfig,
  8. productsPercentageDiscount,
  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 { orderFixedDiscount } from '../src/config/promotion/actions/order-fixed-discount-action';
  18. import { defaultPromotionActions } from '../src/config/promotion/index';
  19. import {
  20. failsToSettlePaymentMethod,
  21. testFailingPaymentMethod,
  22. testSuccessfulPaymentMethod,
  23. } from './fixtures/test-payment-methods';
  24. import {
  25. AddManualPayment,
  26. AdminTransition,
  27. CreateFulfillment,
  28. CreatePromotion,
  29. CreateShippingMethod,
  30. ErrorCode,
  31. GetOrder,
  32. GetOrderHistory,
  33. GetOrderWithModifications,
  34. GetStockMovement,
  35. GlobalFlag,
  36. HistoryEntryType,
  37. LanguageCode,
  38. ModifyOrder,
  39. OrderFragment,
  40. OrderWithLinesFragment,
  41. OrderWithModificationsFragment,
  42. UpdateChannel,
  43. UpdateProductVariants,
  44. } from './graphql/generated-e2e-admin-types';
  45. import {
  46. AddItemToOrderMutationVariables,
  47. ApplyCouponCode,
  48. SetShippingAddress,
  49. SetShippingMethod,
  50. TestOrderWithPaymentsFragment,
  51. TransitionToState,
  52. UpdatedOrderFragment,
  53. } from './graphql/generated-e2e-shop-types';
  54. import {
  55. ADMIN_TRANSITION_TO_STATE,
  56. CREATE_FULFILLMENT,
  57. CREATE_PROMOTION,
  58. CREATE_SHIPPING_METHOD,
  59. GET_ORDER,
  60. GET_ORDER_HISTORY,
  61. GET_PRODUCT_WITH_VARIANTS,
  62. GET_STOCK_MOVEMENT,
  63. UPDATE_CHANNEL,
  64. UPDATE_PRODUCT_VARIANTS,
  65. } from './graphql/shared-definitions';
  66. import {
  67. APPLY_COUPON_CODE,
  68. SET_SHIPPING_ADDRESS,
  69. SET_SHIPPING_METHOD,
  70. TRANSITION_TO_STATE,
  71. } from './graphql/shop-definitions';
  72. import { addPaymentToOrder, proceedToArrangingPayment } from './utils/test-order-utils';
  73. const SHIPPING_GB = 500;
  74. const SHIPPING_US = 1000;
  75. const SHIPPING_OTHER = 750;
  76. const testCalculator = new ShippingCalculator({
  77. code: 'test-calculator',
  78. description: [{ languageCode: LanguageCode.en, value: 'Has metadata' }],
  79. args: {},
  80. calculate: (ctx, order, args) => {
  81. let price;
  82. switch (order.shippingAddress.countryCode) {
  83. case 'GB':
  84. price = SHIPPING_GB;
  85. break;
  86. case 'US':
  87. price = SHIPPING_US;
  88. break;
  89. default:
  90. price = SHIPPING_OTHER;
  91. }
  92. return {
  93. price,
  94. priceIncludesTax: true,
  95. taxRate: 20,
  96. };
  97. },
  98. });
  99. describe('Order modification', () => {
  100. const { server, adminClient, shopClient } = createTestEnvironment(
  101. mergeConfig(testConfig(), {
  102. paymentOptions: {
  103. paymentMethodHandlers: [
  104. testSuccessfulPaymentMethod,
  105. failsToSettlePaymentMethod,
  106. testFailingPaymentMethod,
  107. ],
  108. },
  109. shippingOptions: {
  110. shippingCalculators: [defaultShippingCalculator, testCalculator],
  111. },
  112. customFields: {
  113. Order: [{ name: 'points', type: 'int', defaultValue: 0 }],
  114. OrderLine: [{ name: 'color', type: 'string', nullable: true }],
  115. },
  116. }),
  117. );
  118. let orderId: string;
  119. let testShippingMethodId: string;
  120. const orderGuard: ErrorResultGuard<
  121. UpdatedOrderFragment | OrderWithModificationsFragment | OrderFragment
  122. > = createErrorResultGuard(input => !!input.id);
  123. beforeAll(async () => {
  124. await server.init({
  125. initialData: {
  126. ...initialData,
  127. paymentMethods: [
  128. {
  129. name: testSuccessfulPaymentMethod.code,
  130. handler: { code: testSuccessfulPaymentMethod.code, arguments: [] },
  131. },
  132. {
  133. name: failsToSettlePaymentMethod.code,
  134. handler: { code: failsToSettlePaymentMethod.code, arguments: [] },
  135. },
  136. {
  137. name: testFailingPaymentMethod.code,
  138. handler: { code: testFailingPaymentMethod.code, arguments: [] },
  139. },
  140. ],
  141. },
  142. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  143. customerCount: 2,
  144. });
  145. await adminClient.asSuperAdmin();
  146. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  147. UPDATE_PRODUCT_VARIANTS,
  148. {
  149. input: [
  150. {
  151. id: 'T_1',
  152. trackInventory: GlobalFlag.TRUE,
  153. },
  154. {
  155. id: 'T_2',
  156. trackInventory: GlobalFlag.TRUE,
  157. },
  158. {
  159. id: 'T_3',
  160. trackInventory: GlobalFlag.TRUE,
  161. },
  162. ],
  163. },
  164. );
  165. const { createShippingMethod } = await adminClient.query<
  166. CreateShippingMethod.Mutation,
  167. CreateShippingMethod.Variables
  168. >(CREATE_SHIPPING_METHOD, {
  169. input: {
  170. code: 'new-method',
  171. fulfillmentHandler: manualFulfillmentHandler.code,
  172. checker: {
  173. code: defaultShippingEligibilityChecker.code,
  174. arguments: [
  175. {
  176. name: 'orderMinimum',
  177. value: '0',
  178. },
  179. ],
  180. },
  181. calculator: {
  182. code: testCalculator.code,
  183. arguments: [],
  184. },
  185. translations: [{ languageCode: LanguageCode.en, name: 'test method', description: '' }],
  186. },
  187. });
  188. testShippingMethodId = createShippingMethod.id;
  189. // create an order and check out
  190. await shopClient.asUserWithCredentials('hayden.zieme12@hotmail.com', 'test');
  191. await shopClient.query(gql(ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS), {
  192. productVariantId: 'T_1',
  193. quantity: 1,
  194. customFields: {
  195. color: 'green',
  196. },
  197. } as any);
  198. await shopClient.query(gql(ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS), {
  199. productVariantId: 'T_4',
  200. quantity: 2,
  201. });
  202. await proceedToArrangingPayment(shopClient);
  203. const result = await addPaymentToOrder(shopClient, testSuccessfulPaymentMethod);
  204. orderGuard.assertSuccess(result);
  205. orderId = result.id;
  206. }, TEST_SETUP_TIMEOUT_MS);
  207. afterAll(async () => {
  208. await server.destroy();
  209. });
  210. it('modifyOrder returns error result when not in Modifying state', async () => {
  211. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  212. id: orderId,
  213. });
  214. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  215. MODIFY_ORDER,
  216. {
  217. input: {
  218. dryRun: false,
  219. orderId,
  220. adjustOrderLines: order!.lines.map(l => ({ orderLineId: l.id, quantity: 3 })),
  221. },
  222. },
  223. );
  224. orderGuard.assertErrorResult(modifyOrder);
  225. expect(modifyOrder.errorCode).toBe(ErrorCode.ORDER_MODIFICATION_STATE_ERROR);
  226. });
  227. it('transition to Modifying state', async () => {
  228. const transitionOrderToState = await adminTransitionOrderToState(orderId, 'Modifying');
  229. orderGuard.assertSuccess(transitionOrderToState);
  230. expect(transitionOrderToState.state).toBe('Modifying');
  231. });
  232. describe('error cases', () => {
  233. it('no changes specified error', async () => {
  234. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  235. MODIFY_ORDER,
  236. {
  237. input: {
  238. dryRun: false,
  239. orderId,
  240. },
  241. },
  242. );
  243. orderGuard.assertErrorResult(modifyOrder);
  244. expect(modifyOrder.errorCode).toBe(ErrorCode.NO_CHANGES_SPECIFIED_ERROR);
  245. });
  246. it('no refund paymentId specified', async () => {
  247. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  248. id: orderId,
  249. });
  250. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  251. MODIFY_ORDER,
  252. {
  253. input: {
  254. dryRun: false,
  255. orderId,
  256. surcharges: [{ price: -500, priceIncludesTax: true, description: 'Discount' }],
  257. },
  258. },
  259. );
  260. orderGuard.assertErrorResult(modifyOrder);
  261. expect(modifyOrder.errorCode).toBe(ErrorCode.REFUND_PAYMENT_ID_MISSING_ERROR);
  262. await assertOrderIsUnchanged(order!);
  263. });
  264. it('addItems negative quantity', async () => {
  265. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  266. id: orderId,
  267. });
  268. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  269. MODIFY_ORDER,
  270. {
  271. input: {
  272. dryRun: false,
  273. orderId,
  274. addItems: [{ productVariantId: 'T_3', quantity: -1 }],
  275. },
  276. },
  277. );
  278. orderGuard.assertErrorResult(modifyOrder);
  279. expect(modifyOrder.errorCode).toBe(ErrorCode.NEGATIVE_QUANTITY_ERROR);
  280. await assertOrderIsUnchanged(order!);
  281. });
  282. it('adjustOrderLines negative quantity', async () => {
  283. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  284. id: orderId,
  285. });
  286. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  287. MODIFY_ORDER,
  288. {
  289. input: {
  290. dryRun: false,
  291. orderId,
  292. adjustOrderLines: [{ orderLineId: order!.lines[0].id, quantity: -1 }],
  293. },
  294. },
  295. );
  296. orderGuard.assertErrorResult(modifyOrder);
  297. expect(modifyOrder.errorCode).toBe(ErrorCode.NEGATIVE_QUANTITY_ERROR);
  298. await assertOrderIsUnchanged(order!);
  299. });
  300. it('addItems insufficient stock', async () => {
  301. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  302. id: orderId,
  303. });
  304. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  305. MODIFY_ORDER,
  306. {
  307. input: {
  308. dryRun: false,
  309. orderId,
  310. addItems: [{ productVariantId: 'T_3', quantity: 500 }],
  311. },
  312. },
  313. );
  314. orderGuard.assertErrorResult(modifyOrder);
  315. expect(modifyOrder.errorCode).toBe(ErrorCode.INSUFFICIENT_STOCK_ERROR);
  316. await assertOrderIsUnchanged(order!);
  317. });
  318. it('adjustOrderLines insufficient stock', async () => {
  319. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  320. id: orderId,
  321. });
  322. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  323. MODIFY_ORDER,
  324. {
  325. input: {
  326. dryRun: false,
  327. orderId,
  328. adjustOrderLines: [{ orderLineId: order!.lines[0].id, quantity: 500 }],
  329. },
  330. },
  331. );
  332. orderGuard.assertErrorResult(modifyOrder);
  333. expect(modifyOrder.errorCode).toBe(ErrorCode.INSUFFICIENT_STOCK_ERROR);
  334. await assertOrderIsUnchanged(order!);
  335. });
  336. it('addItems order limit', async () => {
  337. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  338. id: orderId,
  339. });
  340. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  341. MODIFY_ORDER,
  342. {
  343. input: {
  344. dryRun: false,
  345. orderId,
  346. addItems: [{ productVariantId: 'T_4', quantity: 9999 }],
  347. },
  348. },
  349. );
  350. orderGuard.assertErrorResult(modifyOrder);
  351. expect(modifyOrder.errorCode).toBe(ErrorCode.ORDER_LIMIT_ERROR);
  352. await assertOrderIsUnchanged(order!);
  353. });
  354. it('adjustOrderLines order limit', async () => {
  355. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  356. id: orderId,
  357. });
  358. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  359. MODIFY_ORDER,
  360. {
  361. input: {
  362. dryRun: false,
  363. orderId,
  364. adjustOrderLines: [{ orderLineId: order!.lines[1].id, quantity: 9999 }],
  365. },
  366. },
  367. );
  368. orderGuard.assertErrorResult(modifyOrder);
  369. expect(modifyOrder.errorCode).toBe(ErrorCode.ORDER_LIMIT_ERROR);
  370. await assertOrderIsUnchanged(order!);
  371. });
  372. });
  373. describe('dry run', () => {
  374. it('addItems', async () => {
  375. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  376. id: orderId,
  377. });
  378. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  379. MODIFY_ORDER,
  380. {
  381. input: {
  382. dryRun: true,
  383. orderId,
  384. addItems: [{ productVariantId: 'T_5', quantity: 1 }],
  385. },
  386. },
  387. );
  388. orderGuard.assertSuccess(modifyOrder);
  389. const expectedTotal = order!.totalWithTax + Math.round(14374 * 1.2); // price of variant T_5
  390. expect(modifyOrder.totalWithTax).toBe(expectedTotal);
  391. expect(modifyOrder.lines.length).toBe(order!.lines.length + 1);
  392. await assertOrderIsUnchanged(order!);
  393. });
  394. it('addItems with existing variant id increments existing OrderLine', async () => {
  395. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  396. id: orderId,
  397. });
  398. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  399. MODIFY_ORDER,
  400. {
  401. input: {
  402. dryRun: true,
  403. orderId,
  404. addItems: [
  405. { productVariantId: 'T_1', quantity: 1, customFields: { color: 'green' } } as any,
  406. ],
  407. },
  408. },
  409. );
  410. orderGuard.assertSuccess(modifyOrder);
  411. const lineT1 = modifyOrder.lines.find(l => l.productVariant.id === 'T_1');
  412. expect(modifyOrder.lines.length).toBe(2);
  413. expect(lineT1?.quantity).toBe(2);
  414. await assertOrderIsUnchanged(order!);
  415. });
  416. it('addItems with existing variant id but different customFields adds new OrderLine', async () => {
  417. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  418. id: orderId,
  419. });
  420. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  421. MODIFY_ORDER,
  422. {
  423. input: {
  424. dryRun: true,
  425. orderId,
  426. addItems: [
  427. { productVariantId: 'T_1', quantity: 1, customFields: { color: 'blue' } } as any,
  428. ],
  429. },
  430. },
  431. );
  432. orderGuard.assertSuccess(modifyOrder);
  433. const lineT1 = modifyOrder.lines.find(l => l.productVariant.id === 'T_1');
  434. expect(modifyOrder.lines.length).toBe(3);
  435. expect(
  436. modifyOrder.lines.map(l => ({ variantId: l.productVariant.id, quantity: l.quantity })),
  437. ).toEqual([
  438. { variantId: 'T_1', quantity: 1 },
  439. { variantId: 'T_4', quantity: 2 },
  440. { variantId: 'T_1', quantity: 1 },
  441. ]);
  442. await assertOrderIsUnchanged(order!);
  443. });
  444. it('adjustOrderLines up', async () => {
  445. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  446. id: orderId,
  447. });
  448. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  449. MODIFY_ORDER,
  450. {
  451. input: {
  452. dryRun: true,
  453. orderId,
  454. adjustOrderLines: [{ orderLineId: order!.lines[0].id, quantity: 3 }],
  455. },
  456. },
  457. );
  458. orderGuard.assertSuccess(modifyOrder);
  459. const expectedTotal = order!.totalWithTax + order!.lines[0].unitPriceWithTax * 2;
  460. expect(modifyOrder.lines[0].items.length).toBe(3);
  461. expect(modifyOrder.totalWithTax).toBe(expectedTotal);
  462. await assertOrderIsUnchanged(order!);
  463. });
  464. it('adjustOrderLines down', async () => {
  465. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  466. id: orderId,
  467. });
  468. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  469. MODIFY_ORDER,
  470. {
  471. input: {
  472. dryRun: true,
  473. orderId,
  474. adjustOrderLines: [{ orderLineId: order!.lines[1].id, quantity: 1 }],
  475. },
  476. },
  477. );
  478. orderGuard.assertSuccess(modifyOrder);
  479. const expectedTotal = order!.totalWithTax - order!.lines[1].unitPriceWithTax;
  480. expect(modifyOrder.lines[1].items.filter(i => i.cancelled).length).toBe(1);
  481. expect(modifyOrder.lines[1].items.filter(i => !i.cancelled).length).toBe(1);
  482. expect(modifyOrder.totalWithTax).toBe(expectedTotal);
  483. await assertOrderIsUnchanged(order!);
  484. });
  485. it('adjustOrderLines to zero', async () => {
  486. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  487. id: orderId,
  488. });
  489. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  490. MODIFY_ORDER,
  491. {
  492. input: {
  493. dryRun: true,
  494. orderId,
  495. adjustOrderLines: [{ orderLineId: order!.lines[0].id, quantity: 0 }],
  496. },
  497. },
  498. );
  499. orderGuard.assertSuccess(modifyOrder);
  500. const expectedTotal = order!.totalWithTax - order!.lines[0].linePriceWithTax;
  501. expect(modifyOrder.totalWithTax).toBe(expectedTotal);
  502. expect(modifyOrder.lines[0].items.every(i => i.cancelled)).toBe(true);
  503. await assertOrderIsUnchanged(order!);
  504. });
  505. it('surcharge positive', async () => {
  506. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  507. id: orderId,
  508. });
  509. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  510. MODIFY_ORDER,
  511. {
  512. input: {
  513. dryRun: true,
  514. orderId,
  515. surcharges: [
  516. {
  517. description: 'extra fee',
  518. sku: '123',
  519. price: 300,
  520. priceIncludesTax: true,
  521. taxRate: 20,
  522. taxDescription: 'VAT',
  523. },
  524. ],
  525. },
  526. },
  527. );
  528. orderGuard.assertSuccess(modifyOrder);
  529. const expectedTotal = order!.totalWithTax + 300;
  530. expect(modifyOrder.totalWithTax).toBe(expectedTotal);
  531. expect(modifyOrder.surcharges.map(s => omit(s, ['id']))).toEqual([
  532. {
  533. description: 'extra fee',
  534. sku: '123',
  535. price: 250,
  536. priceWithTax: 300,
  537. taxRate: 20,
  538. },
  539. ]);
  540. await assertOrderIsUnchanged(order!);
  541. });
  542. it('surcharge negative', async () => {
  543. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  544. id: orderId,
  545. });
  546. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  547. MODIFY_ORDER,
  548. {
  549. input: {
  550. dryRun: true,
  551. orderId,
  552. surcharges: [
  553. {
  554. description: 'special discount',
  555. sku: '123',
  556. price: -300,
  557. priceIncludesTax: true,
  558. taxRate: 20,
  559. taxDescription: 'VAT',
  560. },
  561. ],
  562. },
  563. },
  564. );
  565. orderGuard.assertSuccess(modifyOrder);
  566. const expectedTotal = order!.totalWithTax + -300;
  567. expect(modifyOrder.totalWithTax).toBe(expectedTotal);
  568. expect(modifyOrder.surcharges.map(s => omit(s, ['id']))).toEqual([
  569. {
  570. description: 'special discount',
  571. sku: '123',
  572. price: -250,
  573. priceWithTax: -300,
  574. taxRate: 20,
  575. },
  576. ]);
  577. await assertOrderIsUnchanged(order!);
  578. });
  579. it('does not add a history entry', async () => {
  580. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  581. id: orderId,
  582. });
  583. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  584. MODIFY_ORDER,
  585. {
  586. input: {
  587. dryRun: true,
  588. orderId,
  589. addItems: [{ productVariantId: 'T_5', quantity: 1 }],
  590. },
  591. },
  592. );
  593. orderGuard.assertSuccess(modifyOrder);
  594. const { order: history } = await adminClient.query<
  595. GetOrderHistory.Query,
  596. GetOrderHistory.Variables
  597. >(GET_ORDER_HISTORY, {
  598. id: orderId,
  599. options: { filter: { type: { eq: HistoryEntryType.ORDER_MODIFIED } } },
  600. });
  601. orderGuard.assertSuccess(history);
  602. expect(history.history.totalItems).toBe(0);
  603. });
  604. });
  605. describe('wet run', () => {
  606. async function assertModifiedOrderIsPersisted(order: OrderWithModificationsFragment) {
  607. const { order: order2 } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  608. id: order.id,
  609. });
  610. expect(order2!.totalWithTax).toBe(order!.totalWithTax);
  611. expect(order2!.lines.length).toBe(order!.lines.length);
  612. expect(order2!.surcharges.length).toBe(order!.surcharges.length);
  613. expect(order2!.payments!.length).toBe(order!.payments!.length);
  614. expect(order2!.payments!.map(p => pick(p, ['id', 'amount', 'method']))).toEqual(
  615. order!.payments!.map(p => pick(p, ['id', 'amount', 'method'])),
  616. );
  617. }
  618. it('addItems', async () => {
  619. const order = await createOrderAndTransitionToModifyingState([
  620. {
  621. productVariantId: 'T_1',
  622. quantity: 1,
  623. },
  624. ]);
  625. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  626. MODIFY_ORDER,
  627. {
  628. input: {
  629. dryRun: false,
  630. orderId: order.id,
  631. addItems: [{ productVariantId: 'T_5', quantity: 1 }],
  632. },
  633. },
  634. );
  635. orderGuard.assertSuccess(modifyOrder);
  636. const priceDelta = Math.round(14374 * 1.2); // price of variant T_5
  637. const expectedTotal = order!.totalWithTax + priceDelta;
  638. expect(modifyOrder.totalWithTax).toBe(expectedTotal);
  639. expect(modifyOrder.lines.length).toBe(order!.lines.length + 1);
  640. expect(modifyOrder.modifications.length).toBe(1);
  641. expect(modifyOrder.modifications[0].priceChange).toBe(priceDelta);
  642. expect(modifyOrder.modifications[0].orderItems?.length).toBe(1);
  643. expect(modifyOrder.modifications[0].orderItems?.map(i => i.id)).toEqual([
  644. modifyOrder.lines[1].items[0].id,
  645. ]);
  646. await assertModifiedOrderIsPersisted(modifyOrder);
  647. });
  648. it('adjustOrderLines up', async () => {
  649. const order = await createOrderAndTransitionToModifyingState([
  650. {
  651. productVariantId: 'T_1',
  652. quantity: 1,
  653. },
  654. ]);
  655. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  656. MODIFY_ORDER,
  657. {
  658. input: {
  659. dryRun: false,
  660. orderId: order.id,
  661. adjustOrderLines: [{ orderLineId: order!.lines[0].id, quantity: 2 }],
  662. },
  663. },
  664. );
  665. orderGuard.assertSuccess(modifyOrder);
  666. const priceDelta = order!.lines[0].unitPriceWithTax;
  667. const expectedTotal = order!.totalWithTax + priceDelta;
  668. expect(modifyOrder.totalWithTax).toBe(expectedTotal);
  669. expect(modifyOrder.lines[0].quantity).toBe(2);
  670. expect(modifyOrder.modifications.length).toBe(1);
  671. expect(modifyOrder.modifications[0].priceChange).toBe(priceDelta);
  672. expect(modifyOrder.modifications[0].orderItems?.length).toBe(1);
  673. expect(
  674. modifyOrder.lines[0].items
  675. .map(i => i.id)
  676. .includes(modifyOrder.modifications?.[0].orderItems?.[0].id as string),
  677. ).toBe(true);
  678. await assertModifiedOrderIsPersisted(modifyOrder);
  679. });
  680. it('adjustOrderLines down', async () => {
  681. const order = await createOrderAndTransitionToModifyingState([
  682. {
  683. productVariantId: 'T_1',
  684. quantity: 2,
  685. },
  686. ]);
  687. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  688. MODIFY_ORDER,
  689. {
  690. input: {
  691. dryRun: false,
  692. orderId: order.id,
  693. adjustOrderLines: [{ orderLineId: order!.lines[0].id, quantity: 1 }],
  694. refund: { paymentId: order!.payments![0].id },
  695. },
  696. },
  697. );
  698. orderGuard.assertSuccess(modifyOrder);
  699. const priceDelta = -order!.lines[0].unitPriceWithTax;
  700. const expectedTotal = order!.totalWithTax + priceDelta;
  701. expect(modifyOrder.totalWithTax).toBe(expectedTotal);
  702. expect(modifyOrder.lines[0].quantity).toBe(1);
  703. expect(modifyOrder.payments?.length).toBe(1);
  704. expect(modifyOrder.payments?.[0].refunds.length).toBe(1);
  705. expect(modifyOrder.payments?.[0].refunds[0]).toEqual({
  706. id: 'T_1',
  707. state: 'Pending',
  708. total: -priceDelta,
  709. paymentId: modifyOrder.payments?.[0].id,
  710. });
  711. expect(modifyOrder.modifications.length).toBe(1);
  712. expect(modifyOrder.modifications[0].priceChange).toBe(priceDelta);
  713. expect(modifyOrder.modifications[0].surcharges).toEqual(modifyOrder.surcharges.map(pick(['id'])));
  714. expect(modifyOrder.modifications[0].orderItems?.length).toBe(1);
  715. expect(
  716. modifyOrder.lines[0].items
  717. .map(i => i.id)
  718. .includes(modifyOrder.modifications?.[0].orderItems?.[0].id as string),
  719. ).toBe(true);
  720. await assertModifiedOrderIsPersisted(modifyOrder);
  721. });
  722. it('adjustOrderLines with changed customField value', async () => {
  723. const order = await createOrderAndTransitionToModifyingState([
  724. {
  725. productVariantId: 'T_1',
  726. quantity: 1,
  727. customFields: {
  728. color: 'green',
  729. },
  730. },
  731. ]);
  732. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  733. MODIFY_ORDER,
  734. {
  735. input: {
  736. dryRun: false,
  737. orderId: order.id,
  738. adjustOrderLines: [
  739. {
  740. orderLineId: order!.lines[0].id,
  741. quantity: 1,
  742. customFields: { color: 'black' },
  743. } as any,
  744. ],
  745. },
  746. },
  747. );
  748. orderGuard.assertSuccess(modifyOrder);
  749. expect(modifyOrder.lines.length).toBe(1);
  750. const { order: orderWithLines } = await adminClient.query(gql(GET_ORDER_WITH_CUSTOM_FIELDS), {
  751. id: order.id,
  752. });
  753. expect(orderWithLines.lines[0]).toEqual({
  754. id: order!.lines[0].id,
  755. customFields: { color: 'black' },
  756. });
  757. });
  758. it('adjustOrderLines handles quantity correctly', async () => {
  759. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  760. UPDATE_PRODUCT_VARIANTS,
  761. {
  762. input: [
  763. {
  764. id: 'T_6',
  765. stockOnHand: 1,
  766. trackInventory: GlobalFlag.TRUE,
  767. },
  768. ],
  769. },
  770. );
  771. const order = await createOrderAndTransitionToModifyingState([
  772. {
  773. productVariantId: 'T_6',
  774. quantity: 1,
  775. },
  776. ]);
  777. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  778. MODIFY_ORDER,
  779. {
  780. input: {
  781. dryRun: false,
  782. orderId: order.id,
  783. adjustOrderLines: [
  784. {
  785. orderLineId: order.lines[0].id,
  786. quantity: 1,
  787. },
  788. ],
  789. updateShippingAddress: {
  790. fullName: 'Jim',
  791. },
  792. },
  793. },
  794. );
  795. orderGuard.assertSuccess(modifyOrder);
  796. });
  797. it('surcharge positive', async () => {
  798. const order = await createOrderAndTransitionToModifyingState([
  799. {
  800. productVariantId: 'T_1',
  801. quantity: 1,
  802. },
  803. ]);
  804. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  805. MODIFY_ORDER,
  806. {
  807. input: {
  808. dryRun: false,
  809. orderId: order.id,
  810. surcharges: [
  811. {
  812. description: 'extra fee',
  813. sku: '123',
  814. price: 300,
  815. priceIncludesTax: true,
  816. taxRate: 20,
  817. taxDescription: 'VAT',
  818. },
  819. ],
  820. },
  821. },
  822. );
  823. orderGuard.assertSuccess(modifyOrder);
  824. const priceDelta = 300;
  825. const expectedTotal = order!.totalWithTax + priceDelta;
  826. expect(modifyOrder.totalWithTax).toBe(expectedTotal);
  827. expect(modifyOrder.surcharges.map(s => omit(s, ['id']))).toEqual([
  828. {
  829. description: 'extra fee',
  830. sku: '123',
  831. price: 250,
  832. priceWithTax: 300,
  833. taxRate: 20,
  834. },
  835. ]);
  836. expect(modifyOrder.modifications.length).toBe(1);
  837. expect(modifyOrder.modifications[0].priceChange).toBe(priceDelta);
  838. expect(modifyOrder.modifications[0].surcharges).toEqual(modifyOrder.surcharges.map(pick(['id'])));
  839. await assertModifiedOrderIsPersisted(modifyOrder);
  840. });
  841. it('surcharge negative', async () => {
  842. const order = await createOrderAndTransitionToModifyingState([
  843. {
  844. productVariantId: 'T_1',
  845. quantity: 1,
  846. },
  847. ]);
  848. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  849. MODIFY_ORDER,
  850. {
  851. input: {
  852. dryRun: false,
  853. orderId: order!.id,
  854. surcharges: [
  855. {
  856. description: 'special discount',
  857. sku: '123',
  858. price: -300,
  859. priceIncludesTax: true,
  860. taxRate: 20,
  861. taxDescription: 'VAT',
  862. },
  863. ],
  864. refund: {
  865. paymentId: order!.payments![0].id,
  866. },
  867. },
  868. },
  869. );
  870. orderGuard.assertSuccess(modifyOrder);
  871. const expectedTotal = order!.totalWithTax + -300;
  872. expect(modifyOrder.totalWithTax).toBe(expectedTotal);
  873. expect(modifyOrder.surcharges.map(s => omit(s, ['id']))).toEqual([
  874. {
  875. description: 'special discount',
  876. sku: '123',
  877. price: -250,
  878. priceWithTax: -300,
  879. taxRate: 20,
  880. },
  881. ]);
  882. expect(modifyOrder.modifications.length).toBe(1);
  883. expect(modifyOrder.modifications[0].priceChange).toBe(-300);
  884. await assertModifiedOrderIsPersisted(modifyOrder);
  885. });
  886. it('update updateShippingAddress, recalculate shipping', async () => {
  887. const order = await createOrderAndTransitionToModifyingState([
  888. {
  889. productVariantId: 'T_1',
  890. quantity: 1,
  891. },
  892. ]);
  893. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  894. MODIFY_ORDER,
  895. {
  896. input: {
  897. dryRun: false,
  898. orderId: order!.id,
  899. updateShippingAddress: {
  900. countryCode: 'US',
  901. },
  902. options: {
  903. recalculateShipping: true,
  904. },
  905. },
  906. },
  907. );
  908. orderGuard.assertSuccess(modifyOrder);
  909. const priceDelta = SHIPPING_US - SHIPPING_OTHER;
  910. const expectedTotal = order!.totalWithTax + priceDelta;
  911. expect(modifyOrder.totalWithTax).toBe(expectedTotal);
  912. expect(modifyOrder.shippingAddress?.countryCode).toBe('US');
  913. expect(modifyOrder.modifications.length).toBe(1);
  914. expect(modifyOrder.modifications[0].priceChange).toBe(priceDelta);
  915. await assertModifiedOrderIsPersisted(modifyOrder);
  916. });
  917. it('update updateShippingAddress, do not recalculate shipping', async () => {
  918. const order = await createOrderAndTransitionToModifyingState([
  919. {
  920. productVariantId: 'T_1',
  921. quantity: 1,
  922. },
  923. ]);
  924. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  925. MODIFY_ORDER,
  926. {
  927. input: {
  928. dryRun: false,
  929. orderId: order!.id,
  930. updateShippingAddress: {
  931. countryCode: 'US',
  932. },
  933. options: {
  934. recalculateShipping: false,
  935. },
  936. },
  937. },
  938. );
  939. orderGuard.assertSuccess(modifyOrder);
  940. const priceDelta = 0;
  941. const expectedTotal = order!.totalWithTax + priceDelta;
  942. expect(modifyOrder.totalWithTax).toBe(expectedTotal);
  943. expect(modifyOrder.shippingAddress?.countryCode).toBe('US');
  944. expect(modifyOrder.modifications.length).toBe(1);
  945. expect(modifyOrder.modifications[0].priceChange).toBe(priceDelta);
  946. await assertModifiedOrderIsPersisted(modifyOrder);
  947. });
  948. it('update Order customFields', async () => {
  949. const order = await createOrderAndTransitionToModifyingState([
  950. {
  951. productVariantId: 'T_1',
  952. quantity: 1,
  953. },
  954. ]);
  955. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  956. MODIFY_ORDER,
  957. {
  958. input: {
  959. dryRun: false,
  960. orderId: order.id,
  961. customFields: {
  962. points: 42,
  963. },
  964. } as any,
  965. },
  966. );
  967. orderGuard.assertSuccess(modifyOrder);
  968. const { order: orderWithCustomFields } = await adminClient.query(
  969. gql(GET_ORDER_WITH_CUSTOM_FIELDS),
  970. { id: order.id },
  971. );
  972. expect(orderWithCustomFields.customFields).toEqual({
  973. points: 42,
  974. });
  975. });
  976. it('adds a history entry', async () => {
  977. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  978. id: orderId,
  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. addItems: [{ productVariantId: 'T_5', quantity: 1 }],
  987. },
  988. },
  989. );
  990. orderGuard.assertSuccess(modifyOrder);
  991. const { order: history } = await adminClient.query<
  992. GetOrderHistory.Query,
  993. GetOrderHistory.Variables
  994. >(GET_ORDER_HISTORY, {
  995. id: orderId,
  996. options: { filter: { type: { eq: HistoryEntryType.ORDER_MODIFIED } } },
  997. });
  998. orderGuard.assertSuccess(history);
  999. expect(history.history.totalItems).toBe(1);
  1000. expect(history.history.items[0].data).toEqual({
  1001. modificationId: modifyOrder.modifications[0].id,
  1002. });
  1003. });
  1004. });
  1005. describe('additional payment handling', () => {
  1006. let orderId2: string;
  1007. beforeAll(async () => {
  1008. const order = await createOrderAndTransitionToModifyingState([
  1009. {
  1010. productVariantId: 'T_1',
  1011. quantity: 1,
  1012. },
  1013. ]);
  1014. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  1015. MODIFY_ORDER,
  1016. {
  1017. input: {
  1018. dryRun: false,
  1019. orderId: order.id,
  1020. surcharges: [
  1021. {
  1022. description: 'extra fee',
  1023. sku: '123',
  1024. price: 300,
  1025. priceIncludesTax: true,
  1026. taxRate: 20,
  1027. taxDescription: 'VAT',
  1028. },
  1029. ],
  1030. },
  1031. },
  1032. );
  1033. orderGuard.assertSuccess(modifyOrder);
  1034. orderId2 = modifyOrder.id;
  1035. });
  1036. it('cannot transition back to original state if no payment is set', async () => {
  1037. const transitionOrderToState = await adminTransitionOrderToState(orderId2, 'PaymentSettled');
  1038. orderGuard.assertErrorResult(transitionOrderToState);
  1039. expect(transitionOrderToState!.errorCode).toBe(ErrorCode.ORDER_STATE_TRANSITION_ERROR);
  1040. expect(transitionOrderToState!.transitionError).toBe(
  1041. `Can only transition to the "ArrangingAdditionalPayment" state`,
  1042. );
  1043. });
  1044. it('can transition to ArrangingAdditionalPayment state', async () => {
  1045. const transitionOrderToState = await adminTransitionOrderToState(
  1046. orderId2,
  1047. 'ArrangingAdditionalPayment',
  1048. );
  1049. orderGuard.assertSuccess(transitionOrderToState);
  1050. expect(transitionOrderToState!.state).toBe('ArrangingAdditionalPayment');
  1051. });
  1052. it('cannot transition from ArrangingAdditionalPayment when total not covered by Payments', async () => {
  1053. const transitionOrderToState = await adminTransitionOrderToState(orderId2, 'PaymentSettled');
  1054. orderGuard.assertErrorResult(transitionOrderToState);
  1055. expect(transitionOrderToState!.errorCode).toBe(ErrorCode.ORDER_STATE_TRANSITION_ERROR);
  1056. expect(transitionOrderToState!.transitionError).toBe(
  1057. `Cannot transition away from "ArrangingAdditionalPayment" unless Order total is covered by Payments`,
  1058. );
  1059. });
  1060. it('addManualPaymentToOrder', async () => {
  1061. const { addManualPaymentToOrder } = await adminClient.query<
  1062. AddManualPayment.Mutation,
  1063. AddManualPayment.Variables
  1064. >(ADD_MANUAL_PAYMENT, {
  1065. input: {
  1066. orderId: orderId2,
  1067. method: 'test',
  1068. transactionId: 'ABC123',
  1069. metadata: {
  1070. foo: 'bar',
  1071. },
  1072. },
  1073. });
  1074. orderGuard.assertSuccess(addManualPaymentToOrder);
  1075. expect(addManualPaymentToOrder.payments?.length).toBe(2);
  1076. expect(omit(addManualPaymentToOrder.payments![1], ['id'])).toEqual({
  1077. transactionId: 'ABC123',
  1078. state: 'Settled',
  1079. amount: 300,
  1080. method: 'test',
  1081. metadata: {
  1082. foo: 'bar',
  1083. },
  1084. refunds: [],
  1085. });
  1086. expect(addManualPaymentToOrder.modifications[0].isSettled).toBe(true);
  1087. expect(addManualPaymentToOrder.modifications[0].payment?.id).toBe(
  1088. addManualPaymentToOrder.payments![1].id,
  1089. );
  1090. });
  1091. it('transition back to original state', async () => {
  1092. const transitionOrderToState = await adminTransitionOrderToState(orderId2, 'PaymentSettled');
  1093. orderGuard.assertSuccess(transitionOrderToState);
  1094. expect(transitionOrderToState.state).toBe('PaymentSettled');
  1095. });
  1096. });
  1097. describe('refund handling', () => {
  1098. let orderId3: string;
  1099. beforeAll(async () => {
  1100. const order = await createOrderAndTransitionToModifyingState([
  1101. {
  1102. productVariantId: 'T_1',
  1103. quantity: 1,
  1104. },
  1105. ]);
  1106. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  1107. MODIFY_ORDER,
  1108. {
  1109. input: {
  1110. dryRun: false,
  1111. orderId: order.id,
  1112. surcharges: [
  1113. {
  1114. description: 'discount',
  1115. sku: '123',
  1116. price: -300,
  1117. priceIncludesTax: true,
  1118. taxRate: 20,
  1119. taxDescription: 'VAT',
  1120. },
  1121. ],
  1122. refund: {
  1123. paymentId: order.payments![0].id,
  1124. reason: 'discount',
  1125. },
  1126. },
  1127. },
  1128. );
  1129. orderGuard.assertSuccess(modifyOrder);
  1130. orderId3 = modifyOrder.id;
  1131. });
  1132. it('modification is settled', async () => {
  1133. const { order } = await adminClient.query<
  1134. GetOrderWithModifications.Query,
  1135. GetOrderWithModifications.Variables
  1136. >(GET_ORDER_WITH_MODIFICATIONS, { id: orderId3 });
  1137. expect(order?.modifications.length).toBe(1);
  1138. expect(order?.modifications[0].isSettled).toBe(true);
  1139. });
  1140. it('cannot transition to ArrangingAdditionalPayment state if no payment is needed', async () => {
  1141. const transitionOrderToState = await adminTransitionOrderToState(
  1142. orderId3,
  1143. 'ArrangingAdditionalPayment',
  1144. );
  1145. orderGuard.assertErrorResult(transitionOrderToState);
  1146. expect(transitionOrderToState!.errorCode).toBe(ErrorCode.ORDER_STATE_TRANSITION_ERROR);
  1147. expect(transitionOrderToState!.transitionError).toBe(
  1148. `Cannot transition Order to the \"ArrangingAdditionalPayment\" state as no additional payments are needed`,
  1149. );
  1150. });
  1151. it('can transition to original state', async () => {
  1152. const transitionOrderToState = await adminTransitionOrderToState(orderId3, 'PaymentSettled');
  1153. orderGuard.assertSuccess(transitionOrderToState);
  1154. expect(transitionOrderToState!.state).toBe('PaymentSettled');
  1155. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  1156. id: orderId3,
  1157. });
  1158. expect(order?.payments![0].refunds.length).toBe(1);
  1159. expect(order?.payments![0].refunds[0].total).toBe(300);
  1160. expect(order?.payments![0].refunds[0].reason).toBe('discount');
  1161. });
  1162. });
  1163. // https://github.com/vendure-ecommerce/vendure/issues/688 - 4th point
  1164. it('correct additional payment when discounts applied', async () => {
  1165. await adminClient.query<CreatePromotion.Mutation, CreatePromotion.Variables>(CREATE_PROMOTION, {
  1166. input: {
  1167. name: '$5 off',
  1168. couponCode: '5OFF',
  1169. enabled: true,
  1170. conditions: [],
  1171. actions: [
  1172. {
  1173. code: orderFixedDiscount.code,
  1174. arguments: [{ name: 'discount', value: '500' }],
  1175. },
  1176. ],
  1177. },
  1178. });
  1179. await shopClient.asUserWithCredentials('trevor_donnelly96@hotmail.com', 'test');
  1180. await shopClient.query(gql(ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS), {
  1181. productVariantId: 'T_1',
  1182. quantity: 1,
  1183. } as any);
  1184. await shopClient.query<ApplyCouponCode.Mutation, ApplyCouponCode.Variables>(APPLY_COUPON_CODE, {
  1185. couponCode: '5OFF',
  1186. });
  1187. await proceedToArrangingPayment(shopClient);
  1188. const order = await addPaymentToOrder(shopClient, testSuccessfulPaymentMethod);
  1189. orderGuard.assertSuccess(order);
  1190. const originalTotalWithTax = order.totalWithTax;
  1191. const surcharge = 300;
  1192. const transitionOrderToState = await adminTransitionOrderToState(order.id, 'Modifying');
  1193. orderGuard.assertSuccess(transitionOrderToState);
  1194. expect(transitionOrderToState.state).toBe('Modifying');
  1195. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  1196. MODIFY_ORDER,
  1197. {
  1198. input: {
  1199. dryRun: false,
  1200. orderId: order.id,
  1201. surcharges: [
  1202. {
  1203. description: 'extra fee',
  1204. sku: '123',
  1205. price: surcharge,
  1206. priceIncludesTax: true,
  1207. taxRate: 20,
  1208. taxDescription: 'VAT',
  1209. },
  1210. ],
  1211. },
  1212. },
  1213. );
  1214. orderGuard.assertSuccess(modifyOrder);
  1215. expect(modifyOrder.totalWithTax).toBe(originalTotalWithTax + surcharge);
  1216. });
  1217. // https://github.com/vendure-ecommerce/vendure/issues/872
  1218. describe('correct price calculations when prices include tax', () => {
  1219. async function modifyOrderLineQuantity(order: TestOrderWithPaymentsFragment) {
  1220. const transitionOrderToState = await adminTransitionOrderToState(order.id, 'Modifying');
  1221. orderGuard.assertSuccess(transitionOrderToState);
  1222. expect(transitionOrderToState.state).toBe('Modifying');
  1223. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  1224. MODIFY_ORDER,
  1225. {
  1226. input: {
  1227. dryRun: true,
  1228. orderId: order.id,
  1229. adjustOrderLines: [{ orderLineId: order!.lines[0].id, quantity: 2 }],
  1230. },
  1231. },
  1232. );
  1233. orderGuard.assertSuccess(modifyOrder);
  1234. return modifyOrder;
  1235. }
  1236. beforeAll(async () => {
  1237. await adminClient.query<UpdateChannel.Mutation, UpdateChannel.Variables>(UPDATE_CHANNEL, {
  1238. input: {
  1239. id: 'T_1',
  1240. pricesIncludeTax: true,
  1241. },
  1242. });
  1243. });
  1244. it('without promotion', async () => {
  1245. await shopClient.asUserWithCredentials('hayden.zieme12@hotmail.com', 'test');
  1246. await shopClient.query(gql(ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS), {
  1247. productVariantId: 'T_1',
  1248. quantity: 1,
  1249. } as any);
  1250. await proceedToArrangingPayment(shopClient);
  1251. const order = await addPaymentToOrder(shopClient, testSuccessfulPaymentMethod);
  1252. orderGuard.assertSuccess(order);
  1253. const modifyOrder = await modifyOrderLineQuantity(order);
  1254. expect(modifyOrder.lines[0].linePriceWithTax).toBe(order.lines[0].linePriceWithTax * 2);
  1255. });
  1256. it('with promotion', async () => {
  1257. await adminClient.query<CreatePromotion.Mutation, CreatePromotion.Variables>(CREATE_PROMOTION, {
  1258. input: {
  1259. name: 'half price',
  1260. couponCode: 'HALF',
  1261. enabled: true,
  1262. conditions: [],
  1263. actions: [
  1264. {
  1265. code: productsPercentageDiscount.code,
  1266. arguments: [
  1267. { name: 'discount', value: '50' },
  1268. { name: 'productVariantIds', value: JSON.stringify(['T_1']) },
  1269. ],
  1270. },
  1271. ],
  1272. },
  1273. });
  1274. await shopClient.asUserWithCredentials('trevor_donnelly96@hotmail.com', 'test');
  1275. await shopClient.query(gql(ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS), {
  1276. productVariantId: 'T_1',
  1277. quantity: 1,
  1278. } as any);
  1279. await shopClient.query<ApplyCouponCode.Mutation, ApplyCouponCode.Variables>(APPLY_COUPON_CODE, {
  1280. couponCode: 'HALF',
  1281. });
  1282. await proceedToArrangingPayment(shopClient);
  1283. const order = await addPaymentToOrder(shopClient, testSuccessfulPaymentMethod);
  1284. orderGuard.assertSuccess(order);
  1285. const modifyOrder = await modifyOrderLineQuantity(order);
  1286. expect(modifyOrder.lines[0].discountedLinePriceWithTax).toBe(
  1287. modifyOrder.lines[0].linePriceWithTax / 2,
  1288. );
  1289. expect(modifyOrder.lines[0].linePriceWithTax).toBe(order.lines[0].linePriceWithTax * 2);
  1290. });
  1291. });
  1292. // https://github.com/vendure-ecommerce/vendure/issues/890
  1293. describe('refund handling when promotions are active on order', () => {
  1294. it('refunds correct amount when order-level promotion applied', async () => {
  1295. await adminClient.query<CreatePromotion.Mutation, CreatePromotion.Variables>(CREATE_PROMOTION, {
  1296. input: {
  1297. name: '$5 off',
  1298. couponCode: '5OFF2',
  1299. enabled: true,
  1300. conditions: [],
  1301. actions: [
  1302. {
  1303. code: orderFixedDiscount.code,
  1304. arguments: [{ name: 'discount', value: '500' }],
  1305. },
  1306. ],
  1307. },
  1308. });
  1309. await shopClient.asUserWithCredentials('trevor_donnelly96@hotmail.com', 'test');
  1310. await shopClient.query(gql(ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS), {
  1311. productVariantId: 'T_1',
  1312. quantity: 2,
  1313. } as any);
  1314. await shopClient.query<ApplyCouponCode.Mutation, ApplyCouponCode.Variables>(APPLY_COUPON_CODE, {
  1315. couponCode: '5OFF2',
  1316. });
  1317. await proceedToArrangingPayment(shopClient);
  1318. const order = await addPaymentToOrder(shopClient, testSuccessfulPaymentMethod);
  1319. orderGuard.assertSuccess(order);
  1320. const originalTotalWithTax = order.totalWithTax;
  1321. const transitionOrderToState = await adminTransitionOrderToState(order.id, 'Modifying');
  1322. orderGuard.assertSuccess(transitionOrderToState);
  1323. expect(transitionOrderToState.state).toBe('Modifying');
  1324. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  1325. MODIFY_ORDER,
  1326. {
  1327. input: {
  1328. dryRun: false,
  1329. orderId: order.id,
  1330. adjustOrderLines: [{ orderLineId: order.lines[0].id, quantity: 1 }],
  1331. refund: {
  1332. paymentId: order.payments![0].id,
  1333. reason: 'requested',
  1334. },
  1335. },
  1336. },
  1337. );
  1338. orderGuard.assertSuccess(modifyOrder);
  1339. expect(modifyOrder.totalWithTax).toBe(
  1340. originalTotalWithTax - order.lines[0].proratedUnitPriceWithTax,
  1341. );
  1342. expect(modifyOrder.payments![0].refunds![0].total).toBe(order.lines[0].proratedUnitPriceWithTax);
  1343. });
  1344. });
  1345. // https://github.com/vendure-ecommerce/vendure/issues/1197
  1346. describe('refund on shipping when change made to shippingAddress', () => {
  1347. let order: OrderWithModificationsFragment;
  1348. beforeAll(async () => {
  1349. const createdOrder = await createOrderAndTransitionToModifyingState([
  1350. {
  1351. productVariantId: 'T_1',
  1352. quantity: 1,
  1353. },
  1354. ]);
  1355. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  1356. MODIFY_ORDER,
  1357. {
  1358. input: {
  1359. dryRun: false,
  1360. orderId: createdOrder.id,
  1361. updateShippingAddress: {
  1362. countryCode: 'GB',
  1363. },
  1364. refund: {
  1365. paymentId: createdOrder.payments![0].id,
  1366. reason: 'discount',
  1367. },
  1368. },
  1369. },
  1370. );
  1371. orderGuard.assertSuccess(modifyOrder);
  1372. order = modifyOrder;
  1373. });
  1374. it('creates a Refund with the correct amount', async () => {
  1375. expect(order.payments?.[0].refunds[0].total).toBe(SHIPPING_OTHER - SHIPPING_GB);
  1376. });
  1377. it('allows transition to PaymentSettled', async () => {
  1378. const transitionOrderToState = await adminTransitionOrderToState(order.id, 'PaymentSettled');
  1379. orderGuard.assertSuccess(transitionOrderToState);
  1380. expect(transitionOrderToState.state).toBe('PaymentSettled');
  1381. });
  1382. });
  1383. // https://github.com/vendure-ecommerce/vendure/issues/1210
  1384. describe('updating stock levels', () => {
  1385. async function getVariant(id: 'T_1' | 'T_2' | 'T_3') {
  1386. const { product } = await adminClient.query<GetStockMovement.Query, GetStockMovement.Variables>(
  1387. GET_STOCK_MOVEMENT,
  1388. {
  1389. id: 'T_1',
  1390. },
  1391. );
  1392. return product?.variants.find(v => v.id === id)!;
  1393. }
  1394. let orderId4: string;
  1395. let orderId5: string;
  1396. it('updates stock when increasing quantity before fulfillment', async () => {
  1397. const variant1 = await getVariant('T_2');
  1398. expect(variant1.stockOnHand).toBe(100);
  1399. expect(variant1.stockAllocated).toBe(0);
  1400. const order = await createOrderAndTransitionToModifyingState([
  1401. {
  1402. productVariantId: 'T_2',
  1403. quantity: 1,
  1404. },
  1405. ]);
  1406. orderId4 = order.id;
  1407. const variant2 = await getVariant('T_2');
  1408. expect(variant2.stockOnHand).toBe(100);
  1409. expect(variant2.stockAllocated).toBe(1);
  1410. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  1411. MODIFY_ORDER,
  1412. {
  1413. input: {
  1414. dryRun: false,
  1415. orderId: order.id,
  1416. adjustOrderLines: [{ orderLineId: order.lines[0].id, quantity: 2 }],
  1417. },
  1418. },
  1419. );
  1420. orderGuard.assertSuccess(modifyOrder);
  1421. const variant3 = await getVariant('T_2');
  1422. expect(variant3.stockOnHand).toBe(100);
  1423. expect(variant3.stockAllocated).toBe(2);
  1424. });
  1425. it('updates stock when increasing quantity after fulfillment', async () => {
  1426. const result = await adminTransitionOrderToState(orderId4, 'ArrangingAdditionalPayment');
  1427. orderGuard.assertSuccess(result);
  1428. expect(result!.state).toBe('ArrangingAdditionalPayment');
  1429. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  1430. id: orderId4,
  1431. });
  1432. const { addManualPaymentToOrder } = await adminClient.query<
  1433. AddManualPayment.Mutation,
  1434. AddManualPayment.Variables
  1435. >(ADD_MANUAL_PAYMENT, {
  1436. input: {
  1437. orderId: orderId4,
  1438. method: 'test',
  1439. transactionId: 'ABC123',
  1440. metadata: {
  1441. foo: 'bar',
  1442. },
  1443. },
  1444. });
  1445. orderGuard.assertSuccess(addManualPaymentToOrder);
  1446. await adminTransitionOrderToState(orderId4, 'PaymentSettled');
  1447. await adminClient.query<CreateFulfillment.Mutation, CreateFulfillment.Variables>(
  1448. CREATE_FULFILLMENT,
  1449. {
  1450. input: {
  1451. lines: order?.lines.map(l => ({ orderLineId: l.id, quantity: l.quantity })) ?? [],
  1452. handler: {
  1453. code: manualFulfillmentHandler.code,
  1454. arguments: [
  1455. { name: 'method', value: 'test method' },
  1456. { name: 'trackingCode', value: 'ABC123' },
  1457. ],
  1458. },
  1459. },
  1460. },
  1461. );
  1462. const variant1 = await getVariant('T_2');
  1463. expect(variant1.stockOnHand).toBe(98);
  1464. expect(variant1.stockAllocated).toBe(0);
  1465. await adminTransitionOrderToState(orderId4, 'Modifying');
  1466. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  1467. MODIFY_ORDER,
  1468. {
  1469. input: {
  1470. dryRun: false,
  1471. orderId: order!.id,
  1472. adjustOrderLines: [{ orderLineId: order!.lines[0].id, quantity: 3 }],
  1473. },
  1474. },
  1475. );
  1476. orderGuard.assertSuccess(modifyOrder);
  1477. const variant2 = await getVariant('T_2');
  1478. expect(variant2.stockOnHand).toBe(98);
  1479. expect(variant2.stockAllocated).toBe(1);
  1480. const { order: order2 } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  1481. id: orderId4,
  1482. });
  1483. });
  1484. it('updates stock when adding item before fulfillment', async () => {
  1485. const variant1 = await getVariant('T_3');
  1486. expect(variant1.stockOnHand).toBe(100);
  1487. expect(variant1.stockAllocated).toBe(0);
  1488. const order = await createOrderAndTransitionToModifyingState([
  1489. {
  1490. productVariantId: 'T_2',
  1491. quantity: 1,
  1492. },
  1493. ]);
  1494. orderId5 = order.id;
  1495. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  1496. MODIFY_ORDER,
  1497. {
  1498. input: {
  1499. dryRun: false,
  1500. orderId: order!.id,
  1501. addItems: [{ productVariantId: 'T_3', quantity: 1 }],
  1502. },
  1503. },
  1504. );
  1505. orderGuard.assertSuccess(modifyOrder);
  1506. const variant2 = await getVariant('T_3');
  1507. expect(variant2.stockOnHand).toBe(100);
  1508. expect(variant2.stockAllocated).toBe(1);
  1509. });
  1510. it('updates stock when removing item before fulfillment', async () => {
  1511. const variant1 = await getVariant('T_3');
  1512. expect(variant1.stockOnHand).toBe(100);
  1513. expect(variant1.stockAllocated).toBe(1);
  1514. const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  1515. id: orderId5,
  1516. });
  1517. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  1518. MODIFY_ORDER,
  1519. {
  1520. input: {
  1521. dryRun: false,
  1522. orderId: orderId5,
  1523. adjustOrderLines: [
  1524. {
  1525. orderLineId: order!.lines.find(l => l.productVariant.id === 'T_3')!.id,
  1526. quantity: 0,
  1527. },
  1528. ],
  1529. refund: {
  1530. paymentId: order!.payments![0].id,
  1531. },
  1532. },
  1533. },
  1534. );
  1535. orderGuard.assertSuccess(modifyOrder);
  1536. const variant2 = await getVariant('T_3');
  1537. expect(variant2.stockOnHand).toBe(100);
  1538. expect(variant2.stockAllocated).toBe(0);
  1539. });
  1540. it('updates stock when removing item after fulfillment', async () => {
  1541. const variant1 = await getVariant('T_3');
  1542. expect(variant1.stockOnHand).toBe(100);
  1543. expect(variant1.stockAllocated).toBe(0);
  1544. const order = await createOrderAndCheckout([
  1545. {
  1546. productVariantId: 'T_3',
  1547. quantity: 1,
  1548. },
  1549. ]);
  1550. const { addFulfillmentToOrder } = await adminClient.query<
  1551. CreateFulfillment.Mutation,
  1552. CreateFulfillment.Variables
  1553. >(CREATE_FULFILLMENT, {
  1554. input: {
  1555. lines: order?.lines.map(l => ({ orderLineId: l.id, quantity: l.quantity })) ?? [],
  1556. handler: {
  1557. code: manualFulfillmentHandler.code,
  1558. arguments: [
  1559. { name: 'method', value: 'test method' },
  1560. { name: 'trackingCode', value: 'ABC123' },
  1561. ],
  1562. },
  1563. },
  1564. });
  1565. orderGuard.assertSuccess(addFulfillmentToOrder);
  1566. const variant2 = await getVariant('T_3');
  1567. expect(variant2.stockOnHand).toBe(99);
  1568. expect(variant2.stockAllocated).toBe(0);
  1569. await adminTransitionOrderToState(order.id, 'Modifying');
  1570. const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
  1571. MODIFY_ORDER,
  1572. {
  1573. input: {
  1574. dryRun: false,
  1575. orderId: order.id,
  1576. adjustOrderLines: [
  1577. {
  1578. orderLineId: order!.lines.find(l => l.productVariant.id === 'T_3')!.id,
  1579. quantity: 0,
  1580. },
  1581. ],
  1582. refund: {
  1583. paymentId: order!.payments![0].id,
  1584. },
  1585. },
  1586. },
  1587. );
  1588. const variant3 = await getVariant('T_3');
  1589. expect(variant3.stockOnHand).toBe(100);
  1590. expect(variant3.stockAllocated).toBe(0);
  1591. });
  1592. });
  1593. async function adminTransitionOrderToState(id: string, state: string) {
  1594. const result = await adminClient.query<AdminTransition.Mutation, AdminTransition.Variables>(
  1595. ADMIN_TRANSITION_TO_STATE,
  1596. {
  1597. id,
  1598. state,
  1599. },
  1600. );
  1601. return result.transitionOrderToState;
  1602. }
  1603. async function assertOrderIsUnchanged(order: OrderWithLinesFragment) {
  1604. const { order: order2 } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
  1605. id: order.id,
  1606. });
  1607. expect(order2!.totalWithTax).toBe(order!.totalWithTax);
  1608. expect(order2!.lines.length).toBe(order!.lines.length);
  1609. expect(order2!.surcharges.length).toBe(order!.surcharges.length);
  1610. expect(order2!.totalQuantity).toBe(order!.totalQuantity);
  1611. }
  1612. async function createOrderAndCheckout(
  1613. items: Array<AddItemToOrderMutationVariables & { customFields?: any }>,
  1614. ) {
  1615. await shopClient.asUserWithCredentials('hayden.zieme12@hotmail.com', 'test');
  1616. for (const itemInput of items) {
  1617. await shopClient.query(gql(ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS), itemInput);
  1618. }
  1619. await shopClient.query<SetShippingAddress.Mutation, SetShippingAddress.Variables>(
  1620. SET_SHIPPING_ADDRESS,
  1621. {
  1622. input: {
  1623. fullName: 'name',
  1624. streetLine1: '12 the street',
  1625. city: 'foo',
  1626. postalCode: '123456',
  1627. countryCode: 'AT',
  1628. },
  1629. },
  1630. );
  1631. await shopClient.query<SetShippingMethod.Mutation, SetShippingMethod.Variables>(SET_SHIPPING_METHOD, {
  1632. id: testShippingMethodId,
  1633. });
  1634. await shopClient.query<TransitionToState.Mutation, TransitionToState.Variables>(TRANSITION_TO_STATE, {
  1635. state: 'ArrangingPayment',
  1636. });
  1637. const order = await addPaymentToOrder(shopClient, testSuccessfulPaymentMethod);
  1638. orderGuard.assertSuccess(order);
  1639. return order;
  1640. }
  1641. async function createOrderAndTransitionToModifyingState(
  1642. items: Array<AddItemToOrderMutationVariables & { customFields?: any }>,
  1643. ): Promise<TestOrderWithPaymentsFragment> {
  1644. const order = await createOrderAndCheckout(items);
  1645. await adminTransitionOrderToState(order.id, 'Modifying');
  1646. return order;
  1647. }
  1648. });
  1649. export const ORDER_WITH_MODIFICATION_FRAGMENT = gql`
  1650. fragment OrderWithModifications on Order {
  1651. id
  1652. state
  1653. total
  1654. totalWithTax
  1655. lines {
  1656. id
  1657. quantity
  1658. linePrice
  1659. linePriceWithTax
  1660. discountedLinePriceWithTax
  1661. productVariant {
  1662. id
  1663. name
  1664. }
  1665. items {
  1666. id
  1667. createdAt
  1668. updatedAt
  1669. cancelled
  1670. unitPrice
  1671. }
  1672. }
  1673. surcharges {
  1674. id
  1675. description
  1676. sku
  1677. price
  1678. priceWithTax
  1679. taxRate
  1680. }
  1681. payments {
  1682. id
  1683. transactionId
  1684. state
  1685. amount
  1686. method
  1687. metadata
  1688. refunds {
  1689. id
  1690. state
  1691. total
  1692. paymentId
  1693. }
  1694. }
  1695. modifications {
  1696. id
  1697. note
  1698. priceChange
  1699. isSettled
  1700. orderItems {
  1701. id
  1702. }
  1703. surcharges {
  1704. id
  1705. }
  1706. payment {
  1707. id
  1708. state
  1709. amount
  1710. method
  1711. }
  1712. refund {
  1713. id
  1714. state
  1715. total
  1716. paymentId
  1717. }
  1718. }
  1719. shippingAddress {
  1720. streetLine1
  1721. city
  1722. postalCode
  1723. province
  1724. countryCode
  1725. country
  1726. }
  1727. billingAddress {
  1728. streetLine1
  1729. city
  1730. postalCode
  1731. province
  1732. countryCode
  1733. country
  1734. }
  1735. }
  1736. `;
  1737. export const GET_ORDER_WITH_MODIFICATIONS = gql`
  1738. query GetOrderWithModifications($id: ID!) {
  1739. order(id: $id) {
  1740. ...OrderWithModifications
  1741. }
  1742. }
  1743. ${ORDER_WITH_MODIFICATION_FRAGMENT}
  1744. `;
  1745. export const MODIFY_ORDER = gql`
  1746. mutation ModifyOrder($input: ModifyOrderInput!) {
  1747. modifyOrder(input: $input) {
  1748. ...OrderWithModifications
  1749. ... on ErrorResult {
  1750. errorCode
  1751. message
  1752. }
  1753. }
  1754. }
  1755. ${ORDER_WITH_MODIFICATION_FRAGMENT}
  1756. `;
  1757. export const ADD_MANUAL_PAYMENT = gql`
  1758. mutation AddManualPayment($input: ManualPaymentInput!) {
  1759. addManualPaymentToOrder(input: $input) {
  1760. ...OrderWithModifications
  1761. ... on ErrorResult {
  1762. errorCode
  1763. message
  1764. }
  1765. }
  1766. }
  1767. ${ORDER_WITH_MODIFICATION_FRAGMENT}
  1768. `;
  1769. // Note, we don't use the gql tag around these due to the customFields which
  1770. // would cause a codegen error.
  1771. const ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS = `
  1772. mutation AddItemToOrder($productVariantId: ID!, $quantity: Int!, $customFields: OrderLineCustomFieldsInput) {
  1773. addItemToOrder(productVariantId: $productVariantId, quantity: $quantity, customFields: $customFields) {
  1774. ...on Order { id }
  1775. }
  1776. }
  1777. `;
  1778. const GET_ORDER_WITH_CUSTOM_FIELDS = `
  1779. query GetOrderCustomFields($id: ID!) {
  1780. order(id: $id) {
  1781. customFields { points }
  1782. lines { id, customFields { color } }
  1783. }
  1784. }
  1785. `;