Last active
April 26, 2018 13:59
-
-
Save ptdecker/e680797764c14cfb020a12d91cfdf03b to your computer and use it in GitHub Desktop.
Returns an array that represents the _types_ of plans (i.e. offer classes) that should be offered to the customer. An empty array is returned if an offer class cannot be determined due to a coding error and the caller needs to handle accordingly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// getOfferClasses() | |
// | |
// Returns an array that represents the _types_ of plans (i.e. offer classes) that should be offered to the | |
// customer. An empty array is returned if an offer class cannot be determined due to a coding error and the | |
// caller needs to handle accordingly. | |
// | |
// Parameters: | |
// custSegment - String customer segment ('A'-Port, 'B'-New, 'C'-Migrator, 'D'-Legacy, 'E'-Inner Circle) | |
// deviceType - String identifying type of device ('byo'-BYOD, 'c_pl'-Certified Preloved, 'new'-New Device) | |
// isAppleICDevice - Boolean true if the device is Apple and qualified for an Inner Circle plan | |
// inAppleStore - Boolean true if customer is physically in an Apple store | |
// isCompatible - Boolean true if customers device is compatible with their current plan | |
// isRecent - Boolean true if device not active on the current subscribe nor any within 48 days | |
// isActive - Boolean true if the 'isAlreadyActivated' attribute is true | |
// | |
// Note: deviceType of 'new' describes the actual device SKU as opposed to its usage whereas | |
// isRecent describes if the device is recently used by the customer. isRecent is false | |
// for a "phone taken out of my drawer" use case. | |
// | |
// Integer offer classes returned in array: | |
// Empty Array: Code error -- calling function needs to handle | |
// 0 - No plan (i.e. no offer class applies, transacton is not allowed) | |
// 1 - Current plan | |
// 2 - Promo plan | |
// 3 - Base plan | |
// 4 - Legacy plan | |
/* jshint esnext:true */ | |
function getOfferClass(custSegment, deviceType, isAppleICDevice, inAppleStore, isCompatible, isRecent, isActive) { | |
const noPlan = 0; | |
const currentPlan = 1; | |
const promoPlan = 2; | |
const basePlan = 3; | |
const legacyPlan = 4; | |
// ** Safety Assertion ** | |
// Note: This is guard code to make sure this function receives the expected types for its parameters. If | |
// invalids are passed then return to no offer class to caller (empty array) | |
if (typeof custSegment !== 'string' || typeof deviceType !== 'string' || typeof isAppleICDevice !== 'boolean' || | |
typeof inAppleStore !== 'boolean' || typeof isCompatible !== 'boolean' || typeof isRecent !== 'boolean' || | |
typeof isActive !== 'boolean') { | |
console.error("One or more parameters passed to 'getOfferClass()' are not of the expected types"); | |
return []; | |
} | |
// Handle deviceType | |
var isBYOD = (deviceType === "byo"); | |
var isCPL = (deviceType === "c_pl"); | |
var isNew = (deviceType === "new"); // Describes the SKU itself (~byo && ~c_pl) | |
// ** Safety Assertion ** | |
// Note: This is guard code to make sure we have been passed a valid deviceType attribute. If an invalid | |
// value is passed, then return no offer class to caller (empty array) | |
if (!isBYOD && !isCPL && !isNew) { | |
console.error("An invalid device type of '" + deviceType + "' was passed to 'getOfferClass()'"); | |
return []; | |
} | |
// Handle porting and migrating customers | |
if (custSegment === 'A' || custSegment === 'C') { | |
return [promoPlan]; | |
} | |
// New number customers in an Apple Store buying a new phone exception | |
if (custSegment === 'B' && inAppleStore && isNew) { | |
return [promoPlan]; | |
} | |
// New number customers [and not in Apple store] | |
if (custSegment === 'B') { | |
return [basePlan]; | |
} | |
// ** Safety assertion ** | |
// Note: This is guard code in case 'A', 'B', and 'C' customers made it this far | |
// because a future developer does not handle them prior to this point in | |
// the code. This would also fire if an invalid custSegment is passed. | |
if (custSegment !== 'D' && custSegment !== 'E') { | |
console.error("New, porting, or migrating customers are not being properly handled in 'getOfferClasses()'"); | |
return []; | |
} | |
// Legacy or IC BYOD customers | |
if (isBYOD) { | |
// Legacy BYOD customers with device not compatible with their current plan | |
if (custSegment === "D" && !isCompatible) { | |
return [basePlan, legacyPlan]; | |
} | |
// Fallback for BYOD customers | |
return [currentPlan]; | |
} | |
// Legacy or IC Certified Pre-Loved customers | |
if (isCPL) { | |
// Legacy CPL customers with a non-recent device and plan that is not compatible with that device | |
if (custSegment === 'D' && !isRecent && !isCompatible) { | |
return [promoPlan]; | |
} | |
// Legacy CPL customers with a non-recent device [and compatible plan] | |
if (custSegment === 'D' && !isRecent) { | |
return [currentPlan, promoPlan]; | |
} | |
// Legacy CPL customers with a recent device that is not compatible | |
if (custSegment === 'D' && !isCompatible) { | |
return [basePlan, legacyPlan]; | |
} | |
// IC customers with a non-recent device | |
if (custSegment !== 'D' && !isRecent) { | |
return [promoPlan]; | |
} | |
// Fallback for CPL customers | |
return [currentPlan]; | |
} | |
// Active legacy customers with an IC compatible device but with a plan | |
// that is not compatible with their new device | |
if (custSegment === 'D' && isAppleICDevice && !isCompatible && isActive) { | |
return [basePlan, legacyPlan]; | |
} | |
// Inactive legacy customers with an IC compatible device but with a | |
// plan that is not compatible with their new device | |
if (custSegment === 'D' && isAppleICDevice && !isCompatible) { | |
return [promoPlan]; | |
} | |
// Active legacy customers with a plan compatible with thier new device | |
if (custSegment === 'D' && isCompatible && isActive) { | |
return [currentPlan]; | |
} | |
// Inacitve legacy customers with a plan compatible with their new device | |
if (custSegment === 'D' && isCompatible) { | |
return [currentPlan, promoPlan]; | |
} | |
// Active legacy customers | |
if (custSegment === 'D' && isActive) { | |
return [legacyPlan]; | |
} | |
// Inactive legacy customers | |
if (custSegment === 'D') { | |
return [promoPlan]; | |
} | |
// Active IC customers with a currnet plan compatible with new device | |
if (custSegment === 'E' && isCompatible && isActive) { | |
return [currentPlan]; | |
} | |
// Inactive IC customers | |
if (custSegment === 'E' && !isActive) { | |
return [promoPlan]; | |
} | |
// Active IC customers | |
if (custSegment === 'E') { | |
return [noPlan]; | |
} | |
// Fallback always on current plan | |
// Note: New customers, porting, and migrating customers never make it here | |
return [currentPlan]; | |
} | |
// Test function | |
// Loops through all possible call values | |
function test() { | |
const tlist = ["A", "B", "C", "D", "E"]; | |
const dlist = ["byo", "c_pl", "new"]; | |
console.log('"Segment","DeviceType","isAppleIC?","inStore?","Compatible?","Recent?","Active?","Result"'); | |
for (var t = 0; t < tlist.length; t++) { | |
for (var d = 0; d < dlist.length; d++) { | |
for (var bit = 0; bit < 32; bit++) { | |
isActive = ((bit & (1 << 0)) !== 0); // tests to see if bit zero is set | |
isRecent = ((bit & (1 << 1)) !== 0); | |
isCompatible = ((bit & (1 << 2)) !== 0); | |
isInStore = ((bit & (1 << 3)) !== 0); | |
isAppleIC = ((bit & (1 << 4)) !== 0); | |
results = getOfferClass(tlist[t], dlist[d], isAppleIC, isInStore, isCompatible, isRecent, isActive); | |
console.log(['"'+tlist[t]+'"', '"'+dlist[d]+'"', isAppleIC, isInStore, isCompatible, isRecent, isActive, '"'+results+'"'].join()); | |
} | |
} | |
} | |
} | |
test(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment