Skip to content

Commit

Permalink
Merge pull request #35 from amcpanaligan/CheckListMembership
Browse files Browse the repository at this point in the history
Check List Membership Implementation
  • Loading branch information
iamEAP authored Apr 8, 2020
2 parents 2444f83 + cc46d3f commit 8202b14
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/client/client-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as pardot from 'lew-pardot';
import * as Retry from 'retry';
import { Field } from '../core/base-step';
import { FieldDefinition } from '../proto/cog_pb';
import { ProspectAwareMixin } from './mixins';
import { ListMembershipAware, ProspectAwareMixin } from './mixins';

class ClientWrapper {
public static expectedAuthFields: Field[] = [{
Expand Down Expand Up @@ -77,8 +77,8 @@ class ClientWrapper {
}
}

interface ClientWrapper extends ProspectAwareMixin {}
applyMixins(ClientWrapper, [ProspectAwareMixin]);
interface ClientWrapper extends ProspectAwareMixin, ListMembershipAware {}
applyMixins(ClientWrapper, [ProspectAwareMixin, ListMembershipAware]);

function applyMixins(derivedCtor: any, baseCtors: any[]) {
baseCtors.forEach((baseCtor) => {
Expand Down
1 change: 1 addition & 0 deletions src/client/mixins/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './prospect-aware';
export * from './list-membership-aware';
17 changes: 17 additions & 0 deletions src/client/mixins/list-membership-aware.ts
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);
});
});
}

}
148 changes: 148 additions & 0 deletions src/steps/listMembership/check-list-membership.ts
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 };

0 comments on commit 8202b14

Please sign in to comment.