multivendor.plugin.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { OnApplicationBootstrap } from '@nestjs/common';
  2. import {
  3. Channel,
  4. ChannelService,
  5. configureDefaultOrderProcess,
  6. LanguageCode,
  7. PaymentMethod,
  8. PaymentMethodService,
  9. PluginCommonModule,
  10. RequestContextService,
  11. TransactionalConnection,
  12. VendurePlugin,
  13. } from '@vendure/core';
  14. import { multivendorOrderProcess } from './config/mv-order-process';
  15. import { MultivendorSellerStrategy } from './config/mv-order-seller-strategy';
  16. import { multivendorPaymentMethodHandler } from './config/mv-payment-handler';
  17. import { MultivendorShippingLineAssignmentStrategy } from './config/mv-shipping-line-assignment-strategy';
  18. import { CONNECTED_PAYMENT_METHOD_CODE } from './constants';
  19. @VendurePlugin({
  20. imports: [PluginCommonModule],
  21. configuration: config => {
  22. config.customFields.Seller.push({
  23. name: 'connectedAccountId',
  24. label: [{ languageCode: LanguageCode.en, value: 'Connected account ID' }],
  25. description: [
  26. { languageCode: LanguageCode.en, value: 'The ID used to process connected payments' },
  27. ],
  28. type: 'string',
  29. public: false,
  30. });
  31. config.paymentOptions.paymentMethodHandlers.push(multivendorPaymentMethodHandler);
  32. const customDefaultOrderProcess = configureDefaultOrderProcess({
  33. checkFulfillmentStates: false,
  34. });
  35. config.orderOptions.process = [customDefaultOrderProcess, multivendorOrderProcess];
  36. config.orderOptions.orderSellerStrategy = new MultivendorSellerStrategy();
  37. config.shippingOptions.shippingLineAssignmentStrategy =
  38. new MultivendorShippingLineAssignmentStrategy();
  39. return config;
  40. },
  41. })
  42. export class MultivendorPlugin implements OnApplicationBootstrap {
  43. constructor(
  44. private connection: TransactionalConnection,
  45. private channelService: ChannelService,
  46. private requestContextService: RequestContextService,
  47. private paymentMethodService: PaymentMethodService,
  48. ) {}
  49. async onApplicationBootstrap() {
  50. await this.ensureConnectedPaymentMethodExists();
  51. }
  52. private async ensureConnectedPaymentMethodExists() {
  53. const paymentMethod = await this.connection.rawConnection.getRepository(PaymentMethod).findOne({
  54. where: {
  55. code: CONNECTED_PAYMENT_METHOD_CODE,
  56. },
  57. });
  58. if (!paymentMethod) {
  59. const ctx = await this.requestContextService.create({ apiType: 'admin' });
  60. const allChannels = await this.connection.getRepository(ctx, Channel).find();
  61. const createdPaymentMethod = await this.paymentMethodService.create(ctx, {
  62. code: CONNECTED_PAYMENT_METHOD_CODE,
  63. name: 'Connected Payments',
  64. enabled: true,
  65. handler: {
  66. code: multivendorPaymentMethodHandler.code,
  67. arguments: [],
  68. },
  69. });
  70. await this.channelService.assignToChannels(
  71. ctx,
  72. PaymentMethod,
  73. createdPaymentMethod.id,
  74. allChannels.map(c => c.id),
  75. );
  76. }
  77. }
  78. }