Skip to content

Commit 66e15f3

Browse files
committed
Add engaging networks integration
1 parent 6c453cb commit 66e15f3

File tree

3 files changed

+285
-0
lines changed

3 files changed

+285
-0
lines changed

data/naturecanada.json

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"action": {
3+
"actionType": "register",
4+
"createdAt": "2025-02-05T01:09:10Z",
5+
"customFields": {
6+
"emailProvider": "icloud.com"
7+
},
8+
"testing": false
9+
},
10+
"actionId": 5376426,
11+
"actionPage": {
12+
"locale": "en",
13+
"name": "proca test/en",
14+
"supporterConfirmTemplate": null,
15+
"thankYouTemplate": null,
16+
"thankYouTemplateRef": null
17+
},
18+
"actionPageId": 3788,
19+
"campaign": {
20+
"contactSchema": "basic",
21+
"externalId": null,
22+
"name": "proca test",
23+
"title": "proca test"
24+
},
25+
"campaignId": 833,
26+
"contact": {
27+
"area": "CA",
28+
"contactRef": "-8pbQz1i23e8A5Ek62FV4z65gulIWywDZHtDvx1VqDU",
29+
"country": "CA",
30+
"dupeRank": 0,
31+
"email": "[email protected]",
32+
"firstName": "BruceUpdated",
33+
"lastName": "Wayne",
34+
"postcode": "M4N2G7"
35+
},
36+
"org": {
37+
"name": "naturecanada",
38+
"title": "Nature Canada"
39+
},
40+
"orgId": 1028,
41+
"personalInfo": null,
42+
"privacy": {
43+
"emailStatus": null,
44+
"emailStatusChanged": null,
45+
"givenAt": "2025-02-05T01:09:10Z",
46+
"optIn": true,
47+
"withConsent": true
48+
},
49+
"schema": "proca:action:2",
50+
"stage": "deliver",
51+
"tracking": {
52+
"campaign": "proca test"
53+
54+
}
55+
}

dist/crm/engagingnetworks.js

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4+
return new (P || (P = Promise))(function (resolve, reject) {
5+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8+
step((generator = generator.apply(thisArg, _arguments || [])).next());
9+
});
10+
};
11+
var __importDefault = (this && this.__importDefault) || function (mod) {
12+
return (mod && mod.__esModule) ? mod : { "default": mod };
13+
};
14+
Object.defineProperty(exports, "__esModule", { value: true });
15+
exports.upsertSupporter = exports.getToken = void 0;
16+
const crm_1 = require("../crm");
17+
const dotenv_1 = __importDefault(require("dotenv"));
18+
dotenv_1.default.config();
19+
const apiUrl = process.env.CRM_URL;
20+
const apiToken = process.env.CRM_API_TOKEN;
21+
if (!apiUrl || !apiToken) {
22+
console.error("No ccccredentials");
23+
process.exit(1);
24+
}
25+
const authHeaders = {
26+
'Accept': 'application/json',
27+
'Content-Type': 'application/json'
28+
};
29+
const getToken = () => __awaiter(void 0, void 0, void 0, function* () {
30+
try {
31+
const response = yield fetch(apiUrl + 'authenticate', {
32+
method: 'POST',
33+
headers: authHeaders,
34+
body: apiToken
35+
});
36+
if (!response.ok) {
37+
const errorBody = yield response.text();
38+
throw new Error(`Error fetching token: ${response.statusText} - ${errorBody}`);
39+
}
40+
const data = yield response.json();
41+
return data['ens-auth-token'];
42+
}
43+
catch (error) {
44+
console.error('Error:', error.message);
45+
}
46+
});
47+
exports.getToken = getToken;
48+
const upsertSupporter = (data, token) => __awaiter(void 0, void 0, void 0, function* () {
49+
try {
50+
const response = yield fetch(apiUrl + 'supporter', {
51+
method: "POST",
52+
headers: {
53+
"Accept": "application/json",
54+
"Content-Type": "application/json",
55+
"ens-auth-token": token,
56+
},
57+
body: JSON.stringify(data)
58+
});
59+
if (!response.ok) {
60+
throw new Error(`HTTP error! Status: ${response.status}`);
61+
}
62+
const result = yield response.json();
63+
return response.status;
64+
}
65+
catch (error) {
66+
console.error("Error:", error);
67+
}
68+
});
69+
exports.upsertSupporter = upsertSupporter;
70+
class CleverreachCRM extends crm_1.CRM {
71+
constructor(opt) {
72+
super(opt);
73+
this._token = null;
74+
this.handleContact = (message) => __awaiter(this, void 0, void 0, function* () {
75+
var _a, _b;
76+
console.log("Action taken from the queue", message.action.id);
77+
if (this.verbose) {
78+
console.log(message);
79+
}
80+
const token = yield this.getToken();
81+
if (!token) {
82+
throw new Error("Auth token is missing");
83+
}
84+
const data = {
85+
'Email Address': message.contact.email,
86+
'First Name': message.contact.firstName,
87+
'Last Name': message.contact.lastName || "",
88+
'Address 1': message.contact.street || "",
89+
Postcode: ((_a = message.contact) === null || _a === void 0 ? void 0 : _a.postcode) || "",
90+
Phone: ((_b = message.contact) === null || _b === void 0 ? void 0 : _b.phone) || "",
91+
"questions": {
92+
"Accepts Email": "Y"
93+
}
94+
};
95+
console.log(data);
96+
const status = yield (0, exports.upsertSupporter)(data, token);
97+
return status === 200;
98+
});
99+
this.crmType = crm_1.CRMType.OptIn;
100+
}
101+
// Getter for token that initializes it if needed
102+
getToken() {
103+
return __awaiter(this, void 0, void 0, function* () {
104+
if (!this._token) {
105+
this._token = yield (0, exports.getToken)();
106+
}
107+
return this._token;
108+
});
109+
}
110+
}
111+
exports.default = CleverreachCRM;

