Work with new intent model and get intent by name

This commit is contained in:
Apher 2026-07-18 00:06:00 +00:00
parent f1ac1c6af0
commit fec97eb018
3 changed files with 28 additions and 26 deletions

View File

@ -18,7 +18,21 @@ exports.getAllIntents = async (req, res) => {
}
};
// Get intent by name
exports.getIntentByName = async (req, res) => {
try {
const intent = await Intent.findOne({where: {name: req.params.name}});
return res.status(200).send(intent);
} catch (err) {
console.error("getIntentByName error:", err);
return res.status(500).send({message: err.message || "Internal server error."});
}
}
// Get intent command (for now by description)
// [UNUSED FOR NOW] Doesn't work with the current intent model
exports.getIntentCommand = async (req, res) => {
try {
const {text, device_id} = req.body;
@ -92,38 +106,22 @@ exports.getIntentCommand = async (req, res) => {
// Create intent
exports.createIntent = async (req, res) => {
try {
const {command, description, type, device_id} = req.body;
const {name, examples} = req.body;
console.log(`\n[Backend Intents] Creating intent.`)
await redis.client.connect().catch(console.error);
if (!command || !description || !type || !device_id) {
return res.status(400).send({message: "Missing required fields: command, description, type, device_id"});
if (!name || !examples) {
return res.status(400).send({message: "Missing required fields: name, examples"});
}
const intent = await Intent.create({
command,
description,
type
name,
examples
});
await redis.client.connect().catch(console.error);
const commandId = await redis.client.xAdd(
'communication',
'*',
{
device_id: device_id,
command: "CREATE_INTENT",
processed_at: new Date().toISOString(),
source: config.DEVICE_ID,
}
);
await redis.client.quit();
return res.status(201).send({message: "Intent created successfully.", intent, commandId});
return res.status(201).send({message: "Intent created successfully.", intent});
} catch (err) {
console.error("createIntent error:", err);

View File

@ -15,10 +15,14 @@ const controller = require("../controllers/intent.controller");
// Get all intents
router.get("/", /*[authJwt.verifyToken, authJwt.isAdmin],*/ controller.getAllIntents);
// Get intent command (for now by description)
router.post("/getintentcommand", controller.getIntentCommand);
// Get intent by name
router.get("/name/:name", controller.getIntentByName);
// Get intent command by description
// [UNUSED FOR NOW]
//router.post("/getintentcommand", controller.getIntentCommand);
// Create intent
router.post("/createintent", controller.createIntent);
router.post("/", controller.createIntent);
module.exports = router;

View File

@ -60,7 +60,7 @@ const createApp = async () => {
app.use("/auth", require("./routes/auth.routes"));
app.use("/user", require("./routes/user.routes"));
app.use("/bills", require("./routes/bill.routes"));
app.use("/fallback/intent", require("./routes/intent.routes"));
app.use("/intent", require("./routes/intent.routes"));
// 404 handler
app.use((req, res) => res.status(404).send({message: "Not Found"}));