Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions packages/admin-portal/src/resources/Tally/TallyCeremonyTrustees.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ import {useGetList, useGetOne, useRecordContext} from "react-admin"
import {WizardStyles} from "@/components/styles/WizardStyles"
import {RESTORE_PRIVATE_KEY} from "@/queries/RestorePrivateKey"
import {useMutation} from "@apollo/client"
import {
ICeremonyStatus,
ITallyExecutionStatus,
ITallyTrusteeStatus,
ITrusteeStatus,
} from "@/types/ceremonies"
import {ITallyCeremonyStatus, ITallyTrusteeStatus} from "@/types/ceremonies"
import {Box} from "@mui/material"
import {
RestorePrivateKeyMutation,
Expand All @@ -34,6 +29,7 @@ import {
import {AuthContext} from "@/providers/AuthContextProvider"
import {useTenantStore} from "@/providers/TenantContextProvider"
import {SettingsContext} from "@/providers/SettingsContextProvider"
import {canTrusteeRestorePrivateKey} from "./utils"

const WizardSteps = {
Start: 0,
Expand All @@ -48,14 +44,14 @@ export const TallyCeremonyTrustees: React.FC = () => {
const [tenantId] = useTenantStore()
const authContext = useContext(AuthContext)

const [page, setPage] = useState<number>(WizardSteps.Start)
const [page, setPage] = useState<number>(WizardSteps.Status)
const [selectedElections, setSelectedElections] = useState<string[]>([])
const [selectedTrustees, setSelectedTrustees] = useState<boolean>(false)
const [tally, setTally] = useState<Sequent_Backend_Tally_Session>()
const [verified, setVerified] = useState<boolean>(false)
const [uploading, setUploading] = useState<boolean>(false)
const [errors, setErrors] = useState<String | null>(null)
const [trusteeStatus, setTrusteeStatus] = useState<ITrusteeStatus | null>(null)
const [trusteeStatus, setTrusteeStatus] = useState<ITallyTrusteeStatus | null>(null)
const {globalSettings} = useContext(SettingsContext)
const [isTallyCompleted, setIsTallyCompleted] = useState<boolean>(false)

Expand Down Expand Up @@ -122,25 +118,23 @@ export const TallyCeremonyTrustees: React.FC = () => {

useEffect(() => {
if (tallySessionExecutions) {
const username = authContext?.username
const ceremonyStatus: ICeremonyStatus | undefined = tallySessionExecutions?.[0]?.status
const trusteeName = authContext?.trustee
const ceremonyStatus: ITallyCeremonyStatus | undefined =
tallySessionExecutions?.[0]?.status
const trusteeStatus = ceremonyStatus?.trustees.find(
(item) => item.name === username
(item) => item.name === trusteeName
)?.status
setTrusteeStatus(trusteeStatus ?? null)
}
}, [tallySessionExecutions])
}, [authContext?.trustee, tallySessionExecutions])

useEffect(() => {
setPage(
!trusteeStatus && tally?.execution_status !== ITallyExecutionStatus.CANCELLED
canTrusteeRestorePrivateKey(trusteeStatus, tally?.execution_status)
? WizardSteps.Start
: trusteeStatus === ITrusteeStatus.WAITING &&
tally?.execution_status !== ITallyExecutionStatus.CANCELLED
? WizardSteps.Start
: WizardSteps.Status
: WizardSteps.Status
)
}, [trusteeStatus])
}, [tally?.execution_status, trusteeStatus])

const CancelButton = styled(Button)`
background-color: ${({theme}) => theme.palette.white};
Expand Down
39 changes: 38 additions & 1 deletion packages/admin-portal/src/resources/Tally/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,44 @@
//
// SPDX-License-Identifier: AGPL-3.0-only

import {orderItemsByIds} from "./utils"
import {ITallyExecutionStatus, ITallyTrusteeStatus} from "@/types/ceremonies"
import {canTrusteeRestorePrivateKey, orderItemsByIds} from "./utils"

describe("canTrusteeRestorePrivateKey", () => {
it.each([ITallyExecutionStatus.STARTED, ITallyExecutionStatus.CONNECTED])(
"allows a waiting trustee while the tally accepts keys (%s)",
(executionStatus) => {
expect(canTrusteeRestorePrivateKey(ITallyTrusteeStatus.WAITING, executionStatus)).toBe(
true
)
}
)

it.each([
ITallyExecutionStatus.NOT_STARTED,
ITallyExecutionStatus.IN_PROGRESS,
ITallyExecutionStatus.AWAITING_INPUT,
ITallyExecutionStatus.SUCCESS,
ITallyExecutionStatus.CANCELLED,
])("does not allow key restoration while the tally is %s", (executionStatus) => {
expect(canTrusteeRestorePrivateKey(ITallyTrusteeStatus.WAITING, executionStatus)).toBe(
false
)
})

it("does not allow a trustee whose key is already restored", () => {
expect(
canTrusteeRestorePrivateKey(
ITallyTrusteeStatus.KEY_RESTORED,
ITallyExecutionStatus.STARTED
)
).toBe(false)
})

it("does not allow a user who is absent from the tally ceremony", () => {
expect(canTrusteeRestorePrivateKey(null, ITallyExecutionStatus.STARTED)).toBe(false)
})
})

describe("orderItemsByIds", () => {
const items = [
Expand Down
9 changes: 9 additions & 0 deletions packages/admin-portal/src/resources/Tally/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
import {ParsedAnnotations, RunoffStatus} from "./types"
import {Sequent_Backend_Candidate, Sequent_Backend_Contest} from "@/gql/graphql"
import {ICandidate, IContest, ICountingAlgorithm} from "@sequentech/ui-core"
import {ITallyExecutionStatus, ITallyTrusteeStatus} from "@/types/ceremonies"

export const canTrusteeRestorePrivateKey = (
trusteeStatus: ITallyTrusteeStatus | null,
tallyExecutionStatus: string | null | undefined
): boolean =>
trusteeStatus === ITallyTrusteeStatus.WAITING &&
(tallyExecutionStatus === ITallyExecutionStatus.STARTED ||
tallyExecutionStatus === ITallyExecutionStatus.CONNECTED)

/**
* Safely extracts the value from a GraphQL 'Maybe<T>' type.
Expand Down
Loading