1
1
import CreateReferendum from '../commands/CreateReferendum' ;
2
+ import ModifyReferendumProposal from '../commands/ModifyReferendumProposal' ;
2
3
import DeleteReferendum from '../commands/DeleteReferendum' ;
3
4
import AuthenticateVoter from "../commands/AuthenticateVoter"
4
5
import OpenPolls from "../commands/OpenPolls"
5
6
import ClosePolls from "../commands/ClosePolls"
6
7
import CastVote from "../commands/CastVote"
7
8
import ReferendumCreated from '../events/ReferendumCreated' ;
9
+ import ReferendumProposalModified from '../events/ReferendumProposalModified' ;
8
10
import ReferendumDeleted from '../events/ReferendumDeleted' ;
9
11
import PollsOpened from '../events/PollsOpened' ;
10
12
import PollsClosed from '../events/PollsClosed' ;
@@ -26,6 +28,9 @@ export default class Referendum {
26
28
if ( evt instanceof ReferendumCreated ) {
27
29
this . _onReferendumCreated ( evt ) ;
28
30
}
31
+ if ( evt instanceof ReferendumProposalModified ) {
32
+ this . _onReferendumProposalModified ( evt ) ;
33
+ }
29
34
if ( evt instanceof PollsOpened ) {
30
35
this . _onPollsOpened ( ) ;
31
36
}
@@ -45,6 +50,10 @@ export default class Referendum {
45
50
this . _options = evt . options
46
51
}
47
52
53
+ _onReferendumProposalModified ( evt ) {
54
+ this . _proposal = evt . proposal ;
55
+ }
56
+
48
57
_onPollsOpened ( ) {
49
58
this . _status = "polls_open" ;
50
59
}
@@ -65,6 +74,9 @@ export default class Referendum {
65
74
if ( command instanceof CreateReferendum ) {
66
75
return this . _CreateReferendum ( command ) ;
67
76
}
77
+ if ( command instanceof ModifyReferendumProposal ) {
78
+ return this . _ModifyReferendumProposal ( command ) ;
79
+ }
68
80
if ( command instanceof DeleteReferendum ) {
69
81
return this . _DeleteReferendum ( command ) ;
70
82
}
@@ -96,25 +108,50 @@ export default class Referendum {
96
108
}
97
109
if ( ! command . proposal ) {
98
110
validationErrors . push ( { "field" : "proposal" , "msg" : "Referendum proposal is a required field." } ) ;
99
- }
111
+ }
100
112
if ( ! command . name ) {
101
113
validationErrors . push ( { "field" : "name" , "msg" : "Referendum name is a required field." } ) ;
102
- }
114
+ }
103
115
if ( ! command . options ) {
104
116
validationErrors . push ( { "field" : "options" , "msg" : "Referendum options are required." } ) ;
105
117
}
106
118
if ( command . options && command . options . length < 2 ) {
107
119
validationErrors . push ( { "field" : "options" , "msg" : "At least two options are required." } ) ;
108
- }
120
+ }
109
121
if ( validationErrors . length > 0 ) {
110
122
throw new errors . ValidationFailed ( validationErrors ) ;
111
- }
123
+ }
112
124
command . options . push ( "None of the above" ) ;
113
125
var result = [ ] ;
114
126
result . push ( new ReferendumCreated ( command . referendumId , command . organizationId , command . name , command . proposal , command . options ) ) ;
115
127
return result ;
116
128
}
117
129
130
+ _ModifyReferendumProposal ( command ) {
131
+ var validationErrors = [ ] ;
132
+
133
+ if ( ! this . _id ) {
134
+ validationErrors . push ( { "field" : "" , "msg" : "Referendum does not exist." } ) ;
135
+ }
136
+ if ( ! command . referendumId ) {
137
+ validationErrors . push ( { "field" : "referendum" , "msg" : "ReferendumId is a required field" } ) ;
138
+ }
139
+ if ( ! command . organizationId ) {
140
+ validationErrors . push ( { "field" : "organization" , "msg" : "organizationId is a required field" } ) ;
141
+ }
142
+ if ( ! command . proposal ) {
143
+ validationErrors . push ( { "field" : "proposal" , "msg" : "proposal is a required field" } ) ;
144
+ }
145
+
146
+ if ( validationErrors . length > 0 ) {
147
+ throw new errors . ValidationFailed ( validationErrors ) ;
148
+ }
149
+
150
+ var result = [ ] ;
151
+ result . push ( new ReferendumProposalModified ( command . referendumId , command . organizationId , command . proposal ) ) ;
152
+ return result ;
153
+ }
154
+
118
155
_DeleteReferendum ( command ) {
119
156
var validationErrors = [ ] ;
120
157
if ( ! this . _id ) {
@@ -131,7 +168,7 @@ export default class Referendum {
131
168
}
132
169
if ( validationErrors . length > 0 ) {
133
170
throw new errors . ValidationFailed ( validationErrors ) ;
134
- }
171
+ }
135
172
var result = [ ] ;
136
173
result . push ( new ReferendumDeleted ( command . referendumId ) ) ;
137
174
return result ;
@@ -141,19 +178,19 @@ export default class Referendum {
141
178
var validationErrors = [ ] ;
142
179
if ( ! this . _id ) {
143
180
validationErrors . push ( { "field" : "" , "msg" : "Referendum does not exist." } )
144
- }
181
+ }
145
182
if ( ! command . referendumId ) {
146
183
validationErrors . push ( { "field" : "referendumId" , "msg" : "Referendum id is a required field." } ) ;
147
184
}
148
185
if ( this . _status === "polls_open" ) {
149
- validationErrors . push ( { "field" : "" , "msg" : "Polls are already open." } )
186
+ validationErrors . push ( { "field" : "" , "msg" : "Polls are already open." } )
150
187
}
151
188
if ( this . _status === "polls_closed" ) {
152
- validationErrors . push ( { "field" : "" , "msg" : "Polls have already been closed." } )
189
+ validationErrors . push ( { "field" : "" , "msg" : "Polls have already been closed." } )
153
190
}
154
191
if ( validationErrors . length > 0 ) {
155
192
throw new errors . ValidationFailed ( validationErrors ) ;
156
- }
193
+ }
157
194
var result = [ ] ;
158
195
result . push ( new PollsOpened ( command . referendumId ) ) ;
159
196
return result ;
@@ -163,16 +200,16 @@ export default class Referendum {
163
200
var validationErrors = [ ] ;
164
201
if ( ! this . _id ) {
165
202
validationErrors . push ( { "field" : "" , "msg" : "Referendum does not exist." } )
166
- }
203
+ }
167
204
if ( ! command . referendumId ) {
168
205
validationErrors . push ( { "field" : "referendumId" , "msg" : "Referendum id is a required field." } ) ;
169
206
}
170
207
if ( ! ( this . _status === "polls_open" ) ) {
171
- validationErrors . push ( { "field" : "referendumId" , "msg" : "Polls are not open." } )
208
+ validationErrors . push ( { "field" : "referendumId" , "msg" : "Polls are not open." } )
172
209
}
173
210
if ( validationErrors . length > 0 ) {
174
211
throw new errors . ValidationFailed ( validationErrors ) ;
175
- }
212
+ }
176
213
var result = [ ] ;
177
214
result . push ( new PollsClosed ( command . referendumId ) ) ;
178
215
return result ;
@@ -187,8 +224,8 @@ export default class Referendum {
187
224
validationErrors . push ( { "field" : "organizationId" , "msg" : "Organization does not exist." } ) ;
188
225
}
189
226
if ( this . _status != "polls_open" ) {
190
- validationErrors . push ( { "field" : "" , "msg" : "Polls are not open." } )
191
- }
227
+ validationErrors . push ( { "field" : "" , "msg" : "Polls are not open." } )
228
+ }
192
229
if ( ! command . voterId ) {
193
230
validationErrors . push ( { "field" : "voterId" , "msg" : "Voter id is a required field." } ) ;
194
231
}
@@ -197,11 +234,11 @@ export default class Referendum {
197
234
validationErrors . push ( { "field" : "voterId" , "msg" : "Voter is not on voter list" } ) ;
198
235
}
199
236
if ( this . _authenticatedVoters . indexOf ( command . voterId ) != - 1 ) {
200
- validationErrors . push ( { "field" : "voterId" , "msg" : "Voter has already voted" } ) ;
237
+ validationErrors . push ( { "field" : "voterId" , "msg" : "Voter has already voted" } ) ;
201
238
}
202
239
if ( validationErrors . length > 0 ) {
203
240
throw new errors . ValidationFailed ( validationErrors ) ;
204
- }
241
+ }
205
242
var result = [ ] ;
206
243
result . push ( new VoterAuthenticated ( command . referendumId , command . organizationId , command . voterId ) ) ;
207
244
return result ;
@@ -213,8 +250,8 @@ export default class Referendum {
213
250
validationErrors . push ( { "field" : "referendumId" , "msg" : "Referendum id is a required field." } ) ;
214
251
}
215
252
if ( this . _status != "polls_open" ) {
216
- validationErrors . push ( { "field" : "" , "msg" : "Polls are not open." } )
217
- }
253
+ validationErrors . push ( { "field" : "" , "msg" : "Polls are not open." } )
254
+ }
218
255
if ( ! command . vote ) {
219
256
validationErrors . push ( { "field" : "vote" , "msg" : "Vote is a required field." } ) ;
220
257
}
0 commit comments