feat: Add missing votes API proxy routes for blockchain queries

Created proxy routes to expose blockchain-related endpoints:
- GET /api/votes/public-keys - Get ElGamal public keys for vote encryption
- GET /api/votes/blockchain - Get blockchain state for an election
- GET /api/votes/results - Get election results from blockchain
- GET /api/votes/transaction-status - Check vote confirmation status

These routes forward requests to the backend and are required for the
frontend to access blockchain features like vote verification and
transaction status tracking.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Alexis Bruneteau 2025-11-07 16:26:32 +01:00
parent 9b616f00ac
commit 4c239c4552
9 changed files with 157 additions and 4 deletions

View File

@ -0,0 +1,30 @@
import { NextRequest, NextResponse } from 'next/server'
import { getBackendUrl } from '@/lib/api-config'
/**
* Proxy API route for getting blockchain state
* Forwards GET requests to the backend API
*/
export async function GET(request: NextRequest) {
try {
const backendUrl = getBackendUrl()
const searchParams = request.nextUrl.searchParams
const electionId = searchParams.get('election_id')
if (!electionId) {
return NextResponse.json({ detail: 'election_id is required' }, { status: 400 })
}
const response = await fetch(`${backendUrl}/api/votes/blockchain?election_id=${electionId}`, {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
})
const data = await response.json()
return NextResponse.json(data, { status: response.status })
} catch (error) {
console.error('[Blockchain]', error)
const msg = error instanceof Error ? error.message : 'Unknown error'
return NextResponse.json({ detail: msg }, { status: 500 })
}
}

View File

@ -0,0 +1,30 @@
import { NextRequest, NextResponse } from 'next/server'
import { getBackendUrl } from '@/lib/api-config'
/**
* Proxy API route for getting public keys
* Forwards GET requests to the backend API
*/
export async function GET(request: NextRequest) {
try {
const backendUrl = getBackendUrl()
const searchParams = request.nextUrl.searchParams
const electionId = searchParams.get('election_id')
if (!electionId) {
return NextResponse.json({ detail: 'election_id is required' }, { status: 400 })
}
const response = await fetch(`${backendUrl}/api/votes/public-keys?election_id=${electionId}`, {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
})
const data = await response.json()
return NextResponse.json(data, { status: response.status })
} catch (error) {
console.error('[PublicKeys]', error)
const msg = error instanceof Error ? error.message : 'Unknown error'
return NextResponse.json({ detail: msg }, { status: 500 })
}
}

View File

@ -0,0 +1,36 @@
import { NextRequest, NextResponse } from 'next/server'
import { getBackendUrl } from '@/lib/api-config'
/**
* Proxy API route for getting election results
* Forwards GET requests to the backend API
*/
export async function GET(request: NextRequest) {
try {
const backendUrl = getBackendUrl()
const searchParams = request.nextUrl.searchParams
const electionId = searchParams.get('election_id')
if (!electionId) {
return NextResponse.json({ detail: 'election_id is required' }, { status: 400 })
}
const token = request.headers.get('Authorization')
const headers: Record<string, string> = { 'Content-Type': 'application/json' }
if (token) {
headers['Authorization'] = token
}
const response = await fetch(`${backendUrl}/api/votes/results?election_id=${electionId}`, {
method: 'GET',
headers,
})
const data = await response.json()
return NextResponse.json(data, { status: response.status })
} catch (error) {
console.error('[Results]', error)
const msg = error instanceof Error ? error.message : 'Unknown error'
return NextResponse.json({ detail: msg }, { status: 500 })
}
}

View File

@ -1,8 +1,9 @@
import { NextRequest, NextResponse } from 'next/server'
import { getBackendUrl } from '@/lib/api-config'
export async function GET(request: NextRequest) {
try {
const backendUrl = process.env.BACKEND_URL || 'http://nginx:8000'
const backendUrl = getBackendUrl()
const searchParams = request.nextUrl.searchParams
const url = new URL('/api/votes', backendUrl)
searchParams.forEach((value, key) => url.searchParams.append(key, value))

View File

@ -1,8 +1,9 @@
import { NextRequest, NextResponse } from 'next/server'
import { getBackendUrl } from '@/lib/api-config'
export async function POST(request: NextRequest) {
try {
const backendUrl = process.env.BACKEND_URL || 'http://nginx:8000'
const backendUrl = getBackendUrl()
const searchParams = request.nextUrl.searchParams
const url = new URL('/api/votes/setup', backendUrl)
searchParams.forEach((value, key) => url.searchParams.append(key, value))

View File

@ -1,8 +1,9 @@
import { NextRequest, NextResponse } from 'next/server'
import { getBackendUrl } from '@/lib/api-config'
export async function POST(request: NextRequest) {
try {
const backendUrl = process.env.BACKEND_URL || 'http://nginx:8000'
const backendUrl = getBackendUrl()
const body = await request.json()
const headers: HeadersInit = { 'Content-Type': 'application/json' }
const authHeader = request.headers.get('authorization')

View File

@ -0,0 +1,40 @@
import { NextRequest, NextResponse } from 'next/server'
import { getBackendUrl } from '@/lib/api-config'
/**
* Proxy API route for getting transaction status
* Forwards GET requests to the backend API
*/
export async function GET(request: NextRequest) {
try {
const backendUrl = getBackendUrl()
const searchParams = request.nextUrl.searchParams
const transactionId = searchParams.get('transaction_id')
const electionId = searchParams.get('election_id')
if (!transactionId || !electionId) {
return NextResponse.json({ detail: 'transaction_id and election_id are required' }, { status: 400 })
}
const token = request.headers.get('Authorization')
const headers: Record<string, string> = { 'Content-Type': 'application/json' }
if (token) {
headers['Authorization'] = token
}
const response = await fetch(
`${backendUrl}/api/votes/transaction-status?transaction_id=${transactionId}&election_id=${electionId}`,
{
method: 'GET',
headers,
}
)
const data = await response.json()
return NextResponse.json(data, { status: response.status })
} catch (error) {
console.error('[TransactionStatus]', error)
const msg = error instanceof Error ? error.message : 'Unknown error'
return NextResponse.json({ detail: msg }, { status: 500 })
}
}

View File

@ -1,8 +1,9 @@
import { NextRequest, NextResponse } from 'next/server'
import { getBackendUrl } from '@/lib/api-config'
export async function POST(request: NextRequest) {
try {
const backendUrl = process.env.BACKEND_URL || 'http://nginx:8000'
const backendUrl = getBackendUrl()
const searchParams = request.nextUrl.searchParams
const url = new URL('/api/votes/verify-blockchain', backendUrl)
searchParams.forEach((value, key) => url.searchParams.append(key, value))

View File

@ -251,6 +251,19 @@ export const votesApi = {
async getTransactionStatus(transactionId: string, electionId: number) {
return apiRequest<any>(`/api/votes/transaction-status?transaction_id=${transactionId}&election_id=${electionId}`)
},
async getPublicKeys(electionId: number) {
return apiRequest<any>(`/api/votes/public-keys?election_id=${electionId}`, {
skipAuth: true,
})
},
async setupElection(electionId: number) {
return apiRequest<any>("/api/votes/setup", {
method: "POST",
body: JSON.stringify({ election_id: electionId }),
})
},
}
/**