Files
bigcapital/packages/server/src/modules/PaymentReceived/PaymentsReceived.controller.ts
2025-06-22 20:58:53 +02:00

203 lines
5.5 KiB
TypeScript

import {
ApiExtraModels,
ApiOperation,
ApiResponse,
ApiTags,
getSchemaPath,
} from '@nestjs/swagger';
import {
Body,
Controller,
Delete,
Get,
Headers,
HttpCode,
Param,
ParseIntPipe,
Post,
Put,
Query,
} from '@nestjs/common';
import { PaymentReceivesApplication } from './PaymentReceived.application';
import {
IPaymentsReceivedFilter,
PaymentReceiveMailOptsDTO,
} from './types/PaymentReceived.types';
import {
CreatePaymentReceivedDto,
EditPaymentReceivedDto,
} from './dtos/PaymentReceived.dto';
import { AcceptType } from '@/constants/accept-type';
import { PaymentReceivedResponseDto } from './dtos/PaymentReceivedResponse.dto';
import { PaginatedResponseDto } from '@/common/dtos/PaginatedResults.dto';
import { PaymentReceivedStateResponseDto } from './dtos/PaymentReceivedStateResponse.dto';
@Controller('payments-received')
@ApiTags('Payments Received')
@ApiExtraModels(PaymentReceivedResponseDto)
@ApiExtraModels(PaginatedResponseDto)
@ApiExtraModels(PaymentReceivedStateResponseDto)
export class PaymentReceivesController {
constructor(private paymentReceivesApplication: PaymentReceivesApplication) {}
@Post(':id/mail')
@HttpCode(200)
@ApiResponse({
status: 200,
description: 'The payment receive mail has been successfully sent.',
})
public sendPaymentReceiveMail(
@Param('id', ParseIntPipe) paymentReceiveId: number,
@Body() messageOpts: PaymentReceiveMailOptsDTO,
) {
return this.paymentReceivesApplication.notifyPaymentByMail(
paymentReceiveId,
messageOpts,
);
}
@Get(':id/edit-page')
@ApiResponse({
status: 200,
description:
'The payment received edit page has been successfully retrieved.',
})
public getPaymentReceiveEditPage(
@Param('id', ParseIntPipe) paymentReceiveId: number,
) {
return this.paymentReceivesApplication.getPaymentReceivedEditPage(
paymentReceiveId,
);
}
@Get(':id/mail')
@ApiResponse({
status: 200,
description:
'The payment receive mail options have been successfully retrieved.',
})
public getPaymentReceiveMailOptions(
@Param('id', ParseIntPipe) paymentReceiveId: number,
) {
return this.paymentReceivesApplication.getPaymentMailOptions(
paymentReceiveId,
);
}
@Post()
@ApiOperation({ summary: 'Create a new payment received.' })
public createPaymentReceived(
@Body() paymentReceiveDTO: CreatePaymentReceivedDto,
) {
return this.paymentReceivesApplication.createPaymentReceived(
paymentReceiveDTO,
);
}
@Put(':id')
@ApiOperation({ summary: 'Edit the given payment received.' })
public editPaymentReceive(
@Param('id', ParseIntPipe) paymentReceiveId: number,
@Body() paymentReceiveDTO: EditPaymentReceivedDto,
) {
return this.paymentReceivesApplication.editPaymentReceive(
paymentReceiveId,
paymentReceiveDTO,
);
}
@Delete(':id')
@ApiOperation({ summary: 'Delete the given payment received.' })
public deletePaymentReceive(
@Param('id', ParseIntPipe) paymentReceiveId: number,
) {
return this.paymentReceivesApplication.deletePaymentReceive(
paymentReceiveId,
);
}
@Get()
@ApiOperation({ summary: 'Retrieves the payment received list.' })
@ApiResponse({
status: 200,
description: 'The payment received has been retrieved successfully.',
schema: {
allOf: [
{ $ref: getSchemaPath(PaginatedResponseDto) },
{
properties: {
data: {
type: 'array',
items: { $ref: getSchemaPath(PaymentReceivedResponseDto) },
},
},
},
],
},
})
public getPaymentsReceived(
@Query() filterDTO: Partial<IPaymentsReceivedFilter>,
) {
return this.paymentReceivesApplication.getPaymentsReceived(filterDTO);
}
@Get('state')
@ApiOperation({ summary: 'Retrieves the payment received state.' })
@ApiResponse({
status: 200,
description: 'The payment received state has been successfully retrieved.',
schema: {
$ref: getSchemaPath(PaymentReceivedStateResponseDto),
},
})
public getPaymentReceivedState() {
return this.paymentReceivesApplication.getPaymentReceivedState();
}
@Get(':id/invoices')
@ApiOperation({ summary: 'Retrieves the payment received invoices.' })
@ApiResponse({
status: 200,
description:
'The payment received invoices have been successfully retrieved.',
})
public getPaymentReceiveInvoices(
@Param('id', ParseIntPipe) paymentReceiveId: number,
) {
return this.paymentReceivesApplication.getPaymentReceiveInvoices(
paymentReceiveId,
);
}
@Get(':id')
@ApiOperation({ summary: 'Retrieves the payment received details.' })
@ApiResponse({
status: 200,
description:
'The payment received details have been successfully retrieved.',
schema: {
$ref: getSchemaPath(PaymentReceivedResponseDto),
},
})
public async getPaymentReceive(
@Param('id', ParseIntPipe) paymentReceiveId: number,
@Headers('accept') acceptHeader: string,
) {
if (acceptHeader.includes(AcceptType.ApplicationPdf)) {
return this.paymentReceivesApplication.getPaymentReceivePdf(
paymentReceiveId,
);
} else if (acceptHeader.includes(AcceptType.ApplicationTextHtml)) {
const htmlContent =
await this.paymentReceivesApplication.getPaymentReceivedHtml(
paymentReceiveId,
);
return { htmlContent };
} else {
return this.paymentReceivesApplication.getPaymentReceive(
paymentReceiveId,
);
}
}
}