Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cant access translations in mailer over a BullJS processor #634

Open
5 tasks done
fr6nco opened this issue May 6, 2024 · 0 comments
Open
5 tasks done

Cant access translations in mailer over a BullJS processor #634

fr6nco opened this issue May 6, 2024 · 0 comments

Comments

@fr6nco
Copy link

fr6nco commented May 6, 2024

Describe the bug

I have a specific use case, where I want to send out emails based on requests from a BullJS @processor().

Such component does not have an execution Context so resolvers are not executed. How can I force certain locale using the mailerService?

  @Process()
  async send(job: Job<Order>) {
    const jobContext = this.orderService.createOrderContext(job.data);
    this.mailerService.sendMail({
      to: jobContext.body.billing_address.email,
      from: 'shop@----',
      subject: 'Order Created',
      template: './order-create',
      context: jobContext,
    });
  }

This is a very quick snippet above, the selected locale is passed down in the job data as job.data.locale. Tried to use a custom resolver, but on a processor the middleware is not executed.

Here is my module config:


@Module({
  imports: [
    CommonModule,
    ConfigModule.forRoot({
      isGlobal: true,
    }),
    I18nModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      resolvers: [new ProcessorDataResolver()],
      useFactory: (configService: ConfigService) => ({
        fallbackLanguage: 'sk',
        loaderOptions: {
          path:
            configService.get('MAILER_LOCALE_DIR') ??
            process.cwd() + '/src/locale',
          watch: true,
        },
      }),
    }),
    BullModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => ({
        redis: {
          host: configService.get('REDIS_HOST'),
          port: configService.get('REDIS_PORT'),
          password: configService.get('REDIS_PASSWORD') ?? undefined,
        },
      }),
    }),
    BullModule.registerQueue({
      name: 'mailer-order-create',
    }),
    MailerModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService, I18nService],
      useFactory: (configService: ConfigService, i18n: I18nService) => ({
        transport: {
          host: configService.get('MAILER_HOST'),
          port: +configService.get('MAILER_PORT'),
          secure: false,
          auth: {
            user: configService.get('MAILER_USER') ?? undefined,
            pass: configService.get('MAILER_PASS') ?? undefined,
          },
        },
        defaults: {
          from: configService.get('MAILER_FROM'),
        },
        preview: configService.get('MAILER_PREV') === 'true',
        template: {
          dir:
            configService.get('MAILER_TEMPLATES_DIR') ??
            process.cwd() + '/src/templates',
          adapter: new HandlebarsAdapter({ t: i18n.hbsHelper }),
          options: {
            strict: true,
          },
        },
        options: {
          partials: {
            dir:
              configService.get('MAILER_PARTIALS_DIR') ??
              process.cwd() + '/src/templates/partials',
          },
        },
      }),
    }),
    CommonModule,
  ],
  controllers: [AppController],
  providers: [OrderCreateConsumer, OrderService],
  exports: [],
})
export class AppModule {}

Any idea how can I force a language dynamically without resolvers?

Reproduction

in comment

System Info

Tomass-MacBook-Pro:paintit-mailer thomas$ npx envinfo --system --binaries --browsers
Need to install the following packages:
[email protected]
Ok to proceed? (y) 

  System:
    OS: macOS 14.0
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 22.89 MB / 16.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 20.10.0 - ~/.nvm/versions/node/v20.10.0/bin/node
    npm: 10.2.3 - ~/.nvm/versions/node/v20.10.0/bin/npm
  Browsers:
    Chrome: 124.0.6367.119
    Safari: 17.0

Used Package Manager

npm

Validations

  • Follow our Code of Conduct
  • Read the Contributing Guide.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
  • The provided reproduction is a minimal reproducible of the bug.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant