/** * @author wuweiru * @date 2020/10/12 */ import { Injectable } from '@nestjs/common'; import { Model } from 'mongoose'; import { InjectModel } from '@nestjs/mongoose'; import { MailCode } from '../model/mail'; import { BaseService } from '../../../base/base.service'; import { MongoDBCollection } from '../../../constant'; import { ServerError } from '../../../common/error/server.error'; @Injectable() export class MailService extends BaseService { constructor( @InjectModel(MongoDBCollection.MailCode) protected readonly model: Model ) { super(model); } async getMailCodeValidationResult( email: string, code: string ): Promise { const mailModel = await this.model.findOne({ email }); if (mailModel) { if ( mailModel.invalidAt && new Date().getTime() > mailModel.invalidAt.getTime() ) { throw ServerError.code.EmailCodeExpired; } else { return mailModel.code === code; } } else { throw ServerError.code.EmailCodeExpired; } } }