1
1
import { z } from 'zod' ;
2
2
import { Injectable } from '@nestjs/common' ;
3
- import { Prisma } from '@prisma/client' ;
3
+ import * as qs from 'qs' ;
4
+ import { Prisma , AccessLevel } from '@prisma/client' ;
4
5
import { DefaultArgs } from '@prisma/client/runtime/library' ;
6
+
5
7
import { PrismaService } from '../prisma/prisma.service' ;
6
8
import {
7
- ComplexSearchSchema ,
8
9
CreateShelterSchema ,
9
10
FullUpdateShelterSchema ,
10
11
UpdateShelterSchema ,
11
- } from './types' ;
12
- import { SeachQueryProps } from '@/decorators/search-query/types' ;
13
- import { SupplyPriority } from 'src/supply/types' ;
12
+ } from './types/types' ;
13
+ import { SearchSchema } from '../types' ;
14
+ import { ShelterSearch , parseTagResponse } from './ShelterSearch' ;
15
+ import { SupplyPriority } from '../supply/types' ;
16
+ import { IFilterFormProps } from './types/search.types' ;
17
+ import { isRightSessionRole } from '@/guards/utils' ;
14
18
15
19
@Injectable ( )
16
20
export class ShelterService {
17
- constructor ( private readonly prismaService : PrismaService ) { }
21
+ private voluntaryIds : string [ ] = [ ] ;
22
+
23
+ constructor ( private readonly prismaService : PrismaService ) {
24
+ this . loadVoluntaryIds ( ) ;
25
+ }
18
26
19
27
async store ( body : z . infer < typeof CreateShelterSchema > ) {
20
28
const payload = CreateShelterSchema . parse ( body ) ;
@@ -53,7 +61,13 @@ export class ShelterService {
53
61
} ) ;
54
62
}
55
63
56
- async show ( id : string ) {
64
+ async show ( id : string , user : any ) {
65
+ const isLogged = await isRightSessionRole (
66
+ [ AccessLevel . User , AccessLevel . Staff ] ,
67
+ user ?. sessionId ,
68
+ user ?. userId ,
69
+ ) ;
70
+
57
71
const data = await this . prismaService . shelter . findFirst ( {
58
72
where : {
59
73
id,
@@ -65,7 +79,7 @@ export class ShelterService {
65
79
pix : true ,
66
80
shelteredPeople : true ,
67
81
capacity : true ,
68
- contact : true ,
82
+ contact : isLogged ,
69
83
petFriendly : true ,
70
84
prioritySum : true ,
71
85
latitude : true ,
@@ -74,6 +88,7 @@ export class ShelterService {
74
88
shelterSupplies : {
75
89
select : {
76
90
priority : true ,
91
+ quantity : true ,
77
92
supply : {
78
93
select : {
79
94
id : true ,
@@ -99,229 +114,82 @@ export class ShelterService {
99
114
return data ;
100
115
}
101
116
102
- async index ( props : SeachQueryProps ) {
103
- const { handleSearch } = props ;
104
-
105
- return await handleSearch < Prisma . ShelterSelect < DefaultArgs > > (
106
- this . prismaService . shelter ,
107
- {
108
- select : {
109
- id : true ,
110
- name : true ,
111
- pix : true ,
112
- address : true ,
113
- capacity : true ,
114
- contact : true ,
115
- petFriendly : true ,
116
- shelteredPeople : true ,
117
- prioritySum : true ,
118
- verified : true ,
119
- latitude : true ,
120
- longitude : true ,
121
- createdAt : true ,
122
- updatedAt : true ,
123
- shelterSupplies : {
124
- where : {
125
- priority : {
126
- gt : SupplyPriority . UnderControl ,
127
- } ,
128
- } ,
129
- select : {
130
- priority : true ,
131
- supply : {
132
- select : {
133
- id : true ,
134
- name : true ,
135
- supplyCategory : {
136
- select : {
137
- id : true ,
138
- name : true ,
139
- } ,
140
- } ,
141
- createdAt : true ,
142
- updatedAt : true ,
143
- } ,
144
- } ,
145
- } ,
146
- } ,
147
- } ,
148
- } ,
149
- ) ;
150
- }
151
-
152
- async search ( props : z . infer < typeof ComplexSearchSchema > ) {
153
- const payload = ComplexSearchSchema . parse ( {
154
- ...props ,
155
- supplyCategories :
156
- typeof props [ 'supplyCategories[]' ] === 'string'
157
- ? [ props [ 'supplyCategories[]' ] ]
158
- : props [ 'supplyCategories[]' ] ,
159
- supplies :
160
- typeof props [ 'supplies[]' ] === 'string'
161
- ? [ props [ 'supplies[]' ] ]
162
- : props [ 'supplies[]' ] ,
163
- } ) ;
164
-
165
- const shelterStatusFilter = this . addShelterStatusFilter ( payload ) ;
166
- const where = this . mountWhereFilter ( payload ) ;
167
- const take = payload . perPage ;
168
- const skip = payload . perPage * ( payload . page - 1 ) ;
169
-
170
- if ( shelterStatusFilter . length > 0 ) {
171
- where [ 'AND' ] . push ( {
172
- OR : shelterStatusFilter ,
173
- } ) ;
174
- }
175
-
176
- const count = await this . prismaService . shelter . count ( {
177
- where : where ,
178
- } ) ;
179
-
180
- const results = await this . prismaService . shelter . findMany ( {
181
- where : where ,
182
- orderBy : {
183
- prioritySum : 'desc' ,
184
- } ,
117
+ async index ( query : any ) {
118
+ const {
119
+ order,
120
+ orderBy,
121
+ page,
122
+ perPage,
123
+ search : searchQuery ,
124
+ } = SearchSchema . parse ( query ) ;
125
+ const queryData = qs . parse ( searchQuery ) as unknown as IFilterFormProps ;
126
+ const { query : where } = new ShelterSearch ( this . prismaService , queryData ) ;
127
+ const count = await this . prismaService . shelter . count ( { where } ) ;
128
+
129
+ const take = perPage ;
130
+ const skip = perPage * ( page - 1 ) ;
131
+
132
+ const whereData : Prisma . ShelterFindManyArgs < DefaultArgs > = {
185
133
take,
186
134
skip,
135
+ orderBy : { [ orderBy ] : order } ,
136
+ where,
137
+ } ;
138
+
139
+ const results = await this . prismaService . shelter . findMany ( {
140
+ ...whereData ,
187
141
select : {
188
142
id : true ,
189
143
name : true ,
190
144
pix : true ,
191
145
address : true ,
192
146
capacity : true ,
193
147
contact : true ,
194
- verified : true ,
195
148
petFriendly : true ,
196
149
shelteredPeople : true ,
197
150
prioritySum : true ,
151
+ verified : true ,
198
152
latitude : true ,
199
153
longitude : true ,
200
154
createdAt : true ,
201
155
updatedAt : true ,
202
156
shelterSupplies : {
203
157
where : {
204
158
priority : {
205
- gt : SupplyPriority . UnderControl ,
159
+ notIn : [ SupplyPriority . UnderControl ] ,
206
160
} ,
207
161
} ,
208
- select : {
209
- priority : true ,
210
- supply : {
211
- select : {
212
- id : true ,
213
- name : true ,
214
- supplyCategory : {
215
- select : {
216
- id : true ,
217
- name : true ,
218
- } ,
219
- } ,
220
- createdAt : true ,
221
- updatedAt : true ,
222
- } ,
223
- } ,
162
+ orderBy : {
163
+ updatedAt : 'desc' ,
164
+ } ,
165
+ include : {
166
+ supply : true ,
224
167
} ,
225
168
} ,
226
169
} ,
227
170
} ) ;
228
- return { perPage : payload . perPage , page : payload . page , count, results } ;
229
- }
230
171
231
- private mountWhereFilter ( payload : z . infer < typeof ComplexSearchSchema > ) {
232
- const filter : any = {
233
- AND : [
234
- {
235
- OR : [
236
- { address : { contains : payload . search , mode : 'insensitive' } } ,
237
- { name : { contains : payload . search , mode : 'insensitive' } } ,
238
- ] ,
239
- } ,
240
- ] ,
241
- } ;
172
+ const parsed = parseTagResponse ( queryData , results , this . voluntaryIds ) ;
242
173
243
- const shelterSuppliesFilter = {
244
- shelterSupplies : {
245
- some : { } ,
246
- } ,
174
+ return {
175
+ page,
176
+ perPage,
177
+ count,
178
+ results : parsed ,
247
179
} ;
248
-
249
- if ( payload . priority ) {
250
- shelterSuppliesFilter . shelterSupplies . some [ 'priority' ] = parseInt (
251
- payload . priority ,
252
- ) ;
253
- }
254
-
255
- if ( payload ?. supplyCategories && payload ?. supplyCategories . length !== 0 ) {
256
- shelterSuppliesFilter . shelterSupplies . some [ 'supply' ] = {
257
- supplyCategoryId : {
258
- in : payload . supplyCategories ,
259
- } ,
260
- } ;
261
- }
262
-
263
- if ( payload ?. supplies && payload ?. supplies . length !== 0 ) {
264
- shelterSuppliesFilter . shelterSupplies . some [ 'supplyId' ] = {
265
- in : payload . supplies ,
266
- } ;
267
- }
268
-
269
- if ( Object . keys ( shelterSuppliesFilter . shelterSupplies . some ) . length !== 0 ) {
270
- filter [ 'AND' ] . push ( shelterSuppliesFilter ) ;
271
- }
272
-
273
- return filter ;
274
180
}
275
181
276
- private addShelterStatusFilter ( payload : z . infer < typeof ComplexSearchSchema > ) {
277
- const shelterStatusFilter : any = [ ] ;
278
-
279
- if ( payload . filterAvailableShelter ) {
280
- shelterStatusFilter . push ( {
281
- AND : [
282
- {
283
- capacity : {
284
- gt : this . prismaService . shelter . fields . shelteredPeople ,
285
- } ,
182
+ loadVoluntaryIds ( ) {
183
+ this . prismaService . supplyCategory
184
+ . findMany ( {
185
+ where : {
186
+ name : {
187
+ in : [ 'Especialistas e Profissionais' , 'Voluntariado' ] ,
286
188
} ,
287
- {
288
- capacity : { not : null } ,
289
- } ,
290
- {
291
- shelteredPeople : { not : null } ,
292
- } ,
293
- ] ,
294
- } ) ;
295
- }
296
-
297
- if ( payload . filterUnavailableShelter ) {
298
- shelterStatusFilter . push ( {
299
- AND : [
300
- {
301
- capacity : {
302
- lte : this . prismaService . shelter . fields . shelteredPeople ,
303
- } ,
304
- } ,
305
- {
306
- capacity : { not : null } ,
307
- } ,
308
- {
309
- shelteredPeople : { not : null } ,
310
- } ,
311
- ] ,
312
- } ) ;
313
- }
314
-
315
- if ( payload . waitingShelterAvailability ) {
316
- shelterStatusFilter . push ( {
317
- capacity : null ,
318
- } ) ;
319
-
320
- shelterStatusFilter . push ( {
321
- shelteredPeople : null ,
189
+ } ,
190
+ } )
191
+ . then ( ( resp ) => {
192
+ this . voluntaryIds . push ( ...resp . map ( ( s ) => s . id ) ) ;
322
193
} ) ;
323
- }
324
-
325
- return shelterStatusFilter ;
326
194
}
327
195
}
0 commit comments