All files / db/services mail-event.service.ts

90% Statements 18/20
50% Branches 1/2
100% Functions 5/5
89.47% Lines 17/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 585x 5x     5x     5x 5x       5x       3x 3x 3x       3x             3x       3x 1x 1x   2x             1x       1x                  
import { InjectModel } from '@m8a/nestjs-typegoose'
import { Injectable, Logger } from '@nestjs/common'
import { ReturnModelType } from '@typegoose/typegoose'
import { ObjectId } from 'mongoose'
import { MailEvent } from '../entities/mail-event.entity'
 
@Injectable()
export class MailEventDBService {
  private readonly logger = new Logger(MailEventDBService.name)
 
  constructor(
    @InjectModel(MailEvent)
    private readonly mailEventModel: ReturnModelType<typeof MailEvent>,
  ) {}
 
  async createEvent(event: Partial<MailEvent>): Promise<MailEvent> {
    this.logger.log('Creating mail event')
    const newEvent = await this.mailEventModel.create(event)
    return newEvent
  }
 
  async getOpenEventsBefore(endDate: Date): Promise<MailEvent[]> {
    const events = this.mailEventModel
      .find({
        scheduledAt: {
          $lte: endDate,
        },
      })
      .lean()
    return events
  }
 
  async markMailsAsSend(ids: ObjectId[]): Promise<void> {
    if (ids.length == 0) {
      this.logger.log('Empty update will not be executed')
      return
    }
    await this.mailEventModel.updateMany(
      { _id: { $in: ids } },
      { hasBeenSent: true },
    )
  }
 
  async rescheduleMails(ids: ObjectId[], newDate: Date): Promise<void> {
    Iif (ids.length == 0) {
      this.logger.log('Empty update will not be executed')
      return
    }
    await this.mailEventModel.updateMany(
      { _id: { $in: ids } },
      {
        scheduledAt: newDate,
        hasBeenRescheduled: true,
      },
    )
  }
}