refactor(v4.0.0-alpha): beginning of a new era

This commit is contained in:
Amruth Pillai
2023-11-05 12:31:42 +01:00
parent 0ba6a444e2
commit 22933bd412
505 changed files with 81829 additions and 0 deletions
@@ -0,0 +1,43 @@
import { CanActivate, ExecutionContext, Injectable, NotFoundException } from "@nestjs/common";
import { UserWithSecrets } from "@reactive-resume/dto";
import { Request } from "express";
import { ErrorMessage } from "@/server/constants/error-message";
import { ResumeService } from "../resume.service";
@Injectable()
export class ResumeGuard implements CanActivate {
constructor(private readonly resumeService: ResumeService) {}
async canActivate(context: ExecutionContext): Promise<boolean> {
const request = context.switchToHttp().getRequest<Request>();
const user = request.user as UserWithSecrets | false;
try {
const resume = await this.resumeService.findOne(
request.params.id,
user ? user.id : undefined,
);
// First check if the resume is public, if yes, attach the resume to the request payload.
if (resume.visibility === "public") {
request.payload = { resume };
}
// If the resume is private and the user is authenticated and is the owner of the resume, attach the resume to the request payload.
// Else, if either the user is not authenticated or is not the owner of the resume, throw a 404 error.
if (resume.visibility === "private") {
if (user && user && user.id === resume.userId) {
request.payload = { resume };
} else {
throw new NotFoundException(ErrorMessage.ResumeNotFound);
}
}
return true;
} catch (error) {
throw new NotFoundException(ErrorMessage.ResumeNotFound);
}
}
}