Skip to content

Commit 0d03fda

Browse files
committed
feature(app): refactor and add filters
1 parent 7e2fa06 commit 0d03fda

File tree

18 files changed

+329
-157
lines changed

18 files changed

+329
-157
lines changed

apps/api/src/controllers/bankaccount.controller.ts

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { Request, Response } from 'express'
22
import prisma from '@poveroh/prisma'
3-
import { IBankAccount, IBankAccountBase } from '@poveroh/types'
3+
import { IBankAccount, IBankAccountBase, IBankAccountFilters } from '@poveroh/types'
44
import _ from 'lodash'
55
import { MediaHelper } from '../helpers/media.helper'
6+
import { buildWhere } from '../helpers/filter.helper'
67

78
export class BankAccountController {
89
static async add(req: Request, res: Response) {
@@ -75,34 +76,15 @@ export class BankAccountController {
7576

7677
static async read(req: Request, res: Response) {
7778
try {
78-
const { id, title, description } = req.body
79+
const filters: IBankAccountFilters | string[] = req.body
80+
const where = buildWhere(filters)
7981

80-
const sql: any = {
81-
where: {},
82-
orderBy: {
83-
created_at: 'desc'
84-
}
85-
}
86-
87-
if (Array.isArray(req.body)) {
88-
sql.where = {
89-
id: {
90-
in: req.body
91-
}
92-
}
93-
} else if (!_.isEmpty(req.body)) {
94-
sql.where = {
95-
OR: [
96-
id && { id },
97-
title && { title: { contains: title, mode: 'insensitive' } },
98-
description && { description: { contains: description, mode: 'insensitive' } }
99-
].filter(Boolean)
100-
}
101-
}
102-
103-
const accounts = await prisma.bank_accounts.findMany(sql)
82+
const data = await prisma.bank_accounts.findMany({
83+
where,
84+
orderBy: { created_at: 'desc' }
85+
})
10486

105-
res.status(200).json(accounts)
87+
res.status(200).json(data)
10688
} catch (error) {
10789
console.error(error)
10890
res.status(500).json({ message: 'An error occurred', error })

apps/api/src/controllers/category.controller.ts

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Request, Response } from 'express'
22
import prisma from '@poveroh/prisma'
3-
import { ICategory, ICategoryBase } from '@poveroh/types'
3+
import { ICategory, ICategoryBase, ICategoryFilters } from '@poveroh/types'
44
import _ from 'lodash'
5+
import { buildWhere } from '../helpers/filter.helper'
56

67
export class CategoryController {
78
static async add(req: Request, res: Response) {
@@ -68,39 +69,18 @@ export class CategoryController {
6869

6970
static async read(req: Request, res: Response) {
7071
try {
71-
const { id, title, description } = req.body
72+
const filters: ICategoryFilters | string[] = req.body
73+
const where = buildWhere(filters)
7274

73-
const sql: any = {
74-
where: {},
75-
orderBy: {
76-
created_at: 'desc'
77-
},
78-
include: {
79-
subcategories: true
80-
}
81-
}
82-
83-
if (Array.isArray(req.body)) {
84-
sql.where = {
85-
id: {
86-
in: req.body
87-
}
88-
}
89-
} else if (!_.isEmpty(req.body)) {
90-
sql.where = {
91-
OR: [
92-
id && { id },
93-
title && { title: { contains: title, mode: 'insensitive' } },
94-
description && { description: { contains: description, mode: 'insensitive' } }
95-
].filter(Boolean)
96-
}
97-
}
98-
99-
const categories = await prisma.categories.findMany(sql)
75+
const data = await prisma.categories.findMany({
76+
where,
77+
include: { subcategories: true },
78+
orderBy: { created_at: 'desc' }
79+
})
10080

101-
res.status(200).json(categories)
81+
res.status(200).json(data)
10282
} catch (error) {
103-
console.log(error)
83+
console.error(error)
10484
res.status(500).json({ message: 'An error occurred', error })
10585
}
10686
}

apps/api/src/controllers/subcategory.controller.ts

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Request, Response } from 'express'
22
import prisma from '@poveroh/prisma'
3-
import { ISubcategory, ISubcategoryBase } from '@poveroh/types'
3+
import { ISubcategory, ISubcategoryBase, ISubcategoryFilters } from '@poveroh/types'
44
import _ from 'lodash'
5+
import { buildWhere } from '../helpers/filter.helper'
56

67
export class SubcategoryController {
78
static async add(req: Request, res: Response) {
@@ -54,36 +55,17 @@ export class SubcategoryController {
5455

5556
static async read(req: Request, res: Response) {
5657
try {
57-
const { id, title, description } = req.body
58+
const filters: ISubcategoryFilters | string[] = req.body
59+
const where = buildWhere(filters)
5860

59-
const sql: any = {
60-
where: {},
61-
orderBy: {
62-
created_at: 'desc'
63-
}
64-
}
65-
66-
if (Array.isArray(req.body)) {
67-
sql.where = {
68-
id: {
69-
in: req.body
70-
}
71-
}
72-
} else if (!_.isEmpty(req.body)) {
73-
sql.where = {
74-
OR: [
75-
id && { id },
76-
title && { title: { contains: title, mode: 'insensitive' } },
77-
description && { description: { contains: description, mode: 'insensitive' } }
78-
].filter(Boolean)
79-
}
80-
}
81-
82-
const subcategories = await prisma.subcategories.findMany(sql)
61+
const data = await prisma.subcategories.findMany({
62+
where,
63+
orderBy: { created_at: 'desc' }
64+
})
8365

84-
res.status(200).json(subcategories)
66+
res.status(200).json(data)
8567
} catch (error) {
86-
console.log(error)
68+
console.error(error)
8769
res.status(500).json({ message: 'An error occurred', error })
8870
}
8971
}

apps/api/src/controllers/transaction.controller.ts

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { Request, Response } from 'express'
22
import prisma from '@poveroh/prisma'
33
import _ from 'lodash'
44
import { TransactionHelper } from '../helpers/transaction.helper'
5+
import { buildWhere } from '../helpers/filter.helper'
6+
import { ITransactionFilters } from '@poveroh/types'
57

68
export class TransactionController {
79
static async add(req: Request, res: Response) {
@@ -49,35 +51,26 @@ export class TransactionController {
4951

5052
static async read(req: Request, res: Response) {
5153
try {
52-
const { id, title, description } = req.body
54+
const filters: ITransactionFilters | string[] = req.body
55+
const isArrayRequest = Array.isArray(filters)
5356

54-
const sql: any = {
55-
where: {},
56-
orderBy: {
57-
created_at: 'desc'
58-
},
59-
include: {
60-
amounts: true
61-
}
62-
}
63-
64-
if (Array.isArray(req.body)) {
65-
sql.where = {
66-
id: {
67-
in: req.body
68-
}
69-
}
70-
} else if (!_.isEmpty(req.body)) {
71-
sql.where = {
72-
OR: [
73-
id && { id },
74-
title && { title: { contains: title, mode: 'insensitive' } },
75-
description && { description: { contains: description, mode: 'insensitive' } }
76-
].filter(Boolean)
77-
}
78-
}
57+
const where = isArrayRequest
58+
? { id: { in: filters } }
59+
: {
60+
...buildWhere(filters),
61+
...(filters.fromDate && {
62+
date: {
63+
...(filters.date || {}),
64+
gte: new Date(filters.fromDate)
65+
}
66+
})
67+
}
7968

80-
const data = await prisma.transactions.findMany(sql)
69+
const data = await prisma.transactions.findMany({
70+
where,
71+
include: { amounts: true },
72+
orderBy: { created_at: 'desc' }
73+
})
8174

8275
res.status(200).json(data)
8376
} catch (error) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export function buildWhere<T extends Record<string, any>>(filters: T): any {
2+
if (Array.isArray(filters)) return { id: { in: filters } }
3+
4+
const { search, ...rest } = filters
5+
6+
const andConditions = Object.entries(rest).map(([key, value]) => {
7+
if (value === undefined) return null
8+
9+
if (typeof value === 'object' && value !== null && ('contains' in value || 'equals' in value)) {
10+
return { [key]: value }
11+
}
12+
13+
return { [key]: value }
14+
})
15+
16+
const searchConditions =
17+
search && typeof search === 'string'
18+
? {
19+
OR: [
20+
{ title: { contains: search, mode: 'insensitive' } },
21+
{ description: { contains: search, mode: 'insensitive' } },
22+
{ note: { contains: search, mode: 'insensitive' } }
23+
]
24+
}
25+
: {}
26+
27+
return {
28+
AND: [...andConditions.filter(Boolean), searchConditions]
29+
}
30+
}

0 commit comments

Comments
 (0)