@@ -17,12 +17,15 @@ class CleverreachCRM extends crm_1.CRM {
17
17
constructor ( opt ) {
18
18
super ( opt ) ;
19
19
this . token = null ;
20
+ this . campaignCache = new Map ( ) ; // Store campaigns in memory
20
21
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 ) ;
22
24
return true ;
23
25
} ) ;
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 ) ;
26
29
return r ;
27
30
} ) ;
28
31
this . initializeToken = ( ) => __awaiter ( this , void 0 , void 0 , function * ( ) {
@@ -38,18 +41,19 @@ class CleverreachCRM extends crm_1.CRM {
38
41
if ( this . verbose ) {
39
42
console . log ( message ) ;
40
43
}
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
+ }
42
48
// listId might be different for each campaign
43
49
// custom label is different for each campaign
44
50
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 ) ) {
45
51
console . error ( `Campaign config params missing, set the listId and custom label for the campaign ${ message . campaign . name } ` ) ;
46
52
}
47
53
;
48
54
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 ;
53
57
if ( ! this . token ) {
54
58
throw new Error ( "Token is not available" ) ;
55
59
}
0 commit comments