| import { CanActivate, ExecutionContext, Injectable, NotFoundException } from "@nestjs/common"; |
| import { UserWithSecrets } from "@reactive-resume/dto"; |
| import { ErrorMessage } from "@reactive-resume/utils"; |
| import { Request } from "express"; |
|
|
| 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, |
| ); |
|
|
| |
| if (resume.visibility === "public") { |
| request.payload = { resume }; |
| } |
|
|
| |
| |
| if (resume.visibility === "private") { |
| if (user && user.id === resume.userId) { |
| request.payload = { resume }; |
| } else { |
| throw new NotFoundException(ErrorMessage.ResumeNotFound); |
| } |
| } |
|
|
| return true; |
| } catch { |
| throw new NotFoundException(ErrorMessage.ResumeNotFound); |
| } |
| } |
| } |
|
|