mv-shipping-eligibility-checker.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. import { LanguageCode } from '@vendure/common/lib/generated-types';
  2. import { DEFAULT_CHANNEL_CODE } from '@vendure/common/lib/shared-constants';
  3. import { EntityHydrator, idsAreEqual, ShippingEligibilityChecker } from '@vendure/core';
  4. let entityHydrator: EntityHydrator;
  5. /**
  6. * @description
  7. * Shipping method is eligible if at least one OrderLine is associated with the Seller's Channel.
  8. */
  9. export const multivendorShippingEligibilityChecker = new ShippingEligibilityChecker({
  10. code: 'multivendor-shipping-eligibility-checker',
  11. description: [{ languageCode: LanguageCode.en, value: 'Multivendor Shipping Eligibility Checker' }],
  12. args: {},
  13. init(injector) {
  14. entityHydrator = injector.get(EntityHydrator);
  15. },
  16. check: async (ctx, order, args, method) => {
  17. await entityHydrator.hydrate(ctx, method, { relations: ['channels'] });
  18. await entityHydrator.hydrate(ctx, order, { relations: ['lines.sellerChannel'] });
  19. const sellerChannel = method.channels.find(c => c.code !== DEFAULT_CHANNEL_CODE);
  20. if (!sellerChannel) {
  21. return false;
  22. }
  23. for (const line of order.lines) {
  24. if (line.sellerChannelId && idsAreEqual(line.sellerChannelId, sellerChannel.id)) {
  25. return true;
  26. }
  27. }
  28. return false;
  29. },
  30. });