update fix spam mail

This commit is contained in:
Admin 2025-09-25 07:55:43 +07:00
parent 20ada47452
commit 07e3a9fcfa
1 changed files with 31 additions and 4 deletions

View File

@ -1,12 +1,13 @@
import { Log } from '@/entities/log.entity';
import { BadRequestException, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Repository, MoreThan } from 'typeorm';
import { SendLogDto } from './dtos/send-log.dto';
import { ZulipService } from './zulip.service';
import AppResponse from '@/system/filters/response/app-response';
import { SystemLang } from '@/system/lang/system.lang';
import { MailerService } from '../mailer/mailer.service';
@Injectable()
export class LogsService {
constructor(
@ -17,19 +18,46 @@ export class LogsService {
) {}
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({
message: data.message,
type: data.type,
});
if (!result)
if (!result) {
throw new BadRequestException(
AppResponse.toResponse(null, {
message: SystemLang.getText('messages', 'error'),
}),
);
}
// 2. Build nội dung email
// 3. Gửi mail
const subject = `[${result.type.toUpperCase()}] - New Log Message`;
const content = `
<h3>Log Notification</h3>
@ -38,7 +66,6 @@ export class LogsService {
<p><b>Created At:</b> ${new Date().toLocaleString()}</p>
`;
// 3. Gửi email
await this.malerService.sendMail(subject, content);
return AppResponse.toResponse(result);