test-shipping-eligibility-checkers.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { LanguageCode } from '@vendure/common/lib/generated-types';
  2. import { EntityHydrator, ShippingEligibilityChecker } from '@vendure/core';
  3. export const countryCodeShippingEligibilityChecker = new ShippingEligibilityChecker({
  4. code: 'country-code-shipping-eligibility-checker',
  5. description: [{ languageCode: LanguageCode.en, value: 'Country Shipping Eligibility Checker' }],
  6. args: {
  7. countryCode: {
  8. type: 'string',
  9. },
  10. },
  11. check: (ctx, order, args) => {
  12. return order.shippingAddress?.countryCode === args.countryCode;
  13. },
  14. });
  15. let entityHydrator: EntityHydrator;
  16. /**
  17. * @description
  18. * This checker does nothing except for hydrating the Order in order to test
  19. * an edge-case which would cause inconsistencies when modifying the Order in which
  20. * the OrderService would e.g. remove an OrderLine, but then this step would re-add it
  21. * because the removal had not yet been persisted by the time the `applyPriceAdjustments()`
  22. * step was run (during which this checker will run).
  23. *
  24. * See https://github.com/vendurehq/vendure/issues/2548
  25. */
  26. export const hydratingShippingEligibilityChecker = new ShippingEligibilityChecker({
  27. code: 'hydrating-shipping-eligibility-checker',
  28. description: [{ languageCode: LanguageCode.en, value: 'Hydrating Shipping Eligibility Checker' }],
  29. args: {},
  30. init(injector) {
  31. entityHydrator = injector.get(EntityHydrator);
  32. },
  33. check: async (ctx, order) => {
  34. await entityHydrator.hydrate(ctx, order, { relations: ['lines.sellerChannel'] });
  35. return true;
  36. },
  37. });