✨ Cron that deletes codes expired for more than 30 seconds
This commit is contained in:
parent
5a51fc63d0
commit
a3e88cdbd2
@ -67,4 +67,11 @@ export default class TotpCodesRepository extends BaseRepository {
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete many totp codes
|
||||
*/
|
||||
public async deleteMany(query: Prisma.TotpCodesDeleteManyArgs) {
|
||||
return this.model.deleteMany({ ...query });
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import IdNotService from "../IdNotService/IdNotService";
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
import NotificationBuilder from "@Common/notifications/NotificationBuilder";
|
||||
import EmailBuilder from "@Common/emails/EmailBuilder";
|
||||
import TotpService from "@Services/customer/TotpService/TotpService";
|
||||
// import { PrismaClient } from "@prisma/client";
|
||||
|
||||
@Service()
|
||||
@ -16,6 +17,7 @@ export default class CronService {
|
||||
private idNotService: IdNotService,
|
||||
private notificationBuilder: NotificationBuilder,
|
||||
private emailBuilder: EmailBuilder,
|
||||
private totpService: TotpService,
|
||||
) {}
|
||||
|
||||
public async sendMails() {
|
||||
@ -86,8 +88,8 @@ export default class CronService {
|
||||
// Once a day at midnight
|
||||
try {
|
||||
const prisma = new PrismaClient();
|
||||
const expiringDocuments15Days: [{uid: string, expiration_date: Date}] = await prisma.$queryRaw
|
||||
`SELECT distinct o.uid, f.created_at as expiration_date
|
||||
const expiringDocuments15Days: [{ uid: string; expiration_date: Date }] =
|
||||
await prisma.$queryRaw`SELECT distinct o.uid, f.created_at as expiration_date
|
||||
FROM documents d
|
||||
JOIN files f ON d.uid=f.document_uid
|
||||
JOIN office_folders o ON o.uid=d.folder_uid
|
||||
@ -95,11 +97,24 @@ export default class CronService {
|
||||
AND d.document_status = 'DEPOSITED'
|
||||
AND current_date = (DATE(f.created_at) + interval '3 months' - interval '2 days');`;
|
||||
|
||||
expiringDocuments15Days.forEach(expiringFolder => {
|
||||
expiringDocuments15Days.forEach((expiringFolder) => {
|
||||
this.notificationBuilder.sendDocumentExpiringSoonNotification(expiringFolder.uid, expiringFolder.expiration_date);
|
||||
});
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
});
|
||||
// Start job
|
||||
if (!cronJob.running) {
|
||||
cronJob.start();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async cleanExpiredTotpCodes() {
|
||||
const cronJob = new CronJob("0 0 * * *", async () => {
|
||||
// Once a day at midnight
|
||||
try {
|
||||
await this.totpService.cleanExpiredTotpCodes();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
|
51
src/services/customer/TotpService/TotpService.ts
Normal file
51
src/services/customer/TotpService/TotpService.ts
Normal file
@ -0,0 +1,51 @@
|
||||
import { Prisma, TotpCodes } from "@prisma/client";
|
||||
import TotpCodesRepository from "@Repositories/TotpCodesRepository";
|
||||
import BaseService from "@Services/BaseService";
|
||||
import { Service } from "typedi";
|
||||
|
||||
@Service()
|
||||
export default class TotpService extends BaseService {
|
||||
constructor(private totpCodesRepository: TotpCodesRepository) {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* @description : Get all totp codes
|
||||
* @throws {Error} If totp codes cannot be get
|
||||
*/
|
||||
public async get(query: Prisma.TotpCodesFindManyArgs): Promise<TotpCodes[]> {
|
||||
return this.totpCodesRepository.findMany(query);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description : Get one totp code
|
||||
* @throws {Error} If totp code cannot be get
|
||||
*/
|
||||
public async getOne(query: Prisma.TotpCodesFindFirstArgs): Promise<TotpCodes | null> {
|
||||
return this.totpCodesRepository.findOne(query);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description : Delete many totp codes
|
||||
* @throws {Error} If totp code cannot be deleted
|
||||
*/
|
||||
public async deleteMany(query: Prisma.TotpCodesDeleteManyArgs): Promise<Prisma.BatchPayload> {
|
||||
return this.totpCodesRepository.deleteMany(query);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description : Delete every totp code expired for more than 30 days
|
||||
* @throws {Error} If totp codes cannot be deleted
|
||||
*/
|
||||
public async cleanExpiredTotpCodes() {
|
||||
const query: Prisma.TotpCodesDeleteManyArgs = {
|
||||
where: {
|
||||
expire_at: {
|
||||
lte: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000),
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
return this.totpCodesRepository.deleteMany(query);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user