woocommerce integration, controllers and handlers ready, evolution api simulator ready
This commit is contained in:
87
index.js
87
index.js
@@ -1,12 +1,15 @@
|
||||
import "dotenv/config";
|
||||
import express from "express";
|
||||
import cors from "cors";
|
||||
import crypto from "crypto";
|
||||
import path from "path";
|
||||
import { fileURLToPath } from "url";
|
||||
import { ensureTenant, listConversations, listRuns, getRunById } from "./src/db/repo.js";
|
||||
import { addSseClient, removeSseClient, sseSend } from "./src/services/sse.js";
|
||||
import { processMessage } from "./src/services/pipeline.js";
|
||||
import { ensureTenant } from "./src/db/repo.js";
|
||||
import { addSseClient, removeSseClient } from "./src/services/sse.js";
|
||||
import { makeGetConversations } from "./src/controllers/conversations.js";
|
||||
import { makeListRuns, makeGetRunById } from "./src/controllers/runs.js";
|
||||
import { makeSimSend } from "./src/controllers/sim.js";
|
||||
import { makeEvolutionWebhook } from "./src/controllers/evolution.js";
|
||||
import { makeGetConversationState } from "./src/controllers/conversationState.js";
|
||||
|
||||
const app = express();
|
||||
app.use(cors());
|
||||
@@ -46,79 +49,19 @@ app.get("/stream", (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* --- Simulator ---
|
||||
* POST /sim/send { chat_id, from_phone, text }
|
||||
*/
|
||||
app.post("/sim/send", async (req, res) => {
|
||||
const { chat_id, from_phone, text } = req.body || {};
|
||||
if (!chat_id || !from_phone || !text) {
|
||||
return res.status(400).json({ ok: false, error: "chat_id, from_phone, text are required" });
|
||||
}
|
||||
|
||||
try {
|
||||
const provider = "sim";
|
||||
const message_id = crypto.randomUUID(); // idempotencia por mensaje sim
|
||||
const result = await processMessage({
|
||||
tenantId: TENANT_ID,
|
||||
chat_id,
|
||||
from: from_phone,
|
||||
text,
|
||||
provider,
|
||||
message_id,
|
||||
});
|
||||
res.json({ ok: true, run_id: result.run_id, reply: result.reply });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ ok: false, error: "internal_error", detail: String(err?.message || err) });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* --- UI data endpoints ---
|
||||
*/
|
||||
app.get("/conversations", async (req, res) => {
|
||||
const { q = "", status = "", state = "", limit = "50" } = req.query;
|
||||
try {
|
||||
const items = await listConversations({
|
||||
tenant_id: TENANT_ID,
|
||||
q: String(q || ""),
|
||||
status: String(status || ""),
|
||||
state: String(state || ""),
|
||||
limit: parseInt(limit, 10) || 50,
|
||||
});
|
||||
res.json({ items });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ ok: false, error: "internal_error" });
|
||||
}
|
||||
});
|
||||
app.post("/sim/send", makeSimSend());
|
||||
|
||||
app.get("/runs", async (req, res) => {
|
||||
const { chat_id = null, limit = "50" } = req.query;
|
||||
try {
|
||||
const items = await listRuns({
|
||||
tenant_id: TENANT_ID,
|
||||
wa_chat_id: chat_id ? String(chat_id) : null,
|
||||
limit: parseInt(limit, 10) || 50,
|
||||
});
|
||||
res.json({ items });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ ok: false, error: "internal_error" });
|
||||
}
|
||||
});
|
||||
app.get("/conversations", makeGetConversations(() => TENANT_ID));
|
||||
app.get("/conversations/state", makeGetConversationState(() => TENANT_ID));
|
||||
|
||||
app.get("/runs/:run_id", async (req, res) => {
|
||||
try {
|
||||
const run = await getRunById({ tenant_id: TENANT_ID, run_id: req.params.run_id });
|
||||
if (!run) return res.status(404).json({ ok: false, error: "not_found" });
|
||||
res.json(run);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ ok: false, error: "internal_error" });
|
||||
}
|
||||
});
|
||||
app.get("/runs", makeListRuns(() => TENANT_ID));
|
||||
|
||||
app.get("/runs/:run_id", makeGetRunById(() => TENANT_ID));
|
||||
|
||||
app.post("/webhook/evolution", makeEvolutionWebhook());
|
||||
|
||||
app.get("/", (req, res) => {
|
||||
res.sendFile(path.join(publicDir, "index.html"));
|
||||
|
||||
Reference in New Issue
Block a user