Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sorting for bank transactions #549

Merged
merged 5 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -53,6 +55,8 @@ export class BankTransactionsController {
query?.search,
query?.pageindex,
query?.pagesize,
query?.sortBy,
query?.sortOrder,
)
}

Expand Down
10 changes: 10 additions & 0 deletions apps/api/src/bank-transactions/bank-transactions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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({
Expand Down
10 changes: 10 additions & 0 deletions apps/api/src/bank-transactions/dto/bank-transactions-query-dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading