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

97.18% Statements 69/71
43.75% Branches 7/16
100% Functions 4/4
97.18% Lines 69/71

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 721x 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 5x 5x 5x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x     4x 4x 4x 1x 1x 1x 1x 4x 4x 4x 4x 4x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 1x  
import { Body, Controller, Delete, ForbiddenException, Get, Inject, Param, Post, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
 
import { Company } from '@amalia/core/models';
import { type PaymentContract } from '@amalia/core/types';
import { UuidPipe } from '@amalia/kernel/api';
import {
  AmaliaAuthGuard,
  CheckPolicies,
  CurrentAuthenticatedContext,
  CurrentUserCompany,
  PoliciesGuard,
} from '@amalia/kernel/auth/core';
import {
  canOverwritePayments,
  canViewPayments,
  getViewPaymentsSubset,
  SubsetAccessEnum,
  SubsetsAccessHelper,
} from '@amalia/kernel/auth/shared';
import { type AuthenticatedContext } from '@amalia/kernel/auth/types';
 
import { PaymentSplitConfigurationDto } from '../dto/paymentSplit.dto';
 
import { PaymentsSplitService } from './payments.split.service';
 
@UseGuards(AmaliaAuthGuard, PoliciesGuard)
@ApiBearerAuth()
@ApiTags('payments/split')
@Controller('payments/split')
export class PaymentsSplitController {
  public constructor(
    @Inject(PaymentsSplitService)
    private readonly paymentsSplitsService: PaymentsSplitService,
  ) {}
 
  @Get(':masterPaymentId')
  @CheckPolicies((ability) => canViewPayments(ability))
  public async detailsAboutPayment(
    @CurrentUserCompany() company: Company,
    @CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
    @Param('masterPaymentId', new UuidPipe()) masterPaymentId: string,
  ): Promise<PaymentContract[]> {
    const subsetAccess = SubsetsAccessHelper.getSubset(authenticatedContext, getViewPaymentsSubset);
 
    if (subsetAccess === SubsetAccessEnum.NOTHING) {
      throw new ForbiddenException("You don't have access to Payments");
    }
 
    return this.paymentsSplitsService.getAllDetailsForMasterPaymentId(company, masterPaymentId);
  }
 
  @Delete(':masterPaymentId')
  @CheckPolicies((ability) => canOverwritePayments(ability))
  public async removeSplit(
    @CurrentUserCompany() company: Company,
    @Param('masterPaymentId', new UuidPipe()) masterPaymentId: string,
  ): Promise<void> {
    return this.paymentsSplitsService.removeSplitForMasterPaymentId(company, masterPaymentId);
  }
 
  @Post(':masterPaymentId')
  @CheckPolicies((ability) => canOverwritePayments(ability))
  public async addSplit(
    @CurrentUserCompany() company: Company,
    @Param('masterPaymentId', new UuidPipe()) masterPaymentId: string,
    @Body() paymentSplitConfiguration: PaymentSplitConfigurationDto,
  ): Promise<void> {
    return this.paymentsSplitsService.addSplitForMasterPaymentId(company, masterPaymentId, paymentSplitConfiguration);
  }
}