Skip to content

Commit 7d8d516

Browse files
committed
Refetch the campaign when it is updated for hateaid
1 parent e8c4755 commit 7d8d516

File tree

5 files changed

+27
-24
lines changed

5 files changed

+27
-24
lines changed

data/hateaid.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@
1818
"actionPageId": 3012,
1919
"campaign": {
2020
"contactSchema": "basic",
21-
"externalId": 62198700,
22-
"name": "ha_testone",
23-
"title": "HA Test-One 11"
21+
"name": "ha_testone"
22+
2423
},
2524
"campaignId": 492,
2625
"contact": {
2726
"area": null,
2827
"contactRef": "1E4HalXr1WJkKnaeSyOgI8zTG8cKMu-vgFzWqeICtxY",
2928
"dupeRank": 16,
30-
"email": "ivana@fixthestatusquo.org",
29+
"email": "brucewayne@test.fixthestatusquo.org",
3130
"firstName": "Bruce"
3231
},
3332
"org": {

dist/crm/cleverreach.js

+12-8
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ class CleverreachCRM extends crm_1.CRM {
1717
constructor(opt) {
1818
super(opt);
1919
this.token = null;
20+
this.campaignCache = new Map(); // Store campaigns in memory
2021
this.handleCampaignUpdate = (message) => __awaiter(this, void 0, void 0, function* () {
21-
//we are handling campaign updates to remove them from the queue
22+
//we need to refetch campaign when it is updated
23+
yield this.fetchCampaign(message.campaignId);
2224
return true;
2325
});
24-
this.fetchCampaign = (campaign) => __awaiter(this, void 0, void 0, function* () {
25-
const r = yield (0, proca_1.fetchCampaign)(campaign.id);
26+
this.fetchCampaign = (id) => __awaiter(this, void 0, void 0, function* () {
27+
const r = yield (0, proca_1.fetchCampaign)(id);
28+
this.campaignCache.set(id, r);
2629
return r;
2730
});
2831
this.initializeToken = () => __awaiter(this, void 0, void 0, function* () {
@@ -38,18 +41,19 @@ class CleverreachCRM extends crm_1.CRM {
3841
if (this.verbose) {
3942
console.log(message);
4043
}
41-
const camp = yield this.campaign(message.campaign);
44+
let camp = this.campaignCache.get(message.campaign.id);
45+
if (!camp) {
46+
camp = yield this.fetchCampaign(message.campaign.id);
47+
}
4248
// listId might be different for each campaign
4349
// custom label is different for each campaign
4450
if (!((_c = (_b = (_a = camp.config) === null || _a === void 0 ? void 0 : _a.component) === null || _b === void 0 ? void 0 : _b.sync) === null || _c === void 0 ? void 0 : _c.listId) || !((_f = (_e = (_d = camp.config) === null || _d === void 0 ? void 0 : _d.component) === null || _e === void 0 ? void 0 : _e.sync) === null || _f === void 0 ? void 0 : _f.customLabel)) {
4551
console.error(`Campaign config params missing, set the listId and custom label for the campaign ${message.campaign.name}`);
4652
}
4753
;
4854
yield this.initializeToken();
49-
const listId = ((_j = (_h = (_g = camp.config) === null || _g === void 0 ? void 0 : _g.component) === null || _h === void 0 ? void 0 : _h.sync) === null || _j === void 0 ? void 0 : _j.listId)
50-
|| process.env.CRM_LIST_ID || "666";
51-
const customLabel = ((_m = (_l = (_k = camp.config) === null || _k === void 0 ? void 0 : _k.component) === null || _l === void 0 ? void 0 : _l.sync) === null || _m === void 0 ? void 0 : _m.customLabel)
52-
|| message.campaign.id + " " + message.campaign.title;
55+
const listId = (_j = (_h = (_g = camp.config) === null || _g === void 0 ? void 0 : _g.component) === null || _h === void 0 ? void 0 : _h.sync) === null || _j === void 0 ? void 0 : _j.listId;
56+
const customLabel = (_m = (_l = (_k = camp.config) === null || _k === void 0 ? void 0 : _k.component) === null || _l === void 0 ? void 0 : _l.sync) === null || _m === void 0 ? void 0 : _m.customLabel;
5357
if (!this.token) {
5458
throw new Error("Token is not available");
5559
}

dist/crm/cleverreach/client.js

-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ const getGroups = (token, listId) => __awaiter(void 0, void 0, void 0, function*
8787
throw new Error(`Get groups failed: ${response.statusText}`);
8888
}
8989
const data = yield response.text();
90-
console.log('Get groups response status:', data);
9190
return data;
9291
}
9392
catch (error) {

src/crm/cleverreach.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export type Message = ActionMessage | EventMessage;
1515

1616
class CleverreachCRM extends CRM {
1717
token: string | null = null;
18+
campaignCache = new Map<number, ProcaCampaign>(); // Store campaigns in memory
1819

1920
constructor(opt: {}) {
2021
super(opt);
@@ -23,12 +24,14 @@ class CleverreachCRM extends CRM {
2324
}
2425

2526
handleCampaignUpdate = async (message: CampaignUpdatedEvent): Promise<handleResult | boolean> => {
26-
//we are handling campaign updates to remove them from the queue
27+
//we need to refetch campaign when it is updated
28+
await this.fetchCampaign(message.campaignId);
2729
return true;
2830
}
2931

30-
fetchCampaign = async (campaign: ProcaCampaign): Promise<any> => {
31-
const r = await procaCampaign(campaign.id);
32+
fetchCampaign = async (id: number): Promise<any> => {
33+
const r = await procaCampaign(id);
34+
this.campaignCache.set(id, r);
3235
return r;
3336
}
3437

@@ -44,8 +47,10 @@ class CleverreachCRM extends CRM {
4447
if (this.verbose) {
4548
console.log(message);
4649
}
47-
48-
const camp = await this.campaign(message.campaign);
50+
let camp = this.campaignCache.get(message.campaign.id);
51+
if (!camp) {
52+
camp = await this.fetchCampaign(message.campaign.id);
53+
}
4954

5055
// listId might be different for each campaign
5156
// custom label is different for each campaign
@@ -55,11 +60,8 @@ class CleverreachCRM extends CRM {
5560

5661
await this.initializeToken();
5762

58-
const listId = camp.config?.component?.sync?.listId
59-
|| process.env.CRM_LIST_ID || "666";
60-
61-
const customLabel = camp.config?.component?.sync?.customLabel
62-
|| message.campaign.id + " " + message.campaign.title;
63+
const listId = camp.config?.component?.sync?.listId;
64+
const customLabel = camp.config?.component?.sync?.customLabel;
6365

6466
if (!this.token) {
6567
throw new Error("Token is not available");

src/crm/cleverreach/client.ts

-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ export const getGroups = async (token: string, listId: number) => {
8181
}
8282

8383
const data = await response.text();
84-
console.log('Get groups response status:', data);
8584
return data;
8685
} catch (error) {
8786
console.error('Get groups contact error:', error.message);

0 commit comments

Comments
 (0)