-
Notifications
You must be signed in to change notification settings - Fork 6
/
published.ts
238 lines (202 loc) · 6 KB
/
published.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import { Election, IElectionParameters, IElectionType, IVoteType } from './election';
import { MultiLanguage } from '../../util/lang';
import { ElectionResultsType, ElectionResultsTypeNames, IQuestion } from '../metadata';
import { PublishedCensus } from '../census';
import { Vote } from '../vote';
import { MultiChoiceElection } from './multichoice';
import { BudgetElection } from './budget';
import { ApprovalElection } from './approval';
import { QuadraticElection } from './quadratic';
export enum ElectionStatus {
PROCESS_UNKNOWN = 'PROCESS_UNKNOWN',
UPCOMING = 'UPCOMING',
ONGOING = 'ONGOING',
ENDED = 'ENDED',
CANCELED = 'CANCELED',
PAUSED = 'PAUSED',
RESULTS = 'RESULTS',
}
export enum ElectionStatusReady {
READY = 'READY',
}
export type AllElectionStatus = ElectionStatus | ElectionStatusReady;
export interface IPublishedElectionParameters extends IElectionParameters {
id: string;
organizationId: string;
status: AllElectionStatus;
voteCount: number;
finalResults: boolean;
results: Array<Array<string>>;
manuallyEnded: boolean;
chainId: string;
creationTime: string;
metadataURL: string;
resultsType: ElectionResultsType;
raw: object;
}
/**
* Represents a published election
*/
export class PublishedElection extends Election {
private readonly _id: string;
private readonly _organizationId: string;
private readonly _status: ElectionStatus;
private readonly _voteCount: number;
private readonly _finalResults: boolean;
private readonly _manuallyEnded: boolean;
private readonly _chainId: string;
private readonly _results: Array<Array<string>>;
private readonly _creationTime: Date;
private readonly _metadataURL: string;
private readonly _resultsType: ElectionResultsType;
private readonly _raw: object;
/**
* Constructs a published election
*
* @param params - Election parameters
*/
public constructor(params: IPublishedElectionParameters) {
super({
title: params.title,
description: params.description,
header: params.header,
streamUri: params.streamUri,
meta: params.meta,
startDate: params.startDate,
endDate: params.endDate,
electionType: params.electionType,
voteType: params.voteType,
questions: params.questions,
census: params.census,
maxCensusSize: params.maxCensusSize,
});
this._id = params.id;
this._organizationId = params.organizationId;
this._status = PublishedElection.getStatus(params.status, this.startDate);
this._voteCount = params.voteCount;
this._finalResults = params.finalResults;
this._results = params.results;
this._manuallyEnded = params.manuallyEnded;
this._chainId = params.chainId;
this._creationTime = new Date(params.creationTime);
this._metadataURL = params.metadataURL;
this._resultsType = params.resultsType;
this._raw = params.raw;
}
/**
* Returns a published election object
*
* @param params - Published election parameters
*/
public static build(params: IPublishedElectionParameters) {
return new PublishedElection(params);
}
public static getStatus(status: AllElectionStatus, startDate: Date): ElectionStatus {
switch (status) {
case ElectionStatusReady.READY:
return startDate <= new Date() ? ElectionStatus.ONGOING : ElectionStatus.UPCOMING;
default:
return status;
}
}
public checkVote(vote: Vote): void {
switch (this.resultsType?.name) {
case ElectionResultsTypeNames.MULTIPLE_CHOICE:
return MultiChoiceElection.checkVote(vote, this.voteType, this.resultsType.properties);
case ElectionResultsTypeNames.APPROVAL:
return ApprovalElection.checkVote(vote, this.voteType);
case ElectionResultsTypeNames.BUDGET:
return BudgetElection.checkVote(vote, this.resultsType, this.voteType);
case ElectionResultsTypeNames.QUADRATIC:
return QuadraticElection.checkVote(vote, this.resultsType, this.voteType);
case ElectionResultsTypeNames.SINGLE_CHOICE_MULTIQUESTION:
default:
return PublishedElection.checkVote(vote, this.voteType);
}
}
public static checkVote(vote: Vote, voteType: IVoteType): void {
if (voteType.uniqueChoices && new Set(vote.votes).size !== vote.votes.length) {
throw new Error('Choices are not unique');
}
if (voteType.maxCount < vote.votes.length) {
throw new Error('Invalid number of choices');
}
vote.votes.forEach((vote) => {
if (vote > voteType.maxValue) {
throw new Error('Invalid choice value');
}
});
}
get title(): MultiLanguage<string> {
return super.title;
}
get description(): MultiLanguage<string> {
return super.description;
}
get header(): string {
return super.header;
}
get streamUri(): string {
return super.streamUri;
}
get startDate(): Date {
return super.startDate;
}
get endDate(): Date {
return super.endDate;
}
get electionType(): IElectionType {
return super.electionType;
}
get voteType(): IVoteType {
return super.voteType;
}
get questions(): IQuestion[] {
return super.questions;
}
get census(): PublishedCensus {
return super.census;
}
get maxCensusSize(): number {
return super.maxCensusSize;
}
get id(): string {
return this._id;
}
get organizationId(): string {
return this._organizationId;
}
get status(): ElectionStatus {
return this._status;
}
get voteCount(): number {
return this._voteCount;
}
get finalResults(): boolean {
return this._finalResults;
}
get results(): Array<Array<string>> {
return this._results;
}
get manuallyEnded(): boolean {
return this._manuallyEnded;
}
get chainId(): string {
return this._chainId;
}
get creationTime(): Date {
return this._creationTime;
}
get metadataURL(): string {
return this._metadataURL;
}
get resultsType(): ElectionResultsType {
return this._resultsType;
}
get raw(): object {
return this._raw;
}
get isValid(): boolean {
return true;
}
}