99 ApproveVouchDto ,
1010 RequestVouchDto ,
1111 VouchResponseDto ,
12+ VouchRequestItemDto ,
1213 VouchStatus ,
1314} from './dto/vouch.dto' ;
1415
@@ -17,6 +18,7 @@ interface VouchRow {
1718 mentor_wallet : string ;
1819 learner_wallet : string ;
1920 message : string | null ;
21+ loan_amount : number | null ;
2022 status : VouchStatus ;
2123 created_at : string ;
2224 expires_at : string ;
@@ -167,6 +169,56 @@ export class VouchingService {
167169 return rows . map ( ( row ) => this . mapToDto ( row ) ) ;
168170 }
169171
172+ async getIncomingRequests ( mentorWallet : string ) : Promise < VouchRequestItemDto [ ] > {
173+ const client = this . supabaseService . getClient ( ) ;
174+
175+ const { data, error } = await client
176+ . from ( 'vouches' )
177+ . select ( 'learner_wallet, message, loan_amount, created_at' )
178+ . eq ( 'mentor_wallet' , mentorWallet )
179+ . eq ( 'status' , VouchStatus . PENDING )
180+ . order ( 'created_at' , { ascending : false } ) ;
181+
182+ if ( error ) {
183+ this . logger . error ( `Failed to fetch incoming requests for ${ mentorWallet } : ${ error . message } ` ) ;
184+ throw new Error ( 'Failed to fetch vouch requests.' ) ;
185+ }
186+
187+ const rows = data ?? [ ] ;
188+ const results : VouchRequestItemDto [ ] = [ ] ;
189+
190+ for ( const row of rows ) {
191+ const reputationScore = await this . getLearnerReputationScore ( row . learner_wallet ) ;
192+ results . push ( {
193+ learnerWallet : row . learner_wallet ,
194+ reputationScore,
195+ requestedLoanAmount : row . loan_amount ?? null ,
196+ loanPurpose : row . message ?? null ,
197+ requestedAt : row . created_at ,
198+ } ) ;
199+ }
200+
201+ return results ;
202+ }
203+
204+ private async getLearnerReputationScore ( wallet : string ) : Promise < number > {
205+ try {
206+ const { data, error } = await this . supabaseService . getClient ( )
207+ . from ( 'reputation_cache' )
208+ . select ( 'score' )
209+ . eq ( 'wallet_address' , wallet )
210+ . maybeSingle ( ) ;
211+
212+ if ( error || ! data ) {
213+ return 0 ;
214+ }
215+
216+ return data . score ;
217+ } catch {
218+ return 0 ;
219+ }
220+ }
221+
170222 private mapToDto ( data : VouchRow ) : VouchResponseDto {
171223 return {
172224 id : data . id ,
0 commit comments