mirror of
https://github.com/siv-org/siv.git
synced 2026-01-09 18:37:57 -05:00
/admin/guarantee: Add backend endpoint for Submit Pledge
This commit is contained in:
36
pages/api/election/[election_id]/admin/fund-guarantee.ts
Normal file
36
pages/api/election/[election_id]/admin/fund-guarantee.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { firebase } from 'api/_services'
|
||||
import { pushover } from 'api/_services'
|
||||
import { checkJwtOwnsElection } from 'api/validate-admin-jwt'
|
||||
import { NextApiRequest, NextApiResponse } from 'next'
|
||||
|
||||
export default async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
const { amount } = req.body
|
||||
const { election_id } = req.query
|
||||
|
||||
if (typeof election_id !== 'string') return res.status(401).json({ error: 'Missing election_id' })
|
||||
|
||||
// Confirm they're a valid admin that created this election
|
||||
const jwt = await checkJwtOwnsElection(req, res, election_id)
|
||||
if (!jwt.valid) return
|
||||
|
||||
// Validate amount
|
||||
const numAmount = typeof amount === 'string' ? Number(amount) : amount
|
||||
if (!numAmount || isNaN(numAmount) || numAmount <= 0) return res.status(400).json({ error: 'Invalid amount' })
|
||||
|
||||
// Store the pledge in a subcollection
|
||||
const electionDoc = firebase.firestore().collection('elections').doc(election_id)
|
||||
await electionDoc.collection('guarantee_pledges').add({
|
||||
amount: numAmount,
|
||||
pledged_at: new Date(),
|
||||
})
|
||||
|
||||
await pushover(
|
||||
'New financial guarantee pledge',
|
||||
`Admin ${jwt.email} pledged $${numAmount} to support vote integrity.
|
||||
|
||||
Election:
|
||||
${election_id}: ${jwt.election_title}`,
|
||||
)
|
||||
|
||||
return res.status(201).json({ amount: numAmount, success: true })
|
||||
}
|
||||
@@ -1,11 +1,16 @@
|
||||
import React, { useState } from 'react'
|
||||
import { OnClickButton } from 'src/_shared/Button'
|
||||
import { api } from 'src/api-helper'
|
||||
|
||||
import { useStored } from '../useStored'
|
||||
|
||||
export const InputFunds = () => {
|
||||
const [amount, setAmount] = useState('')
|
||||
const { election_id } = useStored()
|
||||
|
||||
const handleSubmit = () => {
|
||||
const handleSubmit = async () => {
|
||||
if (!amount || isNaN(Number(amount)) || Number(amount) <= 0) return
|
||||
await api(`election/${election_id}/admin/fund-guarantee`, { amount })
|
||||
alert(`Thank you! You pledged $${amount} to support vote integrity.`)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user