All files / libs/tenants/assignments/teams/api/src/lib/controllers team-assignment.controller.ts

91.72% Statements 133/145
46.87% Branches 15/32
83.33% Functions 5/6
91.72% Lines 133/145

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 1461x 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 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 2x 2x 5x 5x 5x 5x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x                         1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x  
import { Controller, Delete, ForbiddenException, Get, Inject, Patch, Post, Query, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
 
import { Company, type TeamAssignment } from '@amalia/core/models';
import { Relations, UuidPipe } from '@amalia/kernel/api';
import {
  AmaliaAuthGuard,
  CheckPolicies,
  CurrentAuthenticatedContext,
  CurrentUserCompany,
  PoliciesGuard,
  ZodBodyValidationPipe,
} from '@amalia/kernel/auth/core';
import {
  canModifyTeamAssignments,
  canViewTeamAssignments,
  canViewThisTeamAssignments,
  defineAbilityFor,
} from '@amalia/kernel/auth/shared';
import { type AuthenticatedContext } from '@amalia/kernel/auth/types';
import {
  CreateTeamAssignmentsUseCase,
  DeleteTeamAssignmentsUseCase,
  TeamAssignmentsBulkImportUseCase,
  TeamAssignmentService,
  UpdateTeamAssignmentsUseCase,
} from '@amalia/tenants/assignments/teams/core';
import {
  TeamRole,
  type BulkImportTeamAssignmentsRequest,
  type CreateTeamAssignmentsRequest,
  type DeleteTeamAssignmentsRequest,
  type TeamAssignmentsValidateResponse,
  type UpdateTeamAssignmentsRequest,
} from '@amalia/tenants/assignments/teams/types';
 
import { bulkImportTeamAssignmentsRequestSchema } from '../dto/bulk-import-team-assignments.dto';
import { createTeamAssignmentsRequestSchema } from '../dto/create-team-assignments.dto';
import { deleteTeamAssignmentsRequestSchema } from '../dto/delete-team-assignments.dto';
import { updateTeamAssignmentsRequestSchema } from '../dto/update-team-assignments.dto';
 
@UseGuards(AmaliaAuthGuard, PoliciesGuard)
@ApiBearerAuth()
@ApiTags('team_assignments')
@Controller('team_assignments')
export class TeamAssignmentController {
  public constructor(
    @Inject(TeamAssignmentService)
    private readonly teamAssignmentService: TeamAssignmentService,
    private readonly teamAssignmentsBulkImportUseCase: TeamAssignmentsBulkImportUseCase,
    private readonly createTeamAssignmentsUseCase: CreateTeamAssignmentsUseCase,
    private readonly deleteTeamAssignmentsUseCase: DeleteTeamAssignmentsUseCase,
    private readonly updateTeamAssignmentsUseCase: UpdateTeamAssignmentsUseCase,
  ) {}
 
  @Get()
  @CheckPolicies((ability) => canViewTeamAssignments(ability))
  public async findAllByParameters(
    @CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
    @Relations('company,user,team') relations: string[],
    @CurrentUserCompany() company: Company,
    @Query('teamId', new UuidPipe({ multiple: true, optional: true })) teamIds: string[] = [],
    @Query('userId', new UuidPipe({ optional: true })) userId: string | null = null,
    @Query('teamRole') teamRole?: TeamRole,
  ): Promise<TeamAssignment[]> {
    const ability = defineAbilityFor(authenticatedContext);
 
    const teamWithNoAccess = teamIds.filter((teamId) => !canViewThisTeamAssignments(ability, { id: teamId }));
 
    if (teamWithNoAccess.length !== 0 && teamWithNoAccess.length === teamIds.length) {
      throw new ForbiddenException("You don't have access to this team");
    }
 
    return this.teamAssignmentService.findAllByParameters(
      company,
      teamIds.filter((teamId) => !teamWithNoAccess.includes(teamId)),
      userId ? [userId] : null,
      teamRole,
      undefined,
      relations,
      authenticatedContext,
    );
  }
 
  @Post('/bulk-import')
  @CheckPolicies((ability) => canModifyTeamAssignments(ability))
  public async bulkImport(
    @ZodBodyValidationPipe({ schema: bulkImportTeamAssignmentsRequestSchema })
    teamAssignmentsRequest: BulkImportTeamAssignmentsRequest,
    @CurrentUserCompany() company: Company,
    @CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
  ): Promise<TeamAssignmentsValidateResponse> {
    return this.teamAssignmentsBulkImportUseCase.execute({
      authenticatedContext,
      companyId: company.id,
      teamAssignmentsRowsToImport: teamAssignmentsRequest.rowsToImport,
      dryRun: teamAssignmentsRequest.dryRun,
    });
  }
 
  @Post()
  @CheckPolicies((ability) => canModifyTeamAssignments(ability))
  public async create(
    @CurrentUserCompany() company: Company,
    @CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
    @ZodBodyValidationPipe({ schema: createTeamAssignmentsRequestSchema })
    createTeamAssignmentsRequest: CreateTeamAssignmentsRequest,
  ): Promise<TeamAssignment[]> {
    return this.createTeamAssignmentsUseCase.execute({
      company,
      authenticatedContext,
      createTeamAssignmentsRequest,
    });
  }
 
  @Patch()
  @CheckPolicies((ability) => canModifyTeamAssignments(ability))
  public async update(
    @CurrentUserCompany() company: Company,
    @CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
    @ZodBodyValidationPipe({ schema: updateTeamAssignmentsRequestSchema })
    updateTeamAssignmentsRequest: UpdateTeamAssignmentsRequest,
  ): Promise<TeamAssignment[]> {
    return this.updateTeamAssignmentsUseCase.execute({
      company,
      authenticatedContext,
      updateTeamAssignmentsRequest,
    });
  }
 
  @Delete()
  @CheckPolicies((ability) => canModifyTeamAssignments(ability))
  public async delete(
    @CurrentUserCompany() company: Company,
    @CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
    @ZodBodyValidationPipe({ schema: deleteTeamAssignmentsRequestSchema })
    deleteTeamAssignmentsRequest: DeleteTeamAssignmentsRequest,
  ): Promise<void> {
    await this.deleteTeamAssignmentsUseCase.execute({
      company,
      authenticatedContext,
      deleteTeamAssignmentsRequest,
    });
  }
}