This code snippet constructs a SQL query to retrieve data about active subscriptions from a database, filtering by subscription status, date range, currency, and excluding specific rate plans and products. The query is incomplete and requires further development to include additional filtering criteria.
npm run import -- "zuora renewals query"
var moment = require('moment');
var chrono = require('chrono-node');
var excludedRatePlans = [
'Act! Pro - New License',
'Act! Pro - 30 Day Support',
'Act! Pro - Upgrade License',
'Act! Password Reset Charge',
'Act! Premium Cloud - Trial',
'Act! Pro V19 - Upgrade License',
'Act! Pro V20 - Upgrade License',
]
var excludedProductSkus = [
'00000006'
]
var currencies = [
'',
'USD',
'AUD',
'NZD',
]
var query = `SELECT
Account.Id,
Account.Name,
Account.AccountNumber,
Account.resellerofRecord__c,
Account.renewalRep__c,
Account.commisionedSalesRep__c,
Account.CreatedDate,
Account.Currency,
SoldToContact.WorkEmail,
SoldToContact.Country,
SoldToContact.State,
BillToContact.WorkEmail,
RatePlan.Id,
RatePlan.Name,
RatePlanCharge.Id,
RatePlanCharge.BillingPeriod,
RatePlanCharge.Description,
RatePlanCharge.Quantity,
RatePlanCharge.Version,
RatePlanCharge.CreatedDate,
RatePlanCharge.EffectiveEndDate,
DefaultPaymentMethod.CreditCardExpirationMonth,
DefaultPaymentMethod.CreditCardExpirationYear,
DefaultPaymentMethod.CreditCardMaskNumber,
ProductRatePlanCharge.Id,
ProductRatePlan.planType__c,
ProductRatePlan.planSubType__c,
ProductRatePlan.Id,
ProductRatePlan.Name,
Product.productType__c,
Product.Name,
Product.Description,
Product.Id,
Product.SKU,
Subscription.Id,
Subscription.Name,
Subscription.Status,
Subscription.Reseller__c,
Subscription.SubscriptionEndDate,
Subscription.SubscriptionStartDate,
Subscription.TermStartDate,
Subscription.TermEndDate,
Subscription.AutoRenew
FROM RatePlanCharge
WHERE Subscription.Status!='Draft' AND Subscription.Status!='Cancelled' AND Subscription.Status!='Expired'
AND Subscription.TermEndDate >='{0}' AND Subscription.TermEndDate <='{1}'
AND (Account.Currency='${currencies.join("' OR Account.Currency='")}')
AND (ProductRatePlan.Name!='${excludedRatePlans.join("' AND ProductRatePlan.Name!='")}')
AND (Product.SKU!='${excludedProductSkus.join("' AND Product.SKU!='")}')
AND NOT (SoldToContact.WorkEmail LIKE 'qaaw%@gmail.com')
AND NOT (BillToContact.WorkEmail LIKE 'qaaw%@gmail.com')
AND NOT (Account.Name LIKE '%do not use%')
`;
// AND (RatePlanCharge.EffectiveEndDate >='{2}' OR RatePlanCharge.ChargeType='OneTime')
// removed this so that discounts show up on the account
// AND RatePlanCharge.BillingPeriod!='Month'
function getQuery(start, end) {
// TODO: add option for pulling based on subscription term or based on modified fields
return {
Query: query.replace('{0}', moment(chrono.parseDate(start)).format('YYYY-MM-DD'))
.replace('{1}', moment(chrono.parseDate(end)).format('YYYY-MM-DD'))
.replace('{2}', moment(new Date()).format('YYYY-MM-DD')),
Format: 'csv',
Zip: false
};
}
module.exports = {
getQuery
};
const moment = require('moment');
const chrono = require('chrono-node');
// Define excluded rate plans and product SKUs as constants for better maintainability
const EXCLUDED_RATE_PLANS = [
'Act! Pro - New License',
'Act! Pro - 30 Day Support',
'Act! Pro - Upgrade License',
'Act! Password Reset Charge',
'Act! Premium Cloud - Trial',
'Act! Pro V19 - Upgrade License',
'Act! Pro V20 - Upgrade License',
];
const EXCLUDED_PRODUCT_SKUS = ['00000006'];
const CURRENCIES = ['', 'USD', 'AUD', 'NZD'];
const SQL_QUERY = `
SELECT
Account.Id,
Account.Name,
Account.AccountNumber,
Account.resellerofRecord__c,
Account.renewalRep__c,
Account.commisionedSalesRep__c,
Account.CreatedDate,
Account.Currency,
SoldToContact.WorkEmail,
SoldToContact.Country,
SoldToContact.State,
BillToContact.WorkEmail,
RatePlan.Id,
RatePlan.Name,
RatePlanCharge.Id,
RatePlanCharge.BillingPeriod,
RatePlanCharge.Description,
RatePlanCharge.Quantity,
RatePlanCharge.Version,
RatePlanCharge.CreatedDate,
RatePlanCharge.EffectiveEndDate,
DefaultPaymentMethod.CreditCardExpirationMonth,
DefaultPaymentMethod.CreditCardExpirationYear,
DefaultPaymentMethod.CreditCardMaskNumber,
ProductRatePlanCharge.Id,
ProductRatePlan.planType__c,
ProductRatePlan.planSubType__c,
ProductRatePlan.Id,
ProductRatePlan.Name,
Product.productType__c,
Product.Name,
Product.Description,
Product.Id,
Product.SKU,
Subscription.Id,
Subscription.Name,
Subscription.Status,
Subscription.Reseller__c,
Subscription.SubscriptionEndDate,
Subscription.SubscriptionStartDate,
Subscription.TermStartDate,
Subscription.TermEndDate,
Subscription.AutoRenew
FROM RatePlanCharge
WHERE (
Subscription.Status IN ('Draft', 'Cancelled', 'Expired')
OR Subscription.Status IN ('Active', 'Suspended', 'Pending')
)
AND Subscription.TermEndDate >= :start_date
AND Subscription.TermEndDate <= :end_date
AND Account.Currency IN (:currencies)
AND ProductRatePlan.Name NOT IN (:excludedRatePlans)
AND Product.SKU NOT IN (:excludedProductSkus)
AND (
SoldToContact.WorkEmail NOT LIKE 'qaaw%@gmail.com'
AND BillToContact.WorkEmail NOT LIKE 'qaaw%@gmail.com'
AND Account.Name NOT LIKE '%do not use%'
)
`;
// Function to create the query with start and end dates
function getQuery(start, end) {
// Parse the start and end dates using chrono-node
const startDate = moment(chrono.parseDate(start));
const endDate = moment(chrono.parseDate(end));
// Check if the end date is before the start date and throw an error if true
if (endDate.isBefore(startDate)) {
throw new Error('End date cannot be before start date');
}
// Format the query placeholders
const formattedQuery = SQL_QUERY.replace(':start_date', startDate.format('YYYY-MM-DD'))
.replace(':end_date', endDate.format('YYYY-MM-DD'))
.replace(':currencies', CURRENCIES.join(', '))
.replace(':excludedRatePlans', EXCLUDED_RATE_PLANS.join(', '))
.replace(':excludedProductSkus', EXCLUDED_PRODUCT_SKUS.join(', '));
// Return the formatted query and its configuration
return {
Query: formattedQuery,
Format: 'csv',
Zip: false,
};
}
module.exports = {
getQuery,
};
This code snippet appears to be constructing a SQL query for retrieving data about active subscriptions from a database.
Here's a breakdown:
Dependencies:
moment
: A library for working with dates and times.chrono
: A library for parsing natural language dates and times.Filtering Criteria:
excludedRatePlans
: An array of rate plan names to exclude from the results.excludedProductSkus
: An array of product SKUs to exclude.currencies
: An array of supported currencies.SQL Query Construction:
SELECT
statement that specifies the columns to retrieve from various tables: Account
, SoldToContact
, BillToContact
, RatePlan
, RatePlanCharge
, DefaultPaymentMethod
, ProductRatePlan
, Product
, Subscription
.WHERE
clause filters the results based on several conditions:
Subscription.Status
: Excludes subscriptions that are "Draft", "Cancelled", or "Expired".Subscription.TermEndDate
: Filters subscriptions within a specified date range (placeholders {0}
and {1}
likely represent start and end dates).Account.Currency
: Filters subscriptions based on the supported currencies.ProductRatePlan.Name
: Excludes subscriptions with rate plans listed in excludedRatePlans
.Incomplete Query:
ProductRatePlan.Name
and Product.SKU
.In essence:
This code snippet is building a SQL query to retrieve information about active subscriptions within a specific date range, excluding certain rate plans and products.