order-promotion.e2e-spec.ts 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649
  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. containsProducts,
  6. customerGroup,
  7. defaultShippingCalculator,
  8. defaultShippingEligibilityChecker,
  9. discountOnItemWithFacets,
  10. hasFacetValues,
  11. manualFulfillmentHandler,
  12. minimumOrderAmount,
  13. orderPercentageDiscount,
  14. productsPercentageDiscount,
  15. } from '@vendure/core';
  16. import {
  17. createErrorResultGuard,
  18. createTestEnvironment,
  19. E2E_DEFAULT_CHANNEL_TOKEN,
  20. ErrorResultGuard,
  21. } from '@vendure/testing';
  22. import gql from 'graphql-tag';
  23. import path from 'path';
  24. import { initialData } from '../../../e2e-common/e2e-initial-data';
  25. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  26. import { freeShipping } from '../src/config/promotion/actions/free-shipping-action';
  27. import { orderFixedDiscount } from '../src/config/promotion/actions/order-fixed-discount-action';
  28. import { testSuccessfulPaymentMethod } from './fixtures/test-payment-methods';
  29. import { CurrencyCode, HistoryEntryType, LanguageCode } from './graphql/generated-e2e-admin-types';
  30. import * as Codegen from './graphql/generated-e2e-admin-types';
  31. import { AdjustmentType, ErrorCode } from './graphql/generated-e2e-shop-types';
  32. import * as CodegenShop from './graphql/generated-e2e-shop-types';
  33. import {
  34. ASSIGN_PRODUCT_TO_CHANNEL,
  35. ASSIGN_PROMOTIONS_TO_CHANNEL,
  36. CANCEL_ORDER,
  37. CREATE_CHANNEL,
  38. CREATE_CUSTOMER_GROUP,
  39. CREATE_PROMOTION,
  40. CREATE_SHIPPING_METHOD,
  41. GET_FACET_LIST,
  42. GET_PRODUCTS_WITH_VARIANT_PRICES,
  43. REMOVE_CUSTOMERS_FROM_GROUP,
  44. } from './graphql/shared-definitions';
  45. import {
  46. ADD_ITEM_TO_ORDER,
  47. ADJUST_ITEM_QUANTITY,
  48. APPLY_COUPON_CODE,
  49. GET_ACTIVE_ORDER,
  50. GET_ORDER_PROMOTIONS_BY_CODE,
  51. REMOVE_COUPON_CODE,
  52. REMOVE_ITEM_FROM_ORDER,
  53. SET_CUSTOMER,
  54. SET_SHIPPING_METHOD,
  55. } from './graphql/shop-definitions';
  56. import { addPaymentToOrder, proceedToArrangingPayment } from './utils/test-order-utils';
  57. describe('Promotions applied to Orders', () => {
  58. const { server, adminClient, shopClient } = createTestEnvironment({
  59. ...testConfig(),
  60. paymentOptions: {
  61. paymentMethodHandlers: [testSuccessfulPaymentMethod],
  62. },
  63. });
  64. const freeOrderAction = {
  65. code: orderPercentageDiscount.code,
  66. arguments: [{ name: 'discount', value: '100' }],
  67. };
  68. const minOrderAmountCondition = (min: number) => ({
  69. code: minimumOrderAmount.code,
  70. arguments: [
  71. { name: 'amount', value: min.toString() },
  72. { name: 'taxInclusive', value: 'true' },
  73. ],
  74. });
  75. type OrderSuccessResult = CodegenShop.UpdatedOrderFragment | CodegenShop.TestOrderFragmentFragment;
  76. const orderResultGuard: ErrorResultGuard<OrderSuccessResult> = createErrorResultGuard(
  77. input => !!input.lines,
  78. );
  79. let products: Codegen.GetProductsWithVariantPricesQuery['products']['items'];
  80. beforeAll(async () => {
  81. await server.init({
  82. initialData: {
  83. ...initialData,
  84. paymentMethods: [
  85. {
  86. name: testSuccessfulPaymentMethod.code,
  87. handler: { code: testSuccessfulPaymentMethod.code, arguments: [] },
  88. },
  89. ],
  90. },
  91. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-promotions.csv'),
  92. customerCount: 2,
  93. });
  94. await adminClient.asSuperAdmin();
  95. await getProducts();
  96. await createGlobalPromotions();
  97. }, TEST_SETUP_TIMEOUT_MS);
  98. afterAll(async () => {
  99. await server.destroy();
  100. });
  101. describe('coupon codes', () => {
  102. const TEST_COUPON_CODE = 'TESTCOUPON';
  103. const EXPIRED_COUPON_CODE = 'EXPIRED';
  104. let promoFreeWithCoupon: Codegen.PromotionFragment;
  105. let promoFreeWithExpiredCoupon: Codegen.PromotionFragment;
  106. beforeAll(async () => {
  107. promoFreeWithCoupon = await createPromotion({
  108. enabled: true,
  109. name: 'Free with test coupon',
  110. couponCode: TEST_COUPON_CODE,
  111. conditions: [],
  112. actions: [freeOrderAction],
  113. });
  114. promoFreeWithExpiredCoupon = await createPromotion({
  115. enabled: true,
  116. name: 'Expired coupon',
  117. endsAt: new Date(2010, 0, 0),
  118. couponCode: EXPIRED_COUPON_CODE,
  119. conditions: [],
  120. actions: [freeOrderAction],
  121. });
  122. await shopClient.asAnonymousUser();
  123. await shopClient.query<
  124. CodegenShop.AddItemToOrderMutation,
  125. CodegenShop.AddItemToOrderMutationVariables
  126. >(ADD_ITEM_TO_ORDER, {
  127. productVariantId: getVariantBySlug('item-5000').id,
  128. quantity: 1,
  129. });
  130. });
  131. afterAll(async () => {
  132. await deletePromotion(promoFreeWithCoupon.id);
  133. await deletePromotion(promoFreeWithExpiredCoupon.id);
  134. });
  135. it('applyCouponCode returns error result when code is nonexistant', async () => {
  136. const { applyCouponCode } = await shopClient.query<
  137. CodegenShop.ApplyCouponCodeMutation,
  138. CodegenShop.ApplyCouponCodeMutationVariables
  139. >(APPLY_COUPON_CODE, {
  140. couponCode: 'bad code',
  141. });
  142. orderResultGuard.assertErrorResult(applyCouponCode);
  143. expect(applyCouponCode.message).toBe('Coupon code "bad code" is not valid');
  144. expect(applyCouponCode.errorCode).toBe(ErrorCode.COUPON_CODE_INVALID_ERROR);
  145. });
  146. it('applyCouponCode returns error when code is expired', async () => {
  147. const { applyCouponCode } = await shopClient.query<
  148. CodegenShop.ApplyCouponCodeMutation,
  149. CodegenShop.ApplyCouponCodeMutationVariables
  150. >(APPLY_COUPON_CODE, {
  151. couponCode: EXPIRED_COUPON_CODE,
  152. });
  153. orderResultGuard.assertErrorResult(applyCouponCode);
  154. expect(applyCouponCode.message).toBe(`Coupon code "${EXPIRED_COUPON_CODE}" has expired`);
  155. expect(applyCouponCode.errorCode).toBe(ErrorCode.COUPON_CODE_EXPIRED_ERROR);
  156. });
  157. it('applies a valid coupon code', async () => {
  158. const { applyCouponCode } = await shopClient.query<
  159. CodegenShop.ApplyCouponCodeMutation,
  160. CodegenShop.ApplyCouponCodeMutationVariables
  161. >(APPLY_COUPON_CODE, {
  162. couponCode: TEST_COUPON_CODE,
  163. });
  164. orderResultGuard.assertSuccess(applyCouponCode);
  165. expect(applyCouponCode!.couponCodes).toEqual([TEST_COUPON_CODE]);
  166. expect(applyCouponCode!.discounts.length).toBe(1);
  167. expect(applyCouponCode!.discounts[0].description).toBe('Free with test coupon');
  168. expect(applyCouponCode!.totalWithTax).toBe(0);
  169. });
  170. it('order history records application', async () => {
  171. const { activeOrder } = await shopClient.query<CodegenShop.GetActiveOrderQuery>(GET_ACTIVE_ORDER);
  172. expect(activeOrder!.history.items.map(i => omit(i, ['id']))).toEqual([
  173. {
  174. type: HistoryEntryType.ORDER_STATE_TRANSITION,
  175. data: {
  176. from: 'Created',
  177. to: 'AddingItems',
  178. },
  179. },
  180. {
  181. type: HistoryEntryType.ORDER_COUPON_APPLIED,
  182. data: {
  183. couponCode: TEST_COUPON_CODE,
  184. promotionId: 'T_3',
  185. },
  186. },
  187. ]);
  188. });
  189. it('de-duplicates existing codes', async () => {
  190. const { applyCouponCode } = await shopClient.query<
  191. CodegenShop.ApplyCouponCodeMutation,
  192. CodegenShop.ApplyCouponCodeMutationVariables
  193. >(APPLY_COUPON_CODE, {
  194. couponCode: TEST_COUPON_CODE,
  195. });
  196. orderResultGuard.assertSuccess(applyCouponCode);
  197. expect(applyCouponCode!.couponCodes).toEqual([TEST_COUPON_CODE]);
  198. });
  199. it('removes a coupon code', async () => {
  200. const { removeCouponCode } = await shopClient.query<
  201. CodegenShop.RemoveCouponCodeMutation,
  202. CodegenShop.RemoveCouponCodeMutationVariables
  203. >(REMOVE_COUPON_CODE, {
  204. couponCode: TEST_COUPON_CODE,
  205. });
  206. expect(removeCouponCode!.discounts.length).toBe(0);
  207. expect(removeCouponCode!.totalWithTax).toBe(6000);
  208. });
  209. // https://github.com/vendure-ecommerce/vendure/issues/649
  210. it('discounts array cleared after coupon code removed', async () => {
  211. const { activeOrder } = await shopClient.query<CodegenShop.GetActiveOrderQuery>(GET_ACTIVE_ORDER);
  212. expect(activeOrder?.discounts).toEqual([]);
  213. });
  214. it('order history records removal', async () => {
  215. const { activeOrder } = await shopClient.query<CodegenShop.GetActiveOrderQuery>(GET_ACTIVE_ORDER);
  216. expect(activeOrder!.history.items.map(i => omit(i, ['id']))).toEqual([
  217. {
  218. type: HistoryEntryType.ORDER_STATE_TRANSITION,
  219. data: {
  220. from: 'Created',
  221. to: 'AddingItems',
  222. },
  223. },
  224. {
  225. type: HistoryEntryType.ORDER_COUPON_APPLIED,
  226. data: {
  227. couponCode: TEST_COUPON_CODE,
  228. promotionId: 'T_3',
  229. },
  230. },
  231. {
  232. type: HistoryEntryType.ORDER_COUPON_REMOVED,
  233. data: {
  234. couponCode: TEST_COUPON_CODE,
  235. },
  236. },
  237. ]);
  238. });
  239. it('does not record removal of coupon code that was not added', async () => {
  240. const { removeCouponCode } = await shopClient.query<
  241. CodegenShop.RemoveCouponCodeMutation,
  242. CodegenShop.RemoveCouponCodeMutationVariables
  243. >(REMOVE_COUPON_CODE, {
  244. couponCode: 'NOT_THERE',
  245. });
  246. expect(removeCouponCode!.history.items.map(i => omit(i, ['id']))).toEqual([
  247. {
  248. type: HistoryEntryType.ORDER_STATE_TRANSITION,
  249. data: {
  250. from: 'Created',
  251. to: 'AddingItems',
  252. },
  253. },
  254. {
  255. type: HistoryEntryType.ORDER_COUPON_APPLIED,
  256. data: {
  257. couponCode: TEST_COUPON_CODE,
  258. promotionId: 'T_3',
  259. },
  260. },
  261. {
  262. type: HistoryEntryType.ORDER_COUPON_REMOVED,
  263. data: {
  264. couponCode: TEST_COUPON_CODE,
  265. },
  266. },
  267. ]);
  268. });
  269. });
  270. describe('default PromotionConditions', () => {
  271. beforeEach(async () => {
  272. await shopClient.asAnonymousUser();
  273. });
  274. it('minimumOrderAmount', async () => {
  275. const promotion = await createPromotion({
  276. enabled: true,
  277. name: 'Free if order total greater than 100',
  278. conditions: [minOrderAmountCondition(10000)],
  279. actions: [freeOrderAction],
  280. });
  281. const { addItemToOrder } = await shopClient.query<
  282. CodegenShop.AddItemToOrderMutation,
  283. CodegenShop.AddItemToOrderMutationVariables
  284. >(ADD_ITEM_TO_ORDER, {
  285. productVariantId: getVariantBySlug('item-5000').id,
  286. quantity: 1,
  287. });
  288. orderResultGuard.assertSuccess(addItemToOrder);
  289. expect(addItemToOrder!.totalWithTax).toBe(6000);
  290. expect(addItemToOrder!.discounts.length).toBe(0);
  291. const { adjustOrderLine } = await shopClient.query<
  292. CodegenShop.AdjustItemQuantityMutation,
  293. CodegenShop.AdjustItemQuantityMutationVariables
  294. >(ADJUST_ITEM_QUANTITY, {
  295. orderLineId: addItemToOrder!.lines[0].id,
  296. quantity: 2,
  297. });
  298. orderResultGuard.assertSuccess(adjustOrderLine);
  299. expect(adjustOrderLine!.totalWithTax).toBe(0);
  300. expect(adjustOrderLine!.discounts[0].description).toBe('Free if order total greater than 100');
  301. expect(adjustOrderLine!.discounts[0].amountWithTax).toBe(-12000);
  302. await deletePromotion(promotion.id);
  303. });
  304. it('atLeastNWithFacets', async () => {
  305. const { facets } = await adminClient.query<Codegen.GetFacetListQuery>(GET_FACET_LIST);
  306. const saleFacetValue = facets.items[0].values[0];
  307. const promotion = await createPromotion({
  308. enabled: true,
  309. name: 'Free if order contains 2 items with Sale facet value',
  310. conditions: [
  311. {
  312. code: hasFacetValues.code,
  313. arguments: [
  314. { name: 'minimum', value: '2' },
  315. { name: 'facets', value: `["${saleFacetValue.id}"]` },
  316. ],
  317. },
  318. ],
  319. actions: [freeOrderAction],
  320. });
  321. const { addItemToOrder: res1 } = await shopClient.query<
  322. CodegenShop.AddItemToOrderMutation,
  323. CodegenShop.AddItemToOrderMutationVariables
  324. >(ADD_ITEM_TO_ORDER, {
  325. productVariantId: getVariantBySlug('item-sale-100').id,
  326. quantity: 1,
  327. });
  328. orderResultGuard.assertSuccess(res1);
  329. expect(res1!.totalWithTax).toBe(120);
  330. expect(res1!.discounts.length).toBe(0);
  331. const { addItemToOrder: res2 } = await shopClient.query<
  332. CodegenShop.AddItemToOrderMutation,
  333. CodegenShop.AddItemToOrderMutationVariables
  334. >(ADD_ITEM_TO_ORDER, {
  335. productVariantId: getVariantBySlug('item-sale-1000').id,
  336. quantity: 1,
  337. });
  338. orderResultGuard.assertSuccess(res2);
  339. expect(res2!.totalWithTax).toBe(0);
  340. expect(res2!.discounts.length).toBe(1);
  341. expect(res2!.totalWithTax).toBe(0);
  342. expect(res2!.discounts[0].description).toBe(
  343. 'Free if order contains 2 items with Sale facet value',
  344. );
  345. expect(res2!.discounts[0].amountWithTax).toBe(-1320);
  346. await deletePromotion(promotion.id);
  347. });
  348. it('containsProducts', async () => {
  349. const promotion = await createPromotion({
  350. enabled: true,
  351. name: 'Free if buying 3 or more offer products',
  352. conditions: [
  353. {
  354. code: containsProducts.code,
  355. arguments: [
  356. { name: 'minimum', value: '3' },
  357. {
  358. name: 'productVariantIds',
  359. value: JSON.stringify([
  360. getVariantBySlug('item-5000').id,
  361. getVariantBySlug('item-1000').id,
  362. ]),
  363. },
  364. ],
  365. },
  366. ],
  367. actions: [freeOrderAction],
  368. });
  369. await shopClient.query<
  370. CodegenShop.AddItemToOrderMutation,
  371. CodegenShop.AddItemToOrderMutationVariables
  372. >(ADD_ITEM_TO_ORDER, {
  373. productVariantId: getVariantBySlug('item-5000').id,
  374. quantity: 1,
  375. });
  376. const { addItemToOrder } = await shopClient.query<
  377. CodegenShop.AddItemToOrderMutation,
  378. CodegenShop.AddItemToOrderMutationVariables
  379. >(ADD_ITEM_TO_ORDER, {
  380. productVariantId: getVariantBySlug('item-1000').id,
  381. quantity: 1,
  382. });
  383. orderResultGuard.assertSuccess(addItemToOrder);
  384. expect(addItemToOrder!.totalWithTax).toBe(7200);
  385. expect(addItemToOrder!.discounts.length).toBe(0);
  386. const { adjustOrderLine } = await shopClient.query<
  387. CodegenShop.AdjustItemQuantityMutation,
  388. CodegenShop.AdjustItemQuantityMutationVariables
  389. >(ADJUST_ITEM_QUANTITY, {
  390. orderLineId: addItemToOrder!.lines[0].id,
  391. quantity: 2,
  392. });
  393. orderResultGuard.assertSuccess(adjustOrderLine);
  394. expect(adjustOrderLine!.total).toBe(0);
  395. expect(adjustOrderLine!.discounts[0].description).toBe('Free if buying 3 or more offer products');
  396. expect(adjustOrderLine!.discounts[0].amountWithTax).toBe(-13200);
  397. await deletePromotion(promotion.id);
  398. });
  399. it('customerGroup', async () => {
  400. const { createCustomerGroup } = await adminClient.query<
  401. Codegen.CreateCustomerGroupMutation,
  402. Codegen.CreateCustomerGroupMutationVariables
  403. >(CREATE_CUSTOMER_GROUP, {
  404. input: { name: 'Test Group', customerIds: ['T_1'] },
  405. });
  406. await shopClient.asUserWithCredentials('hayden.zieme12@hotmail.com', 'test');
  407. const promotion = await createPromotion({
  408. enabled: true,
  409. name: 'Free for group members',
  410. conditions: [
  411. {
  412. code: customerGroup.code,
  413. arguments: [{ name: 'customerGroupId', value: createCustomerGroup.id }],
  414. },
  415. ],
  416. actions: [freeOrderAction],
  417. });
  418. const { addItemToOrder } = await shopClient.query<
  419. CodegenShop.AddItemToOrderMutation,
  420. CodegenShop.AddItemToOrderMutationVariables
  421. >(ADD_ITEM_TO_ORDER, {
  422. productVariantId: getVariantBySlug('item-5000').id,
  423. quantity: 1,
  424. });
  425. orderResultGuard.assertSuccess(addItemToOrder);
  426. expect(addItemToOrder!.totalWithTax).toBe(0);
  427. expect(addItemToOrder!.discounts.length).toBe(1);
  428. expect(addItemToOrder!.discounts[0].description).toBe('Free for group members');
  429. expect(addItemToOrder!.discounts[0].amountWithTax).toBe(-6000);
  430. await adminClient.query<
  431. Codegen.RemoveCustomersFromGroupMutation,
  432. Codegen.RemoveCustomersFromGroupMutationVariables
  433. >(REMOVE_CUSTOMERS_FROM_GROUP, {
  434. groupId: createCustomerGroup.id,
  435. customerIds: ['T_1'],
  436. });
  437. const { adjustOrderLine } = await shopClient.query<
  438. CodegenShop.AdjustItemQuantityMutation,
  439. CodegenShop.AdjustItemQuantityMutationVariables
  440. >(ADJUST_ITEM_QUANTITY, {
  441. orderLineId: addItemToOrder!.lines[0].id,
  442. quantity: 2,
  443. });
  444. orderResultGuard.assertSuccess(adjustOrderLine);
  445. expect(adjustOrderLine!.totalWithTax).toBe(12000);
  446. expect(adjustOrderLine!.discounts.length).toBe(0);
  447. await deletePromotion(promotion.id);
  448. });
  449. });
  450. describe('default PromotionActions', () => {
  451. const TAX_INCLUDED_CHANNEL_TOKEN = 'tax_included_channel';
  452. let taxIncludedChannel: Codegen.ChannelFragment;
  453. beforeAll(async () => {
  454. // Create a channel where the prices include tax, so we can ensure
  455. // that PromotionActions are working as expected when taxes are included
  456. const { createChannel } = await adminClient.query<
  457. Codegen.CreateChannelMutation,
  458. Codegen.CreateChannelMutationVariables
  459. >(CREATE_CHANNEL, {
  460. input: {
  461. code: 'tax-included-channel',
  462. currencyCode: CurrencyCode.GBP,
  463. pricesIncludeTax: true,
  464. defaultTaxZoneId: 'T_1',
  465. defaultShippingZoneId: 'T_1',
  466. defaultLanguageCode: LanguageCode.en,
  467. token: TAX_INCLUDED_CHANNEL_TOKEN,
  468. },
  469. });
  470. taxIncludedChannel = createChannel as Codegen.ChannelFragment;
  471. await adminClient.query<
  472. Codegen.AssignProductsToChannelMutation,
  473. Codegen.AssignProductsToChannelMutationVariables
  474. >(ASSIGN_PRODUCT_TO_CHANNEL, {
  475. input: {
  476. channelId: taxIncludedChannel.id,
  477. priceFactor: 1,
  478. productIds: products.map(p => p.id),
  479. },
  480. });
  481. });
  482. beforeEach(async () => {
  483. await shopClient.asAnonymousUser();
  484. });
  485. async function assignPromotionToTaxIncludedChannel(promotionId: string | string[]) {
  486. await adminClient.query<
  487. Codegen.AssignPromotionToChannelMutation,
  488. Codegen.AssignPromotionToChannelMutationVariables
  489. >(ASSIGN_PROMOTIONS_TO_CHANNEL, {
  490. input: {
  491. promotionIds: Array.isArray(promotionId) ? promotionId : [promotionId],
  492. channelId: taxIncludedChannel.id,
  493. },
  494. });
  495. }
  496. describe('orderPercentageDiscount', () => {
  497. const couponCode = '50%_off_order';
  498. let promotion: Codegen.PromotionFragment;
  499. beforeAll(async () => {
  500. promotion = await createPromotion({
  501. enabled: true,
  502. name: '20% discount on order',
  503. couponCode,
  504. conditions: [],
  505. actions: [
  506. {
  507. code: orderPercentageDiscount.code,
  508. arguments: [{ name: 'discount', value: '20' }],
  509. },
  510. ],
  511. });
  512. await assignPromotionToTaxIncludedChannel(promotion.id);
  513. });
  514. afterAll(async () => {
  515. await deletePromotion(promotion.id);
  516. });
  517. it('prices exclude tax', async () => {
  518. shopClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  519. const { addItemToOrder } = await shopClient.query<
  520. CodegenShop.AddItemToOrderMutation,
  521. CodegenShop.AddItemToOrderMutationVariables
  522. >(ADD_ITEM_TO_ORDER, {
  523. productVariantId: getVariantBySlug('item-5000').id,
  524. quantity: 1,
  525. });
  526. orderResultGuard.assertSuccess(addItemToOrder);
  527. expect(addItemToOrder!.totalWithTax).toBe(6000);
  528. expect(addItemToOrder!.discounts.length).toBe(0);
  529. const { applyCouponCode } = await shopClient.query<
  530. CodegenShop.ApplyCouponCodeMutation,
  531. CodegenShop.ApplyCouponCodeMutationVariables
  532. >(APPLY_COUPON_CODE, {
  533. couponCode,
  534. });
  535. orderResultGuard.assertSuccess(applyCouponCode);
  536. expect(applyCouponCode!.discounts.length).toBe(1);
  537. expect(applyCouponCode!.discounts[0].description).toBe('20% discount on order');
  538. expect(applyCouponCode!.totalWithTax).toBe(4800);
  539. });
  540. it('prices include tax', async () => {
  541. shopClient.setChannelToken(TAX_INCLUDED_CHANNEL_TOKEN);
  542. const { addItemToOrder } = await shopClient.query<
  543. CodegenShop.AddItemToOrderMutation,
  544. CodegenShop.AddItemToOrderMutationVariables
  545. >(ADD_ITEM_TO_ORDER, {
  546. productVariantId: getVariantBySlug('item-5000').id,
  547. quantity: 1,
  548. });
  549. orderResultGuard.assertSuccess(addItemToOrder);
  550. expect(addItemToOrder!.totalWithTax).toBe(6000);
  551. expect(addItemToOrder!.discounts.length).toBe(0);
  552. const { applyCouponCode } = await shopClient.query<
  553. CodegenShop.ApplyCouponCodeMutation,
  554. CodegenShop.ApplyCouponCodeMutationVariables
  555. >(APPLY_COUPON_CODE, {
  556. couponCode,
  557. });
  558. orderResultGuard.assertSuccess(applyCouponCode);
  559. expect(applyCouponCode!.discounts.length).toBe(1);
  560. expect(applyCouponCode!.discounts[0].description).toBe('20% discount on order');
  561. expect(applyCouponCode!.totalWithTax).toBe(4800);
  562. });
  563. });
  564. describe('orderFixedDiscount', () => {
  565. const couponCode = '10_off_order';
  566. let promotion: Codegen.PromotionFragment;
  567. beforeAll(async () => {
  568. promotion = await createPromotion({
  569. enabled: true,
  570. name: '$10 discount on order',
  571. couponCode,
  572. conditions: [],
  573. actions: [
  574. {
  575. code: orderFixedDiscount.code,
  576. arguments: [{ name: 'discount', value: '1000' }],
  577. },
  578. ],
  579. });
  580. await assignPromotionToTaxIncludedChannel(promotion.id);
  581. });
  582. afterAll(async () => {
  583. await deletePromotion(promotion.id);
  584. shopClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  585. });
  586. it('prices exclude tax', async () => {
  587. shopClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  588. const { addItemToOrder } = await shopClient.query<
  589. CodegenShop.AddItemToOrderMutation,
  590. CodegenShop.AddItemToOrderMutationVariables
  591. >(ADD_ITEM_TO_ORDER, {
  592. productVariantId: getVariantBySlug('item-5000').id,
  593. quantity: 1,
  594. });
  595. orderResultGuard.assertSuccess(addItemToOrder);
  596. expect(addItemToOrder!.total).toBe(5000);
  597. expect(addItemToOrder!.totalWithTax).toBe(6000);
  598. expect(addItemToOrder!.discounts.length).toBe(0);
  599. const { applyCouponCode } = await shopClient.query<
  600. CodegenShop.ApplyCouponCodeMutation,
  601. CodegenShop.ApplyCouponCodeMutationVariables
  602. >(APPLY_COUPON_CODE, {
  603. couponCode,
  604. });
  605. orderResultGuard.assertSuccess(applyCouponCode);
  606. expect(applyCouponCode!.discounts.length).toBe(1);
  607. expect(applyCouponCode!.discounts[0].description).toBe('$10 discount on order');
  608. expect(applyCouponCode!.total).toBe(4000);
  609. expect(applyCouponCode!.totalWithTax).toBe(4800);
  610. });
  611. it('prices include tax', async () => {
  612. shopClient.setChannelToken(TAX_INCLUDED_CHANNEL_TOKEN);
  613. const { addItemToOrder } = await shopClient.query<
  614. CodegenShop.AddItemToOrderMutation,
  615. CodegenShop.AddItemToOrderMutationVariables
  616. >(ADD_ITEM_TO_ORDER, {
  617. productVariantId: getVariantBySlug('item-5000').id,
  618. quantity: 1,
  619. });
  620. orderResultGuard.assertSuccess(addItemToOrder);
  621. expect(addItemToOrder!.totalWithTax).toBe(6000);
  622. expect(addItemToOrder!.discounts.length).toBe(0);
  623. const { applyCouponCode } = await shopClient.query<
  624. CodegenShop.ApplyCouponCodeMutation,
  625. CodegenShop.ApplyCouponCodeMutationVariables
  626. >(APPLY_COUPON_CODE, {
  627. couponCode,
  628. });
  629. orderResultGuard.assertSuccess(applyCouponCode);
  630. expect(applyCouponCode!.discounts.length).toBe(1);
  631. expect(applyCouponCode!.discounts[0].description).toBe('$10 discount on order');
  632. expect(applyCouponCode!.totalWithTax).toBe(5000);
  633. });
  634. });
  635. describe('discountOnItemWithFacets', () => {
  636. const couponCode = '50%_off_sale_items';
  637. let promotion: Codegen.PromotionFragment;
  638. function getItemSale1Line<
  639. T extends Array<
  640. | CodegenShop.UpdatedOrderFragment['lines'][number]
  641. | CodegenShop.TestOrderFragmentFragment['lines'][number]
  642. >,
  643. >(lines: T): T[number] {
  644. return lines.find(l => l.productVariant.id === getVariantBySlug('item-sale-100').id)!;
  645. }
  646. beforeAll(async () => {
  647. const { facets } = await adminClient.query<Codegen.GetFacetListQuery>(GET_FACET_LIST);
  648. const saleFacetValue = facets.items[0].values[0];
  649. promotion = await createPromotion({
  650. enabled: true,
  651. name: '50% off sale items',
  652. couponCode,
  653. conditions: [],
  654. actions: [
  655. {
  656. code: discountOnItemWithFacets.code,
  657. arguments: [
  658. { name: 'discount', value: '50' },
  659. { name: 'facets', value: `["${saleFacetValue.id}"]` },
  660. ],
  661. },
  662. ],
  663. });
  664. await assignPromotionToTaxIncludedChannel(promotion.id);
  665. });
  666. afterAll(async () => {
  667. await deletePromotion(promotion.id);
  668. shopClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  669. });
  670. it('prices exclude tax', async () => {
  671. await shopClient.query<
  672. CodegenShop.AddItemToOrderMutation,
  673. CodegenShop.AddItemToOrderMutationVariables
  674. >(ADD_ITEM_TO_ORDER, {
  675. productVariantId: getVariantBySlug('item-1000').id,
  676. quantity: 1,
  677. });
  678. await shopClient.query<
  679. CodegenShop.AddItemToOrderMutation,
  680. CodegenShop.AddItemToOrderMutationVariables
  681. >(ADD_ITEM_TO_ORDER, {
  682. productVariantId: getVariantBySlug('item-sale-1000').id,
  683. quantity: 1,
  684. });
  685. const { addItemToOrder } = await shopClient.query<
  686. CodegenShop.AddItemToOrderMutation,
  687. CodegenShop.AddItemToOrderMutationVariables
  688. >(ADD_ITEM_TO_ORDER, {
  689. productVariantId: getVariantBySlug('item-sale-100').id,
  690. quantity: 2,
  691. });
  692. orderResultGuard.assertSuccess(addItemToOrder);
  693. expect(addItemToOrder!.discounts.length).toBe(0);
  694. expect(getItemSale1Line(addItemToOrder!.lines).discounts.length).toBe(0);
  695. expect(addItemToOrder!.total).toBe(2200);
  696. expect(addItemToOrder!.totalWithTax).toBe(2640);
  697. const { applyCouponCode } = await shopClient.query<
  698. CodegenShop.ApplyCouponCodeMutation,
  699. CodegenShop.ApplyCouponCodeMutationVariables
  700. >(APPLY_COUPON_CODE, {
  701. couponCode,
  702. });
  703. orderResultGuard.assertSuccess(applyCouponCode);
  704. expect(applyCouponCode!.total).toBe(1600);
  705. expect(applyCouponCode!.totalWithTax).toBe(1920);
  706. expect(getItemSale1Line(applyCouponCode!.lines).discounts.length).toBe(1); // 1x promotion
  707. const { removeCouponCode } = await shopClient.query<
  708. CodegenShop.RemoveCouponCodeMutation,
  709. CodegenShop.RemoveCouponCodeMutationVariables
  710. >(REMOVE_COUPON_CODE, {
  711. couponCode,
  712. });
  713. expect(getItemSale1Line(removeCouponCode!.lines).discounts.length).toBe(0);
  714. expect(removeCouponCode!.total).toBe(2200);
  715. expect(removeCouponCode!.totalWithTax).toBe(2640);
  716. const { activeOrder } = await shopClient.query<CodegenShop.GetActiveOrderQuery>(
  717. GET_ACTIVE_ORDER,
  718. );
  719. expect(getItemSale1Line(activeOrder!.lines).discounts.length).toBe(0);
  720. expect(activeOrder!.total).toBe(2200);
  721. expect(activeOrder!.totalWithTax).toBe(2640);
  722. });
  723. it('prices include tax', async () => {
  724. shopClient.setChannelToken(TAX_INCLUDED_CHANNEL_TOKEN);
  725. await shopClient.query<
  726. CodegenShop.AddItemToOrderMutation,
  727. CodegenShop.AddItemToOrderMutationVariables
  728. >(ADD_ITEM_TO_ORDER, {
  729. productVariantId: getVariantBySlug('item-1000').id,
  730. quantity: 1,
  731. });
  732. await shopClient.query<
  733. CodegenShop.AddItemToOrderMutation,
  734. CodegenShop.AddItemToOrderMutationVariables
  735. >(ADD_ITEM_TO_ORDER, {
  736. productVariantId: getVariantBySlug('item-sale-1000').id,
  737. quantity: 1,
  738. });
  739. const { addItemToOrder } = await shopClient.query<
  740. CodegenShop.AddItemToOrderMutation,
  741. CodegenShop.AddItemToOrderMutationVariables
  742. >(ADD_ITEM_TO_ORDER, {
  743. productVariantId: getVariantBySlug('item-sale-100').id,
  744. quantity: 2,
  745. });
  746. orderResultGuard.assertSuccess(addItemToOrder);
  747. expect(addItemToOrder!.discounts.length).toBe(0);
  748. expect(getItemSale1Line(addItemToOrder!.lines).discounts.length).toBe(0);
  749. expect(addItemToOrder!.total).toBe(2200);
  750. expect(addItemToOrder!.totalWithTax).toBe(2640);
  751. const { applyCouponCode } = await shopClient.query<
  752. CodegenShop.ApplyCouponCodeMutation,
  753. CodegenShop.ApplyCouponCodeMutationVariables
  754. >(APPLY_COUPON_CODE, {
  755. couponCode,
  756. });
  757. orderResultGuard.assertSuccess(applyCouponCode);
  758. expect(applyCouponCode!.total).toBe(1600);
  759. expect(applyCouponCode!.totalWithTax).toBe(1920);
  760. expect(getItemSale1Line(applyCouponCode!.lines).discounts.length).toBe(1); // 1x promotion
  761. const { removeCouponCode } = await shopClient.query<
  762. CodegenShop.RemoveCouponCodeMutation,
  763. CodegenShop.RemoveCouponCodeMutationVariables
  764. >(REMOVE_COUPON_CODE, {
  765. couponCode,
  766. });
  767. expect(getItemSale1Line(removeCouponCode!.lines).discounts.length).toBe(0);
  768. expect(removeCouponCode!.total).toBe(2200);
  769. expect(removeCouponCode!.totalWithTax).toBe(2640);
  770. const { activeOrder } = await shopClient.query<CodegenShop.GetActiveOrderQuery>(
  771. GET_ACTIVE_ORDER,
  772. );
  773. expect(getItemSale1Line(activeOrder!.lines).discounts.length).toBe(0);
  774. expect(activeOrder!.total).toBe(2200);
  775. expect(activeOrder!.totalWithTax).toBe(2640);
  776. });
  777. });
  778. describe('productsPercentageDiscount', () => {
  779. const couponCode = '50%_off_product';
  780. let promotion: Codegen.PromotionFragment;
  781. beforeAll(async () => {
  782. promotion = await createPromotion({
  783. enabled: true,
  784. name: '50% off product',
  785. couponCode,
  786. conditions: [],
  787. actions: [
  788. {
  789. code: productsPercentageDiscount.code,
  790. arguments: [
  791. { name: 'discount', value: '50' },
  792. {
  793. name: 'productVariantIds',
  794. value: `["${getVariantBySlug('item-5000').id}"]`,
  795. },
  796. ],
  797. },
  798. ],
  799. });
  800. await assignPromotionToTaxIncludedChannel(promotion.id);
  801. });
  802. afterAll(async () => {
  803. await deletePromotion(promotion.id);
  804. shopClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  805. });
  806. it('prices exclude tax', async () => {
  807. const { addItemToOrder } = await shopClient.query<
  808. CodegenShop.AddItemToOrderMutation,
  809. CodegenShop.AddItemToOrderMutationVariables
  810. >(ADD_ITEM_TO_ORDER, {
  811. productVariantId: getVariantBySlug('item-5000').id,
  812. quantity: 1,
  813. });
  814. orderResultGuard.assertSuccess(addItemToOrder);
  815. expect(addItemToOrder!.discounts.length).toBe(0);
  816. expect(addItemToOrder!.lines[0].discounts.length).toBe(0);
  817. expect(addItemToOrder!.total).toBe(5000);
  818. expect(addItemToOrder!.totalWithTax).toBe(6000);
  819. const { applyCouponCode } = await shopClient.query<
  820. CodegenShop.ApplyCouponCodeMutation,
  821. CodegenShop.ApplyCouponCodeMutationVariables
  822. >(APPLY_COUPON_CODE, {
  823. couponCode,
  824. });
  825. orderResultGuard.assertSuccess(applyCouponCode);
  826. expect(applyCouponCode!.total).toBe(2500);
  827. expect(applyCouponCode!.totalWithTax).toBe(3000);
  828. expect(applyCouponCode!.lines[0].discounts.length).toBe(1); // 1x promotion
  829. });
  830. it('prices include tax', async () => {
  831. shopClient.setChannelToken(TAX_INCLUDED_CHANNEL_TOKEN);
  832. const { addItemToOrder } = await shopClient.query<
  833. CodegenShop.AddItemToOrderMutation,
  834. CodegenShop.AddItemToOrderMutationVariables
  835. >(ADD_ITEM_TO_ORDER, {
  836. productVariantId: getVariantBySlug('item-5000').id,
  837. quantity: 1,
  838. });
  839. orderResultGuard.assertSuccess(addItemToOrder);
  840. expect(addItemToOrder!.discounts.length).toBe(0);
  841. expect(addItemToOrder!.lines[0].discounts.length).toBe(0);
  842. expect(addItemToOrder!.total).toBe(5000);
  843. expect(addItemToOrder!.totalWithTax).toBe(6000);
  844. const { applyCouponCode } = await shopClient.query<
  845. CodegenShop.ApplyCouponCodeMutation,
  846. CodegenShop.ApplyCouponCodeMutationVariables
  847. >(APPLY_COUPON_CODE, {
  848. couponCode,
  849. });
  850. orderResultGuard.assertSuccess(applyCouponCode);
  851. expect(applyCouponCode!.total).toBe(2500);
  852. expect(applyCouponCode!.totalWithTax).toBe(3000);
  853. expect(applyCouponCode!.lines[0].discounts.length).toBe(1); // 1x promotion
  854. });
  855. });
  856. describe('freeShipping', () => {
  857. const couponCode = 'FREE_SHIPPING';
  858. let promotion: Codegen.PromotionFragment;
  859. // The test shipping method needs to be created in each Channel, since ShippingMethods
  860. // are ChannelAware
  861. async function createTestShippingMethod(channelToken: string) {
  862. adminClient.setChannelToken(channelToken);
  863. const result = await adminClient.query<
  864. Codegen.CreateShippingMethodMutation,
  865. Codegen.CreateShippingMethodMutationVariables
  866. >(CREATE_SHIPPING_METHOD, {
  867. input: {
  868. code: 'test-method',
  869. fulfillmentHandler: manualFulfillmentHandler.code,
  870. checker: {
  871. code: defaultShippingEligibilityChecker.code,
  872. arguments: [
  873. {
  874. name: 'orderMinimum',
  875. value: '0',
  876. },
  877. ],
  878. },
  879. calculator: {
  880. code: defaultShippingCalculator.code,
  881. arguments: [
  882. { name: 'rate', value: '345' },
  883. { name: 'includesTax', value: 'auto' },
  884. { name: 'taxRate', value: '20' },
  885. ],
  886. },
  887. translations: [
  888. { languageCode: LanguageCode.en, name: 'test method', description: '' },
  889. ],
  890. },
  891. });
  892. adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  893. return result.createShippingMethod;
  894. }
  895. beforeAll(async () => {
  896. promotion = await createPromotion({
  897. enabled: true,
  898. name: 'Free shipping',
  899. couponCode,
  900. conditions: [],
  901. actions: [
  902. {
  903. code: freeShipping.code,
  904. arguments: [],
  905. },
  906. ],
  907. });
  908. await assignPromotionToTaxIncludedChannel(promotion.id);
  909. });
  910. afterAll(async () => {
  911. await deletePromotion(promotion.id);
  912. shopClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  913. });
  914. it('prices exclude tax', async () => {
  915. const { addItemToOrder } = await shopClient.query<
  916. CodegenShop.AddItemToOrderMutation,
  917. CodegenShop.AddItemToOrderMutationVariables
  918. >(ADD_ITEM_TO_ORDER, {
  919. productVariantId: getVariantBySlug('item-5000').id,
  920. quantity: 1,
  921. });
  922. const method = await createTestShippingMethod(E2E_DEFAULT_CHANNEL_TOKEN);
  923. const { setOrderShippingMethod } = await shopClient.query<
  924. CodegenShop.SetShippingMethodMutation,
  925. CodegenShop.SetShippingMethodMutationVariables
  926. >(SET_SHIPPING_METHOD, {
  927. id: method.id,
  928. });
  929. orderResultGuard.assertSuccess(setOrderShippingMethod);
  930. expect(setOrderShippingMethod.discounts).toEqual([]);
  931. expect(setOrderShippingMethod.shipping).toBe(345);
  932. expect(setOrderShippingMethod.shippingWithTax).toBe(414);
  933. expect(setOrderShippingMethod.total).toBe(5345);
  934. expect(setOrderShippingMethod.totalWithTax).toBe(6414);
  935. const { applyCouponCode } = await shopClient.query<
  936. CodegenShop.ApplyCouponCodeMutation,
  937. CodegenShop.ApplyCouponCodeMutationVariables
  938. >(APPLY_COUPON_CODE, {
  939. couponCode,
  940. });
  941. orderResultGuard.assertSuccess(applyCouponCode);
  942. expect(applyCouponCode.discounts.length).toBe(1);
  943. expect(applyCouponCode.discounts[0].description).toBe('Free shipping');
  944. expect(applyCouponCode.shipping).toBe(0);
  945. expect(applyCouponCode.shippingWithTax).toBe(0);
  946. expect(applyCouponCode.total).toBe(5000);
  947. expect(applyCouponCode.totalWithTax).toBe(6000);
  948. });
  949. it('prices include tax', async () => {
  950. shopClient.setChannelToken(TAX_INCLUDED_CHANNEL_TOKEN);
  951. const { addItemToOrder } = await shopClient.query<
  952. CodegenShop.AddItemToOrderMutation,
  953. CodegenShop.AddItemToOrderMutationVariables
  954. >(ADD_ITEM_TO_ORDER, {
  955. productVariantId: getVariantBySlug('item-5000').id,
  956. quantity: 1,
  957. });
  958. const method = await createTestShippingMethod(TAX_INCLUDED_CHANNEL_TOKEN);
  959. const { setOrderShippingMethod } = await shopClient.query<
  960. CodegenShop.SetShippingMethodMutation,
  961. CodegenShop.SetShippingMethodMutationVariables
  962. >(SET_SHIPPING_METHOD, {
  963. id: method.id,
  964. });
  965. orderResultGuard.assertSuccess(setOrderShippingMethod);
  966. expect(setOrderShippingMethod.discounts).toEqual([]);
  967. expect(setOrderShippingMethod.shipping).toBe(287);
  968. expect(setOrderShippingMethod.shippingWithTax).toBe(345);
  969. expect(setOrderShippingMethod.total).toBe(5287);
  970. expect(setOrderShippingMethod.totalWithTax).toBe(6345);
  971. const { applyCouponCode } = await shopClient.query<
  972. CodegenShop.ApplyCouponCodeMutation,
  973. CodegenShop.ApplyCouponCodeMutationVariables
  974. >(APPLY_COUPON_CODE, {
  975. couponCode,
  976. });
  977. orderResultGuard.assertSuccess(applyCouponCode);
  978. expect(applyCouponCode.discounts.length).toBe(1);
  979. expect(applyCouponCode.discounts[0].description).toBe('Free shipping');
  980. expect(applyCouponCode.shipping).toBe(0);
  981. expect(applyCouponCode.shippingWithTax).toBe(0);
  982. expect(applyCouponCode.total).toBe(5000);
  983. expect(applyCouponCode.totalWithTax).toBe(6000);
  984. });
  985. // https://github.com/vendure-ecommerce/vendure/pull/1150
  986. it('shipping discounts get correctly removed', async () => {
  987. shopClient.setChannelToken(TAX_INCLUDED_CHANNEL_TOKEN);
  988. const { addItemToOrder } = await shopClient.query<
  989. CodegenShop.AddItemToOrderMutation,
  990. CodegenShop.AddItemToOrderMutationVariables
  991. >(ADD_ITEM_TO_ORDER, {
  992. productVariantId: getVariantBySlug('item-5000').id,
  993. quantity: 1,
  994. });
  995. const method = await createTestShippingMethod(TAX_INCLUDED_CHANNEL_TOKEN);
  996. const { setOrderShippingMethod } = await shopClient.query<
  997. CodegenShop.SetShippingMethodMutation,
  998. CodegenShop.SetShippingMethodMutationVariables
  999. >(SET_SHIPPING_METHOD, {
  1000. id: method.id,
  1001. });
  1002. orderResultGuard.assertSuccess(setOrderShippingMethod);
  1003. expect(setOrderShippingMethod.discounts).toEqual([]);
  1004. expect(setOrderShippingMethod.shippingWithTax).toBe(345);
  1005. const { applyCouponCode } = await shopClient.query<
  1006. CodegenShop.ApplyCouponCodeMutation,
  1007. CodegenShop.ApplyCouponCodeMutationVariables
  1008. >(APPLY_COUPON_CODE, {
  1009. couponCode,
  1010. });
  1011. orderResultGuard.assertSuccess(applyCouponCode);
  1012. expect(applyCouponCode.discounts.length).toBe(1);
  1013. expect(applyCouponCode.discounts[0].description).toBe('Free shipping');
  1014. expect(applyCouponCode.shippingWithTax).toBe(0);
  1015. const { removeCouponCode } = await shopClient.query<
  1016. CodegenShop.RemoveCouponCodeMutation,
  1017. CodegenShop.RemoveCouponCodeMutationVariables
  1018. >(REMOVE_COUPON_CODE, {
  1019. couponCode,
  1020. });
  1021. orderResultGuard.assertSuccess(removeCouponCode);
  1022. expect(removeCouponCode.discounts).toEqual([]);
  1023. expect(removeCouponCode.shippingWithTax).toBe(345);
  1024. });
  1025. });
  1026. describe('multiple promotions simultaneously', () => {
  1027. const saleItem50pcOffCoupon = 'CODE1';
  1028. const order15pcOffCoupon = 'CODE2';
  1029. let promotion1: Codegen.PromotionFragment;
  1030. let promotion2: Codegen.PromotionFragment;
  1031. beforeAll(async () => {
  1032. const { facets } = await adminClient.query<Codegen.GetFacetListQuery>(GET_FACET_LIST);
  1033. const saleFacetValue = facets.items[0].values[0];
  1034. promotion1 = await createPromotion({
  1035. enabled: true,
  1036. name: 'item promo',
  1037. couponCode: saleItem50pcOffCoupon,
  1038. conditions: [],
  1039. actions: [
  1040. {
  1041. code: discountOnItemWithFacets.code,
  1042. arguments: [
  1043. { name: 'discount', value: '50' },
  1044. { name: 'facets', value: `["${saleFacetValue.id}"]` },
  1045. ],
  1046. },
  1047. ],
  1048. });
  1049. promotion2 = await createPromotion({
  1050. enabled: true,
  1051. name: 'order promo',
  1052. couponCode: order15pcOffCoupon,
  1053. conditions: [],
  1054. actions: [
  1055. {
  1056. code: orderPercentageDiscount.code,
  1057. arguments: [{ name: 'discount', value: '15' }],
  1058. },
  1059. ],
  1060. });
  1061. await assignPromotionToTaxIncludedChannel([promotion1.id, promotion2.id]);
  1062. });
  1063. afterAll(async () => {
  1064. await deletePromotion(promotion1.id);
  1065. await deletePromotion(promotion2.id);
  1066. shopClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  1067. });
  1068. it('prices exclude tax', async () => {
  1069. await shopClient.query<
  1070. CodegenShop.AddItemToOrderMutation,
  1071. CodegenShop.AddItemToOrderMutationVariables
  1072. >(ADD_ITEM_TO_ORDER, {
  1073. productVariantId: getVariantBySlug('item-sale-1000').id,
  1074. quantity: 2,
  1075. });
  1076. await shopClient.query<
  1077. CodegenShop.AddItemToOrderMutation,
  1078. CodegenShop.AddItemToOrderMutationVariables
  1079. >(ADD_ITEM_TO_ORDER, {
  1080. productVariantId: getVariantBySlug('item-5000').id,
  1081. quantity: 1,
  1082. });
  1083. // Apply the OrderItem-level promo
  1084. const { applyCouponCode: apply1 } = await shopClient.query<
  1085. CodegenShop.ApplyCouponCodeMutation,
  1086. CodegenShop.ApplyCouponCodeMutationVariables
  1087. >(APPLY_COUPON_CODE, {
  1088. couponCode: saleItem50pcOffCoupon,
  1089. });
  1090. orderResultGuard.assertSuccess(apply1);
  1091. const saleItemLine = apply1.lines.find(
  1092. l => l.productVariant.id === getVariantBySlug('item-sale-1000').id,
  1093. )!;
  1094. expect(saleItemLine.discounts.length).toBe(1); // 1x promotion
  1095. expect(
  1096. saleItemLine.discounts.find(a => a.type === AdjustmentType.PROMOTION)?.description,
  1097. ).toBe('item promo');
  1098. expect(apply1.discounts.length).toBe(1);
  1099. expect(apply1.total).toBe(6000);
  1100. expect(apply1.totalWithTax).toBe(7200);
  1101. // Apply the Order-level promo
  1102. const { applyCouponCode: apply2 } = await shopClient.query<
  1103. CodegenShop.ApplyCouponCodeMutation,
  1104. CodegenShop.ApplyCouponCodeMutationVariables
  1105. >(APPLY_COUPON_CODE, {
  1106. couponCode: order15pcOffCoupon,
  1107. });
  1108. orderResultGuard.assertSuccess(apply2);
  1109. expect(apply2.discounts.map(d => d.description).sort()).toEqual([
  1110. 'item promo',
  1111. 'order promo',
  1112. ]);
  1113. expect(apply2.total).toBe(5100);
  1114. expect(apply2.totalWithTax).toBe(6120);
  1115. });
  1116. it('prices include tax', async () => {
  1117. shopClient.setChannelToken(TAX_INCLUDED_CHANNEL_TOKEN);
  1118. await shopClient.query<
  1119. CodegenShop.AddItemToOrderMutation,
  1120. CodegenShop.AddItemToOrderMutationVariables
  1121. >(ADD_ITEM_TO_ORDER, {
  1122. productVariantId: getVariantBySlug('item-sale-1000').id,
  1123. quantity: 2,
  1124. });
  1125. await shopClient.query<
  1126. CodegenShop.AddItemToOrderMutation,
  1127. CodegenShop.AddItemToOrderMutationVariables
  1128. >(ADD_ITEM_TO_ORDER, {
  1129. productVariantId: getVariantBySlug('item-5000').id,
  1130. quantity: 1,
  1131. });
  1132. // Apply the OrderItem-level promo
  1133. const { applyCouponCode: apply1 } = await shopClient.query<
  1134. CodegenShop.ApplyCouponCodeMutation,
  1135. CodegenShop.ApplyCouponCodeMutationVariables
  1136. >(APPLY_COUPON_CODE, {
  1137. couponCode: saleItem50pcOffCoupon,
  1138. });
  1139. orderResultGuard.assertSuccess(apply1);
  1140. const saleItemLine = apply1.lines.find(
  1141. l => l.productVariant.id === getVariantBySlug('item-sale-1000').id,
  1142. )!;
  1143. expect(saleItemLine.discounts.length).toBe(1); // 1x promotion
  1144. expect(
  1145. saleItemLine.discounts.find(a => a.type === AdjustmentType.PROMOTION)?.description,
  1146. ).toBe('item promo');
  1147. expect(apply1.discounts.length).toBe(1);
  1148. expect(apply1.total).toBe(6000);
  1149. expect(apply1.totalWithTax).toBe(7200);
  1150. // Apply the Order-level promo
  1151. const { applyCouponCode: apply2 } = await shopClient.query<
  1152. CodegenShop.ApplyCouponCodeMutation,
  1153. CodegenShop.ApplyCouponCodeMutationVariables
  1154. >(APPLY_COUPON_CODE, {
  1155. couponCode: order15pcOffCoupon,
  1156. });
  1157. orderResultGuard.assertSuccess(apply2);
  1158. expect(apply2.discounts.map(d => d.description).sort()).toEqual([
  1159. 'item promo',
  1160. 'order promo',
  1161. ]);
  1162. expect(apply2.total).toBe(5100);
  1163. expect(apply2.totalWithTax).toBe(6120);
  1164. });
  1165. });
  1166. });
  1167. describe('per-customer usage limit', () => {
  1168. const TEST_COUPON_CODE = 'TESTCOUPON';
  1169. const orderGuard: ErrorResultGuard<CodegenShop.TestOrderWithPaymentsFragment> =
  1170. createErrorResultGuard(input => !!input.lines);
  1171. let promoWithUsageLimit: Codegen.PromotionFragment;
  1172. beforeAll(async () => {
  1173. promoWithUsageLimit = await createPromotion({
  1174. enabled: true,
  1175. name: 'Free with test coupon',
  1176. couponCode: TEST_COUPON_CODE,
  1177. perCustomerUsageLimit: 1,
  1178. conditions: [],
  1179. actions: [freeOrderAction],
  1180. });
  1181. });
  1182. afterAll(async () => {
  1183. await deletePromotion(promoWithUsageLimit.id);
  1184. });
  1185. async function createNewActiveOrder() {
  1186. const { addItemToOrder } = await shopClient.query<
  1187. CodegenShop.AddItemToOrderMutation,
  1188. CodegenShop.AddItemToOrderMutationVariables
  1189. >(ADD_ITEM_TO_ORDER, {
  1190. productVariantId: getVariantBySlug('item-5000').id,
  1191. quantity: 1,
  1192. });
  1193. return addItemToOrder;
  1194. }
  1195. describe('guest customer', () => {
  1196. const GUEST_EMAIL_ADDRESS = 'guest@test.com';
  1197. let orderCode: string;
  1198. function addGuestCustomerToOrder() {
  1199. return shopClient.query<
  1200. CodegenShop.SetCustomerForOrderMutation,
  1201. CodegenShop.SetCustomerForOrderMutationVariables
  1202. >(SET_CUSTOMER, {
  1203. input: {
  1204. emailAddress: GUEST_EMAIL_ADDRESS,
  1205. firstName: 'Guest',
  1206. lastName: 'Customer',
  1207. },
  1208. });
  1209. }
  1210. it('allows initial usage', async () => {
  1211. await shopClient.asAnonymousUser();
  1212. await createNewActiveOrder();
  1213. await addGuestCustomerToOrder();
  1214. const { applyCouponCode } = await shopClient.query<
  1215. CodegenShop.ApplyCouponCodeMutation,
  1216. CodegenShop.ApplyCouponCodeMutationVariables
  1217. >(APPLY_COUPON_CODE, { couponCode: TEST_COUPON_CODE });
  1218. orderResultGuard.assertSuccess(applyCouponCode);
  1219. expect(applyCouponCode!.totalWithTax).toBe(0);
  1220. expect(applyCouponCode!.couponCodes).toEqual([TEST_COUPON_CODE]);
  1221. await proceedToArrangingPayment(shopClient);
  1222. const order = await addPaymentToOrder(shopClient, testSuccessfulPaymentMethod);
  1223. orderGuard.assertSuccess(order);
  1224. expect(order.state).toBe('PaymentSettled');
  1225. expect(order.active).toBe(false);
  1226. orderCode = order.code;
  1227. });
  1228. it('adds Promotions to Order once payment arranged', async () => {
  1229. const { orderByCode } = await shopClient.query<
  1230. CodegenShop.GetOrderPromotionsByCodeQuery,
  1231. CodegenShop.GetOrderPromotionsByCodeQueryVariables
  1232. >(GET_ORDER_PROMOTIONS_BY_CODE, {
  1233. code: orderCode,
  1234. });
  1235. expect(orderByCode!.promotions.map(pick(['name']))).toEqual([
  1236. { name: 'Free with test coupon' },
  1237. ]);
  1238. });
  1239. it('returns error result when usage exceeds limit', async () => {
  1240. await shopClient.asAnonymousUser();
  1241. await createNewActiveOrder();
  1242. await addGuestCustomerToOrder();
  1243. const { applyCouponCode } = await shopClient.query<
  1244. CodegenShop.ApplyCouponCodeMutation,
  1245. CodegenShop.ApplyCouponCodeMutationVariables
  1246. >(APPLY_COUPON_CODE, { couponCode: TEST_COUPON_CODE });
  1247. orderResultGuard.assertErrorResult(applyCouponCode);
  1248. expect(applyCouponCode.message).toEqual(
  1249. 'Coupon code cannot be used more than once per customer',
  1250. );
  1251. });
  1252. it('removes couponCode from order when adding customer after code applied', async () => {
  1253. await shopClient.asAnonymousUser();
  1254. await createNewActiveOrder();
  1255. const { applyCouponCode } = await shopClient.query<
  1256. CodegenShop.ApplyCouponCodeMutation,
  1257. CodegenShop.ApplyCouponCodeMutationVariables
  1258. >(APPLY_COUPON_CODE, { couponCode: TEST_COUPON_CODE });
  1259. orderResultGuard.assertSuccess(applyCouponCode);
  1260. expect(applyCouponCode!.totalWithTax).toBe(0);
  1261. expect(applyCouponCode!.couponCodes).toEqual([TEST_COUPON_CODE]);
  1262. await addGuestCustomerToOrder();
  1263. const { activeOrder } = await shopClient.query<CodegenShop.GetActiveOrderQuery>(
  1264. GET_ACTIVE_ORDER,
  1265. );
  1266. expect(activeOrder!.couponCodes).toEqual([]);
  1267. expect(activeOrder!.totalWithTax).toBe(6000);
  1268. });
  1269. });
  1270. describe('signed-in customer', () => {
  1271. function logInAsRegisteredCustomer() {
  1272. return shopClient.asUserWithCredentials('hayden.zieme12@hotmail.com', 'test');
  1273. }
  1274. let orderId: string;
  1275. it('allows initial usage', async () => {
  1276. await logInAsRegisteredCustomer();
  1277. await createNewActiveOrder();
  1278. const { applyCouponCode } = await shopClient.query<
  1279. CodegenShop.ApplyCouponCodeMutation,
  1280. CodegenShop.ApplyCouponCodeMutationVariables
  1281. >(APPLY_COUPON_CODE, { couponCode: TEST_COUPON_CODE });
  1282. orderResultGuard.assertSuccess(applyCouponCode);
  1283. expect(applyCouponCode!.totalWithTax).toBe(0);
  1284. expect(applyCouponCode!.couponCodes).toEqual([TEST_COUPON_CODE]);
  1285. await proceedToArrangingPayment(shopClient);
  1286. const order = await addPaymentToOrder(shopClient, testSuccessfulPaymentMethod);
  1287. orderGuard.assertSuccess(order);
  1288. orderId = order.id;
  1289. expect(order.state).toBe('PaymentSettled');
  1290. expect(order.active).toBe(false);
  1291. });
  1292. it('returns error result when usage exceeds limit', async () => {
  1293. await logInAsRegisteredCustomer();
  1294. await createNewActiveOrder();
  1295. const { applyCouponCode } = await shopClient.query<
  1296. CodegenShop.ApplyCouponCodeMutation,
  1297. CodegenShop.ApplyCouponCodeMutationVariables
  1298. >(APPLY_COUPON_CODE, { couponCode: TEST_COUPON_CODE });
  1299. orderResultGuard.assertErrorResult(applyCouponCode);
  1300. expect(applyCouponCode.message).toEqual(
  1301. 'Coupon code cannot be used more than once per customer',
  1302. );
  1303. expect(applyCouponCode.errorCode).toBe(ErrorCode.COUPON_CODE_LIMIT_ERROR);
  1304. });
  1305. it('removes couponCode from order when logging in after code applied', async () => {
  1306. await shopClient.asAnonymousUser();
  1307. await createNewActiveOrder();
  1308. const { applyCouponCode } = await shopClient.query<
  1309. CodegenShop.ApplyCouponCodeMutation,
  1310. CodegenShop.ApplyCouponCodeMutationVariables
  1311. >(APPLY_COUPON_CODE, { couponCode: TEST_COUPON_CODE });
  1312. orderResultGuard.assertSuccess(applyCouponCode);
  1313. expect(applyCouponCode!.couponCodes).toEqual([TEST_COUPON_CODE]);
  1314. expect(applyCouponCode!.totalWithTax).toBe(0);
  1315. await logInAsRegisteredCustomer();
  1316. const { activeOrder } = await shopClient.query<CodegenShop.GetActiveOrderQuery>(
  1317. GET_ACTIVE_ORDER,
  1318. );
  1319. expect(activeOrder!.totalWithTax).toBe(6000);
  1320. expect(activeOrder!.couponCodes).toEqual([]);
  1321. });
  1322. // https://github.com/vendure-ecommerce/vendure/issues/1466
  1323. it('cancelled orders do not count against usage limit', async () => {
  1324. const { cancelOrder } = await adminClient.query<
  1325. Codegen.CancelOrderMutation,
  1326. Codegen.CancelOrderMutationVariables
  1327. >(CANCEL_ORDER, {
  1328. input: {
  1329. orderId,
  1330. cancelShipping: true,
  1331. reason: 'request',
  1332. },
  1333. });
  1334. orderResultGuard.assertSuccess(cancelOrder);
  1335. expect(cancelOrder.state).toBe('Cancelled');
  1336. await logInAsRegisteredCustomer();
  1337. await createNewActiveOrder();
  1338. const { applyCouponCode } = await shopClient.query<
  1339. CodegenShop.ApplyCouponCodeMutation,
  1340. CodegenShop.ApplyCouponCodeMutationVariables
  1341. >(APPLY_COUPON_CODE, { couponCode: TEST_COUPON_CODE });
  1342. orderResultGuard.assertSuccess(applyCouponCode);
  1343. expect(applyCouponCode!.totalWithTax).toBe(0);
  1344. expect(applyCouponCode!.couponCodes).toEqual([TEST_COUPON_CODE]);
  1345. });
  1346. });
  1347. });
  1348. // https://github.com/vendure-ecommerce/vendure/issues/710
  1349. it('removes order-level discount made invalid by removing OrderLine', async () => {
  1350. const promotion = await createPromotion({
  1351. enabled: true,
  1352. name: 'Test Promo',
  1353. conditions: [minOrderAmountCondition(10000)],
  1354. actions: [
  1355. {
  1356. code: orderFixedDiscount.code,
  1357. arguments: [{ name: 'discount', value: '1000' }],
  1358. },
  1359. ],
  1360. });
  1361. await shopClient.asAnonymousUser();
  1362. await shopClient.query<
  1363. CodegenShop.AddItemToOrderMutation,
  1364. CodegenShop.AddItemToOrderMutationVariables
  1365. >(ADD_ITEM_TO_ORDER, {
  1366. productVariantId: getVariantBySlug('item-1000').id,
  1367. quantity: 8,
  1368. });
  1369. const { addItemToOrder } = await shopClient.query<
  1370. CodegenShop.AddItemToOrderMutation,
  1371. CodegenShop.AddItemToOrderMutationVariables
  1372. >(ADD_ITEM_TO_ORDER, {
  1373. productVariantId: getVariantBySlug('item-5000').id,
  1374. quantity: 1,
  1375. });
  1376. orderResultGuard.assertSuccess(addItemToOrder);
  1377. expect(addItemToOrder!.discounts.length).toBe(1);
  1378. expect(addItemToOrder!.discounts[0].description).toBe('Test Promo');
  1379. const { activeOrder: check1 } = await shopClient.query<CodegenShop.GetActiveOrderQuery>(
  1380. GET_ACTIVE_ORDER,
  1381. );
  1382. expect(check1!.discounts.length).toBe(1);
  1383. expect(check1!.discounts[0].description).toBe('Test Promo');
  1384. const { removeOrderLine } = await shopClient.query<
  1385. CodegenShop.RemoveItemFromOrderMutation,
  1386. CodegenShop.RemoveItemFromOrderMutationVariables
  1387. >(REMOVE_ITEM_FROM_ORDER, {
  1388. orderLineId: addItemToOrder.lines[1].id,
  1389. });
  1390. orderResultGuard.assertSuccess(removeOrderLine);
  1391. expect(removeOrderLine.discounts.length).toBe(0);
  1392. const { activeOrder: check2 } = await shopClient.query<CodegenShop.GetActiveOrderQuery>(
  1393. GET_ACTIVE_ORDER,
  1394. );
  1395. expect(check2!.discounts.length).toBe(0);
  1396. });
  1397. // https://github.com/vendure-ecommerce/vendure/issues/1492
  1398. it('correctly handles pro-ration of variants with 0 price', async () => {
  1399. const couponCode = '20%_off_order';
  1400. const promotion = await createPromotion({
  1401. enabled: true,
  1402. name: '20% discount on order',
  1403. couponCode,
  1404. conditions: [],
  1405. actions: [
  1406. {
  1407. code: orderPercentageDiscount.code,
  1408. arguments: [{ name: 'discount', value: '20' }],
  1409. },
  1410. ],
  1411. });
  1412. await shopClient.asAnonymousUser();
  1413. await shopClient.query<
  1414. CodegenShop.AddItemToOrderMutation,
  1415. CodegenShop.AddItemToOrderMutationVariables
  1416. >(ADD_ITEM_TO_ORDER, {
  1417. productVariantId: getVariantBySlug('item-100').id,
  1418. quantity: 1,
  1419. });
  1420. await shopClient.query<
  1421. CodegenShop.AddItemToOrderMutation,
  1422. CodegenShop.AddItemToOrderMutationVariables
  1423. >(ADD_ITEM_TO_ORDER, {
  1424. productVariantId: getVariantBySlug('item-0').id,
  1425. quantity: 1,
  1426. });
  1427. const { applyCouponCode } = await shopClient.query<
  1428. CodegenShop.ApplyCouponCodeMutation,
  1429. CodegenShop.ApplyCouponCodeMutationVariables
  1430. >(APPLY_COUPON_CODE, { couponCode });
  1431. orderResultGuard.assertSuccess(applyCouponCode);
  1432. expect(applyCouponCode.totalWithTax).toBe(96);
  1433. });
  1434. async function getProducts() {
  1435. const result = await adminClient.query<Codegen.GetProductsWithVariantPricesQuery>(
  1436. GET_PRODUCTS_WITH_VARIANT_PRICES,
  1437. {
  1438. options: {
  1439. take: 10,
  1440. skip: 0,
  1441. },
  1442. },
  1443. );
  1444. products = result.products.items;
  1445. }
  1446. async function createGlobalPromotions() {
  1447. const { facets } = await adminClient.query<Codegen.GetFacetListQuery>(GET_FACET_LIST);
  1448. const saleFacetValue = facets.items[0].values[0];
  1449. await createPromotion({
  1450. enabled: true,
  1451. name: 'Promo not yet started',
  1452. startsAt: new Date(2199, 0, 0),
  1453. conditions: [minOrderAmountCondition(100)],
  1454. actions: [freeOrderAction],
  1455. });
  1456. const deletedPromotion = await createPromotion({
  1457. enabled: true,
  1458. name: 'Deleted promotion',
  1459. conditions: [minOrderAmountCondition(100)],
  1460. actions: [freeOrderAction],
  1461. });
  1462. await deletePromotion(deletedPromotion.id);
  1463. }
  1464. async function createPromotion(input: Codegen.CreatePromotionInput): Promise<Codegen.PromotionFragment> {
  1465. const result = await adminClient.query<
  1466. Codegen.CreatePromotionMutation,
  1467. Codegen.CreatePromotionMutationVariables
  1468. >(CREATE_PROMOTION, {
  1469. input,
  1470. });
  1471. return result.createPromotion as Codegen.PromotionFragment;
  1472. }
  1473. function getVariantBySlug(
  1474. slug: 'item-100' | 'item-1000' | 'item-5000' | 'item-sale-100' | 'item-sale-1000' | 'item-0',
  1475. ): Codegen.GetProductsWithVariantPricesQuery['products']['items'][number]['variants'][number] {
  1476. return products.find(p => p.slug === slug)!.variants[0];
  1477. }
  1478. async function deletePromotion(promotionId: string) {
  1479. await adminClient.query(gql`
  1480. mutation DeletePromotionAdHoc1 {
  1481. deletePromotion(id: "${promotionId}") {
  1482. result
  1483. }
  1484. }
  1485. `);
  1486. }
  1487. });