Skip to content

Commit

Permalink
Updating repository
Browse files Browse the repository at this point in the history
  • Loading branch information
gi-carnaval committed Feb 19, 2025
1 parent 521027f commit 36e00fb
Show file tree
Hide file tree
Showing 25 changed files with 484 additions and 389 deletions.
Binary file modified dist.zip
Binary file not shown.
321 changes: 162 additions & 159 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"body-parser": "^1.20.3",
"dotenv": "^16.4.7",
"express": "^4.21.2",
"ky": "^1.7.5",
"pocketbase": "^0.25.1",
"twilio": "^5.4.2"
},
Expand All @@ -26,7 +27,7 @@
"concurrently": "^9.1.2",
"nodemon": "^3.1.9",
"ts-node": "^10.9.2",
"tsx": "^4.19.2",
"tsx": "^3.12.10",
"typescript": "^5.7.3"
}
}
}
39 changes: 39 additions & 0 deletions src/api/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import ky, { KyResponse } from "ky";
import { ApiError } from "../types/api-errors.js";
import { handleError } from "../utils/logger.js";
import { Response } from "express";

export const api = ky.create({
prefixUrl: process.env.PLOOMES_API_URL,
hooks: {
beforeRequest: [
(request) => {
const userKey = process.env.PLOOMES_API_USER_KEY
request.headers.set("User-Key", `${userKey}`)
}
],
beforeError: [
async error => {
const { response } = error;

if (!response.ok) {
const apiError: ApiError = await response.json()
const expressLikeResponse = adaptKyResponse(response)
const errorMessage = handleError(expressLikeResponse, apiError.message || '', apiError.message || '')
error.name = apiError.message || "Unkown error";
error.message = `${errorMessage}`;
}
return error;
}
]
}
})

function adaptKyResponse(kyRes: KyResponse<unknown>): Partial<Response> {
return {
status: function (code: number) {
return { ...this, statusCode: code } as Response
},
statusCode: kyRes.status,
}
}
18 changes: 18 additions & 0 deletions src/api/ploomes-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { LeadFromPloomesAPIProps } from "../types/ploomes.js";
import { api } from "./client.js";

export const getCompleteDeal = async (dealId: number) => {
const filterDealId = `Id+eq+${dealId}`;
const expandFields = "Contact,OtherProperties,Person($expand=Phones)";

try {
const result = await api
.get(`Deals?$filter=${filterDealId}&$expand=${expandFields}`)
.json<LeadFromPloomesAPIProps>()

return { success: true, data: result, error: null }

} catch (error) {
return { success: false, data: null, error };
}
}
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import express from 'express'
import bodyParser from 'body-parser'
import webhookRoutes from "./routes/webhookRoutes.js";
import webhookRoutes from "./routes/webhook-routes.js";

const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
Expand Down
10 changes: 7 additions & 3 deletions src/config/notifications.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export const NOTIFICATION_NUMBERS = [
'+5514981668995',
'+5514997300621'
]
'+5514981668995', //Giovani
// '+5514997300621', //Lívia
// '+5511947577254', //Marcela
// '+5511959310506', //Xaiane
// '+5511984444383', //Wendell
// '+14075206493', //Tiago
];
109 changes: 0 additions & 109 deletions src/controller/webhookController.ts

This file was deleted.

37 changes: 37 additions & 0 deletions src/controller/webhooks/elementor-webhook-controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Request, Response } from "express";
import { handleError, logWebhook } from "../../utils/logger.js"
import elementorWebhookService from "../../services/elementor-webhook-service.js";
import leadService from "../../services/lead-service.js";
import whatsappService from "../../services/whatsapp-service.js";
import notificationService from "../../services/notificationService.js";

const processElementorWebhook = async (req: Request, res: Response) => {
logWebhook(`📩 [Elementor] Recebendo webhook: ${JSON.stringify(req.body)}`)

try {
const { form_id } = req.query
if (!form_id) return handleError(res, "Form id not provided", "❌ Form id não informado.")

const result = await elementorWebhookService.processLead(form_id.toString(), req.body)
if (!result.success || !result.lead) return handleError(res, "Error processing lead", `❌ ${result.error}`)

const lead = await leadService.createLead(result.lead)
if (!lead.success || !lead.data) {
handleError(res, "Erro ao criar lead", `❌ ${lead.error}`)
return
}

const notificationMessage = notificationService.formatLeadMessage(result.lead, form_id.toString(), lead.data.id)

const { success, error } = await whatsappService.sendMessages(notificationMessage)
if (!success) {
handleError(res, "Erro ao enviar mesagens", `❌ ${error}`)
}

res.status(200).json({ message: "Lead do Elementor processado e cadastrado com sucesso." })
} catch (error) {
handleError(res, "Internal Server Error", `❌ Erro inesperado (Elementor): ${error}`);
}
}

export default { processElementorWebhook }
23 changes: 0 additions & 23 deletions src/controller/webhooks/elementorWebhookController.ts

This file was deleted.

34 changes: 34 additions & 0 deletions src/controller/webhooks/ploomes-webhook-controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Request, Response } from "express";
import { handleError, logWebhook } from "../../utils/logger.js";
import ploomesWebhookService from "../../services/ploomes-webhook-service.js";
import leadService from "../../services/lead-service.js";
import notificationService from "../../services/notificationService.js";
import whatsappService from "../../services/whatsapp-service.js";

const processPloomesWebhook = async (req: Request, res: Response) => {
logWebhook(`📩 [Ploomes] Recebendo webhook: ${JSON.stringify(req.body)}`)

try {
const result = await ploomesWebhookService.processDeal(req.body)
if (!result.success || !result.lead) return handleError(res, "Error processing deal", `❌ ${result.error}`)

const lead = await leadService.createLead(result.lead)
if (!lead.success || !lead.data) {
handleError(res, "Erro ao criar lead", `❌ ${lead.error}`)
return
}

const notificationMessage = notificationService.formatLeadMessage(result.lead, "form_ploomes", lead.data.id)

const { success, error } = await whatsappService.sendMessages(notificationMessage)
if (!success) {
handleError(res, "Erro ao enviar mesagens", `❌ ${error}`)
}

res.status(200).json({ message: "Lead da Plooms processado e cadastrado com sucesso." })
} catch (error) {
handleError(res, "Internal Server Error", `❌ Erro inesperado (Ploomes): ${error}`);
}
}

export default { processPloomesWebhook }
20 changes: 0 additions & 20 deletions src/controller/webhooks/ploomesWebhookController.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/routes/webhookRoutes.ts → src/routes/webhook-routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Router } from "express";
import elementorWebhookController from "../controller/webhooks/elementorWebhookController.js";
import ploomesWebhookController from "../controller/webhooks/ploomesWebhookController.js";
import elementorWebhookController from "../controller/webhooks/elementor-webhook-controller.js";
import ploomesWebhookController from "../controller/webhooks/ploomes-webhook-controller.js";

const router = Router();

Expand Down
Loading

0 comments on commit 36e00fb

Please sign in to comment.