Skip to content

Commit

Permalink
add default sort
Browse files Browse the repository at this point in the history
  • Loading branch information
dphilipov committed Sep 19, 2023
1 parent 1458936 commit 6266b52
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 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 @@ -47,6 +50,10 @@ export class BankTransactionsService {
sortBy?: string,
sortOrder?: string,
) {
const defaultSort: Prisma.BankTransactionOrderByWithRelationInput = {
transactionDate: 'desc',
}

const data = await this.prisma.bankTransaction.findMany({
where: {
bankDonationStatus,
Expand All @@ -67,11 +74,7 @@ export class BankTransactionsService {
},
skip: pageIndex && pageSize ? pageIndex * pageSize : undefined,
take: pageSize ? pageSize : undefined,
orderBy: [
{
[sortBy as string]: sortOrder,
},
],
orderBy: [sortBy ? { [sortBy]: sortOrder ? sortOrder : 'desc' } : defaultSort],
})

const count = await this.prisma.bankTransaction.count({
Expand Down

0 comments on commit 6266b52

Please sign in to comment.