/trustee: Fix accessing partials on the frontned

This commit is contained in:
David Ernst
2025-12-12 05:33:13 -07:00
parent 3ae5b3193c
commit d81bf41353
3 changed files with 19 additions and 9 deletions

View File

@@ -5,7 +5,7 @@ import { PartialWithProof } from 'src/trustee/trustee-state'
import { transform_email_keys } from './commafy'
export type TrusteesLatestPartials = {
trustees: Array<{ email: string; index: number; partials?: Record<string, PartialWithProof[]> }>
trustees: Array<{ email: string; index: number; partials?: Record<string, { partials: PartialWithProof[] }> }>
}
export default async (req: NextApiRequest, res: NextApiResponse) => {
@@ -25,8 +25,8 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
// Get partials from separate sub-docs
const partialDocs = await doc.ref.collection('partials').get()
const partials = {} as Record<string, PartialWithProof[]>
partialDocs.docs.forEach((doc) => (partials[doc.id] = doc.data() as PartialWithProof[]))
const partials = {} as Record<string, { partials: PartialWithProof[] }>
partialDocs.docs.forEach((doc) => (partials[doc.id] = doc.data() as { partials: PartialWithProof[] }))
const public_data: Record<string, unknown> = {
email: trusteeData.email,

View File

@@ -152,7 +152,8 @@ export const VotesToDecrypt = ({
<h3>IV. Votes to Decrypt</h3>
<ol className="pl-5">
{trustees?.map(({ email, you }) => {
const partials = partialsByEmail[email]
const partials = partialsByEmail[email] || {}
return (
<li className="mb-8" key={email}>
{/* Top row */}
@@ -160,7 +161,7 @@ export const VotesToDecrypt = ({
{/* Left */}
<span>
{email}
{you && <YouLabel />} partially decrypted {!partials ? 0 : Object.values(partials)[0]?.length}
{you && <YouLabel />} partially decrypted {!partials ? 0 : Object.values(partials)[0]?.length || 0}
&nbsp;votes.
</span>
{/* Right */}

View File

@@ -41,10 +41,19 @@ export function useLatestPartials(election_id?: string) {
}, [election_id, mutate])
const partialsByEmail =
data?.trustees?.reduce<Record<string, Record<string, PartialWithProof[]> | undefined>>(
(memo, trustee) => ({ ...memo, [trustee.email]: trustee.partials }),
{},
) || {}
data?.trustees?.reduce<Record<string, Record<string, PartialWithProof[]> | undefined>>((memo, trustee) => {
if (!trustee.partials) return memo
// Transform from Record<string, { partials: PartialWithProof[] }> to Record<string, PartialWithProof[]>
// by unwrapping the { partials: [...] } structure
const unwrappedPartials: Record<string, PartialWithProof[]> = {}
Object.entries(trustee.partials).forEach(([column, wrapped]) => {
if (wrapped && typeof wrapped === 'object' && 'partials' in wrapped) {
unwrappedPartials[column] = (wrapped as { partials: PartialWithProof[] }).partials
}
})
return { ...memo, [trustee.email]: unwrappedPartials }
}, {}) || {}
return { partialsByEmail }
}