modularizado de prompts
This commit is contained in:
126
src/modules/0-ui/controllers/takeovers.js
Normal file
126
src/modules/0-ui/controllers/takeovers.js
Normal file
@@ -0,0 +1,126 @@
|
||||
import {
|
||||
handleListPendingTakeovers,
|
||||
handleListAllTakeovers,
|
||||
handleGetTakeover,
|
||||
handleRespondToTakeover,
|
||||
handleCancelTakeover,
|
||||
handleCheckPendingTakeover,
|
||||
} from "../handlers/takeovers.js";
|
||||
|
||||
/**
|
||||
* GET /takeovers - Lista takeovers pendientes
|
||||
*/
|
||||
export const makeListPendingTakeovers = (tenantIdOrFn) => async (req, res) => {
|
||||
try {
|
||||
const tenantId = typeof tenantIdOrFn === "function" ? tenantIdOrFn() : tenantIdOrFn;
|
||||
const limit = parseInt(req.query.limit, 10) || 50;
|
||||
const result = await handleListPendingTakeovers({ tenantId, limit });
|
||||
res.json(result);
|
||||
} catch (err) {
|
||||
console.error("[takeovers] List pending error:", err);
|
||||
res.status(500).json({ ok: false, error: "internal_error", message: err.message });
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* GET /takeovers/all - Lista todos los takeovers
|
||||
*/
|
||||
export const makeListAllTakeovers = (tenantIdOrFn) => async (req, res) => {
|
||||
try {
|
||||
const tenantId = typeof tenantIdOrFn === "function" ? tenantIdOrFn() : tenantIdOrFn;
|
||||
const status = req.query.status || null;
|
||||
const limit = parseInt(req.query.limit, 10) || 100;
|
||||
const result = await handleListAllTakeovers({ tenantId, status, limit });
|
||||
res.json(result);
|
||||
} catch (err) {
|
||||
console.error("[takeovers] List all error:", err);
|
||||
res.status(500).json({ ok: false, error: "internal_error", message: err.message });
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* GET /takeovers/:id - Obtiene detalles de un takeover
|
||||
*/
|
||||
export const makeGetTakeover = (tenantIdOrFn) => async (req, res) => {
|
||||
try {
|
||||
const tenantId = typeof tenantIdOrFn === "function" ? tenantIdOrFn() : tenantIdOrFn;
|
||||
const id = parseInt(req.params.id, 10);
|
||||
const result = await handleGetTakeover({ tenantId, id });
|
||||
res.json(result);
|
||||
} catch (err) {
|
||||
console.error("[takeovers] Get error:", err);
|
||||
if (err.message.includes("not found")) {
|
||||
return res.status(404).json({ ok: false, error: "takeover_not_found" });
|
||||
}
|
||||
res.status(500).json({ ok: false, error: "internal_error", message: err.message });
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* POST /takeovers/:id/respond - Responde a un takeover
|
||||
*/
|
||||
export const makeRespondToTakeover = (tenantIdOrFn) => async (req, res) => {
|
||||
try {
|
||||
const tenantId = typeof tenantIdOrFn === "function" ? tenantIdOrFn() : tenantIdOrFn;
|
||||
const id = parseInt(req.params.id, 10);
|
||||
const { response, responded_by, add_alias } = req.body || {};
|
||||
|
||||
if (!response) {
|
||||
return res.status(400).json({ ok: false, error: "response_required" });
|
||||
}
|
||||
|
||||
const result = await handleRespondToTakeover({
|
||||
tenantId,
|
||||
id,
|
||||
response,
|
||||
respondedBy: responded_by || null,
|
||||
addAlias: add_alias || null,
|
||||
});
|
||||
res.json(result);
|
||||
} catch (err) {
|
||||
console.error("[takeovers] Respond error:", err);
|
||||
if (err.message.includes("not found") || err.message.includes("already")) {
|
||||
return res.status(404).json({ ok: false, error: "takeover_not_found_or_processed" });
|
||||
}
|
||||
res.status(500).json({ ok: false, error: "internal_error", message: err.message });
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* POST /takeovers/:id/cancel - Cancela un takeover
|
||||
*/
|
||||
export const makeCancelTakeover = (tenantIdOrFn) => async (req, res) => {
|
||||
try {
|
||||
const tenantId = typeof tenantIdOrFn === "function" ? tenantIdOrFn() : tenantIdOrFn;
|
||||
const id = parseInt(req.params.id, 10);
|
||||
const { responded_by } = req.body || {};
|
||||
|
||||
const result = await handleCancelTakeover({
|
||||
tenantId,
|
||||
id,
|
||||
respondedBy: responded_by || null,
|
||||
});
|
||||
res.json(result);
|
||||
} catch (err) {
|
||||
console.error("[takeovers] Cancel error:", err);
|
||||
if (err.message.includes("not found") || err.message.includes("already")) {
|
||||
return res.status(404).json({ ok: false, error: "takeover_not_found_or_processed" });
|
||||
}
|
||||
res.status(500).json({ ok: false, error: "internal_error", message: err.message });
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* GET /takeovers/check/:chatId - Verifica si hay takeover pendiente para un chat
|
||||
*/
|
||||
export const makeCheckPendingTakeover = (tenantIdOrFn) => async (req, res) => {
|
||||
try {
|
||||
const tenantId = typeof tenantIdOrFn === "function" ? tenantIdOrFn() : tenantIdOrFn;
|
||||
const chatId = req.params.chatId;
|
||||
const result = await handleCheckPendingTakeover({ tenantId, chatId });
|
||||
res.json(result);
|
||||
} catch (err) {
|
||||
console.error("[takeovers] Check pending error:", err);
|
||||
res.status(500).json({ ok: false, error: "internal_error", message: err.message });
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user