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.
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))
}
// 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
lodash
(_
): a utility library for functional programming.assert
: not used in the provided code snippet.xlsx
: a library for working with Excel files.fs
: the File System module for interacting with the file system.importer
: a custom module for importing functions from other modules.getZuoraMonth
: imports a function from zuora export month
module, which returns the current Zuora month.calculatePrice
: imports a function from calculate price
module, not used in the provided code snippet.getCatalog
: imports a function from zuora to eloqua.ipynb[0]
module, which returns the catalog.PROFILE_PATH
: the path to the user's profile directory.zuoraConfig
: the JSON configuration for Zuora, retrieved from the .credentials
directory.$
is defined, it calls the async
method and then:
zuora
is defined, it resolves to the existing zuora
object. Otherwise:
getZuoraMonth
with zuoraConfig
and a parameter 6
.zuora
object and a unique list of product rate plans using _.uniqBy
from lodash
.$
using sendResult
.$
using sendError
.