Last active
November 16, 2020 23:11
-
-
Save thetumper/909cb465636cbcd1c48f8b80d20cb0e4 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
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
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
function defaultMapIfEmpty(defaultedVal, m, val) { | |
try { | |
const result = m(val); | |
return isEmpty(result) ? defaultedVal : result; | |
} catch (e) { | |
return defaultedVal; | |
} | |
} | |
const State = { | |
NEW: 'new', | |
ADDRESS: 'address', | |
IDENTIFY: 'identify', | |
MERGE: 'merge', | |
AGGREGATE: 'aggregate', | |
DO_NOT_CONTACT: 'do_not_contact', | |
ASSESS: 'assess', | |
ESTIMATE: 'estimate', | |
ASSIGN: 'assign', | |
PUBLISH: 'publish', | |
DONE: 'done', | |
}; | |
// TODO Implement guard functions | |
const hasAbilitecId = (lead) => { | |
return defaultMapIfEmpty([], (l) => l.identifiers.filter((i) => i.consumerLink !== undefined), lead).length > 0; | |
}; | |
/** | |
* @type {Mapping<Lead,boolean>} | |
*/ | |
function hasAddress(lead) { | |
return defaultMapIfEmpty([], (l) => l.contactMechanisms.addresses, lead).length > 0; | |
} | |
const hasFirstPartyData = (context) => { | |
return context !== undefined; | |
}; | |
function isBulkUpload(tags) { | |
return defaultMapIfEmpty( | |
false, | |
(ts) => { | |
const tagSet = new Set(ts); | |
return tagSet.has('BULK') && tagSet.has('AGENT_UPLOAD'); | |
}, | |
tags | |
); | |
} | |
/** | |
* @type {Mapping<Lead,boolean>} | |
*/ | |
function isFromSalesForceBulkUpload(lead) { | |
return defaultMapIfEmpty([], (l) => l.leadProviderDetails, lead).some( | |
({leadProvider, tags}) => leadProvider === 'SALESFORCE' && isBulkUpload(tags) | |
); | |
} | |
function isNotFromAST(lead) { | |
return !defaultMapIfEmpty([], (l) => l.leadProviderDetails, lead).some(({leadProvider}) => leadProvider === 'AST'); | |
} | |
const shouldRetrieveFirstPartyData = (lead) => { | |
return hasAbilitecId(lead) && !hasFirstPartyData(lead); | |
}; | |
const stateMachine = Machine({ | |
id: 'leadsProcessStateMachine', | |
initial: State.NEW, | |
states: { | |
[State.NEW]: { | |
on: { | |
NEXT: [ | |
{ | |
target: State.ADDRESS, | |
cond: hasAddress, | |
}, | |
{ | |
target: State.MERGE, | |
cond: hasAbilitecId, | |
}, | |
{ | |
target: State.IDENTIFY, | |
}, | |
], | |
}, | |
}, | |
[State.ADDRESS]: { | |
on: { | |
NEXT: [ | |
{ | |
target: State.MERGE, | |
cond: hasAbilitecId, | |
}, | |
{ | |
target: State.IDENTIFY | |
} | |
], | |
}, | |
}, | |
[State.IDENTIFY]: { | |
on: { | |
NEXT: [ | |
{ | |
target: State.MERGE, | |
cond: hasAbilitecId, | |
}, | |
{ | |
target: State.AGGREGATE, | |
cond: isNotFromAST, | |
}, | |
{ | |
target: State.ESTIMATE, | |
}, | |
], | |
}, | |
}, | |
[State.MERGE]: { | |
on: { | |
NEXT: [ | |
{ | |
target: State.AGGREGATE, | |
cond: isNotFromAST, | |
}, | |
{ | |
target: State.ESTIMATE, | |
}, | |
], | |
}, | |
}, | |
[State.AGGREGATE]: { | |
on: { | |
NEXT: [ | |
{ | |
target: State.DO_NOT_CONTACT, | |
cond: isFromSalesForceBulkUpload, | |
}, | |
{ | |
target: State.ASSESS, | |
cond: isNotFromAST, | |
}, | |
{ | |
target: State.ESTIMATE, | |
}, | |
], | |
}, | |
}, | |
[State.DO_NOT_CONTACT]: { | |
on: { | |
NEXT: [ | |
{ | |
target: State.ASSESS, | |
cond: isNotFromAST, | |
}, | |
{ | |
target: State.ESTIMATE, | |
}, | |
], | |
}, | |
}, | |
[State.ASSESS]: { | |
on: { | |
NEXT: State.ESTIMATE, | |
}, | |
}, | |
[State.ESTIMATE]: { | |
on: { | |
NEXT: State.ASSIGN, | |
}, | |
}, | |
[State.ASSIGN]: { | |
on: { | |
NEXT: State.PUBLISH, | |
}, | |
}, | |
[State.PUBLISH]: { | |
on: { | |
NEXT: State.DONE, | |
}, | |
}, | |
[State.DONE]: { | |
type: 'final', | |
}, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment