This code provides a function to fetch renewal data from Zuora for a given time period and returns it in a usable JSON format. It leverages modules for querying Zuora, exporting data, and converting CSV to JSON.
npm run import -- "zuora export month"
var importer = require('../Core');
var renewalsQuery = importer.import("zuora renewals query");
var zuoraExport = importer.import("zuora to eloqua.ipynb[0");
function getZuoraMonth(months, zuoraConfig) {
if(!months) {
months = 0;
}
var start = new Date();
start.setMonth(start.getMonth() - months);
start.setDate(1);
start.setHours(0, 0, 0);
var end = new Date();
end.setMonth(end.getMonth() + months + 1);
end.setDate(1);
end.setHours(0, 0, 0);
if(start.getMonth() > end.getMonth()) {
end.getFullYear(end.getFullYear() + 1)
}
return zuoraExport.createBulkExportJob(renewalsQuery.getQuery(start.toString(), end.toString()), zuoraConfig)
.then(exportId => zuoraExport.getBulkExportJobStatus(exportId, zuoraConfig))
.then(fileId => zuoraExport.getBulkExportFile(fileId, zuoraConfig))
.then(r => zuoraExport.csvToJson(r))
}
module.exports = {
getZuoraMonth
};
// Import required modules and utilities
const { importer } = require('../Core');
const { renewalsQuery } = importer.import('zuora renewals query');
const { zuoraExport } = importer.import('zuora to eloqua.ipynb')[0];
/**
* Calculates the start and end dates for a specific number of months.
*
* @param {number} [months=0] The number of months to calculate for.
* @returns {Object} An object with start and end dates.
*/
function getMonthDateRange(months = 0) {
const start = new Date();
start.setMonth(start.getMonth() - months);
start.setDate(1);
start.setHours(0, 0, 0);
const end = new Date();
end.setMonth(end.getMonth() + months + 1);
end.setDate(1);
end.setHours(0, 0, 0);
// Correct for February (29th doesn't exist in some years)
if (start.getMonth() > end.getMonth()) {
end.setFullYear(end.getFullYear() + 1);
}
return { start: start.toISOString(), end: end.toISOString() };
}
/**
* Retrieves data from Zuora for a specific date range.
*
* @param {Object} zuoraConfig - The Zuora API configuration.
* @returns {Promise} The ID of the bulk export job.
*/
function getZuoraExport(zuoraConfig) {
const { start, end } = getMonthDateRange();
const query = renewalsQuery.getQuery(start, end);
return zuoraExport.createBulkExportJob(query, zuoraConfig);
}
/**
* Gets the status of the bulk export job.
*
* @param {string} exportId - The ID of the bulk export job.
* @param {Object} zuoraConfig - The Zuora API configuration.
* @returns {Promise} The status of the bulk export job.
*/
function getBulkExportStatus(exportId, zuoraConfig) {
return zuoraExport.getBulkExportJobStatus(exportId, zuoraConfig);
}
/**
* Gets the CSV file associated with the bulk export job.
*
* @param {string} fileId - The ID of the file.
* @param {Object} zuoraConfig - The Zuora API configuration.
* @returns {Promise} The CSV file contents.
*/
function getBulkExportFile(fileId, zuoraConfig) {
return zuoraExport.getBulkExportFile(fileId, zuoraConfig);
}
/**
* Converts the CSV file to JSON.
*
* @param {any} csvFile - The CSV file contents.
* @returns {Promise} The JSON contents of the CSV file.
*/
function csvToJson(csvFile) {
return zuoraExport.csvToJson(csvFile);
}
/**
* Retrieves the Zuora data for a specific number of months.
*
* @param {number} [months=0] The number of months to retrieve data for.
* @param {Object} zuoraConfig - The Zuora API configuration.
* @returns {Promise} The JSON contents of the CSV file.
*/
module.exports = {
getZuoraMonth: async function (months = 0, zuoraConfig) {
const exportId = await getZuoraExport(zuoraConfig);
const status = await getBulkExportStatus(exportId, zuoraConfig);
const fileId = await getBulkExportStatus(exportId, zuoraConfig);
const csvFile = await getBulkExportFile(fileId, zuoraConfig);
return csvToJson(csvFile);
}
};
This code defines a function getZuoraMonth
that retrieves renewal data from Zuora for a specified period and converts it into a JSON format.
Here's a breakdown:
Initialization:
getZuoraMonth
Function:
months
(number of months to retrieve data for) and zuoraConfig
(configuration object for interacting with Zuora).renewalsQuery
module to generate a query for retrieving renewal data within the calculated date range.zuoraExport
module to:
Export:
getZuoraMonth
function is exported as the main module, making it available for use in other parts of the application.