Skip to content

Commit

Permalink
Merge pull request #6 from webbeetechnologies/improvemement/mattermos…
Browse files Browse the repository at this point in the history
…t-integration
  • Loading branch information
elvisduru authored Apr 18, 2024
2 parents 46409e8 + b754a8a commit b121fa5
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/handlers/mail.handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { sendContactMail, sendDemoMail } from "../mail";

export const contactEmail = async (req: Request, res: Response): Promise<any> => {
try {
await sendContactMail({
const postId = await sendContactMail({
...req.body,
name: req.body.name || req.body.email,
ipAddress: req.ip,
xForwardedForIp: req.headers["x-forwarded-for"] || req.socket.remoteAddress,
});

return res.send({ message: "Success" });
return res.send({ message: "Success", postId });
} catch (e) {
console.error(e);
return res.status(500).send();
Expand Down
80 changes: 78 additions & 2 deletions src/mail/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import axios, { AxiosResponse } from "axios";
import nodemailer from "nodemailer";
const MATTERMOST_INBOUNDS_CHANNEL_ID = "pp9amtzhebdy8bi7ikz6m3jjgw";
const MATTERMOST_LEADS_CHANNEL_ID = "krr34khcafn75qffyfkxod9epa";

import request from "graphql-request";

Expand Down Expand Up @@ -35,6 +36,11 @@ import { generateUniqueString, normalizeString, stringifyForGraphQL } from "./ut

// import { resetMailText } from "./texts";

type MattermostPost = {
id: string;
[key: string]: any;
};

const mailConfig = {
host: MAIL_HOST,
port: MAIL_PORT,
Expand Down Expand Up @@ -118,6 +124,65 @@ const sendMail = async (form: Record<string, any>) => {
}
};

const sendMessageToLeadsChannel = async (form: Record<string, any>) => {
try {
const text = Object.entries(form)
.map(([key, value]) => `${key}: ${value}`)
.join("\n");

const res = await axios.post<MattermostPost>(
"https://mattermost.bambooapp.ai/api/v4/posts",
{
channel_id: MATTERMOST_LEADS_CHANNEL_ID,
message: text,
},
{
headers: {
Authorization: `Bearer ${MATTERMOST_MAIL_BOT_ACCESS_TOKEN}`,
},
}
);

return res.data.id;
} catch (error) {
console.error(error);
}
};

const updateMessageInLeadsChannel = async (form: Record<string, any>) => {
const { postId, ...formData } = form;
try {
const text = Object.entries(formData)
.map(([key, value]) => `${key}: ${value}`)
.join("\n");

const res = await axios.put<MattermostPost>(
`https://mattermost.bambooapp.ai/api/v4/posts/${form.postId}`,
{
id: postId,
message: text,
},
{
headers: {
Authorization: `Bearer ${MATTERMOST_MAIL_BOT_ACCESS_TOKEN}`,
},
}
);

return res.data.id;
} catch (error) {
console.error(error);
}
};

const createOrUpdatePostInLeadsChannel = async (formData: Record<string, any>): Promise<string> => {
const { postId, completeJson, userAgentData, ...form } = formData;
if (postId) {
return await updateMessageInLeadsChannel({ ...form, postId });
}
return await sendMessageToLeadsChannel(form);
};

const sendDataToBambooTable = async (initialForm: Record<string, any>, tableSlug = BAMBOO_TABLE_SLUG) => {
console.debug("START: SENDING DATA TO BAMBOO TABLE", initialForm, tableSlug);
// Map form fields to bamboo fields
Expand Down Expand Up @@ -241,11 +306,22 @@ export const sendContactMail = async (form: Record<string, any>) => {

console.debug("RECORD FOUND", isExistingRecord);

await sendDataToBambooTable(form, tableSlug);
if (!isExistingRecord) await sendMail(form);
const { postId, ...formData } = form;

await sendDataToBambooTable(formData, tableSlug);
if (!isExistingRecord) await sendMail(formData);

// Create or Update post in leads channel
const postIdRes = createOrUpdatePostInLeadsChannel({ ...formData, postId });
console.debug("POST_ID", postIdRes);

return postIdRes;
} catch (err) {
console.debug("Error sending request to bamboo: Sending to Mattermost", err);
await sendMail(form);
// Create or Update post in leads channel
const postIdRes = createOrUpdatePostInLeadsChannel({ ...form });
console.debug("POST_ID", postIdRes);
}
};

Expand Down

0 comments on commit b121fa5

Please sign in to comment.