UNPKG

@questlabs/core

Version:
287 lines (286 loc) 15.4 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EntiyCreditTier = void 0; const constants_1 = require("../../utils/constants"); const helperFunctions_1 = require("../../utils/helperFunctions"); class EntiyCreditTier { constructor(questSdk) { this.questSdk = questSdk; } addCreditTierForEntity({ creditsAmount, creditsTierId, creditsTierName, extraCreditsAmount = 0, price, recurringTimePeriod = "ONETIME", entityId, }, options) { try { const { userId, token } = this.questSdk.getUser(); const defaultEntityId = entityId || this.questSdk.getEntityId(); (0, helperFunctions_1.validateParams)({ entityId: defaultEntityId, creditsAmount, price, creditsTierId, creditsTierName }); if (this.questSdk.getEntityAuthenticationToken() === undefined) { (0, helperFunctions_1.validateParams)({ userId, token }); } const url = `/entities/${defaultEntityId}/credits-tiers`; const body = Object.assign({ creditsAmount, extraCreditsAmount, price, creditsTierId, creditsTierName, recurringTimePeriod }, options === null || options === void 0 ? void 0 : options._body); return this.questSdk.makeRequest({ url, method: constants_1.HTTP_METHOD.POST, body, queryParams: options === null || options === void 0 ? void 0 : options._queryParams, headers: options === null || options === void 0 ? void 0 : options._headers, }); } catch (error) { this.questSdk.captureSentryException(error); throw error; } } updateCreditTierForEntity({ creditsTierId, creditsAmount, creditsTierName, extraCreditsAmount, price, recurringTimePeriod, entityId, }, options) { try { const { userId, token } = this.questSdk.getUser(); const defaultEntityId = entityId || this.questSdk.getEntityId(); (0, helperFunctions_1.validateParams)({ entityId: defaultEntityId, creditsTierId }); if (this.questSdk.getEntityAuthenticationToken() === undefined) { (0, helperFunctions_1.validateParams)({ userId, token }); } const url = `/entities/${defaultEntityId}/credits-tiers`; const body = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ creditsTierId }, (creditsAmount && { creditsAmount })), (extraCreditsAmount && { extraCreditsAmount })), (price && { price })), (creditsTierName && { creditsTierName })), (recurringTimePeriod && { recurringTimePeriod })), options === null || options === void 0 ? void 0 : options._body); if (Object.keys(body).length <= 1) { throw new Error("Invalid request. Atleast one parameter is required to update"); } return this.questSdk.makeRequest({ url, method: constants_1.HTTP_METHOD.PATCH, body, queryParams: options === null || options === void 0 ? void 0 : options._queryParams, headers: options === null || options === void 0 ? void 0 : options._headers, }); } catch (error) { this.questSdk.captureSentryException(error); throw error; } } getCreditTiersForEntity() { return __awaiter(this, arguments, void 0, function* ({ entityId } = {}, options) { try { const { userId, token } = this.questSdk.getUser(); const defaultEntityId = entityId || this.questSdk.getEntityId(); (0, helperFunctions_1.validateParams)({ entityId: defaultEntityId, userId, token }); const url = `/entities/${defaultEntityId}/credits-tiers`; return this.questSdk.makeRequest({ url, method: constants_1.HTTP_METHOD.GET, queryParams: options === null || options === void 0 ? void 0 : options._queryParams, headers: options === null || options === void 0 ? void 0 : options._headers, }); } catch (error) { this.questSdk.captureSentryException(error); throw error; } }); } getCreditInfoForUser() { return __awaiter(this, arguments, void 0, function* ({ entityId, forUserId } = {}, options) { try { const { userId, token } = this.questSdk.getUser(); const defaultEntityId = entityId || this.questSdk.getEntityId(); (0, helperFunctions_1.validateParams)({ entityId: defaultEntityId, userId, token }); const url = `/users/${forUserId || userId}/credits`; const queryParams = Object.assign({ entityId: defaultEntityId }, options === null || options === void 0 ? void 0 : options._queryParams); return this.questSdk.makeRequest({ url, method: constants_1.HTTP_METHOD.GET, queryParams, headers: options === null || options === void 0 ? void 0 : options._headers, }); } catch (error) { this.questSdk.captureSentryException(error); throw error; } }); } getCreditInfoForEntity() { return __awaiter(this, arguments, void 0, function* ({ entityId } = {}, options) { try { const { userId, token } = this.questSdk.getUser(); const defaultEntityId = entityId || this.questSdk.getEntityId(); (0, helperFunctions_1.validateParams)({ entityId: defaultEntityId, userId, token }); const url = `/entities/${defaultEntityId}/credits`; return this.questSdk.makeRequest({ url, method: constants_1.HTTP_METHOD.GET, queryParams: options === null || options === void 0 ? void 0 : options._queryParams, headers: options === null || options === void 0 ? void 0 : options._headers, }); } catch (error) { this.questSdk.captureSentryException(error); throw error; } }); } buyCreditsForUser(_a, options_1) { return __awaiter(this, arguments, void 0, function* ({ creditsTierId, intentId, forUserId, entityId }, options) { try { const { userId, token } = this.questSdk.getUser(); const defaultEntityId = entityId || this.questSdk.getEntityId(); (0, helperFunctions_1.validateParams)({ creditsTierId, intentId, userId, token, entityId: defaultEntityId }); const url = `/users/${forUserId || userId}/buy-credits`; const queryParams = Object.assign({ entityId: defaultEntityId }, options === null || options === void 0 ? void 0 : options._body); const body = Object.assign({ creditsTierId, intentId }, options === null || options === void 0 ? void 0 : options._body); return this.questSdk.makeRequest({ url, method: constants_1.HTTP_METHOD.POST, queryParams, body, headers: options === null || options === void 0 ? void 0 : options._headers, }); } catch (error) { this.questSdk.captureSentryException(error); throw error; } }); } buyCreditsForEntity(_a, options_1) { return __awaiter(this, arguments, void 0, function* ({ creditsTierId, intentId, entityId }, options) { try { const { userId, token } = this.questSdk.getUser(); const defaultEntityId = entityId || this.questSdk.getEntityId(); (0, helperFunctions_1.validateParams)({ creditsTierId, intentId, entityId: defaultEntityId, userId }); if (this.questSdk.getEntityAuthenticationToken() === undefined) { (0, helperFunctions_1.validateParams)({ token }); } const url = `/entities/${defaultEntityId}/buy-credits`; const queryParams = Object.assign({ userId }, options === null || options === void 0 ? void 0 : options._body); const body = Object.assign({ creditsTierId, intentId }, options === null || options === void 0 ? void 0 : options._body); return this.questSdk.makeRequest({ url, method: constants_1.HTTP_METHOD.POST, queryParams, body, headers: options === null || options === void 0 ? void 0 : options._headers, }); } catch (error) { this.questSdk.captureSentryException(error); throw error; } }); } decrementCreditsForUser(_a, options_1) { return __awaiter(this, arguments, void 0, function* ({ entityId, creditsAmount, forUserId }, options) { try { const { userId, token } = this.questSdk.getUser(); const defaultEntityId = entityId || this.questSdk.getEntityId(); (0, helperFunctions_1.validateParams)({ entityId: defaultEntityId, creditsAmount }); if (this.questSdk.getEntityAuthenticationToken() === undefined) { (0, helperFunctions_1.validateParams)({ userId, token }); } if (!forUserId && !userId) { throw new Error("Invalid request. Either login or pass forUserId"); } const url = `/users/${forUserId || userId}/decrement-credits`; const queryParams = Object.assign({ entityId: defaultEntityId }, options === null || options === void 0 ? void 0 : options._queryParams); const body = Object.assign({ creditsAmount }, options === null || options === void 0 ? void 0 : options._body); return this.questSdk.makeRequest({ url, method: constants_1.HTTP_METHOD.POST, queryParams, body, headers: options === null || options === void 0 ? void 0 : options._headers, }); } catch (error) { this.questSdk.captureSentryException(error); throw error; } }); } decrementCreditsForEntity(_a, options_1) { return __awaiter(this, arguments, void 0, function* ({ creditsAmount, entityId }, options) { try { const { userId, token } = this.questSdk.getUser(); const defaultEntityId = entityId || this.questSdk.getEntityId(); (0, helperFunctions_1.validateParams)({ entityId: defaultEntityId, creditsAmount, userId }); if (this.questSdk.getEntityAuthenticationToken() === undefined) { (0, helperFunctions_1.validateParams)({ token }); } const url = `/entities/${defaultEntityId}/decrement-credits`; const queryParams = Object.assign({ userId }, options === null || options === void 0 ? void 0 : options._queryParams); const body = Object.assign({ creditsAmount }, options === null || options === void 0 ? void 0 : options._body); return this.questSdk.makeRequest({ url, method: constants_1.HTTP_METHOD.POST, queryParams, body, headers: options === null || options === void 0 ? void 0 : options._headers, }); } catch (error) { this.questSdk.captureSentryException(error); throw error; } }); } createPaymentIntentForEntity(_a, options_1) { return __awaiter(this, arguments, void 0, function* ({ cardData, intentData, entityId }, options) { try { const { userId, token } = this.questSdk.getUser(); const defaultEntityId = entityId || this.questSdk.getEntityId(); (0, helperFunctions_1.validateParams)({ entityId: defaultEntityId, userId, token, cardData, intentData }); const url = `/entities/${defaultEntityId}/users/${userId}/create-payment-intent`; const body = Object.assign({ cardData, intentData, forEntityId: defaultEntityId }, options === null || options === void 0 ? void 0 : options._body); return this.questSdk.makeRequest({ url, method: constants_1.HTTP_METHOD.POST, queryParams: options === null || options === void 0 ? void 0 : options._queryParams, headers: options === null || options === void 0 ? void 0 : options._headers, body, }); } catch (error) { this.questSdk.captureSentryException(error); throw error; } }); } approvePaymentForEntity(_a, options_1) { return __awaiter(this, arguments, void 0, function* ({ entityId, intentId }, options) { try { const { userId, token } = this.questSdk.getUser(); const defaultEntityId = entityId || this.questSdk.getEntityId(); (0, helperFunctions_1.validateParams)({ userId, token, entityId: defaultEntityId, intentId }); const url = `/entities/${defaultEntityId}/approve-payment`; const body = Object.assign({ intentId }, options === null || options === void 0 ? void 0 : options._body); return this.questSdk.makeRequest({ url, method: constants_1.HTTP_METHOD.POST, queryParams: options === null || options === void 0 ? void 0 : options._queryParams, body, headers: options === null || options === void 0 ? void 0 : options._headers, }); } catch (error) { this.questSdk.captureSentryException(error); throw error; } }); } } exports.EntiyCreditTier = EntiyCreditTier;