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

100% Statements 51/51
37.5% Branches 3/8
100% Functions 2/2
100% Lines 51/51

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 521x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 1x  
import { Controller, ForbiddenException, Get, Param, Query, UseGuards } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
 
import { Company } from '@amalia/core/models';
import { assert } from '@amalia/ext/typescript';
import { DatePipe } from '@amalia/kernel/api';
import {
  AmaliaAuthGuard,
  CurrentAuthenticatedContext,
  CurrentUserCompany,
  PoliciesGuard,
} from '@amalia/kernel/auth/core';
import { canViewThisUserProfile, defineAbilityFor } from '@amalia/kernel/auth/shared';
import { type AuthenticatedContext } from '@amalia/kernel/auth/types';
import { TeamAssignmentsRepository } from '@amalia/tenants/assignments/teams/core';
import { TeamRole } from '@amalia/tenants/assignments/teams/types';
 
@UseGuards(AmaliaAuthGuard, PoliciesGuard)
@Controller('users/:userId/team-assignments')
@ApiTags('assignments')
export class UserTeamAssignmentsController {
  public constructor(private readonly teamAssignmentsRepository: TeamAssignmentsRepository) {}
 
  @Get()
  public async getUserTeamAssignments(
    @CurrentUserCompany() company: Company,
    @CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
    @Param('userId') userId: string,
    @Query('teamRole') teamRole?: TeamRole,
    @Query('effectiveBetween.start', new DatePipe({ optional: true })) start?: Date,
    @Query('effectiveBetween.end', new DatePipe({ optional: true })) end?: Date,
  ) {
    const ability = defineAbilityFor(authenticatedContext);
 
    // For now, limit the access to the team assignments to the user's profile.
    assert(
      canViewThisUserProfile(ability, { id: userId }),
      new ForbiddenException({
        message: `User has no access to Team Assignments`,
        userId,
      }),
    );
 
    const effectiveBetween = { start, end };
    return this.teamAssignmentsRepository.findTeamAssignments(
      company.id,
      { userId, ...(teamRole && { teamRole }) },
      effectiveBetween,
    );
  }
}