Work with new intent model and get intent by name
This commit is contained in:
parent
f1ac1c6af0
commit
fec97eb018
@ -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)
|
// Get intent command (for now by description)
|
||||||
|
// [UNUSED FOR NOW] Doesn't work with the current intent model
|
||||||
exports.getIntentCommand = async (req, res) => {
|
exports.getIntentCommand = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const {text, device_id} = req.body;
|
const {text, device_id} = req.body;
|
||||||
@ -92,38 +106,22 @@ exports.getIntentCommand = async (req, res) => {
|
|||||||
// Create intent
|
// Create intent
|
||||||
exports.createIntent = async (req, res) => {
|
exports.createIntent = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const {command, description, type, device_id} = req.body;
|
const {name, examples} = req.body;
|
||||||
|
|
||||||
console.log(`\n[Backend Intents] Creating intent.`)
|
console.log(`\n[Backend Intents] Creating intent.`)
|
||||||
|
|
||||||
await redis.client.connect().catch(console.error);
|
await redis.client.connect().catch(console.error);
|
||||||
|
|
||||||
if (!command || !description || !type || !device_id) {
|
if (!name || !examples) {
|
||||||
return res.status(400).send({message: "Missing required fields: command, description, type, device_id"});
|
return res.status(400).send({message: "Missing required fields: name, examples"});
|
||||||
}
|
}
|
||||||
|
|
||||||
const intent = await Intent.create({
|
const intent = await Intent.create({
|
||||||
command,
|
name,
|
||||||
description,
|
examples
|
||||||
type
|
|
||||||
});
|
});
|
||||||
|
|
||||||
await redis.client.connect().catch(console.error);
|
return res.status(201).send({message: "Intent created successfully.", intent});
|
||||||
|
|
||||||
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});
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("createIntent error:", err);
|
console.error("createIntent error:", err);
|
||||||
|
|
||||||
|
|||||||
@ -15,10 +15,14 @@ const controller = require("../controllers/intent.controller");
|
|||||||
// Get all intents
|
// Get all intents
|
||||||
router.get("/", /*[authJwt.verifyToken, authJwt.isAdmin],*/ controller.getAllIntents);
|
router.get("/", /*[authJwt.verifyToken, authJwt.isAdmin],*/ controller.getAllIntents);
|
||||||
|
|
||||||
// Get intent command (for now by description)
|
// Get intent by name
|
||||||
router.post("/getintentcommand", controller.getIntentCommand);
|
router.get("/name/:name", controller.getIntentByName);
|
||||||
|
|
||||||
|
// Get intent command by description
|
||||||
|
// [UNUSED FOR NOW]
|
||||||
|
//router.post("/getintentcommand", controller.getIntentCommand);
|
||||||
|
|
||||||
// Create intent
|
// Create intent
|
||||||
router.post("/createintent", controller.createIntent);
|
router.post("/", controller.createIntent);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
@ -60,7 +60,7 @@ const createApp = async () => {
|
|||||||
app.use("/auth", require("./routes/auth.routes"));
|
app.use("/auth", require("./routes/auth.routes"));
|
||||||
app.use("/user", require("./routes/user.routes"));
|
app.use("/user", require("./routes/user.routes"));
|
||||||
app.use("/bills", require("./routes/bill.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
|
// 404 handler
|
||||||
app.use((req, res) => res.status(404).send({message: "Not Found"}));
|
app.use((req, res) => res.status(404).send({message: "Not Found"}));
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user