All files / libs/payout-definition/designer/core/src/lib/relationships relationships.controller.ts

100% Statements 78/78
52.17% Branches 12/23
100% Functions 5/5
100% Lines 78/78

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 791x 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 10x 10x 10x 10x 10x 1x 1x 1x 1x 6x 6x 6x 1x 1x 1x 1x 8x 8x 8x 8x 2x 2x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 1x 1x  
import { Body, Controller, Delete, Get, Param, Patch, Post, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
 
import { Company, type Relationship } from '@amalia/core/models';
import { UuidPipe } from '@amalia/kernel/api';
import {
  AmaliaAuthGuard,
  CheckPolicies,
  CurrentAuthenticatedContext,
  CurrentUserCompany,
  PoliciesGuard,
} from '@amalia/kernel/auth/core';
import { canModifyData, canViewData } from '@amalia/kernel/auth/shared';
import { type AuthenticatedContext } from '@amalia/kernel/auth/types';
 
import { CreateRelationshipDto } from './dto/CreateRelationship.dto';
import { formatRelationshipResponse } from './dto/formatRelationshipResponse';
import { PatchRelationshipDto } from './dto/PatchRelationship.dto';
import { RelationshipsService } from './relationships.service';
import { CreateRelationshipUseCase } from './use-cases/create-relationship.use-case';
import { DeleteRelationshipUseCase } from './use-cases/delete-relationship.use-case';
import { UpdateRelationshipUseCase } from './use-cases/update-relationship.use-case';
 
@UseGuards(AmaliaAuthGuard, PoliciesGuard)
@ApiBearerAuth()
@ApiTags('relationships')
@Controller('relationships')
export class RelationshipsController {
  public constructor(
    private readonly relationshipService: RelationshipsService,
    private readonly createRelationshipUseCase: CreateRelationshipUseCase,
    private readonly updateRelationshipUseCase: UpdateRelationshipUseCase,
    private readonly deleteRelationshipUseCase: DeleteRelationshipUseCase,
  ) {}
 
  @Get()
  @CheckPolicies((ability) => canViewData(ability))
  public async findAll(@CurrentUserCompany() company: Company): Promise<CreateRelationshipDto[]> {
    const relationships = await this.relationshipService.findAll(company);
    return relationships.map((r) => formatRelationshipResponse(r));
  }
 
  @Post()
  @CheckPolicies((ability) => canModifyData(ability))
  public async create(
    @Body() relationship: CreateRelationshipDto,
    @CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
  ): Promise<CreateRelationshipDto> {
    const createdRelationship = await this.createRelationshipUseCase.execute({ authenticatedContext, relationship });
    return formatRelationshipResponse(createdRelationship);
  }
 
  @Patch(':relationshipId')
  @CheckPolicies((ability) => canModifyData(ability))
  public async update(
    @CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
    @Param('relationshipId', new UuidPipe()) relationshipId: string,
    @Body() relationship: PatchRelationshipDto,
  ): Promise<Relationship> {
    return this.updateRelationshipUseCase.execute({
      authenticatedContext,
      relationshipUpdateDto: relationship,
      id: relationshipId,
    });
  }
 
  @Delete(':relationshipId')
  @CheckPolicies((ability) => canModifyData(ability))
  public async delete(
    @Param('relationshipId', new UuidPipe()) relationshipId: string,
    @CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
  ): Promise<void> {
    await this.deleteRelationshipUseCase.execute({
      authenticatedContext,
      relationshipId,
    });
  }
}