Skip to content

Commit

Permalink
Add contact to the list
Browse files Browse the repository at this point in the history
  • Loading branch information
1v4n4 committed Oct 28, 2024
1 parent 24df6c0 commit 7c52673
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions src/crm/mailjetDOI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ class MailjetDOI extends CRM {
return true;
}

addContactToList = async (email: string): Promise<void> => {
try {
const response = await this.mailjet
.post("listrecipient", { 'version': 'v3' })
.request({
"IsUnsubscribed": "true",
"ContactAlt": email,
"ListID": this.list
});

console.log(`Contact ${email} added to list ${this.list}:`, response.body);
} catch (error: any) {
console.log(`Failed to add contact ${email} to list ${this.list}: ${error.statusCode}`);
}
}

handleMessage = async (
message: ActionMessage | EventMessage
): Promise<handleResult | boolean> => {
Expand All @@ -89,16 +105,23 @@ class MailjetDOI extends CRM {
"Email": message.contact.email
});

return (status === 201 ? true : false);
} catch (e: any) {
if (e.response.statusText.includes("already exists")) {
return this.updateContact(message);
}
console.log(e.message);
if (status === 201) {
// Add contact to the list
await this.addContactToList(message.contact.email);
return true;
} else {
return false;
}
} catch (e: any) {
if (e.response.statusText.includes("already exists")) {
await this.updateContact(message);
await this.addContactToList(message.contact.email);
return true;
}
console.log(e.message);
return false;
}

}

updateContact = async (
message: Message
Expand All @@ -124,7 +147,8 @@ class MailjetDOI extends CRM {
console.log(error.message);
return false;
}
}
}

}

export default MailjetDOI;

0 comments on commit 7c52673

Please sign in to comment.