The code imports necessary modules and variables, including lodash
, fs
, and importer
, and sets up a PROFILE_PATH
variable to point to the user's home directory. It then defines a series of functions to query the Zuora API, including getContact
, getAccount
, and others, and provides an example of how to use these functions in an async
function.
var _ = require('lodash');
var fs = require('fs');
var importer = require('../Core');
var { request } = importer.import("request polyfill");
var { getAuthHeaders } = importer.import("zuora export service");
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());
function zuoraQuery(query) {
return request({
followAllRedirects: true,
uri: zuoraConfig.rest_api_url + '/action/query',
json: {
queryString: query
},
method: 'POST',
headers: getAuthHeaders(zuoraConfig)
});
}
function getContact(email) {
return zuoraQuery(`SELECT AccountId, PersonalEmail, WorkEmail FROM Contact WHERE PersonalEmail LIKE '${email}' OR WorkEmail LIKE '${email}'`)
}
function getContactByAccount(accountId) {
return zuoraQuery(`SELECT AccountId, PersonalEmail, WorkEmail FROM Contact WHERE AccountId='${accountId}'`)
}
function getAccountById(accountId) {
return zuoraQuery(`SELECT Id, Status, Name, Currency, DefaultPaymentMethodId FROM Account WHERE Id='${accountId}'`);
}
function getAccount(accountNumber) {
return zuoraQuery(`SELECT Id, Status, Name, Currency, DefaultPaymentMethodId FROM Account WHERE AccountNumber='${accountNumber}'`);
}
function getSubscription(accountId) {
return zuoraQuery(`SELECT Id, Status, TermEndDate FROM Subscription WHERE AccountId='${accountId}'`);
}
function getPaymentMethod(paymentId) {
return zuoraQuery(`SELECT CreditCardMaskNumber FROM PaymentMethod WHERE Id='${paymentId}'`);
}
function getRatePlans(subscriptionIds) {
return zuoraQuery(`SELECT Id, Name, SubscriptionId FROM RatePlan WHERE SubscriptionId='${subscriptionIds.join('\', OR SubscriptionId=\'')}'`);
}
if(typeof $ !== 'undefined') {
$.async();
var accountId, paymentId;
getContact('flyfisher8008@yahoo.com')
.then(r => {
console.log(r.body.records);
return getAccountById(r.body.records[0].AccountId)
})
.then(r => {
console.log(r.body.records);
accountId = r.body.records[0].Id;
paymentId = r.body.records[0].DefaultPaymentMethodId
return getSubscription(accountId)
})
.then(r => {
console.log(r.body.records);
return getRatePlans(r.body.records.map(r => r.Id));
})
.then(r => {
console.log(_.groupBy(r.body.records, r => r.SubscriptionId))
return getPaymentMethod(paymentId);
})
.then(r => {
console.log(r.body.records[0].CreditCardMaskNumber)
return getContactByAccount(accountId);
})
.then(r => r.body.records)
.then(r => $.sendResult(r))
.catch(e => $.sendError(e))
}
// Import required modules
const fs = require('fs');
const { default: importer } = require('../Core');
const { request } = importer.import('request polyfill');
const { getAuthHeaders } = importer.import('zuora export service');
const _ = require('lodash');
// Define constants
const HOME_PATH = [
process.env.HOME,
process.env.HOMEPATH,
process.env.USERPROFILE,
].find(Boolean) || '';
const ZUORA_CONFIG_FILE = `${HOME_PATH}/.credentials/zuoraRest_production.json`;
// Load Zuora configuration from file
const zuoraConfig = JSON.parse(fs.readFileSync(ZUORA_CONFIG_FILE).toString().trim());
// Define API query function
function zuoraQuery(query) {
return request({
followAllRedirects: true,
uri: zuoraConfig.rest_api_url + '/action/query',
json: {
queryString: query
},
method: 'POST',
headers: getAuthHeaders(zuoraConfig)
});
}
// Define API query functions
const queries = {
getContact: (email) => {
return zuoraQuery(`SELECT AccountId, PersonalEmail, WorkEmail FROM Contact WHERE PersonalEmail LIKE '%${email}%' OR WorkEmail LIKE '%${email}%'`);
},
getContactByAccount: (accountId) => {
return zuoraQuery(`SELECT AccountId, PersonalEmail, WorkEmail FROM Contact WHERE AccountId='${accountId}'`);
},
getAccountById: (accountId) => {
return zuoraQuery(`SELECT Id, Status, Name, Currency, DefaultPaymentMethodId FROM Account WHERE Id='${accountId}'`);
},
getAccount: (accountNumber) => {
return zuoraQuery(`SELECT Id, Status, Name, Currency, DefaultPaymentMethodId FROM Account WHERE AccountNumber='${accountNumber}'`);
},
getSubscription: (accountId) => {
return zuoraQuery(`SELECT Id, Status, TermEndDate FROM Subscription WHERE AccountId='${accountId}'`);
},
getPaymentMethod: (paymentId) => {
return zuoraQuery(`SELECT CreditCardMaskNumber FROM PaymentMethod WHERE Id='${paymentId}'`);
},
getRatePlans: (subscriptionIds) => {
return zuoraQuery(`SELECT Id, Name, SubscriptionId FROM RatePlan WHERE SubscriptionId='${subscriptionIds.join('\', OR SubscriptionId=\'')}'`);
}
};
// Define main function
function main() {
// Check if $ is defined
if (typeof $!== 'undefined') {
$().async();
var accountId, paymentId;
// Get contact by email
queries.getContact('flyfisher8008@yahoo.com')
.then(r => {
console.log(r.body.records);
// Get account by ID
return queries.getAccountById(r.body.records[0].AccountId);
})
.then(r => {
console.log(r.body.records);
accountId = r.body.records[0].Id;
paymentId = r.body.records[0].DefaultPaymentMethodId;
// Get subscription by account ID
return queries.getSubscription(accountId);
})
.then(r => {
console.log(r.body.records);
// Get rate plans by subscription IDs
return queries.getRatePlans(r.body.records.map(r => r.Id));
})
.then(r => {
console.log(_.groupBy(r.body.records, r => r.SubscriptionId));
// Get payment method by ID
return queries.getPaymentMethod(paymentId);
})
.then(r => {
console.log(r.body.records[0].CreditCardMaskNumber);
// Get contact by account ID
return queries.getContactByAccount(accountId);
})
.then(r => r.body.records)
.then(r => $().sendResult(r))
.catch(e => $().sendError(e));
}
}
// Call main function
main();
Importing Modules and Variables
lodash
for utility functionsfs
for file system operationsimporter
module from the parent directory (../Core
)importer
module:
request
(a polyfill for the request
function)getAuthHeaders
(a function to generate authentication headers for Zuora)PROFILE_PATH
variable to point to the user's home directory, using environment variables as a fallback.Zuora Configuration and Query Functions
zuoraRest_production.json
) and parses it into a JavaScript object (zuoraConfig
).zuoraQuery
function:
getContact
: Retrieves a contact's information based on their email addressgetContactByAccount
: Retrieves a contact's information based on their account IDgetAccountById
: Retrieves an account's information based on its IDgetAccount
: Retrieves an account's information based on its account numbergetSubscription
: Retrieves a subscription's information based on the account IDgetPaymentMethod
: Retrieves a payment method's information based on its IDgetRatePlans
: Retrieves a list of rate plans based on a comma-separated list of subscription IDsExample Usage
$
variable is defined and not undefined
, the code calls the async
function on it and assigns the results of getContact
to accountId
and paymentId
.