mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2026-07-25 01:15:26 +10:00
refactor(v4.0.0-alpha): beginning of a new era
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user