diff --git a/src/handlers/mail.handlers.ts b/src/handlers/mail.handlers.ts index d48609e..d98a8f9 100644 --- a/src/handlers/mail.handlers.ts +++ b/src/handlers/mail.handlers.ts @@ -4,14 +4,14 @@ import { sendContactMail, sendDemoMail } from "../mail"; export const contactEmail = async (req: Request, res: Response): Promise => { 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(); diff --git a/src/mail/index.ts b/src/mail/index.ts index c1f52af..7fc3080 100644 --- a/src/mail/index.ts +++ b/src/mail/index.ts @@ -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"; @@ -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, @@ -118,6 +124,65 @@ const sendMail = async (form: Record) => { } }; +const sendMessageToLeadsChannel = async (form: Record) => { + try { + const text = Object.entries(form) + .map(([key, value]) => `${key}: ${value}`) + .join("\n"); + + const res = await axios.post( + "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) => { + const { postId, ...formData } = form; + try { + const text = Object.entries(formData) + .map(([key, value]) => `${key}: ${value}`) + .join("\n"); + + const res = await axios.put( + `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): Promise => { + const { postId, completeJson, userAgentData, ...form } = formData; + if (postId) { + return await updateMessageInLeadsChannel({ ...form, postId }); + } + return await sendMessageToLeadsChannel(form); +}; + const sendDataToBambooTable = async (initialForm: Record, tableSlug = BAMBOO_TABLE_SLUG) => { console.debug("START: SENDING DATA TO BAMBOO TABLE", initialForm, tableSlug); // Map form fields to bamboo fields @@ -241,11 +306,22 @@ export const sendContactMail = async (form: Record) => { 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); } };