mirror of
https://github.com/stonith404/pingvin-share.git
synced 2025-01-29 01:28:59 -05:00
refactor(jobs): clear expired tokens and reverse shares
This commit is contained in:
parent
86a7379519
commit
e3f88d0826
2 changed files with 39 additions and 7 deletions
|
@ -1,9 +1,10 @@
|
|||
import { Module } from "@nestjs/common";
|
||||
import { FileModule } from "src/file/file.module";
|
||||
import { ReverseShareModule } from "src/reverseShare/reverseShare.module";
|
||||
import { JobsService } from "./jobs.service";
|
||||
|
||||
@Module({
|
||||
imports: [FileModule],
|
||||
imports: [FileModule, ReverseShareModule],
|
||||
providers: [JobsService],
|
||||
})
|
||||
export class JobsModule {}
|
||||
|
|
|
@ -4,11 +4,13 @@ import * as fs from "fs";
|
|||
import * as moment from "moment";
|
||||
import { FileService } from "src/file/file.service";
|
||||
import { PrismaService } from "src/prisma/prisma.service";
|
||||
import { ReverseShareService } from "src/reverseShare/reverseShare.service";
|
||||
|
||||
@Injectable()
|
||||
export class JobsService {
|
||||
constructor(
|
||||
private prisma: PrismaService,
|
||||
private reverseShareService: ReverseShareService,
|
||||
private fileService: FileService
|
||||
) {}
|
||||
|
||||
|
@ -36,6 +38,24 @@ export class JobsService {
|
|||
console.log(`job: deleted ${expiredShares.length} expired shares`);
|
||||
}
|
||||
|
||||
@Cron("0 * * * *")
|
||||
async deleteExpiredReverseShares() {
|
||||
const expiredReverseShares = await this.prisma.reverseShare.findMany({
|
||||
where: {
|
||||
shareExpiration: { lt: new Date() },
|
||||
},
|
||||
});
|
||||
|
||||
for (const expiredReverseShare of expiredReverseShares) {
|
||||
await this.reverseShareService.remove(expiredReverseShare.id);
|
||||
}
|
||||
|
||||
if (expiredReverseShares.length > 0)
|
||||
console.log(
|
||||
`job: deleted ${expiredReverseShares.length} expired reverse shares`
|
||||
);
|
||||
}
|
||||
|
||||
@Cron("0 0 * * *")
|
||||
deleteTemporaryFiles() {
|
||||
let filesDeleted = 0;
|
||||
|
@ -69,14 +89,25 @@ export class JobsService {
|
|||
}
|
||||
|
||||
@Cron("0 * * * *")
|
||||
async deleteExpiredRefreshTokens() {
|
||||
const expiredRefreshTokens = await this.prisma.refreshToken.deleteMany({
|
||||
async deleteExpiredTokens() {
|
||||
const { count: refreshTokenCount } =
|
||||
await this.prisma.refreshToken.deleteMany({
|
||||
where: { expiresAt: { lt: new Date() } },
|
||||
});
|
||||
|
||||
const { count: loginTokenCount } = await this.prisma.loginToken.deleteMany({
|
||||
where: { expiresAt: { lt: new Date() } },
|
||||
});
|
||||
|
||||
if (expiredRefreshTokens.count > 0)
|
||||
console.log(
|
||||
`job: deleted ${expiredRefreshTokens.count} expired refresh tokens`
|
||||
);
|
||||
const { count: resetPasswordTokenCount } =
|
||||
await this.prisma.resetPasswordToken.deleteMany({
|
||||
where: { expiresAt: { lt: new Date() } },
|
||||
});
|
||||
|
||||
const deletedTokensCount =
|
||||
refreshTokenCount + loginTokenCount + resetPasswordTokenCount;
|
||||
|
||||
if (deletedTokensCount > 0)
|
||||
console.log(`job: deleted ${deletedTokensCount} expired refresh tokens`);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue