Skip to content
This repository has been archived by the owner on Mar 11, 2024. It is now read-only.

Commit

Permalink
refactor(client): Fixes more types
Browse files Browse the repository at this point in the history
  • Loading branch information
eluciano11 committed Dec 11, 2023
1 parent 2de3d67 commit 47c68d3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 28 deletions.
47 changes: 21 additions & 26 deletions src/packages/practica/strategies/mixed-vote-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,34 +128,29 @@ export class MixedVoteStrategy implements VoteUpdateInterface {
vote.position.row === PARTY_ROW
)

if (voteForParty) {
const columnForParty = getColumnForParty(
ballot,
voteForParty as VoteEvent
)
const candidate = columnForParty.find((item, index) => {
const isInElectivePosition = index >= start && index <= end
const receivesImplicitVote =
item instanceof Candidate && item.receivesImpicitVote
const hasVote = filteredVotes.find(
(vote) =>
vote.position.row === index &&
vote.position.column === voteForParty.position.column
)

return isInElectivePosition && receivesImplicitVote && !hasVote
}) as Candidate
const index = columnForParty.findIndex(
(item) => item.id === candidate.id
)
const vote = new Vote(
{ column: voteForParty.position.column, row: index },
Selection.selectedImplicitly,
candidate
if (voteForParty == null) return

const columnForParty = getColumnForParty(ballot, voteForParty)
const candidate = columnForParty.find((item, index) => {
const isInElectivePosition = index >= start && index <= end
const receivesImplicitVote =
item instanceof Candidate && item.receivesImpicitVote
const hasVote = filteredVotes.find(
(vote) =>
vote.position.row === index &&
vote.position.column === voteForParty.position.column
)

return [...filteredVotes, vote]
}
return isInElectivePosition && receivesImplicitVote && !hasVote
}) as Candidate
const index = columnForParty.findIndex((item) => item.id === candidate.id)
const vote = new Vote(
{ column: voteForParty.position.column, row: index },
Selection.selectedImplicitly,
candidate
)

return [...filteredVotes, vote]
}

return filteredVotes
Expand Down
26 changes: 24 additions & 2 deletions src/packages/practica/strategies/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,37 @@ export function getElectivePositionForVote(
return ElectivePosition.atLargeSenator
}

function getStatePositions(position: string) {
if (
position !== ElectivePosition.governor &&
position !== ElectivePosition.commissionerResident
)
throw Error("Unsuppported elective position")

return BallotPositions.state[position]
}

function getLegislativePositions(position: string) {
if (
position !== ElectivePosition.atLargeRepresentative &&
position !== ElectivePosition.atLargeSenator &&
position !== ElectivePosition.districtRepresentative &&
position !== ElectivePosition.districtSenators
)
throw Error("Unsuppported elective position")

return BallotPositions.legislative[position]
}

export function getStartAndEndPositionsForBallot(
ballot: BallotConfigs,
ballotType: BallotType,
electivePosition: ElectivePosition
) {
if (ballotType === BallotType.state) {
return BallotPositions.state[electivePosition]
return getStatePositions(electivePosition)
} else if (ballotType === BallotType.legislative) {
return BallotPositions.legislative[electivePosition]
return getLegislativePositions(electivePosition)
}

if (electivePosition === ElectivePosition.mayor) {
Expand Down

0 comments on commit 47c68d3

Please sign in to comment.