order-test-utils.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { LanguageCode } from '@vendure/common/lib/generated-types';
  2. import { Omit } from '@vendure/common/lib/omit';
  3. import { ID } from '@vendure/common/lib/shared-types';
  4. import { RequestContext } from '../api/common/request-context';
  5. import { Channel } from '../entity/channel/channel.entity';
  6. import { OrderLine } from '../entity/order-line/order-line.entity';
  7. import { Order } from '../entity/order/order.entity';
  8. import { ProductVariant } from '../entity/product-variant/product-variant.entity';
  9. import { TaxCategory } from '../entity/tax-category/tax-category.entity';
  10. import { TaxRate } from '../entity/tax-rate/tax-rate.entity';
  11. import { Zone } from '../entity/zone/zone.entity';
  12. export type SimpleLine = { productVariantId: ID; quantity: number; lineId: ID; customFields?: any };
  13. export function createOrderFromLines(simpleLines: SimpleLine[]): Order {
  14. const lines = simpleLines.map(
  15. ({ productVariantId, quantity, lineId, customFields }) =>
  16. new OrderLine({
  17. id: lineId,
  18. productVariant: new ProductVariant({ id: productVariantId }),
  19. quantity,
  20. ...(customFields ? { customFields } : {}),
  21. }),
  22. );
  23. return new Order({
  24. lines,
  25. });
  26. }
  27. export function createRequestContext(options: { pricesIncludeTax: boolean }): RequestContext {
  28. const channel = new Channel({
  29. defaultTaxZone: zoneDefault,
  30. pricesIncludeTax: options.pricesIncludeTax,
  31. });
  32. const ctx = new RequestContext({
  33. apiType: 'admin',
  34. channel,
  35. authorizedAsOwnerOnly: false,
  36. languageCode: LanguageCode.en,
  37. isAuthorized: true,
  38. session: {} as any,
  39. });
  40. return ctx;
  41. }
  42. export const taxCategoryStandard = new TaxCategory({
  43. id: 'taxCategoryStandard',
  44. name: 'Standard Tax',
  45. });
  46. export const taxCategoryReduced = new TaxCategory({
  47. id: 'taxCategoryReduced',
  48. name: 'Reduced Tax',
  49. });
  50. export const taxCategoryZero = new TaxCategory({
  51. id: 'taxCategoryZero',
  52. name: 'Zero Tax',
  53. });
  54. export const zoneDefault = new Zone({
  55. id: 'zoneDefault',
  56. name: 'Default Zone',
  57. });
  58. export const zoneOther = new Zone({
  59. id: 'zoneOther',
  60. name: 'Other Zone',
  61. });
  62. export const zoneWithNoTaxRate = new Zone({
  63. id: 'zoneWithNoTaxRate',
  64. name: 'Zone for which no TaxRate is configured',
  65. });
  66. export const taxRateDefaultStandard = new TaxRate({
  67. id: 'taxRateDefaultStandard',
  68. name: 'Default Standard',
  69. value: 20,
  70. enabled: true,
  71. zone: zoneDefault,
  72. category: taxCategoryStandard,
  73. });
  74. export const taxRateDefaultReduced = new TaxRate({
  75. id: 'taxRateDefaultReduced',
  76. name: 'Default Reduced',
  77. value: 10,
  78. enabled: true,
  79. zone: zoneDefault,
  80. category: taxCategoryReduced,
  81. });
  82. export const taxRateDefaultZero = new TaxRate({
  83. id: 'taxRateDefaultZero',
  84. name: 'Default Zero Tax',
  85. value: 0,
  86. enabled: true,
  87. zone: zoneDefault,
  88. category: taxCategoryZero,
  89. });
  90. export const taxRateOtherStandard = new TaxRate({
  91. id: 'taxRateOtherStandard',
  92. name: 'Other Standard',
  93. value: 15,
  94. enabled: true,
  95. zone: zoneOther,
  96. category: taxCategoryStandard,
  97. });
  98. export const taxRateOtherReduced = new TaxRate({
  99. id: 'taxRateOtherReduced',
  100. name: 'Other Reduced',
  101. value: 5,
  102. enabled: true,
  103. zone: zoneOther,
  104. category: taxCategoryReduced,
  105. });
  106. export class MockTaxRateService {
  107. private activeTaxRates = [
  108. taxRateDefaultStandard,
  109. taxRateDefaultReduced,
  110. taxRateDefaultZero,
  111. taxRateOtherStandard,
  112. taxRateOtherReduced,
  113. ];
  114. initTaxRates() {
  115. /* noop */
  116. }
  117. async getApplicableTaxRate(ctx: RequestContext, zone: Zone, taxCategory: TaxCategory): Promise<TaxRate> {
  118. const rate = this.activeTaxRates.find(r => r.test(zone, taxCategory));
  119. return rate || taxRateDefaultStandard;
  120. }
  121. }
  122. export function createOrder(
  123. orderConfig: Partial<Omit<Order, 'lines'>> & {
  124. ctx: RequestContext;
  125. lines: Array<{
  126. listPrice: number;
  127. taxCategory: TaxCategory;
  128. quantity: number;
  129. }>;
  130. },
  131. ): Order {
  132. const lines = orderConfig.lines.map(
  133. ({ listPrice, taxCategory, quantity }) =>
  134. new OrderLine({
  135. taxCategory,
  136. quantity,
  137. orderPlacedQuantity: 0,
  138. listPrice,
  139. listPriceIncludesTax: orderConfig.ctx.channel.pricesIncludeTax,
  140. taxLines: [],
  141. adjustments: [],
  142. }),
  143. );
  144. return new Order({
  145. couponCodes: [],
  146. lines,
  147. shippingLines: [],
  148. surcharges: [],
  149. modifications: [],
  150. });
  151. }