All files / libs/reporting/dashboards-v2/api/src/lib/controllers dashboards-v2.controller.ts

51.83% Statements 99/191
5.88% Branches 1/17
7.69% Functions 1/13
51.83% Lines 99/191

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 1921x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x         1x 1x 1x 1x         1x 1x 1x 1x         1x 1x 1x 1x                 1x 1x 1x 1x                 1x 1x 1x 1x                 1x 1x 1x 1x                 1x 1x 1x 1x                     1x 1x 1x 1x                     1x 1x 1x                 1x 1x 1x 1x                     1x 1x 1x 1x                     1x  
import { Body, Controller, Delete, Get, Param, Patch, Post, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
 
import { Dashboard, User } from '@amalia/core/models';
import { FeatureFlagGuard, UuidPipe } from '@amalia/kernel/api';
import { AmaliaAuthGuard, CheckPolicies, CurrentAuthenticatedContext, PoliciesGuard } from '@amalia/kernel/auth/core';
import {
  canCreateDashboardsV2,
  canDeleteDashboardsV2,
  canModifyDashboardsV2,
  canShareDashboardsV2,
  canViewDashboardsV2,
} from '@amalia/kernel/auth/shared';
import { type AuthenticatedContext } from '@amalia/kernel/auth/types';
import {
  CreateDashboardUseCase,
  DeleteDashboardUseCase,
  DeleteDashboardUserSharedWithUseCase,
  DuplicateDashboardUseCase,
  GetDashboardAsHomeUseCase,
  GetDashboardShareCandidatesUseCase,
  GetDashboardsUseCase,
  GetDashboardUseCase,
  PatchDashboardUseCase,
  RemoveDashboardAsHomeUseCase,
  SetDashboardAsHomeUseCase,
  UpdateDashboardSharedWithUseCase,
} from '@amalia/reporting/dashboards-v2/core';
 
import { CreateDashboardDto, PatchDashboardDto, PostDashboardSharedUsersDto } from '../dtos';
import { DuplicateDashboardDto } from '../dtos/duplicate-dashboard.dto';
 
@ApiTags('dashboards')
@ApiBearerAuth()
@UseGuards(AmaliaAuthGuard, PoliciesGuard, FeatureFlagGuard)
@Controller('dashboards/v2')
export class DashboardsV2Controller {
  public constructor(
    private readonly createDashboardUseCase: CreateDashboardUseCase,
    private readonly deleteDashboardUseCase: DeleteDashboardUseCase,
    private readonly deleteDashboardUserSharedWithUseCase: DeleteDashboardUserSharedWithUseCase,
    private readonly duplicateDashboardUseCase: DuplicateDashboardUseCase,
    private readonly getDashboardAsHomeUseCase: GetDashboardAsHomeUseCase,
    private readonly getDashboardShareCandidatesUseCase: GetDashboardShareCandidatesUseCase,
    private readonly getDashboardUseCase: GetDashboardUseCase,
    private readonly getDashboardsUseCase: GetDashboardsUseCase,
    private readonly patchDashboardUseCase: PatchDashboardUseCase,
    private readonly removeDashboardAsHomeUseCase: RemoveDashboardAsHomeUseCase,
    private readonly setDashboardAsHomeUseCase: SetDashboardAsHomeUseCase,
    private readonly updateDashboardSharedWithUseCase: UpdateDashboardSharedWithUseCase,
  ) {}
 
  @Get()
  @CheckPolicies((ability) => canViewDashboardsV2(ability))
  public async findDashboards(@CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext) {
    return this.getDashboardsUseCase.execute({
      authenticatedContext,
    });
  }
 
  @Get('/home-dashboard')
  @CheckPolicies((ability) => canViewDashboardsV2(ability))
  public async getDashboardAsHome(@CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext) {
    return this.getDashboardAsHomeUseCase.execute({
      authenticatedContext,
    });
  }
 
  @Delete('/home-dashboard')
  @CheckPolicies((ability) => canViewDashboardsV2(ability))
  public async deleteDashboardAsHome(@CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext) {
    return this.removeDashboardAsHomeUseCase.execute({
      authenticatedContext,
    });
  }
 
  @Post()
  @CheckPolicies((ability) => canCreateDashboardsV2(ability))
  public async createDashboard(
    @CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
    @Body() createDashboardRequest: CreateDashboardDto,
  ) {
    return this.createDashboardUseCase.execute({
      authenticatedContext,
      createDashboardRequest,
    });
  }
 
  @Get('/:dashboardId/share-candidates')
  @CheckPolicies((ability) => canShareDashboardsV2(ability))
  public async getDashboardShareCandidates(
    @CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
    @Param('dashboardId', new UuidPipe()) dashboardId: Dashboard['id'],
  ) {
    return this.getDashboardShareCandidatesUseCase.execute({
      authenticatedContext,
      dashboardId,
    });
  }
 
  @Get('/:dashboardId')
  @CheckPolicies((ability) => canViewDashboardsV2(ability))
  public async getDashboard(
    @CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
    @Param('dashboardId', new UuidPipe()) dashboardId: Dashboard['id'],
  ) {
    return this.getDashboardUseCase.execute({
      authenticatedContext,
      dashboardId,
    });
  }
 
  @Delete('/:dashboardId')
  @CheckPolicies((ability) => canDeleteDashboardsV2(ability))
  public async deleteDashboard(
    @CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
    @Param('dashboardId', new UuidPipe()) dashboardId: Dashboard['id'],
  ) {
    return this.deleteDashboardUseCase.execute({
      authenticatedContext,
      dashboardId,
    });
  }
 
  @Delete('/:dashboardId/shared-users/:userToRemoveId')
  @CheckPolicies((ability) => canShareDashboardsV2(ability))
  public async removeUserSharedPermission(
    @CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
    @Param('dashboardId', new UuidPipe()) dashboardId: Dashboard['id'],
    @Param('userToRemoveId', new UuidPipe()) userToRemoveId: User['id'],
  ) {
    return this.deleteDashboardUserSharedWithUseCase.execute({
      authenticatedContext,
      dashboardId,
      userToRemoveId,
    });
  }
 
  @Post('/:dashboardId/shared-users')
  @CheckPolicies((ability) => canShareDashboardsV2(ability))
  public async patchDashboardSharedWith(
    @CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
    @Param('dashboardId', new UuidPipe()) dashboardId: Dashboard['id'],
    @Body() postDashboardSharedUsersRequest: PostDashboardSharedUsersDto,
  ) {
    return this.updateDashboardSharedWithUseCase.execute({
      authenticatedContext,
      dashboardId,
      ...postDashboardSharedUsersRequest,
    });
  }
 
  @Post('/:dashboardId/set-as-home')
  public async setDashboardsAsHome(
    @CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
    @Param('dashboardId', new UuidPipe()) dashboardId: Dashboard['id'],
  ) {
    return this.setDashboardAsHomeUseCase.execute({
      authenticatedContext,
      dashboardId,
    });
  }
 
  @Patch('/:dashboardId')
  @CheckPolicies((ability) => canModifyDashboardsV2(ability))
  public async patchDashboard(
    @CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
    @Param('dashboardId', new UuidPipe()) dashboardId: Dashboard['id'],
    @Body() patchDashboardRequest: PatchDashboardDto,
  ) {
    return this.patchDashboardUseCase.execute({
      authenticatedContext,
      dashboardId,
      patchDashboardRequest,
    });
  }
 
  @Post('/:dashboardId/duplicate')
  @CheckPolicies((ability) => canCreateDashboardsV2(ability))
  public async duplicateDashboard(
    @CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
    @Param('dashboardId', new UuidPipe()) dashboardId: Dashboard['id'],
    @Body() duplicateDashboardRequest: DuplicateDashboardDto,
  ) {
    return this.duplicateDashboardUseCase.execute({
      authenticatedContext,
      dashboardId,
      duplicateDashboardRequest,
    });
  }
}