Files
web-nuxt/server/api/cool/vote.post.ts
T
Noel Sabia 2bc52a314d
ci / check (push) Successful in 55s
feat: build the Cool City public route demo
- add /cool, /cool?vote=1, /cool/stage and /cool/city on the existing Nuxt/Onyx stack
- wire real route/shade metrics and OSM geometry from the route bundle; city-gap data for the city view
- show shade % and direct-sun minutes together; recommend Accessible for Ruth with the 15% ramp trade-off
- add in-memory time/scene/round state and one anonymous vote per session, polled every 2 s
- keep the retail module linked but out of the Cool City story
2026-11-14 13:49:22 +01:00

42 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { isCoolRouteId, isCoolRound } from '../../../shared/cool'
import { getCoolState, isValidCoolSessionId, recordCoolVote } from '../../utils/cool-votes'
interface VoteBody {
route?: unknown
sessionId?: unknown
round?: unknown
}
export default defineEventHandler(async (event) => {
setHeader(event, 'cache-control', 'no-store')
const body = await readBody<VoteBody>(event)
const round = body?.round ?? 'preselection'
if (!isCoolRound(round)) {
throw createError({ statusCode: 400, statusMessage: 'Invalid round. Expected preselection | final.' })
}
if (!isCoolRouteId(body?.route)) {
throw createError({
statusCode: 400,
statusMessage: 'Invalid route. Expected shortest | shaded | accessible.',
})
}
if (!isValidCoolSessionId(body?.sessionId)) {
throw createError({
statusCode: 400,
statusMessage: 'Invalid session. Expected an anonymous 8–80 character ID.',
})
}
if (!getCoolState(round).voteOpen) {
throw createError({ statusCode: 409, statusMessage: 'Voting is not open.' })
}
const result = recordCoolVote(round, body.route, body.sessionId)
setResponseStatus(event, result.duplicate ? 200 : 201)
return {
yourRoute: result.route,
duplicate: result.duplicate,
tally: result.tally,
}
})