platform-helpers/cdh-helper.js

'use strict';

const axios = require('axios');
const dotenv = require('dotenv');
const rootRequire = require('rpcm-root-require');
const TiqHelper = rootRequire('/platform-helpers/tiq-helper');
const CdhConfigBeautifier = rootRequire('/platform-helpers/cdh-configuration-beautifier');
const VisitorProfileBeautifier = rootRequire('/platform-helpers/visitor-profile-beautifier');
const LoggerWrapper = rootRequire('/platform-helpers/log4js-wrapper');
const logger = LoggerWrapper.getLogger();
rootRequire('/platform-helpers/string-extensions');

/**
 * CDH Helper class.
 *
 * @static
 * @requires axios
 * @requires string-extensions
 */

class CdhHelper {
  static cache = null;
  /**
   *
   * @param {*} account
   * @param {*} profile
   * @param {*} utk
   * @param {*} jsessionId
   * @param {*} visitorId
   * @param {*} attributeId
   * @returns
   */
  static getRawUserProfile = async (account, profile, utk, jsessionId, visitorId, attributeId) => {
    const payload = await axios.get('https://my.tealiumiq.com/urest/datacloud/{0}/{1}/visitor?attributeId={2}&attributeValue={3}&prettyName=false&includePii=true&utk={4}'
      .format(account, profile, attributeId, encodeURIComponent(visitorId), utk), {
      headers: {
        Cookie: 'JSESSIONID={0}'.format(jsessionId),
        Accept: 'application/json'
      }
    });
    return payload && payload.data;
  };

  /**
   *
   * @param {*} account
   * @param {*} profile
   * @param {*} utk
   * @param {*} jsessionId
   * @returns
   */
  static getRawCdhConfiguration = async (account, profile, tiqSessionDetails) => {
    if (CdhHelper.cache) return CdhHelper.cache;
    const payload = await axios.get('https://my.tealiumiq.com/urest/datacloud/{0}/{1}/profile/?utk={2}'
      .format(account, profile, tiqSessionDetails.utk), {
      headers: {
        Cookie: 'JSESSIONID={0}'.format(tiqSessionDetails.jsessionId),
        Accept: 'application/json'
      }
    });
    CdhHelper.cache = payload && payload.data;
    return CdhHelper.cache;
  };

  /**
   *
   * @param {*} account
   * @param {*} profile
   * @param {*} utk
   * @param {*} jsessionId
   * @returns
   */
  static sendCollectApiRequest = async (payload) => {
    return await axios.post('https://collect.tealiumiq.com/event', payload);
  };

  /**
   * Fetches a specified Visitor Profile from the Visitor Lookup API.  Also adds the '_id' attribute and the '*_pretty' attribute groups.
   *
   * @public
   * @param {string} account - the CDH account used for the current trace
   * @param {string} profile - the CDH profile used for the current trace
   * @param {string} visitorId - the Visitor ID to use for the Visitor Lookup
   * @param {string} [attributeId='76'] - the attribute ID to use for the Visitor Lookup.  Defaults to '76' which is 'first party cookie' in the UI.
   * @returns {visitorProfile} slightly simplified instance of [VisitorProfile]{@link module:traceHelper~VisitorProfile}, without current visit or last event information (among others)
   * @example
   * let customerProfile
   *
   * describe('VISITOR LOOKUP - should get the profile', function () {
   *   it('the customer should have a profile', async function () {
   *     const profile = await CdhHelper.getVisitorProfileFromLookup(cdhAccount, cdhProfile, customerId, customerIdAttributeId)
   *
   *     // share with other tests
   *     customerProfile = profile
   *     chai.expect(profile).to.be.an('object')
   *     reporterHelper.logMessage(JSON.stringify(profile, null, 2))
   * })
   */
  static getVisitorProfile = async (account, profile, visitorId, attributeId = '76') => {
    dotenv.config();
    logger.trace('cdh-helper.js --> getVisitorProfileFromLookup --> Getting visitor profile...');
    const tiqSessionDetails = await TiqHelper.getTiqSessionDetails(account, profile, process.env.TEALIUM_EMAIL, process.env.TEALIUM_PASS);
    const rawCdhConfiguration = await CdhHelper.getRawCdhConfiguration(account, profile, tiqSessionDetails);
    const prettyCdhConfiguration = await CdhConfigBeautifier.beautify(rawCdhConfiguration);
    const rawVisitorProfile = await CdhHelper.getRawUserProfile(account, profile, tiqSessionDetails.utk, tiqSessionDetails.jsessionId, visitorId, attributeId);
    const visitorProfile = await VisitorProfileBeautifier.beautify(rawVisitorProfile.visitor, prettyCdhConfiguration);
    visitorProfile._id = attributeId === '76' ? visitorId : '__{0}_{1}__{2}_{3}__'.format(account, profile, attributeId, visitorId);

    return visitorProfile;
  };
}

module.exports = CdhHelper;