1
1
import { Prisma } from '@prisma/client' ;
2
2
3
- import { IFilterFormProps } from './types' ;
4
3
import { PrismaService } from '../prisma/prisma.service' ;
4
+ import { SupplyPriority } from 'src/supply/types' ;
5
+ import {
6
+ IFilterFormProps ,
7
+ SearchShelterTagResponse ,
8
+ ShelterTagInfo ,
9
+ ShelterTagType ,
10
+ } from './types/search.types' ;
11
+
12
+ const defaultTagsData : ShelterTagInfo = {
13
+ NeedDonations : 10 ,
14
+ NeedVolunteers : 10 ,
15
+ RemainingSupplies : 10 ,
16
+ } ;
5
17
6
18
class ShelterSearch {
7
- private data : IFilterFormProps ;
19
+ private formProps : Partial < IFilterFormProps > ;
8
20
private prismaService : PrismaService ;
9
21
10
- constructor ( prismaService : PrismaService , props : IFilterFormProps ) {
11
- this . data = props ;
22
+ constructor (
23
+ prismaService : PrismaService ,
24
+ props : Partial < IFilterFormProps > = { } ,
25
+ ) {
12
26
this . prismaService = prismaService ;
27
+ this . formProps = { ...props } ;
13
28
}
14
29
15
30
get priority ( ) : Prisma . ShelterWhereInput [ ] {
16
- if ( this . data . priority ) {
31
+ if ( this . formProps . priority ) {
17
32
return [
18
33
{
19
34
shelterSupplies : {
20
35
some : {
21
- priority : + this . data . priority ,
36
+ priority : + this . formProps . priority ,
22
37
} ,
23
38
} ,
24
39
} ,
@@ -28,9 +43,9 @@ class ShelterSearch {
28
43
}
29
44
30
45
get shelterStatus ( ) : Prisma . ShelterWhereInput [ ] {
31
- if ( ! this . data . shelterStatus ) return [ ] ;
46
+ if ( ! this . formProps . shelterStatus ) return [ ] ;
32
47
else {
33
- return this . data . shelterStatus . map ( ( status ) => {
48
+ return this . formProps . shelterStatus . map ( ( status ) => {
34
49
if ( status === 'waiting' )
35
50
return {
36
51
capacity : null ,
@@ -52,13 +67,13 @@ class ShelterSearch {
52
67
}
53
68
54
69
get supplyCategoryIds ( ) : Prisma . ShelterWhereInput {
55
- if ( ! this . data . supplyCategoryIds ) return { } ;
70
+ if ( ! this . formProps . supplyCategoryIds ) return { } ;
56
71
return {
57
72
shelterSupplies : {
58
73
some : {
59
74
supply : {
60
75
supplyCategoryId : {
61
- in : this . data . supplyCategoryIds ,
76
+ in : this . formProps . supplyCategoryIds ,
62
77
} ,
63
78
} ,
64
79
} ,
@@ -67,13 +82,13 @@ class ShelterSearch {
67
82
}
68
83
69
84
get supplyIds ( ) : Prisma . ShelterWhereInput {
70
- if ( ! this . data . supplyIds ) return { } ;
85
+ if ( ! this . formProps . supplyIds ) return { } ;
71
86
return {
72
87
shelterSupplies : {
73
88
some : {
74
89
supply : {
75
90
id : {
76
- in : this . data . supplyIds ,
91
+ in : this . formProps . supplyIds ,
77
92
} ,
78
93
} ,
79
94
} ,
@@ -82,26 +97,26 @@ class ShelterSearch {
82
97
}
83
98
84
99
get search ( ) : Prisma . ShelterWhereInput [ ] {
85
- if ( ! this . data . search ) return [ ] ;
100
+ if ( ! this . formProps . search ) return [ ] ;
86
101
else
87
102
return [
88
103
{
89
104
address : {
90
- contains : this . data . search ,
105
+ contains : this . formProps . search ,
91
106
mode : 'insensitive' ,
92
107
} ,
93
108
} ,
94
109
{
95
110
name : {
96
- contains : this . data . search ,
111
+ contains : this . formProps . search ,
97
112
mode : 'insensitive' ,
98
113
} ,
99
114
} ,
100
115
] ;
101
116
}
102
117
103
118
get query ( ) : Prisma . ShelterWhereInput {
104
- if ( Object . keys ( this . data ) . length === 0 ) return { } ;
119
+ if ( Object . keys ( this . formProps ) . length === 0 ) return { } ;
105
120
const queryData = {
106
121
AND : [
107
122
{ OR : this . search } ,
@@ -116,4 +131,69 @@ class ShelterSearch {
116
131
}
117
132
}
118
133
119
- export { ShelterSearch } ;
134
+ /**
135
+ *
136
+ * @param formProps Uma interface do tipo ShelterTagInfo | null. Que indica a quantidade máxima de cada categoria deverá ser retornada
137
+ * @param results Resultado da query em `this.prismaService.shelter.findMany`
138
+ * @param voluntaryIds
139
+ * @returns Retorna a lista de resultados, adicionando o campo tags em cada supply para assim categoriza-los corretamente e limitar a quantidade de cada retornada respeitando os parametros em formProps
140
+ */
141
+ function parseTagResponse (
142
+ tagProps : Partial < Pick < IFilterFormProps , 'tags' > > = { } ,
143
+ results : SearchShelterTagResponse [ ] ,
144
+ voluntaryIds : string [ ] ,
145
+ ) : SearchShelterTagResponse [ ] {
146
+ const tags : ShelterTagInfo = {
147
+ ...defaultTagsData ,
148
+ ...( tagProps ?. tags ?? { } ) ,
149
+ } ;
150
+
151
+ const parsed = results . map ( ( result ) => {
152
+ const qtd : Required < ShelterTagInfo > = {
153
+ NeedDonations : 0 ,
154
+ NeedVolunteers : 0 ,
155
+ RemainingSupplies : 0 ,
156
+ } ;
157
+ return {
158
+ ...result ,
159
+ shelterSupplies : result . shelterSupplies . reduce ( ( prev , shelterSupply ) => {
160
+ const supplyTags : ShelterTagType [ ] = [ ] ;
161
+ let tagged : boolean = false ;
162
+ if (
163
+ tags . NeedDonations &&
164
+ [ SupplyPriority . Needing , SupplyPriority . Urgent ] . includes (
165
+ shelterSupply . priority ,
166
+ )
167
+ ) {
168
+ if ( qtd . NeedDonations < tags . NeedDonations ) {
169
+ tagged = true ;
170
+ supplyTags . push ( 'NeedDonations' ) ;
171
+ }
172
+ }
173
+ if (
174
+ tags . NeedVolunteers &&
175
+ voluntaryIds . includes ( shelterSupply . supply . supplyCategoryId )
176
+ ) {
177
+ if ( qtd . NeedVolunteers < tags . NeedVolunteers ) {
178
+ tagged = true ;
179
+ supplyTags . push ( 'NeedVolunteers' ) ;
180
+ }
181
+ }
182
+ if (
183
+ tags . RemainingSupplies &&
184
+ [ SupplyPriority . Remaining ] . includes ( shelterSupply . priority )
185
+ ) {
186
+ if ( qtd . RemainingSupplies < tags . RemainingSupplies ) {
187
+ tagged = true ;
188
+ supplyTags . push ( 'RemainingSupplies' ) ;
189
+ }
190
+ }
191
+ if ( tagged ) return [ ...prev , { ...shelterSupply , tags : supplyTags } ] ;
192
+ else return prev ;
193
+ } , [ ] as any ) ,
194
+ } ;
195
+ } ) ;
196
+ return parsed ;
197
+ }
198
+
199
+ export { ShelterSearch , parseTagResponse } ;
0 commit comments