import { Inject, Injectable, Logger, OnModuleInit } from "@nestjs/common";
import PgBoss from "pg-boss";
import { PG_BOSS } from "./boss.provider";

/** Phase 4 will swap this log for Postmark/SES. The queue is already
 * created and drained so transactional enqueue has somewhere to land. */
@Injectable()
export class EmailProcessor implements OnModuleInit {
  private readonly logger = new Logger(EmailProcessor.name);

  constructor(@Inject(PG_BOSS) private readonly boss: PgBoss) {}

  async onModuleInit() {
    await this.boss.work("email", async (jobs) => {
      for (const job of jobs) {
        this.logger.log(`email job ${job.id}: ${JSON.stringify(job.data)}`);
      }
    });
  }
}
