update fix spam mail
This commit is contained in:
parent
20ada47452
commit
07e3a9fcfa
|
|
@ -1,12 +1,13 @@
|
||||||
import { Log } from '@/entities/log.entity';
|
import { Log } from '@/entities/log.entity';
|
||||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { Repository } from 'typeorm';
|
import { Repository, MoreThan } from 'typeorm';
|
||||||
import { SendLogDto } from './dtos/send-log.dto';
|
import { SendLogDto } from './dtos/send-log.dto';
|
||||||
import { ZulipService } from './zulip.service';
|
import { ZulipService } from './zulip.service';
|
||||||
import AppResponse from '@/system/filters/response/app-response';
|
import AppResponse from '@/system/filters/response/app-response';
|
||||||
import { SystemLang } from '@/system/lang/system.lang';
|
import { SystemLang } from '@/system/lang/system.lang';
|
||||||
import { MailerService } from '../mailer/mailer.service';
|
import { MailerService } from '../mailer/mailer.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class LogsService {
|
export class LogsService {
|
||||||
constructor(
|
constructor(
|
||||||
|
|
@ -17,19 +18,46 @@ export class LogsService {
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async saveLog(data: SendLogDto) {
|
async saveLog(data: SendLogDto) {
|
||||||
|
const thirtyMinutesAgo = new Date(Date.now() - 30 * 60 * 1000);
|
||||||
|
|
||||||
|
// 1. Check nếu đã có log trùng trong 30 phút
|
||||||
|
const existingLog = await this.repo
|
||||||
|
.createQueryBuilder('log')
|
||||||
|
.where('log.message = :message', { message: data.message })
|
||||||
|
.andWhere('log.type = :type', { type: data.type })
|
||||||
|
.andWhere('log.created_at > :time', {
|
||||||
|
time: thirtyMinutesAgo.toISOString(),
|
||||||
|
}) // convert sang ISO
|
||||||
|
.getOne();
|
||||||
|
|
||||||
|
// Nếu đã tồn tại thì chỉ lưu log mà không gửi mail
|
||||||
|
if (existingLog) {
|
||||||
|
// const result = await this.repo.save({
|
||||||
|
// message: data.message,
|
||||||
|
// type: data.type,
|
||||||
|
// });
|
||||||
|
// return AppResponse.toResponse(result);
|
||||||
|
|
||||||
|
return AppResponse.toResponse(existingLog, {
|
||||||
|
message: SystemLang.getText('messages', 'error'),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Lưu log mới
|
||||||
const result = await this.repo.save({
|
const result = await this.repo.save({
|
||||||
message: data.message,
|
message: data.message,
|
||||||
type: data.type,
|
type: data.type,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!result)
|
if (!result) {
|
||||||
throw new BadRequestException(
|
throw new BadRequestException(
|
||||||
AppResponse.toResponse(null, {
|
AppResponse.toResponse(null, {
|
||||||
message: SystemLang.getText('messages', 'error'),
|
message: SystemLang.getText('messages', 'error'),
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// 2. Build nội dung email
|
// 3. Gửi mail
|
||||||
const subject = `[${result.type.toUpperCase()}] - New Log Message`;
|
const subject = `[${result.type.toUpperCase()}] - New Log Message`;
|
||||||
const content = `
|
const content = `
|
||||||
<h3>Log Notification</h3>
|
<h3>Log Notification</h3>
|
||||||
|
|
@ -38,7 +66,6 @@ export class LogsService {
|
||||||
<p><b>Created At:</b> ${new Date().toLocaleString()}</p>
|
<p><b>Created At:</b> ${new Date().toLocaleString()}</p>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
// 3. Gửi email
|
|
||||||
await this.malerService.sendMail(subject, content);
|
await this.malerService.sendMail(subject, content);
|
||||||
|
|
||||||
return AppResponse.toResponse(result);
|
return AppResponse.toResponse(result);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue