-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from amcpanaligan/CheckListMembership
Check List Membership Implementation
- Loading branch information
Showing
4 changed files
with
169 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './prospect-aware'; | ||
export * from './list-membership-aware'; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export class ListMembershipAware { | ||
public client: any; | ||
public clientReady: Promise<boolean>; | ||
public retry: any; | ||
|
||
public attempt: (fn: () => Promise<any>, retryCount?: number) => Promise<any>; | ||
|
||
async readByListIdAndProspectId(listId, prospectId) { | ||
await this.clientReady; | ||
return this.attempt(() => { | ||
return new Promise((resolve, reject) => { | ||
this.client.listMemberships.readByListIdAndProspectId(listId, prospectId).then(resolve).catch(reject); | ||
}); | ||
}); | ||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import { BaseStep, ExpectedRecord, Field, StepInterface } from '../../core/base-step'; | ||
import { Step, RunStepResponse, FieldDefinition, StepDefinition, RecordDefinition } from '../../proto/cog_pb'; | ||
|
||
export class CheckListMembership extends BaseStep implements StepInterface { | ||
|
||
protected stepName: string = 'Check Pardot List Membership'; | ||
protected stepExpression: string = 'the (?<email>.+) pardot prospect should (?<optInOut>(be opted in to|be opted out of|not be a member of)) list (?<listId>.+)'; | ||
protected stepType: StepDefinition.Type = StepDefinition.Type.VALIDATION; | ||
protected expectedFields: Field[] = [{ | ||
field: 'email', | ||
type: FieldDefinition.Type.EMAIL, | ||
description: 'The Email Address of the Prospect', | ||
}, { | ||
field: 'optInOut', | ||
type: FieldDefinition.Type.STRING, | ||
description: 'One of "be opted in to", "be opted out of", or "not be a member of"', | ||
}, { | ||
field: 'listId', | ||
type: FieldDefinition.Type.NUMERIC, | ||
description: 'The ID of the Pardot List', | ||
}]; | ||
protected expectedRecords: ExpectedRecord[] = [{ | ||
id: 'listMembership', | ||
type: RecordDefinition.Type.KEYVALUE, | ||
fields: [{ | ||
field: 'id', | ||
type: FieldDefinition.Type.NUMERIC, | ||
description: "List Membership's Pardot ID", | ||
}, { | ||
field: 'list_id', | ||
type: FieldDefinition.Type.NUMERIC, | ||
description: "List's Pardot ID", | ||
}, { | ||
field: 'prospect_id', | ||
type: FieldDefinition.Type.NUMERIC, | ||
description: "Prospect's Pardot ID", | ||
}, { | ||
field: 'opted_out', | ||
type: FieldDefinition.Type.BOOLEAN, | ||
description: 'Opted Out', | ||
}, { | ||
field: 'created_at', | ||
type: FieldDefinition.Type.DATETIME, | ||
description: 'The date created', | ||
}, { | ||
field: 'updated_at', | ||
type: FieldDefinition.Type.DATETIME, | ||
description: 'The date updated', | ||
}], | ||
dynamicFields: false, | ||
}, { | ||
id: 'prospect', | ||
type: RecordDefinition.Type.KEYVALUE, | ||
fields: [{ | ||
field: 'id', | ||
type: FieldDefinition.Type.NUMERIC, | ||
description: "Prospect's Pardot ID", | ||
}, { | ||
field: 'email', | ||
type: FieldDefinition.Type.EMAIL, | ||
description: "Prospect's Email Address", | ||
}, { | ||
field: 'created_at', | ||
type: FieldDefinition.Type.DATETIME, | ||
description: 'The date/time the Prospect was created', | ||
}, { | ||
field: 'updated_at', | ||
type: FieldDefinition.Type.DATETIME, | ||
description: 'The date/time the Prospect was updated', | ||
}], | ||
dynamicFields: true, | ||
}]; | ||
|
||
async executeStep(step: Step): Promise<RunStepResponse> { | ||
const stepData: any = step.getData().toJavaScript(); | ||
const email = stepData.email; | ||
const optInOut = stepData.optInOut; | ||
const listId = stepData.listId; | ||
let listMembershipRecord; | ||
let prospectRecord; | ||
|
||
try { | ||
const prospect = await this.client.readByEmail(email); | ||
|
||
if (!prospect) { | ||
return this.error('No prospect found with email %s', [email]); | ||
} | ||
|
||
prospectRecord = this.keyValue('prospect', 'Checked Prospect', prospect); | ||
|
||
const listMembership = (await this.client.readByListIdAndProspectId(listId, prospect.id)).list_membership; | ||
|
||
if (listMembership) { | ||
listMembershipRecord = this.keyValue('listMembership', 'List Membership', listMembership); | ||
} | ||
|
||
if (optInOut === 'not be a member of') { | ||
if (!listMembership) { | ||
return this.pass( | ||
'Prospect %s is not a member of %d, as expected.', | ||
[email, listId], | ||
[prospectRecord], | ||
); | ||
} else { | ||
return this.fail( | ||
'Expected prospect %s to not be a member of list %d, but a list membership was found', | ||
[email, listId], | ||
[prospectRecord, listMembershipRecord], | ||
); | ||
} | ||
} | ||
|
||
if (optInOut === 'be opted in to') { | ||
if (listMembership.opted_out) { | ||
return this.fail( | ||
'Expected prospect %s to be opted in to list %d, but the prospect is opted out.', | ||
[email, listId], | ||
[prospectRecord, listMembershipRecord], | ||
); | ||
} else { | ||
return this.pass( | ||
'Prospect %s is opted in to list %s, as expected.', | ||
[email, listId], | ||
[prospectRecord, listMembershipRecord], | ||
); | ||
} | ||
} else if (optInOut === 'be opted out of') { | ||
if (listMembership.opted_out) { | ||
return this.pass( | ||
'Prospect %s is opted out of list %s, as expected.', | ||
[email, listId], | ||
[prospectRecord, listMembershipRecord], | ||
); | ||
} else { | ||
return this.fail( | ||
'Expected prospect %s to be opted out of list %d, but the prospect is opted in.', | ||
[email, listId], | ||
[prospectRecord, listMembershipRecord], | ||
); | ||
} | ||
} | ||
} catch (e) { | ||
return this.error('There was a problem checking list membership: %s', [e.toString()]); | ||
} | ||
} | ||
} | ||
|
||
export { CheckListMembership as Step }; |