@@ -53,11 +53,12 @@ export async function POST(req: NextRequest) {
5353 replace ( 'Ü' , 'U' )
5454
5555 try {
56- let probs : number [ ] = [ ]
56+ let mistakesInCW : number [ ] = [ ]
5757 let retMessage = ""
5858 let scholarshipResult : any = null
5959 const requestJson = await req . json ( )
60- const guideId = requestJson [ 'guideId' ] ?? ''
60+ const courseId = + requestJson [ 'courseId' ]
61+ const guideId = + requestJson [ 'guideId' ]
6162 const lang = requestJson [ 'lang' ] ?? ''
6263 const prefix = requestJson [ 'prefix' ] ?? ''
6364 const guide = requestJson [ 'guide' ] ?? ''
@@ -124,8 +125,8 @@ export async function POST(req: NextRequest) {
124125 removeAccents ( grid [ nrow ] [ ncol ] . userInput . toUpperCase ( ) ) !=
125126 removeAccents ( word [ j ] . toUpperCase ( ) )
126127 ) {
127- if ( ! probs . includes ( i + 1 ) ) {
128- probs . push ( i + 1 )
128+ if ( ! mistakesInCW . includes ( i + 1 ) ) {
129+ mistakesInCW . push ( i + 1 )
129130 }
130131 }
131132 if ( dir == "across" ) {
@@ -136,70 +137,72 @@ export async function POST(req: NextRequest) {
136137 }
137138 }
138139
139- // Si no hay errores, intentamos beca
140- if ( probs . length === 0 ) {
141- // Lógica similar a /api/scholarship
142- let usdtDecimals = 0
143- if ( process . env . NEXT_PUBLIC_USDT_DECIMALS != undefined ) {
144- usdtDecimals = + process . env . NEXT_PUBLIC_USDT_DECIMALS
140+ // Intentamos beca
141+ // Lógica similar a /api/scholarship
142+ let usdtDecimals = 0
143+ if ( process . env . NEXT_PUBLIC_USDT_DECIMALS != undefined ) {
144+ usdtDecimals = + process . env . NEXT_PUBLIC_USDT_DECIMALS
145+ }
146+ const rpcUrl = process . env . NEXT_PUBLIC_RPC_URL
147+ const publicClient = createPublicClient ( {
148+ chain : process . env . NEXT_PUBLIC_AUTH_URL == "https://learn.tg" ?
149+ celo : celoSepolia ,
150+ transport : http ( rpcUrl )
151+ } )
152+ const privateKey = process . env . NEXT_PUBLIC_PRIVATE_KEY as string | undefined
153+ let account : ReturnType < typeof privateKeyToAccount > | undefined
154+ if ( privateKey ) {
155+ try {
156+ account = privateKeyToAccount ( privateKey as Address )
157+ } catch ( e ) {
158+ retMessage += "\n" + msg [ locale ] . invalidKey
145159 }
146- const rpcUrl = process . env . NEXT_PUBLIC_RPC_URL
147- const publicClient = createPublicClient ( {
148- chain : process . env . NEXT_PUBLIC_AUTH_URL == "https://learn.tg" ?
149- celo : celoSepolia ,
150- transport : http ( rpcUrl )
160+ }
161+ const walletClient = account ? createWalletClient ( {
162+ account,
163+ chain : process . env . NEXT_PUBLIC_AUTH_URL == "https://learn.tg" ?
164+ celo : celoSepolia ,
165+ transport : http ( rpcUrl )
166+ } ) : undefined
167+ console . log ( "*** walletClient=" , walletClient )
168+ const contractAddress = process . env . NEXT_PUBLIC_DEPLOYED_AT as Address
169+ if ( contractAddress && walletClient ) {
170+ const contract = getContract ( {
171+ address : contractAddress ,
172+ abi : ScholarshipVaultsAbi as any ,
173+ client : { public : publicClient , wallet : walletClient }
151174 } )
152- const privateKey = process . env . NEXT_PUBLIC_PRIVATE_KEY as string | undefined
153- let account : ReturnType < typeof privateKeyToAccount > | undefined
154- if ( privateKey ) {
175+ console . log ( "*** contract=" , contract )
176+ const courseIdArg = BigInt ( courseId )
177+ const guideIdArg = BigInt ( guideId )
178+ // Verificar si puede enviar
179+ const canSubmit = await contract . read . studentCanSubmit ( [
180+ courseIdArg , walletAddress as Address
181+ ] ) as boolean
182+ if ( canSubmit ) {
183+ // Enviar resultado
155184 try {
156- account = privateKeyToAccount ( privateKey as Address )
157- } catch ( e ) {
158- retMessage += "\n" + msg [ locale ] . invalidKey
159- }
160- }
161- const walletClient = account ? createWalletClient ( {
162- account,
163- chain : process . env . NEXT_PUBLIC_AUTH_URL == "https://learn.tg" ?
164- celo : celoSepolia ,
165- transport : http ( rpcUrl )
166- } ) : undefined
167- const contractAddress = process . env . NEXT_PUBLIC_DEPLOYED_AT as Address
168- if ( contractAddress && walletClient ) {
169- const contract = getContract ( {
170- address : contractAddress ,
171- abi : ScholarshipVaultsAbi as any ,
172- client : { public : publicClient , wallet : walletClient }
173- } )
174- const courseIdArg = guideId && / ^ \d + $ / . test ( guideId ) ? BigInt ( guideId ) : guideId
175- // Verificar si puede enviar
176- const canSubmit = await contract . read . studentCanSubmit ( [
177- courseIdArg , walletAddress as Address
178- ] ) as boolean
179- if ( canSubmit ) {
180- // Enviar resultado
181- try {
182- const tx = await contract . write . submitGuideResult ( [
183- courseIdArg , walletAddress as Address , placements . length // guideNumber
184- ] )
185- scholarshipResult = tx
186- retMessage += "\n" + msg [ locale ] . correct
187- } catch ( err ) {
188- retMessage += "\n" + msg [ locale ] . submitError + err
189- }
190- } else {
191- retMessage += "\n" + msg [ locale ] . cannotSubmit
185+ const tx = await contract . write . submitGuideResult ( [
186+ courseIdArg , guideIdArg , walletAddress as Address ,
187+ mistakesInCW . length == 0
188+ ] )
189+ scholarshipResult = tx
190+ retMessage += "\n" + msg [ locale ] . correct
191+ } catch ( err ) {
192+ retMessage += "\n" + msg [ locale ] . submitError + err
192193 }
193194 } else {
194- retMessage += "\n" + msg [ locale ] . contractError
195+ retMessage += "\n" + msg [ locale ] . cannotSubmit
195196 }
197+ } else {
198+ retMessage += "\n" + msg [ locale ] . contractError
196199 }
197200 }
198201 }
199202
200203 return NextResponse . json (
201204 {
202- probs : probs ,
205+ mistakesInCW : mistakesInCW ,
203206 message : retMessage ,
204207 scholarshipResult : scholarshipResult
205208 } ,
0 commit comments