11import { Router } from 'express' ;
2- import { verifyWork , getVerification } from '../services/verification.js' ;
2+ import {
3+ verifyWork ,
4+ getVerification ,
5+ updateVerification ,
6+ deleteVerification ,
7+ } from '../services/verification.js' ;
38import { idempotency } from '../middleware/idempotency.js' ;
49
510export const verificationRouter = Router ( ) ;
@@ -22,6 +27,130 @@ verificationRouter.post('/verify', idempotency(), async (req, res) => {
2227 }
2328} ) ;
2429
30+ // Bulk AI-powered work verification
31+ verificationRouter . post ( '/verify/batch' , idempotency ( ) , async ( req , res ) => {
32+ try {
33+ const { items } = req . body as {
34+ items ?: Array < { repositoryUrl ?: string ; milestoneDescription ?: string ; projectId ?: string } > ;
35+ } ;
36+
37+ if ( ! Array . isArray ( items ) || items . length === 0 ) {
38+ res . status ( 400 ) . json ( { message : 'Missing items for bulk verification' } ) ;
39+ return ;
40+ }
41+
42+ const results = await Promise . all (
43+ items . map ( async ( item , index ) => {
44+ if ( ! item ?. repositoryUrl || ! item ?. milestoneDescription || ! item ?. projectId ) {
45+ return { index, status : 'error' , error : 'Missing required fields' } ;
46+ }
47+
48+ try {
49+ const data = await verifyWork ( {
50+ repositoryUrl : item . repositoryUrl ,
51+ milestoneDescription : item . milestoneDescription ,
52+ projectId : item . projectId ,
53+ } ) ;
54+ return { index, status : 'success' , data } ;
55+ } catch ( error ) {
56+ const message = error instanceof Error ? error . message : 'Verification failed' ;
57+ return { index, status : 'error' , error : message } ;
58+ }
59+ } )
60+ ) ;
61+
62+ res . json ( { results } ) ;
63+ } catch ( error ) {
64+ console . error ( 'Bulk verification error:' , error ) ;
65+ res . status ( 500 ) . json ( { message : 'Bulk verification failed' } ) ;
66+ }
67+ } ) ;
68+
69+ // Bulk update verification results
70+ verificationRouter . patch ( '/batch' , ( req , res ) => {
71+ try {
72+ const { items } = req . body as {
73+ items ?: Array < {
74+ id ?: string ;
75+ status ?: 'passed' | 'failed' | 'pending' ;
76+ score ?: number ;
77+ summary ?: string ;
78+ details ?: string [ ] ;
79+ } > ;
80+ } ;
81+
82+ if ( ! Array . isArray ( items ) || items . length === 0 ) {
83+ res . status ( 400 ) . json ( { message : 'Missing items for bulk update' } ) ;
84+ return ;
85+ }
86+
87+ const results = items . map ( ( item , index ) => {
88+ if ( ! item ?. id ) {
89+ return { index, status : 'error' , error : 'Missing verification id' } ;
90+ }
91+
92+ const hasUpdates =
93+ item . status !== undefined ||
94+ item . score !== undefined ||
95+ item . summary !== undefined ||
96+ item . details !== undefined ;
97+
98+ if ( ! hasUpdates ) {
99+ return { index, status : 'error' , error : 'No update fields provided' } ;
100+ }
101+
102+ const updated = updateVerification ( {
103+ id : item . id ,
104+ status : item . status ,
105+ score : item . score ,
106+ summary : item . summary ,
107+ details : item . details ,
108+ } ) ;
109+
110+ if ( ! updated ) {
111+ return { index, status : 'error' , error : 'Verification not found' } ;
112+ }
113+
114+ return { index, status : 'success' , data : updated } ;
115+ } ) ;
116+
117+ const updatedCount = results . filter ( ( result ) => result . status === 'success' ) . length ;
118+ res . json ( { results, updatedCount } ) ;
119+ } catch ( error ) {
120+ console . error ( 'Bulk update error:' , error ) ;
121+ res . status ( 500 ) . json ( { message : 'Bulk update failed' } ) ;
122+ }
123+ } ) ;
124+
125+ // Bulk delete verification results
126+ verificationRouter . delete ( '/batch' , ( req , res ) => {
127+ try {
128+ const { ids } = req . body as { ids ?: string [ ] } ;
129+
130+ if ( ! Array . isArray ( ids ) || ids . length === 0 ) {
131+ res . status ( 400 ) . json ( { message : 'Missing ids for bulk delete' } ) ;
132+ return ;
133+ }
134+
135+ const results = ids . map ( ( id ) => {
136+ if ( ! id ) {
137+ return { id, status : 'error' , error : 'Missing verification id' } ;
138+ }
139+
140+ const deleted = deleteVerification ( id ) ;
141+ return deleted
142+ ? { id, status : 'deleted' }
143+ : { id, status : 'not_found' } ;
144+ } ) ;
145+
146+ const deletedCount = results . filter ( ( result ) => result . status === 'deleted' ) . length ;
147+ res . json ( { results, deletedCount } ) ;
148+ } catch ( error ) {
149+ console . error ( 'Bulk delete error:' , error ) ;
150+ res . status ( 500 ) . json ( { message : 'Bulk delete failed' } ) ;
151+ }
152+ } ) ;
153+
25154// Get verification result by ID
26155verificationRouter . get ( '/:id' , async ( req , res ) => {
27156 try {
0 commit comments