order-test-utils.ts 5.1 KB

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