Skip to content

Commit 520cdc5

Browse files
committed
refact: index search
1 parent 847252c commit 520cdc5

File tree

2 files changed

+77
-27
lines changed

2 files changed

+77
-27
lines changed

src/shelter/ShelterSearch.ts

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import { Prisma } from '@prisma/client';
2+
23
import { IFilterFormProps } from './types';
4+
import { PrismaService } from '../prisma/prisma.service';
35

46
class ShelterSearch {
57
private data: IFilterFormProps;
8+
private prismaService: PrismaService;
69

7-
constructor(props: IFilterFormProps) {
10+
constructor(prismaService: PrismaService, props: IFilterFormProps) {
811
this.data = props;
12+
this.prismaService = prismaService;
913
}
1014

1115
get priority(): Prisma.ShelterWhereInput[] {
@@ -31,16 +35,83 @@ class ShelterSearch {
3135
return {
3236
capacity: null,
3337
};
34-
else if (status === 'available') return {};
38+
else if (status === 'available')
39+
return {
40+
capacity: {
41+
gt: this.prismaService.shelter.fields.shelteredPeople,
42+
},
43+
};
3544
else
3645
return {
37-
// capacity: {
38-
// lte: Prisma.ShelterScalarFieldEnum.shelteredPeople,
39-
// },
46+
capacity: {
47+
lte: this.prismaService.shelter.fields.shelteredPeople,
48+
},
4049
};
4150
});
4251
}
4352
}
53+
54+
get supplyCategoryIds(): Prisma.ShelterWhereInput {
55+
if (!this.data.supplyCategoryIds) return {};
56+
return {
57+
shelterSupplies: {
58+
some: {
59+
supply: {
60+
supplyCategoryId: {
61+
in: this.data.supplyCategoryIds,
62+
},
63+
},
64+
},
65+
},
66+
};
67+
}
68+
69+
get supplyIds(): Prisma.ShelterWhereInput {
70+
if (!this.data.supplyIds) return {};
71+
return {
72+
shelterSupplies: {
73+
some: {
74+
supply: {
75+
id: {
76+
in: this.data.supplyIds,
77+
},
78+
},
79+
},
80+
},
81+
};
82+
}
83+
84+
get search(): Prisma.ShelterWhereInput[] {
85+
if (!this.data.search) return [];
86+
else
87+
return [
88+
{
89+
address: {
90+
contains: this.data.search,
91+
mode: 'insensitive',
92+
},
93+
},
94+
{
95+
name: {
96+
contains: this.data.search,
97+
mode: 'insensitive',
98+
},
99+
},
100+
];
101+
}
102+
103+
get query(): Prisma.ShelterWhereInput {
104+
if (Object.keys(this.data).length === 0) return {};
105+
return {
106+
OR: [
107+
...this.search,
108+
...this.shelterStatus,
109+
this.supplyCategoryIds,
110+
this.supplyIds,
111+
...this.search,
112+
],
113+
};
114+
}
44115
}
45116

46117
export { ShelterSearch };

src/shelter/shelter.service.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { z } from 'zod';
22
import { Injectable } from '@nestjs/common';
3-
import { Prisma } from '@prisma/client';
43
import * as qs from 'qs';
54

65
import { PrismaService } from '../prisma/prisma.service';
@@ -114,27 +113,7 @@ export class ShelterService {
114113
search: searchQuery,
115114
} = SearchSchema.parse(query);
116115
const queryData = qs.parse(searchQuery) as unknown as IFilterFormProps;
117-
const { priority, search, shelterStatus, supplyCategoryIds, supplyIds } =
118-
queryData;
119-
const shelterSearch = new ShelterSearch(queryData);
120-
const where: Prisma.ShelterWhereInput = {
121-
OR: [
122-
{
123-
address: {
124-
contains: search,
125-
mode: 'insensitive',
126-
},
127-
},
128-
{
129-
name: {
130-
contains: search,
131-
mode: 'insensitive',
132-
},
133-
},
134-
...shelterSearch.priority,
135-
...shelterSearch.shelterStatus,
136-
],
137-
};
116+
const { query: where } = new ShelterSearch(this.prismaService, queryData);
138117

139118
const count = await this.prismaService.shelter.count({ where });
140119

0 commit comments

Comments
 (0)