@@ -11,10 +11,14 @@ import {
11
11
} from './types' ;
12
12
import { SeachQueryProps } from '@/decorators/search-query/types' ;
13
13
import { SupplyPriority } from '../supply/types' ;
14
+ import { ShelterSupplyService } from 'src/shelter-supply/shelter-supply.service' ;
14
15
15
16
@Injectable ( )
16
17
export class ShelterService {
17
- constructor ( private readonly prismaService : PrismaService ) { }
18
+ constructor (
19
+ private readonly prismaService : PrismaService ,
20
+ private readonly shelterSupplyService : ShelterSupplyService ,
21
+ ) { }
18
22
19
23
async store ( body : z . infer < typeof CreateShelterSchema > ) {
20
24
const payload = CreateShelterSchema . parse ( body ) ;
@@ -102,7 +106,40 @@ export class ShelterService {
102
106
async index ( props : SeachQueryProps ) {
103
107
const { handleSearch } = props ;
104
108
105
- return await handleSearch < Prisma . ShelterSelect < DefaultArgs > > (
109
+ const partialResult = await handleSearch < Prisma . ShelterSelect < DefaultArgs > > (
110
+ this . prismaService . shelter ,
111
+ {
112
+ select : {
113
+ id : true ,
114
+ shelterSupplies : {
115
+ where : {
116
+ priority : {
117
+ gte : SupplyPriority . Urgent ,
118
+ } ,
119
+ } ,
120
+ take : 10 ,
121
+ select : {
122
+ priority : true ,
123
+ supply : {
124
+ select : {
125
+ id : true ,
126
+ } ,
127
+ } ,
128
+ createdAt : true ,
129
+ updatedAt : true ,
130
+ } ,
131
+ } ,
132
+ } ,
133
+ } ,
134
+ ) ;
135
+
136
+ await this . shelterSupplyService . checkAndUpdateOutdatedPriorities (
137
+ partialResult . results . flatMap ( ( r ) =>
138
+ r . shelterSupplies . map ( ( s ) => ( { ...s , shelterId : r . id } ) ) ,
139
+ ) ,
140
+ ) ;
141
+
142
+ const result = await handleSearch < Prisma . ShelterSelect < DefaultArgs > > (
106
143
this . prismaService . shelter ,
107
144
{
108
145
select : {
@@ -142,5 +179,7 @@ export class ShelterService {
142
179
} ,
143
180
} ,
144
181
) ;
182
+
183
+ return result ;
145
184
}
146
185
}
0 commit comments