-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RetroFunding R5 API] Batch ballot update endpoint (#486)
* add a post route to update all projects in ballot * update swagger docs
- Loading branch information
Showing
3 changed files
with
222 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...api/v1/retrofunding/rounds/[roundId]/ballots/[ballotCasterAddressOrEns]/projects/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { updateAllProjectsInBallot } from "@/app/api/common/ballots/updateBallotProject"; | ||
import { traceWithUserId } from "@/app/api/v1/apiUtils"; | ||
import { | ||
authenticateApiUser, | ||
getCategoryScope, | ||
validateAddressScope, | ||
} from "@/app/lib/auth/serverAuth"; | ||
import { NextResponse, type NextRequest } from "next/server"; | ||
import { z } from "zod"; | ||
|
||
const ballotPayloadSchema = z.object({ | ||
projects: z.array( | ||
z.object({ | ||
projectId: z.string(), | ||
allocation: z.string(z.number().min(0).max(100)), | ||
impact: z.number(), | ||
}) | ||
), | ||
}); | ||
|
||
export async function POST( | ||
request: NextRequest, | ||
route: { params: { roundId: string; ballotCasterAddressOrEns: string } } | ||
) { | ||
const authResponse = await authenticateApiUser(request); | ||
|
||
if (!authResponse.authenticated) { | ||
return new Response(authResponse.failReason, { status: 401 }); | ||
} | ||
|
||
const { roundId, ballotCasterAddressOrEns } = route.params; | ||
const scopeError = await validateAddressScope( | ||
ballotCasterAddressOrEns, | ||
authResponse | ||
); | ||
if (scopeError) return scopeError; | ||
|
||
return await traceWithUserId(authResponse.userId as string, async () => { | ||
try { | ||
const categoryScope = getCategoryScope(authResponse); | ||
|
||
if (!categoryScope) { | ||
return new Response( | ||
"This user does not have a category scope. Regenerate the JWT token", | ||
{ | ||
status: 401, | ||
} | ||
); | ||
} | ||
|
||
const payload = await request.json(); | ||
const parsedPayload = ballotPayloadSchema.parse(payload); | ||
|
||
const ballot = await updateAllProjectsInBallot( | ||
parsedPayload.projects, | ||
categoryScope, | ||
Number(roundId), | ||
ballotCasterAddressOrEns | ||
); | ||
return NextResponse.json(ballot); | ||
} catch (e: any) { | ||
return new Response("Internal server error: " + e.toString(), { | ||
status: 500, | ||
}); | ||
} | ||
}); | ||
} |