PromoCodes.js

import PromoCode from './models/promocodes/PromoCode.js';
import PromoCodeContent from './models/promocodes/PromoCodeContent.js';
import SDK from './sdk';

import "./thrift/smartinvites_types";

/**
 * @class PromoCodes
 * @hideconstructor
 */
 export default class PromoCodes {
    /**
     * Create a promo code.
     *
     * @memberof PromoCodes
     * @param {PromoCodeContent} content - Promo Code content.
     * @return {Promise<PromoCode>} Promise with created promo code or error.
     */
    static create(content) {
        const request = new CreatePromoCodeRequest({
            code: new SIPromoCode({
                ...content,
                validFrom: content.startDate,
                validUntil: content.endDate
            })
        });

        return SDK.request('createPromoCode', request)
            .then((res) => PromoCode.create(res.code));
    }

    /**
     * Get the promo code entity by its code.
     *
     * @memberof PromoCodes
     * @param {string} code - code.
     * @return {Promise<PromoCode>} - Promise with promo code.
     */
    static get(code) {
        const request = new GetPromoCodeRequest({
            code
        });

        return SDK.request('getPromoCodeV2', request)
            .then((res) => PromoCode.create(res.code));
    }

    /**
     * Claim promo code. Will return an error if promocode is not valid, expired or already claimed.
     *
     * @memberof PromoCodes
     * @param {string} code - Promo code to be claimed.
     * @return {Promise<PromoCode>} Promise with promocode if operation was successful.
     */
    static claim(code) {
        const request = new ClaimPromoCodeRequest({
            code
        });

        return SDK.request('claimPromoCodeV2', request)
            .then((res) => PromoCode.create(res.code));
    }
}