@@ -11,10 +11,14 @@ import {
1111} from './types' ;
1212import { SeachQueryProps } from '@/decorators/search-query/types' ;
1313import { SupplyPriority } from '../supply/types' ;
14+ import { ShelterSupplyService } from 'src/shelter-supply/shelter-supply.service' ;
1415
1516@Injectable ( )
1617export class ShelterService {
17- constructor ( private readonly prismaService : PrismaService ) { }
18+ constructor (
19+ private readonly prismaService : PrismaService ,
20+ private readonly shelterSupplyService : ShelterSupplyService ,
21+ ) { }
1822
1923 async store ( body : z . infer < typeof CreateShelterSchema > ) {
2024 const payload = CreateShelterSchema . parse ( body ) ;
@@ -102,7 +106,40 @@ export class ShelterService {
102106 async index ( props : SeachQueryProps ) {
103107 const { handleSearch } = props ;
104108
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 > > (
106143 this . prismaService . shelter ,
107144 {
108145 select : {
@@ -142,5 +179,7 @@ export class ShelterService {
142179 } ,
143180 } ,
144181 ) ;
182+
183+ return result ;
145184 }
146185}
0 commit comments