forked from vocdoni/vocdoni-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelection.ts
119 lines (103 loc) · 3.04 KB
/
election.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
import { IQuestion } from '../metadata/election';
import { PublishedCensus } from '../census/published';
import { PlainCensus } from '../census/plain';
import { WeightedCensus } from '../census/weighted';
import { MultiLanguage } from '../../util/lang';
import { UnpublishedElection } from './unpublished';
export interface IVoteType {
uniqueChoices?: boolean;
maxVoteOverwrites?: number;
costFromWeight?: boolean;
costExponent?: number;
}
export interface IElectionType {
autoStart?: boolean;
interruptible?: boolean;
dynamicCensus?: boolean;
secretUntilTheEnd?: boolean;
anonymous?: boolean;
}
export interface IElectionParameters {
title: string | MultiLanguage<string>;
description: string | MultiLanguage<string>;
header?: string;
streamUri?: string;
startDate?: string | number | Date;
endDate: string | number | Date;
census: PublishedCensus | PlainCensus | WeightedCensus;
voteType?: IVoteType;
electionType?: IElectionType;
questions?: IQuestion[];
}
/**
* Represents an election
*/
export abstract class Election {
protected _title: MultiLanguage<string>;
protected _description: MultiLanguage<string>;
protected _header: string;
protected _streamUri: string;
protected _startDate: Date;
protected _endDate: Date;
protected _electionType: IElectionType;
protected _voteType: IVoteType;
protected _questions: IQuestion[];
protected _census: PublishedCensus | PlainCensus | WeightedCensus;
/**
* Constructs an election
*
* @param params Election parameters
*/
protected constructor(params?: IElectionParameters) {
if (params) {
this._title = typeof params.title === 'string' ? { default: params.title } : params.title;
this._description = typeof params.description === 'string' ? { default: params.description } : params.description;
this._header = params.header;
this._streamUri = params.streamUri;
this._startDate = params.startDate ? new Date(params.startDate) : null;
this._endDate = new Date(params.endDate);
this._electionType = params.electionType;
this._voteType = params.voteType;
this._questions = params.questions ?? [];
this._census = params.census;
}
}
/**
* Returns an unpublished election object
*
* @param params Unpublished Election parameters
*/
public static from(params: IElectionParameters) {
return new UnpublishedElection(params);
}
get title(): MultiLanguage<string> {
return this._title;
}
get description(): MultiLanguage<string> {
return this._description;
}
get header(): string {
return this._header;
}
get streamUri(): string {
return this._streamUri;
}
get startDate(): Date {
return this._startDate;
}
get endDate(): Date {
return this._endDate;
}
get electionType(): IElectionType {
return this._electionType;
}
get voteType(): IVoteType {
return this._voteType;
}
get questions(): IQuestion[] {
return this._questions;
}
get census(): PublishedCensus | PlainCensus | WeightedCensus {
return this._census;
}
}