This code provides a module for retrieving Zuora account information, including email addresses and the last four digits of associated credit cards, by making API calls and chaining promises to process the results. The getZuoraAccounts
function is the main entry point, accepting a list of email addresses and returning an array of account details.
npm run import -- "zuora account service"
var zuoraExport = importer.import("zuora export service");
var request = importer.import("request polyfill");
module.exports = {
getZuoraAccounts,
zuoraQuery
};
function zuoraQuery(query, zuoraConfig) {
return request.request({
followAllRedirects: true,
uri: zuoraConfig.rest_api_url + '/action/query',
json: {
queryString: query
},
method: 'POST',
headers: zuoraExport.getAuthHeaders(zuoraConfig)
});
}
function getContact(email, zuoraConfig) {
return module.exports.zuoraQuery(`
SELECT AccountId, PersonalEmail, WorkEmail
FROM Contact
WHERE PersonalEmail LIKE '${email}'
OR WorkEmail LIKE '${email}'`, zuoraConfig)
}
function getAccountById(accountId, zuoraConfig) {
return module.exports.zuoraQuery(`
SELECT Id, Status, Name, Currency, DefaultPaymentMethodId
FROM Account
WHERE Id='${accountId}'`, zuoraConfig);
}
function getPaymentMethod(paymentId, zuoraConfig) {
return module.exports.zuoraQuery(`
SELECT CreditCardMaskNumber
FROM PaymentMethod
WHERE Id='${paymentId}'`, zuoraConfig);
}
function getAccountLast4Digits(email, zuoraConfig) {
return getContact(email, zuoraConfig)
.then(r => getAccountById(r.body.records[0].AccountId, zuoraConfig))
.then(r => getPaymentMethod(r.body.records[0].DefaultPaymentMethodId, zuoraConfig))
.then(r => {
return {
"EmailAddress": email,
"Last4DigitsOfCard": r.body.records[0].CreditCardMaskNumber
}
})
}
function getZuoraAccounts(notifyRequest, zuoraConfig) {
if(!notifyRequest.items) {
return Promise.resolve([]).then(() => {
throw new Error('Account items not defined, nothing to do!');
});
}
return Promise.all(notifyRequest.items.map(c => {
return getAccountLast4Digits(c.EmailAddress, zuoraConfig);
}))
}
const { promisify } = require('util');
const request = promisify(require('request-polyfill'));
const ZuoraExportService = require('./zuora-export-service');
module.exports = {
getZuoraAccounts,
zuoraQuery
};
class ZuoraQuery {
constructor(zuoraConfig) {
this.zuoraConfig = zuoraConfig;
}
query(query) {
const authHeaders = ZuoraExportService.getAuthHeaders(this.zuoraConfig);
const options = {
followAllRedirects: true,
uri: this.zuoraConfig.rest_api_url + '/action/query',
json: {
queryString: query
},
method: 'POST',
headers: authHeaders
};
return request(options);
}
}
class ContactService {
constructor(zuoraQuery, zuoraConfig) {
this.zuoraQuery = zuoraQuery;
this.zuoraConfig = zuoraConfig;
}
async getContact(email) {
const query = `
SELECT AccountId, PersonalEmail, WorkEmail
FROM Contact
WHERE PersonalEmail LIKE '${email}'
OR WorkEmail LIKE '${email}'
`;
const response = await this.zuoraQuery.query(query);
return response.body;
}
}
class AccountService {
constructor(zuoraQuery, zuoraConfig) {
this.zuoraQuery = zuoraQuery;
this.zuoraConfig = zuoraConfig;
}
async getAccountById(accountId) {
const query = `
SELECT Id, Status, Name, Currency, DefaultPaymentMethodId
FROM Account
WHERE Id='${accountId}'
`;
const response = await this.zuoraQuery.query(query);
return response.body;
}
}
class PaymentMethodService {
constructor(zuoraQuery, zuoraConfig) {
this.zuoraQuery = zuoraQuery;
this.zuoraConfig = zuoraConfig;
}
async getPaymentMethod(paymentId) {
const query = `
SELECT CreditCardMaskNumber
FROM PaymentMethod
WHERE Id='${paymentId}'
`;
const response = await this.zuoraQuery.query(query);
return response.body;
}
}
class AccountLast4DigitsService {
constructor(contactService, accountService, paymentMethodService, zuoraConfig) {
this.contactService = contactService;
this.accountService = accountService;
this.paymentMethodService = paymentMethodService;
this.zuoraConfig = zuoraConfig;
}
async getAccountLast4Digits(email) {
const contactResponse = await this.contactService.getContact(email);
const accountId = contactResponse.records[0].AccountId;
const accountResponse = await this.accountService.getAccountById(accountId);
const paymentMethodId = accountResponse.records[0].DefaultPaymentMethodId;
const paymentMethodResponse = await this.paymentMethodService.getPaymentMethod(paymentMethodId);
return {
EmailAddress: email,
Last4DigitsOfCard: paymentMethodResponse.records[0].CreditCardMaskNumber
};
}
}
class ZuoraAccountsService {
constructor(zuoraQuery, zuoraConfig) {
this.zuoraQuery = zuoraQuery;
this.zuoraConfig = zuoraConfig;
}
async getZuoraAccounts(notifyRequest) {
if (!notifyRequest.items) {
throw new Error('Account items not defined, nothing to do!');
}
const services = notifyRequest.items.map(c => this.getAccountLast4DigitsService(c.EmailAddress));
return Promise.all(services);
}
getAccountLast4DigitsService(email) {
return new AccountLast4DigitsService(
new ContactService(this.zuoraQuery, this.zuoraConfig),
new AccountService(this.zuoraQuery, this.zuoraConfig),
new PaymentMethodService(this.zuoraQuery, this.zuoraConfig),
this.zuoraConfig
);
}
}
function getZuoraAccounts(notifyRequest, zuoraConfig) {
return new ZuoraAccountsService(new ZuoraQuery(zuoraConfig), zuoraConfig).getZuoraAccounts(notifyRequest);
}
function zuoraQuery(query, zuoraConfig) {
return new ZuoraQuery(zuoraConfig).query(query);
}
This code defines a module for interacting with the Zuora API to retrieve account information, specifically focusing on email addresses and the last four digits of associated credit cards.
Here's a breakdown:
Imports:
zuoraExport
: Likely contains functions for authentication and other Zuora-specific interactions.request polyfill
: A library for making HTTP requests.Exports:
getZuoraAccounts
: The main function that processes a list of email addresses and retrieves account details.zuoraQuery
: A helper function to make API requests to the Zuora query endpoint.Helper Functions:
getContact
, getAccountById
, getPaymentMethod
: These functions use zuoraQuery
to fetch specific data from Zuora based on provided parameters (email, account ID, payment ID).getAccountLast4Digits
: A chain of promises that combines the results of getContact
, getAccountById
, and getPaymentMethod
to retrieve the desired information.getZuoraAccounts
Function:
notifyRequest
object (likely containing a list of email addresses) and zuoraConfig
(API credentials).Promise.all
to concurrently fetch account details for each email address using getAccountLast4Digits
.