multivendor.plugin.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { OnApplicationBootstrap } from '@nestjs/common';
  2. import {
  3. Channel,
  4. ChannelService,
  5. LanguageCode,
  6. PaymentMethod,
  7. PaymentMethodService,
  8. PluginCommonModule,
  9. RequestContextService,
  10. TransactionalConnection,
  11. VendurePlugin,
  12. } from '@vendure/core';
  13. import { multivendorFulfillmentProcess } from './config/mv-fulfillment-process';
  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. config.shippingOptions.customFulfillmentProcess.push(multivendorFulfillmentProcess);
  33. config.orderOptions.process.push(multivendorOrderProcess);
  34. config.orderOptions.orderSellerStrategy = new MultivendorSellerStrategy();
  35. config.shippingOptions.shippingLineAssignmentStrategy =
  36. new MultivendorShippingLineAssignmentStrategy();
  37. return config;
  38. },
  39. })
  40. export class MultivendorPlugin implements OnApplicationBootstrap {
  41. constructor(
  42. private connection: TransactionalConnection,
  43. private channelService: ChannelService,
  44. private requestContextService: RequestContextService,
  45. private paymentMethodService: PaymentMethodService,
  46. ) {}
  47. async onApplicationBootstrap() {
  48. await this.ensureConnectedPaymentMethodExists();
  49. }
  50. private async ensureConnectedPaymentMethodExists() {
  51. const paymentMethod = await this.connection.rawConnection.getRepository(PaymentMethod).findOne({
  52. where: {
  53. code: CONNECTED_PAYMENT_METHOD_CODE,
  54. },
  55. });
  56. if (!paymentMethod) {
  57. const ctx = await this.requestContextService.create({ apiType: 'admin' });
  58. const allChannels = await this.connection.getRepository(ctx, Channel).find();
  59. const createdPaymentMethod = await this.paymentMethodService.create(ctx, {
  60. code: CONNECTED_PAYMENT_METHOD_CODE,
  61. name: 'Connected Payments',
  62. enabled: true,
  63. handler: {
  64. code: multivendorPaymentMethodHandler.code,
  65. arguments: [],
  66. },
  67. });
  68. await this.channelService.assignToChannels(
  69. ctx,
  70. PaymentMethod,
  71. createdPaymentMethod.id,
  72. allChannels.map(c => c.id),
  73. );
  74. }
  75. }
  76. }