|
| 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; |
0 commit comments