email-processor.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { Inject, Injectable } from '@nestjs/common';
  2. import { InternalServerError, Logger } from '@vendure/core';
  3. import fs from 'fs-extra';
  4. import { deserializeAttachments } from './attachment-utils';
  5. import { isDevModeOptions } from './common';
  6. import { EMAIL_PLUGIN_OPTIONS, loggerCtx } from './constants';
  7. import { HandlebarsMjmlGenerator } from './handlebars-mjml-generator';
  8. import { NodemailerEmailSender } from './nodemailer-email-sender';
  9. import { TemplateLoader } from './template-loader';
  10. import {
  11. EmailGenerator,
  12. EmailPluginOptions,
  13. EmailSender,
  14. EmailTransportOptions,
  15. IntermediateEmailDetails,
  16. } from './types';
  17. /**
  18. * This class combines the template loading, generation, and email sending - the actual "work" of
  19. * the EmailPlugin. It is arranged this way primarily to accommodate easier testing, so that the
  20. * tests can be run without needing all the JobQueue stuff which would require a full e2e test.
  21. */
  22. @Injectable()
  23. export class EmailProcessor {
  24. protected templateLoader: TemplateLoader;
  25. protected emailSender: EmailSender;
  26. protected generator: EmailGenerator;
  27. protected transport: EmailTransportOptions;
  28. constructor(@Inject(EMAIL_PLUGIN_OPTIONS) protected options: EmailPluginOptions) {}
  29. async init() {
  30. this.templateLoader = new TemplateLoader(this.options.templatePath);
  31. this.emailSender = this.options.emailSender ? this.options.emailSender : new NodemailerEmailSender();
  32. this.generator = this.options.emailGenerator
  33. ? this.options.emailGenerator
  34. : new HandlebarsMjmlGenerator();
  35. if (this.generator.onInit) {
  36. await this.generator.onInit.call(this.generator, this.options);
  37. }
  38. if (isDevModeOptions(this.options)) {
  39. this.transport = {
  40. type: 'file',
  41. raw: false,
  42. outputPath: this.options.outputPath,
  43. };
  44. } else {
  45. if (!this.options.transport) {
  46. throw new InternalServerError(
  47. `When devMode is not set to true, the 'transport' property must be set.`,
  48. );
  49. }
  50. this.transport = this.options.transport;
  51. }
  52. if (this.transport.type === 'file') {
  53. // ensure the configured directory exists before
  54. // we attempt to write files to it
  55. const emailPath = this.transport.outputPath;
  56. await fs.ensureDir(emailPath);
  57. }
  58. }
  59. async process(data: IntermediateEmailDetails) {
  60. try {
  61. const bodySource = await this.templateLoader.loadTemplate(data.type, data.templateFile);
  62. const generated = await this.generator.generate(
  63. data.from,
  64. data.subject,
  65. bodySource,
  66. data.templateVars,
  67. );
  68. const emailDetails = {
  69. ...generated,
  70. recipient: data.recipient,
  71. attachments: deserializeAttachments(data.attachments),
  72. };
  73. await this.emailSender.send(emailDetails, this.transport);
  74. return true;
  75. } catch (err: unknown) {
  76. if (err instanceof Error) {
  77. Logger.error(err.message, loggerCtx, err.stack);
  78. } else {
  79. Logger.error(String(err), loggerCtx);
  80. }
  81. return false;
  82. }
  83. }
  84. }