Files
swiss-datashare/backend/src/jobs/jobs.service.ts

45 lines
1.3 KiB
TypeScript
Raw Normal View History

import { Injectable } from "@nestjs/common";
import { Cron } from "@nestjs/schedule";
import { FileService } from "src/file/file.service";
import { PrismaService } from "src/prisma/prisma.service";
2022-10-12 16:59:04 -04:00
import * as moment from "moment";
@Injectable()
export class JobsService {
2022-10-16 00:14:02 +02:00
constructor(
private prisma: PrismaService,
private fileService: FileService
) {}
2022-10-16 00:14:02 +02:00
@Cron("0 * * * *")
async deleteExpiredShares() {
const expiredShares = await this.prisma.share.findMany({
where: {
// We want to remove only shares that have an expiration date less than the current date, but not 0
AND: [
{ expiration: { lt: new Date() } },
{ expiration: { not: moment(0).toDate() } },
],
},
});
2022-10-16 00:14:02 +02:00
for (const expiredShare of expiredShares) {
await this.prisma.share.delete({
where: { id: expiredShare.id },
});
2022-10-16 00:14:02 +02:00
await this.fileService.deleteAllFiles(expiredShare.id);
2022-10-12 16:59:04 -04:00
}
2022-10-16 00:14:02 +02:00
console.log(`job: deleted ${expiredShares.length} expired shares`);
}
@Cron("0 * * * *")
async deleteExpiredRefreshTokens() {
const expiredShares = await this.prisma.refreshToken.deleteMany({
where: { expiresAt: { lt: new Date() } },
});
console.log(`job: deleted ${expiredShares.count} expired refresh tokens`);
}
}