styling and create customer woo bug handled
This commit is contained in:
18
src/controllers/messages.js
Normal file
18
src/controllers/messages.js
Normal file
@@ -0,0 +1,18 @@
|
||||
import { handleListMessages } from "../handlers/messages.js";
|
||||
|
||||
export const makeListMessages = (tenantIdOrFn) => async (req, res) => {
|
||||
try {
|
||||
const tenantId = typeof tenantIdOrFn === "function" ? tenantIdOrFn() : tenantIdOrFn;
|
||||
const items = await handleListMessages({
|
||||
tenantId,
|
||||
chat_id: req.query.chat_id || null,
|
||||
limit: req.query.limit || "200",
|
||||
});
|
||||
res.json({ items });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ ok: false, error: "internal_error" });
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -278,6 +278,27 @@ export async function getRecentMessagesForLLM({
|
||||
}));
|
||||
}
|
||||
|
||||
export async function listMessages({ tenant_id, wa_chat_id, limit = 200 }) {
|
||||
const lim = Math.max(1, Math.min(500, parseInt(limit, 10) || 200));
|
||||
const q = `
|
||||
select provider, message_id, direction, ts, text, payload, run_id
|
||||
from wa_messages
|
||||
where tenant_id=$1 and wa_chat_id=$2
|
||||
order by ts asc
|
||||
limit $3
|
||||
`;
|
||||
const { rows } = await pool.query(q, [tenant_id, wa_chat_id, lim]);
|
||||
return rows.map((r) => ({
|
||||
provider: r.provider,
|
||||
message_id: r.message_id,
|
||||
direction: r.direction,
|
||||
ts: r.ts,
|
||||
text: r.text,
|
||||
payload: r.payload,
|
||||
run_id: r.run_id,
|
||||
}));
|
||||
}
|
||||
|
||||
export async function getTenantByKey(key) {
|
||||
const { rows } = await pool.query(`select id, key, name from tenants where key=$1`, [key]);
|
||||
return rows[0] || null;
|
||||
|
||||
12
src/handlers/messages.js
Normal file
12
src/handlers/messages.js
Normal file
@@ -0,0 +1,12 @@
|
||||
import { listMessages } from "../db/repo.js";
|
||||
|
||||
export async function handleListMessages({ tenantId, chat_id, limit = "200" }) {
|
||||
if (!chat_id) return [];
|
||||
return listMessages({
|
||||
tenant_id: tenantId,
|
||||
wa_chat_id: String(chat_id),
|
||||
limit: parseInt(limit, 10) || 200,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user