diff --git a/apps/api/src/bank-transactions/bank-transactions.controller.ts b/apps/api/src/bank-transactions/bank-transactions.controller.ts index 127c1e9b1..f4e111217 100644 --- a/apps/api/src/bank-transactions/bank-transactions.controller.ts +++ b/apps/api/src/bank-transactions/bank-transactions.controller.ts @@ -44,6 +44,8 @@ export class BankTransactionsController { @ApiQuery({ name: 'from', required: false, type: Date }) @ApiQuery({ name: 'to', required: false, type: Date }) @ApiQuery({ name: 'search', required: false, type: String }) + @ApiQuery({ name: 'sortBy', required: false, type: String }) + @ApiQuery({ name: 'sortOrder', required: false, type: String }) findAll(@Query() query?: BankTransactionsQueryDto) { return this.bankTransactionsService.listBankTransactions( query?.status, @@ -53,6 +55,8 @@ export class BankTransactionsController { query?.search, query?.pageindex, query?.pagesize, + query?.sortBy, + query?.sortOrder, ) } diff --git a/apps/api/src/bank-transactions/bank-transactions.service.ts b/apps/api/src/bank-transactions/bank-transactions.service.ts index 0fede8086..99c004600 100644 --- a/apps/api/src/bank-transactions/bank-transactions.service.ts +++ b/apps/api/src/bank-transactions/bank-transactions.service.ts @@ -11,6 +11,7 @@ import { } from '@prisma/client' import { ExportService } from '../export/export.service' import { getTemplateByTable } from '../export/helpers/exportableData' +import { Prisma } from '@prisma/client' import { PrismaService } from '../prisma/prisma.service' import { Response } from 'express' import { CreateBankPaymentDto } from '../donations/dto/create-bank-payment.dto' @@ -35,6 +36,8 @@ export class BankTransactionsService { * @param search (Optional) Search by sender info or description * @param pageIndex (Optional) * @param pageSize (Optional) + * @param sortBy (Optional) Sort by a specific field + * @param sortOrder (Optional) Sort order (ascending or descending) */ async listBankTransactions( bankDonationStatus?: BankDonationStatus, @@ -44,7 +47,13 @@ export class BankTransactionsService { search?: string, pageIndex?: number, pageSize?: number, + sortBy?: string, + sortOrder?: string, ) { + const defaultSort: Prisma.BankTransactionOrderByWithRelationInput = { + transactionDate: 'desc', + } + const data = await this.prisma.bankTransaction.findMany({ where: { bankDonationStatus, @@ -65,6 +74,7 @@ export class BankTransactionsService { }, skip: pageIndex && pageSize ? pageIndex * pageSize : undefined, take: pageSize ? pageSize : undefined, + orderBy: [sortBy ? { [sortBy]: sortOrder ? sortOrder : 'desc' } : defaultSort], }) const count = await this.prisma.bankTransaction.count({ diff --git a/apps/api/src/bank-transactions/dto/bank-transactions-query-dto.ts b/apps/api/src/bank-transactions/dto/bank-transactions-query-dto.ts index 943e5687a..fd6d47a0c 100644 --- a/apps/api/src/bank-transactions/dto/bank-transactions-query-dto.ts +++ b/apps/api/src/bank-transactions/dto/bank-transactions-query-dto.ts @@ -43,6 +43,16 @@ export class BankTransactionsQueryDto { @IsOptional() @Transform(({ value }) => toNumber(value)) pagesize?: number + + @Expose() + @IsOptional() + @Transform(({ value }) => falsyToUndefined(value)) + sortBy?: string + + @Expose() + @IsOptional() + @Transform(({ value }) => falsyToUndefined(value)) + sortOrder?: string } function toNumber(value: string, opts: ToNumberOptions = {}): number | undefined {