Skip to content

Commit fc1db39

Browse files
committed
fix: now deleting a line deletes also intents and chatbots
1 parent a8d3711 commit fc1db39

File tree

2 files changed

+77
-32
lines changed

2 files changed

+77
-32
lines changed

routes/LineRoutes.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const fs = require('fs');
66
const Intent = require('../models/Intent');
77
const FormData = require('form-data');
88
const fetch = require('node-fetch');
9+
const Chatbot = require('../models/Chatbot');
910

1011
const router = express.Router()
1112
require('dotenv').config();
@@ -282,7 +283,23 @@ router.delete('/mine/:id', async (req, res) => {
282283
try {
283284
const pl = await Line.findById(req.params.id);
284285
if (pl.owner.toString() === decoded.id) {
285-
const folderPath = "./lines/" + pl._id;
286+
287+
// delete all intents from attribute intents in pl
288+
for (let i = 0; i < pl.intents.length; i++) {
289+
await Intent.findByIdAndDelete(pl.intents[i]);
290+
}
291+
292+
const chatbots = await Chatbot.find({ pl: pl.id });
293+
if (chatbots.length !== 0) {
294+
for (let i = 0; i < chatbots.length; i++) {
295+
const folderPath = "./bots/" + chatbots.id;
296+
fs.rmdirSync(folderPath, { recursive: true });
297+
await Chatbot.findByIdAndDelete(chatbots[i].id);
298+
}
299+
}
300+
301+
// delete the pl file in the server
302+
const folderPath = "./lines/" + pl.id;
286303
fs.rmdirSync(folderPath, { recursive: true });
287304
await pl.remove();
288305
res.json({ message: "Line deleted" });
@@ -304,8 +321,22 @@ router.delete('/:id', async (req, res) => {
304321
const decoded = jwt.verify(token, process.env.JWT_SECRET);
305322
if (decoded.id === ADMIN_ID) {
306323
try {
307-
const pl = await Line.findById(req.params.id);
308-
const folderPath = "./lines/" + pl._id;
324+
// delete all intents from attribute intents in pl
325+
for (let i = 0; i < pl.intents.length; i++) {
326+
await Intent.findByIdAndDelete(pl.intents[i]);
327+
}
328+
329+
const chatbots = await Chatbot.find({ pl: pl.id });
330+
if (chatbots.length !== 0) {
331+
for (let i = 0; i < chatbots.length; i++) {
332+
const folderPath = "./bots/" + chatbots.id;
333+
fs.rmdirSync(folderPath, { recursive: true });
334+
await Chatbot.findByIdAndDelete(chatbots[i].id);
335+
}
336+
}
337+
338+
// delete the pl file in the server
339+
const folderPath = "./lines/" + pl.id;
309340
fs.rmdirSync(folderPath, { recursive: true });
310341
await pl.remove();
311342
res.json({ message: "Line deleted" });

routes/UserRoutes.js

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,24 @@ router.get('/:id', async (req, res) => {
6868

6969
// Create a new user
7070
router.post('/', async (req, res) => {
71-
const data = new User({
72-
username: req.body.username,
73-
email: req.body.email,
74-
password: req.body.password
75-
})
7671
try {
77-
const dataToSave = await data.save();
78-
res.status(200).json(dataToSave.cleanup())
79-
}
80-
catch (error) {
81-
res.status(400).json({ message: error.message })
72+
const verify = jwt.verify(req.headers.authorization.split(' ')[1], JWT_SECRET);
73+
if (verify) {
74+
res.status(404).json({ message: "You cannot create a user while logged in" });
75+
}
76+
} catch {
77+
const data = new User({
78+
username: req.body.username,
79+
email: req.body.email,
80+
password: req.body.password
81+
})
82+
try {
83+
const dataToSave = await data.save();
84+
res.status(200).json(dataToSave.cleanup())
85+
}
86+
catch (error) {
87+
res.status(400).json({ message: error.message })
88+
}
8289
}
8390
})
8491

@@ -210,28 +217,35 @@ router.delete('/:id', async (req, res) => {
210217

211218
// Log in user and return user data
212219
router.post('/login', async (req, res) => {
213-
if (!req.body.username || !req.body.password) {
214-
return res.status(400).json({ error: 'Username and password are required' });
215-
} else {
216-
try {
217-
const user = await User.findOne({ username: req.body.username });
218-
if (user) {
219-
const validPassword = await bcrypt.compare(req.body.password, user.password);
220-
if (validPassword) {
221-
const payload = user.cleanup();
222-
const accessToken = generateAccessToken(payload, JWT_SECRET);
223-
res.status(200).send({
224-
message: "User authenticated",
225-
accessToken,
226-
});
220+
try {
221+
const verify = jwt.verify(req.headers.authorization.split(' ')[1], JWT_SECRET);
222+
if (verify) {
223+
res.status(404).json({ message: "You cannot login a different user while logged in" });
224+
}
225+
} catch {
226+
if (!req.body.username || !req.body.password) {
227+
return res.status(400).json({ error: 'Username and password are required' });
228+
} else {
229+
try {
230+
const user = await User.findOne({ username: req.body.username });
231+
if (user) {
232+
const validPassword = await bcrypt.compare(req.body.password, user.password);
233+
if (validPassword) {
234+
const payload = user.cleanup();
235+
const accessToken = generateAccessToken(payload, JWT_SECRET);
236+
res.status(200).send({
237+
message: "User authenticated",
238+
accessToken,
239+
});
240+
} else {
241+
res.status(400).json({ message: 'Invalid password' });
242+
}
227243
} else {
228-
res.status(400).json({ message: 'Invalid password' });
244+
res.status(400).json({ message: 'User not found' });
229245
}
230-
} else {
231-
res.status(400).json({ message: 'User not found' });
246+
} catch (err) {
247+
res.status(400).json({ message: 'Cannot login user right now, try again later' });
232248
}
233-
} catch (err) {
234-
res.status(400).json({ message: 'Cannot login user right now, try again later' });
235249
}
236250
}
237251
});

0 commit comments

Comments
 (0)