models/promocodes/PromoCode.js

import User from '../communities/User';

export default class PromoCode {
    /**
    * Creates a new PromoCode
    *
    * @constructor PromoCode
    * @param {Object} codeMap - Promo Code parameters
    * @param {string} codeMap.code - Code
    * @param {Object<string, string>} [codeMap.properties] - Properties
    * @param {number} [codeMap.maxClaimCount] - Maximum claims for this code
    * @param {number} [codeMap.startDate] - Date since the code is claimable
    * @param {number} [codeMap.endDate] - Date until the code is claimable
    * @param {User} codeMap.creator - Date until the code is claimable
    * @param {number} [codeMap.claimCount=0] - Amount of times the code was claimed
    * @param {boolean} codeMap.isEnabled - True if code is enabled
    * @param {boolean} codeMap.isClaimable - True if code is claimable
    */
    constructor(codeMap) {
        this.code = codeMap.code;
        this.properties = codeMap.properties || null;
        this.maxClaimCount = codeMap.maxClaimCount || null;
        this.startDate = codeMap.startDate || null;
        this.endDate = codeMap.endDate || null;
        this.creator = codeMap.creator
            ? User.create(codeMap.creator)
            : null;
        this.claimCount = codeMap.claimCount || 0;
        this.isEnabled = codeMap.isEnabled === true;
        this.isClaimable = codeMap.isClaimable === true;

        Object.freeze(this);
    }

    /**
     * Create PromoCode from SIPromoCode
     *
     * @memberof PromoCode
     * @param {Object} promoCode - SIPromoCode
     * @returns {PromoCode} PromoCode instance
     * @ignore
     */
     static create(promoCode) {
        return new PromoCode({
            ...promoCode,
            creator: promoCode.creator.publicUser,
            maxClaimCount: promoCode.maxClaims,
            claimCount: promoCode.numClaims,
            startDate: promoCode.validFrom,
            endDate: promoCode.validUntil,
            isEnabled: promoCode.enabled,
            isClaimable: promoCode.claimable
        });
    }
}