Skip to content

Commit

Permalink
feature: Added "never" expiration date
Browse files Browse the repository at this point in the history
  • Loading branch information
stautonico committed Oct 12, 2022
1 parent 69ee88a commit 56349c6
Show file tree
Hide file tree
Showing 9 changed files with 412 additions and 396 deletions.
53 changes: 29 additions & 24 deletions backend/src/auth/jobs/jobs.service.ts
Expand Up @@ -2,36 +2,41 @@ import { Injectable } from "@nestjs/common";
import { Cron } from "@nestjs/schedule";
import { FileService } from "src/file/file.service";
import { PrismaService } from "src/prisma/prisma.service";
import * as moment from "moment";

@Injectable()
export class JobsService {
constructor(
private prisma: PrismaService,
private fileService: FileService
) {}
constructor(
private prisma: PrismaService,
private fileService: FileService
) {
}

@Cron("0 * * * *")
async deleteExpiredShares() {
const expiredShares = await this.prisma.share.findMany({
where: { expiration: { lt: new Date() } },
});
@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()}}]
},
});

for (const expiredShare of expiredShares) {
await this.prisma.share.delete({
where: { id: expiredShare.id },
});
for (const expiredShare of expiredShares) {
await this.prisma.share.delete({
where: {id: expiredShare.id},
});

await this.fileService.deleteAllFiles(expiredShare.id);
}
await this.fileService.deleteAllFiles(expiredShare.id);
}

console.log(`job: deleted ${expiredShares.length} expired shares`);
}
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`);
}
@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`);
}
}
2 changes: 1 addition & 1 deletion backend/src/share/guard/shareSecurity.guard.ts
Expand Up @@ -33,7 +33,7 @@ export class ShareSecurityGuard implements CanActivate {
include: { security: true },
});

if (!share || moment().isAfter(share.expiration))
if (!share || (moment().isAfter(share.expiration) && moment(share.expiration).unix() !== 0))
throw new NotFoundException("Share not found");

if (!share.security) return true;
Expand Down

0 comments on commit 56349c6

Please sign in to comment.