zuora to eloqua | Cell 30 | Cell 32 | Search

The code depends on various libraries and modules, including lodash, xlsx, and fs, and imports functions such as getZuoraMonth and getCatalog from other modules. The code initializes Zuora by checking for the existence of the zuora object, retrieving the current month, logging product rate plans, and catching any errors that occur during the process.

Cell 31

var _ = require('lodash');
var assert = require('assert');
var xlsx = require('xlsx');
var importer = require('../Core');
var fs = require('fs');

var {getZuoraMonth} = importer.import("zuora export month");
var calculatePrice = importer.import("calculate price");
var {getCatalog} = importer.import("zuora to eloqua.ipynb[0");

var PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE || '';
var zuoraConfig = JSON.parse(fs.readFileSync(PROFILE_PATH + '/.credentials/zuoraRest_production.json').toString().trim());

var zuora;

if(typeof $ !== 'undefined') {
    $.async();
    (zuora ? Promise.resolve(zuora) : getZuoraMonth(6, zuoraConfig))
        .then(r => {
            zuora = r;
        console.log(zuora.length);
            console.log(JSON.stringify(_.uniqBy(zuora, r => r['ProductRatePlan.Name']), null, 4));
        })
        .then(r => $.sendResult(r))
        .catch(e => $.sendError(e))
}

What the code could have been:

// Import required modules
const _ = require('lodash');
const fs = require('fs');
const xlsx = require('xlsx');
const path = require('path');
const { createLogger, format, transports } = require('winston');

// Setup logging
const logger = createLogger({
  level: 'info',
  format: format.combine(
    format.timestamp(),
    format.json()
  ),
  transports: [
    new transports.Console()
  ]
});

// Define constants
const PROFILE_PATH = getProfilePath();
const ZUORA_CONFIG_FILE = path.join(PROFILE_PATH, '.credentials/zuoraRest_production.json');

// Define functions
function getProfilePath() {
  return process.env.HOME ||
    process.env.HOMEPATH ||
    process.env.USERPROFILE ||
    '';
}

function loadZuoraConfig() {
  try {
    return JSON.parse(fs.readFileSync(ZUORA_CONFIG_FILE).toString().trim());
  } catch (error) {
    logger.error(`Error loading Zuora config: ${error.message}`);
  }
}

function getZuoraMonth(zuoraConfig, month) {
  return Promise.resolve(importer.import('zuora export month')(zuoraConfig, month));
}

function calculatePrice(zuoraConfig, catalog) {
  return Promise.resolve(importer.import('calculate price')(zuoraConfig, catalog));
}

function getCatalog(zuoraConfig) {
  return getZuoraMonth(zuoraConfig, 6)
   .then(zuora => importer.import('zuora to eloqua.ipynb[0]')(zuora));
}

// Import required modules asynchronously
async function init() {
  const importer = await import('../Core');
  const zuoraConfig = loadZuoraConfig();

  if (!zuoraConfig) {
    logger.error('Zuora config is empty');
    return;
  }

  let zuora;

  try {
    zuora = await getZuoraMonth(zuoraConfig, 6);
  } catch (error) {
    logger.error(`Error getting Zuora month: ${error.message}`);
  }

  if (zuora) {
    logger.info(`Zuora length: ${zuora.length}`);
    const uniqueProducts = _.uniqBy(zuora, r => r['ProductRatePlan.Name']);
    logger.info(`Unique products: ${JSON.stringify(uniqueProducts, null, 4)}`);

    await $.sendResult(uniqueProducts);
  } else {
    await $.sendError('Failed to get Zuora data');
  }
}

// Call init function
init();

// TODO: Replace $.sendResult and $.sendError with a more robust error handling mechanism

Code Breakdown

Dependencies

Function Imports

Variables

Zuora Initialization