File

src/project/project.controller.ts

Prefix

project

Index

Methods

Methods

Async create
create(createProjectDto: CreateProjectDto, req)
Decorators :
@Post()
@ApiCreatedResponse({type: Project})
@UsePipes(new ValidationPipe())
Parameters :
Name Type Optional
createProjectDto CreateProjectDto No
req No
Returns : Promise<string>
Async fetchProjectServiceUser
fetchProjectServiceUser(id: number)
Decorators :
@Get('/:id/serviceUser')
Parameters :
Name Type Optional
id number No
Returns : unknown
Async findAll
findAll(request, query: literal type)
Decorators :
@Get()
@ApiOkResponse({type: Project, isArray: true})
@UsePipes(new ValidationPipe())
Parameters :
Name Type Optional
request No
query literal type No
Returns : Promise<Project[]>
Async findOne
findOne(id: string, request)
Decorators :
@Get(':id')
@ApiOkResponse({type: Project})
@UsePipes(new ValidationPipe())
Parameters :
Name Type Optional
id string No
request No
Returns : Promise<Project>
Async getKpis
getKpis()
Decorators :
@Get('kpis')
Returns : unknown
Async getProjectCredentials
getProjectCredentials(id: number, request)
Decorators :
@Get('/:id/credentials')
Parameters :
Name Type Optional
id number No
request No
Returns : unknown
Async getProjectPermission
getProjectPermission(id: string, request)
Decorators :
@Get(':id/permissions')
Parameters :
Name Type Optional
id string No
request No
Returns : unknown
Async remove
remove(id: string, request)
Decorators :
@Delete(':id')
@ApiOkResponse({type: Project})
@UsePipes(new ValidationPipe())
Parameters :
Name Type Optional
id string No
request No
Returns : Promise<Project>
Async update
update(id: string, updateProjectDto: UpdateProjectDto, request)
Decorators :
@Patch(':id')
@ApiOkResponse({type: Project})
@UsePipes(new ValidationPipe())
Parameters :
Name Type Optional
id string No
updateProjectDto UpdateProjectDto No
request No
Returns : Promise<Project>
import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  ValidationPipe,
  UsePipes,
  Request,
  InternalServerErrorException,
  UnauthorizedException,
  Query,
} from "@nestjs/common";
import { ProjectService } from "./project.service";
import { CreateProjectDto } from "./dto/create-project.dto";
import { Project } from "./entities/project.entity";
import {
  ApiBearerAuth,
  ApiCreatedResponse,
  ApiOkResponse,
} from "@nestjs/swagger";
import { UpdateProjectDto } from "./dto/update-project.dto";
import { PolicyService } from "../iam";
import {
  PROJECT_RESOURCE_NAME,
  PROJECT_READ_ACTION,
  PROJECT_UPDATE_ACTION,
  PROJECT_DELETE_ACTION,
  PROJECT_CREATE_KEY_ACTION,
} from "./project.constants";

@ApiBearerAuth("access-token")
@Controller("project")
export class ProjectController {
  constructor(
    private readonly projectService: ProjectService,
    private readonly policyService: PolicyService,
  ) {}

  @Post()
  @ApiCreatedResponse({ type: Project })
  @UsePipes(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true }))
  async create(
    @Body() createProjectDto: CreateProjectDto,
    @Request() req,
  ): Promise<string> {
    // anyone can request project create
    return this.projectService.requestProjectCreate(
      createProjectDto,
      req.user.id,
    );
  }

  @Get()
  @ApiOkResponse({ type: Project, isArray: true })
  @UsePipes(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true }))
  async findAll(
    @Request() request,
    @Query() query: { metrics?: boolean },
  ): Promise<Project[]> {
    const projects = await this.projectService.findAll(query.metrics?.toString() == "true");
    const accessRequested =
      await this.projectService.getUserPendingAccessProjectIdList(
        request.user.id,
      );
    return projects.map((project) => {
      const isProjectUser = this.policyService.checkPermission(
        request.user.permission,
        project.id,
        PROJECT_RESOURCE_NAME,
        PROJECT_READ_ACTION,
      );

      project.isProjectUser = isProjectUser;
      if (accessRequested[project.id]) {
        project.hasPendingAccessRequest = true;
      } else {
        project.hasPendingAccessRequest = false;
      }

      return project;
    });
  }

  @Get("kpis")
  async getKpis() {
    return { feature: "BETA" };
  }

  @Get(":id")
  @ApiOkResponse({ type: Project })
  @UsePipes(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true }))
  async findOne(@Param("id") id: string, @Request() request): Promise<Project> {
    if (
      !(await this.policyService.checkPermission(
        request.user.permission,
        id,
        PROJECT_RESOURCE_NAME,
        PROJECT_READ_ACTION,
      ))
    ) {
      throw new UnauthorizedException();
    }
    return this.projectService.findOne(+id);
  }

  @Patch(":id")
  @ApiOkResponse({ type: Project })
  @UsePipes(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true }))
  async update(
    @Param("id") id: string,
    @Body() updateProjectDto: UpdateProjectDto,
    @Request() request,
  ): Promise<Project> {
    if (
      !(await this.policyService.checkPermission(
        request.user.permission,
        id,
        PROJECT_RESOURCE_NAME,
        PROJECT_UPDATE_ACTION,
      ))
    ) {
      throw new UnauthorizedException();
    }
    return this.projectService.update(+id, updateProjectDto);
  }

  @Delete(":id")
  @ApiOkResponse({ type: Project })
  @UsePipes(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true }))
  async remove(@Param("id") id: string, @Request() request): Promise<Project> {
    if (
      !(await this.policyService.checkPermission(
        request.user.permission,
        id,
        PROJECT_RESOURCE_NAME,
        PROJECT_DELETE_ACTION,
      ))
    ) {
      throw new UnauthorizedException();
    }
    return this.projectService.remove(+id);
  }

  @Get("/:id/credentials")
  async getProjectCredentials(@Param("id") id: number, @Request() request) {
    if (
      !(await this.policyService.checkPermission(
        request.user.permission,
        id,
        PROJECT_RESOURCE_NAME,
        PROJECT_CREATE_KEY_ACTION,
      ))
    ) {
      throw new UnauthorizedException();
    }

    try {
      return this.projectService.getProjectCredentials(+id);
    } catch (error) {
      throw new InternalServerErrorException(error.message);
    }
  }

  @Get("/:id/serviceUser")
  async fetchProjectServiceUser(@Param("id") id: number) {
    const project = await this.projectService.findOrThrow(+id);
    return await this.projectService.fetchProjectServiceUser(project);
  }

  @Get(":id/permissions")
  async getProjectPermission(@Param("id") id: string, @Request() request) {
    const [canUpdate, canDelete] = await Promise.all([
      this.policyService.checkPermission(
        request.user.permission,
        id,
        PROJECT_RESOURCE_NAME,
        PROJECT_UPDATE_ACTION,
      ),
      this.policyService.checkPermission(
        request.user.permission,
        id,
        PROJECT_RESOURCE_NAME,
        PROJECT_DELETE_ACTION,
      ),
    ]);
    return { update: canUpdate, delete: canDelete };
  }
}

results matching ""

    No results matching ""