src/crm/engagingnetworks.ts

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import {
2+
CRM,
3+
CRMType,
4+
ActionMessage,
5+
EventMessage,
6+
handleResult
7+
} from "../crm";
8+
import dotenv from 'dotenv';
9+
10+
dotenv.config();
11+
12+
const apiUrl = process.env.CRM_URL;
13+
const apiToken = process.env.CRM_API_TOKEN;
14+
15+
16+
if (!apiUrl || !apiToken) {
17+
console.error("No ccccredentials");
18+
process.exit(1);
19+
}
20+
21+
const authHeaders = {
22+
'Accept': 'application/json',
23+
'Content-Type': 'application/json'
24+
};
25+
26+
export const getToken = async () => {
27+
try {
28+
const response = await fetch(apiUrl+'authenticate', {
29+
method: 'POST',
30+
headers: authHeaders,
31+
body: apiToken
32+
})
33+
34+
if (!response.ok) {
35+
const errorBody = await response.text();
36+
throw new Error(`Error fetching token: ${response.statusText} - ${errorBody}`);
37+
}
38+
39+
const data = await response.json();
40+
return data['ens-auth-token'];
41+
} catch (error) {
42+
console.error('Error:', error.message);
43+
}
44+
}
45+
46+
export const upsertSupporter = async (data, token: string) => {
47+
try {
48+
const response = await fetch(apiUrl + 'supporter', {
49+
method: "POST",
50+
headers: {
51+
"Accept": "application/json",
52+
"Content-Type": "application/json",
53+
"ens-auth-token": token,
54+
},
55+
body: JSON.stringify(data)
56+
});
57+
58+
if (!response.ok) {
59+
throw new Error(`HTTP error! Status: ${response.status}`);
60+
}
61+
62+
const result = await response.json();
63+
return response.status
64+
} catch (error) {
65+
console.error("Error:", error);
66+
}
67+
}
68+
69+
class CleverreachCRM extends CRM {
70+
private _token: string | null = null;
71+
72+
constructor(opt: {}) {
73+
super(opt);
74+
this.crmType = CRMType.OptIn;
75+
}
76+
77+
// Getter for token that initializes it if needed
78+
private async getToken(): Promise<string> {
79+
if (!this._token) {
80+
this._token = await getToken();
81+
}
82+
return this._token as string;
83+
}
84+
85+
handleContact = async (
86+
message: ActionMessage
87+
): Promise<handleResult | boolean> => {
88+
console.log("Action taken from the queue", message.action.id);
89+
90+
if (this.verbose) {
91+
console.log(message);
92+
}
93+
94+
const token = await this.getToken();
95+
96+
if (!token) {
97+
throw new Error("Auth token is missing");
98+
}
99+
100+
const data = {
101+
'Email Address': message.contact.email,
102+
'First Name': message.contact.firstName,
103+
'Last Name': message.contact.lastName || "",
104+
'Address 1': message.contact.street || "",
105+
Postcode: message.contact?.postcode || "",
106+
Phone: message.contact?.phone || "",
107+
"questions": {
108+
"Accepts Email": "Y"
109+
}
110+
};
111+
112+
console.log(data)
113+
114+
const status = await upsertSupporter(data, token);
115+
return status === 200;
116+
};
117+
}
118+
119+
export default CleverreachCRM;

0 commit comments

Comments
 (0)