models/communities/Poll.js

import PollOption from './PollOption.js';
import UserVotes from './UserVotes.js';

export default class Poll {
    /**
    * Creates a new Poll instance from the provider parameters.
    *
    * @constructor Poll
    * @param {Object} pollMap - Poll parameters
    * @param {boolean} pollMap.allowMultipleVotes - True if user can vote multiple options
    * @param {number} [pollMap.endDate] - Poll end date
    * @param {PollOption[]} [pollMap.options=[]] - Poll options
    * @param {number} [pollMap.totalVotes=0] - Total amount of votes
    * @param {UserVotes[]} [pollMap.voters=[]] - Users who voted
    */
    constructor(pollMap) {
        this.allowMultipleVotes = pollMap.allowMultipleVotes === true;
        this.endDate = pollMap.endDate;
        this.options = [];
        this.totalVotes = pollMap.totalVotes || 0;
        this.voters = [];

        const rawVoters = pollMap.knownVoters;
        if (rawVoters !== undefined && rawVoters != null) {
            rawVoters.forEach((voter) => {
                const userVotes = new UserVotes(voter);
                this.voters.push(userVotes);
            });
        }

        const rawOptions = pollMap.options;
        if (rawOptions !== undefined && rawOptions != null) {
            rawOptions.forEach((pollOptionMap) => {
                const pollOption = new PollOption(pollOptionMap);
                this.options.push(pollOption);
            });
        }

        Object.freeze(this);
    }
}