All files / libs/payout-calculation/payments/core/src/lib/paymentLock paymentLock.controller.ts

90% Statements 54/60
44.44% Branches 8/18
100% Functions 4/4
90% Lines 54/60

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 611x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x     5x 5x 5x 5x 5x 1x 1x 1x 1x 5x 5x 5x 5x     5x 5x 5x 1x  
import { Controller, Delete, ForbiddenException, Get, Inject, Param, Put, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
 
import { Company as CompanyEntity, type PaymentLock } from '@amalia/core/models';
import { type PaymentLock as PaymentLockInterface } from '@amalia/core/types';
import { UuidPipe } from '@amalia/kernel/api';
import { AmaliaAuthGuard, CheckPolicies, CurrentUserCompany, PoliciesGuard } from '@amalia/kernel/auth/core';
import { canModifyPaymentLocks, canViewPaymentLocks } from '@amalia/kernel/auth/shared';
 
import { formatPaymentLockResponse } from './paymentLock.dto';
import { PaymentLockService } from './paymentLock.service';
 
@UseGuards(AmaliaAuthGuard, PoliciesGuard)
@ApiBearerAuth()
@ApiTags('payments')
@Controller('payments')
export class PaymentLockController {
  public constructor(
    @Inject(PaymentLockService)
    private readonly paymentLockService: PaymentLockService,
  ) {}
 
  @Get('locks')
  @CheckPolicies((ability) => canViewPaymentLocks(ability))
  public async listLocks(@CurrentUserCompany() company: CompanyEntity): Promise<PaymentLockInterface[]> {
    if (!company.featureFlags.PAYMENTS_LOCK) {
      throw new ForbiddenException('Payments lock not activated on your account');
    }
 
    return (await this.paymentLockService.getPaymentLocks(company)).map((pl) => formatPaymentLockResponse(pl));
  }
 
  @Put('locks/:periodId')
  @CheckPolicies((ability) => canModifyPaymentLocks(ability))
  public async lockContext(
    @CurrentUserCompany() company: CompanyEntity,
    @Param('periodId', new UuidPipe()) periodId: string,
  ): Promise<PaymentLockInterface> {
    if (!company.featureFlags.PAYMENTS_LOCK) {
      throw new ForbiddenException('Payments lock not activated on your account');
    }
 
    return formatPaymentLockResponse(
      await this.paymentLockService.lockContext(company, { period: { id: periodId } } as PaymentLock),
    );
  }
 
  @Delete('locks/:periodId')
  @CheckPolicies((ability) => canModifyPaymentLocks(ability))
  public async unlockContext(
    @CurrentUserCompany() company: CompanyEntity,
    @Param('periodId', new UuidPipe()) periodId: string,
  ): Promise<void> {
    if (!company.featureFlags.PAYMENTS_LOCK) {
      throw new ForbiddenException('Payments lock not activated on your account');
    }
 
    await this.paymentLockService.unlockContext(company, { period: { id: periodId } } as PaymentLock);
  }
}