UNPKG

@questlabs/core

Version:
80 lines (79 loc) 4.91 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.Campaign = void 0; const config_1 = require("../../utils/config"); const constants_1 = require("../../utils/constants"); const helperFunctions_1 = require("../../utils/helperFunctions"); class Campaign { constructor(questSdk) { this.questSdk = questSdk; } getTextCampaign(_a, options_1) { return __awaiter(this, arguments, void 0, function* ({ campaignId, userId, entityId, isQuestUser, defaultText, timeout }, options) { var _b, _c, _d; const defaultResponse = { success: true, data: { campaignVariationId: "default", title: defaultText || "No response" }, }; try { const defaultEntityId = entityId || this.questSdk.getEntityId(); const res = this.questSdk.getUser(); (0, helperFunctions_1.validateParams)(Object.assign({ entityId: defaultEntityId, campaignId, userId: isQuestUser ? res.userId : userId }, (isQuestUser && { token: res.token }))); const url = `/v2/entities/${defaultEntityId}/text-campaigns/${campaignId}`; const queryParams = isQuestUser ? Object.assign({ userId: res.userId, token: res.token }, options === null || options === void 0 ? void 0 : options._queryParams) : Object.assign({ externalUserId: userId }, options === null || options === void 0 ? void 0 : options._queryParams); const response = yield this.questSdk.makeRequest({ url, method: constants_1.HTTP_METHOD.GET, queryParams, headers: options === null || options === void 0 ? void 0 : options._headers, timeout: timeout || undefined, }); if (((_b = response === null || response === void 0 ? void 0 : response.data) === null || _b === void 0 ? void 0 : _b.campaignVariationId) && ((_c = response === null || response === void 0 ? void 0 : response.data) === null || _c === void 0 ? void 0 : _c.campaignVariationId) !== "default") { if (typeof window !== "undefined" && (window === null || window === void 0 ? void 0 : window.sessionStorage)) { const textCampaignString = window.sessionStorage.getItem(config_1.TEXT_CAMPAIGN_SESSION_ID); let textCampaigns = []; if (textCampaignString !== null) { const parsedCampaigns = JSON.parse(textCampaignString || String([])) || []; if (Array.isArray(parsedCampaigns)) { textCampaigns = parsedCampaigns; } } let campaignExists = false; textCampaigns.forEach((campaign) => { if (campaign.campaignId === campaignId) { campaignExists = true; campaign.campaignVariationId = response.data.campaignVariationId; } }); if (!campaignExists) { textCampaigns.push({ campaignId, campaignVariationId: response.data.campaignVariationId, }); } window.sessionStorage.setItem(config_1.TEXT_CAMPAIGN_SESSION_ID, JSON.stringify(textCampaigns)); } } return response; } catch (error) { this.questSdk.captureSentryException(error); if ((error === null || error === void 0 ? void 0 : error.code) === 'ECONNABORTED' || ((_d = error === null || error === void 0 ? void 0 : error.message) === null || _d === void 0 ? void 0 : _d.includes('timeout'))) { console.log("Request timed out, returning default response"); return defaultResponse; } throw error; } }); } } exports.Campaign = Campaign;