|
| 1 | +const Candidate = require('./Candidate'); |
| 2 | + |
| 3 | +/** |
| 4 | + * SB Government Class |
| 5 | + */ |
| 6 | +class GovernmentData { |
| 7 | + /** |
| 8 | + * constructor |
| 9 | + * @param {Object} data |
| 10 | + */ |
| 11 | + constructor(data) { |
| 12 | + /** |
| 13 | + * Last time this resource was updated |
| 14 | + * @type {number} |
| 15 | + */ |
| 16 | + this.lastUpdatedTimestamp = parseInt(data.lastUpdated, 10); |
| 17 | + /** |
| 18 | + * Last time this resource was updated, as Date |
| 19 | + * @type {Date|null} |
| 20 | + */ |
| 21 | + this.lastUpdatedAt = new Date(this.lastUpdatedTimestamp); |
| 22 | + const lastElectionResults = data.mayor.election.candidates.map((x) => new Candidate(x, x.name === data.mayor.name)); |
| 23 | + /** |
| 24 | + * A map of last election results for each candidate |
| 25 | + * Sorted ascendingly by votes received |
| 26 | + * @type {Map<string, Candidate>} |
| 27 | + */ |
| 28 | + this.lastElectionResults = new Map(lastElectionResults.sort((a, b) => a.votes - b.votes).map((x) => [x.name, x])); |
| 29 | + /** |
| 30 | + * The mayor |
| 31 | + * @type {Candidate} |
| 32 | + */ |
| 33 | + this.mayor = this.lastElectionResults.get(data.mayor.name); |
| 34 | + /** |
| 35 | + * The year the mayor will be running for |
| 36 | + * @type {number} |
| 37 | + */ |
| 38 | + this.runningYear = parseInt(data.mayor.election.year, 10) || 0; |
| 39 | + const thisElection = data.current.candidates.map((x) => new Candidate(x, x.name === data.mayor.name)); |
| 40 | + /** |
| 41 | + * Current elections, valid for next year |
| 42 | + * Sorted ascendingly by votes received |
| 43 | + * RESULTS MIGHT BE TEMPORARY |
| 44 | + * @type {Map<string, Candidate>} |
| 45 | + */ |
| 46 | + this.currentElectionResults = new Map(thisElection.sort((a, b) => a.votes - b.votes).map((x) => [x.name, x])); |
| 47 | + /** |
| 48 | + * The year the current election will be effective for |
| 49 | + * @type {number|null} |
| 50 | + */ |
| 51 | + this.currentElectionFor = parseInt(data.current.year, 10) || null; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +module.exports = GovernmentData; |
0 commit comments