1+ import { NextRequest , NextResponse } from 'next/server'
2+ import { withAuth } from '@/lib/auth/middleware'
3+ import { updateReputationOnChain } from '@/lib/blockchain-reputation'
4+
5+ /**
6+ * Update on-chain reputation data
7+ * POST /api/blockchain-reputation/update
8+ *
9+ * This updates the immutable reputation record on the Stellar blockchain
10+ */
11+ export const POST = withAuth ( async ( request : NextRequest , auth ) => {
12+ try {
13+ const body = await request . json ( )
14+ const { completionScore, disputeCount, totalContracts } = body
15+
16+ // Validate that at least one field is being updated
17+ if ( completionScore === undefined && disputeCount === undefined && totalContracts === undefined ) {
18+ return NextResponse . json (
19+ { error : 'At least one field must be specified for update' , code : 'NO_UPDATE_FIELDS' } ,
20+ { status : 400 }
21+ )
22+ }
23+
24+ // Validate completion score range if provided
25+ if ( completionScore !== undefined && ( completionScore < 0 || completionScore > 100 ) ) {
26+ return NextResponse . json (
27+ { error : 'Completion score must be between 0 and 100' , code : 'INVALID_SCORE' } ,
28+ { status : 400 }
29+ )
30+ }
31+
32+ // Validate counts are non-negative if provided
33+ if ( disputeCount !== undefined && disputeCount < 0 ) {
34+ return NextResponse . json (
35+ { error : 'Dispute count cannot be negative' , code : 'INVALID_DISPUTE_COUNT' } ,
36+ { status : 400 }
37+ )
38+ }
39+
40+ if ( totalContracts !== undefined && totalContracts < 0 ) {
41+ return NextResponse . json (
42+ { error : 'Total contracts cannot be negative' , code : 'INVALID_CONTRACT_COUNT' } ,
43+ { status : 400 }
44+ )
45+ }
46+
47+ const horizonUrl = process . env . STELLAR_HORIZON_URL || 'https://horizon-testnet.stellar.org'
48+
49+ const result = await updateReputationOnChain (
50+ auth . walletAddress ,
51+ {
52+ completionScore,
53+ disputeCount,
54+ totalContracts
55+ } ,
56+ horizonUrl
57+ )
58+
59+ if ( ! result . success ) {
60+ return NextResponse . json (
61+ { error : result . error , code : 'UPDATE_FAILED' } ,
62+ { status : 500 }
63+ )
64+ }
65+
66+ return NextResponse . json ( {
67+ success : true ,
68+ message : 'Reputation updated successfully' ,
69+ transactionHash : result . transactionHash ,
70+ data : result . data
71+ } )
72+ } catch ( error ) {
73+ return NextResponse . json (
74+ {
75+ error : error instanceof Error ? error . message : 'Unknown error' ,
76+ code : 'INTERNAL_ERROR'
77+ } ,
78+ { status : 500 }
79+ )
80+ }
81+ } )
0 commit comments