mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 20:30:33 +00:00
60 lines
2.3 KiB
TypeScript
60 lines
2.3 KiB
TypeScript
import { Controller, Get, Headers, Query, Res } from '@nestjs/common';
|
|
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
|
|
import { ITransactionsByCustomersFilter } from './TransactionsByCustomer.types';
|
|
import { TransactionsByCustomerApplication } from './TransactionsByCustomersApplication';
|
|
import { AcceptType } from '@/constants/accept-type';
|
|
import { Response } from 'express';
|
|
import { TransactionsByCustomerQueryDto } from './TransactionsByCustomerQuery.dto';
|
|
|
|
@Controller('/reports/transactions-by-customers')
|
|
@ApiTags('reports')
|
|
export class TransactionsByCustomerController {
|
|
constructor(
|
|
private readonly transactionsByCustomersApp: TransactionsByCustomerApplication,
|
|
) {}
|
|
|
|
@Get()
|
|
@ApiOperation({ summary: 'Get transactions by customer' })
|
|
@ApiResponse({ status: 200, description: 'Transactions by customer' })
|
|
async transactionsByCustomer(
|
|
@Query() filter: TransactionsByCustomerQueryDto,
|
|
@Res({ passthrough: true }) res: Response,
|
|
@Headers('accept') acceptHeader: string,
|
|
) {
|
|
// Retrieves the json table format.
|
|
if (acceptHeader.includes(AcceptType.ApplicationJsonTable)) {
|
|
return this.transactionsByCustomersApp.table(filter);
|
|
|
|
// Retrieve the csv format.
|
|
} else if (acceptHeader.includes(AcceptType.ApplicationCsv)) {
|
|
const csv = await this.transactionsByCustomersApp.csv(filter);
|
|
|
|
res.setHeader('Content-Disposition', 'attachment; filename=output.csv');
|
|
res.setHeader('Content-Type', 'text/csv');
|
|
|
|
res.send(csv);
|
|
|
|
// Retrieve the xlsx format.
|
|
} else if (acceptHeader.includes(AcceptType.ApplicationXlsx)) {
|
|
const buffer = await this.transactionsByCustomersApp.xlsx(filter);
|
|
res.setHeader('Content-Disposition', 'attachment; filename=output.xlsx');
|
|
res.setHeader(
|
|
'Content-Type',
|
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
);
|
|
res.send(buffer);
|
|
|
|
// Retrieve the json format.
|
|
} else if (acceptHeader.includes(AcceptType.ApplicationPdf)) {
|
|
const pdfContent = await this.transactionsByCustomersApp.pdf(filter);
|
|
res.set({
|
|
'Content-Type': 'application/pdf',
|
|
'Content-Length': pdfContent.length,
|
|
});
|
|
res.send(pdfContent);
|
|
} else {
|
|
return this.transactionsByCustomersApp.sheet(filter);
|
|
}
|
|
}
|
|
}
|