-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreports-controller.js
More file actions
38 lines (33 loc) · 1.15 KB
/
reports-controller.js
File metadata and controls
38 lines (33 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const { reportModel, getAllReports } = require('../models/reports.model');
const tokenservice = require('../services/token-service');
const placeModel = require('../models/places.model');
const { jwtConf } = require('../config/config');
const tokenService = new tokenservice(jwtConf);
exports.getReportsById = async (req, res) => {
try {
const { reportId } = req.params;
const reportById = await reportModel.find({ placeId: reportId });
res.status(200).send(reportById);
} catch (err) {
res.status(404).send({ message: 'Not found' });
}
};
exports.postReport = async (req, res) => {
try {
const { placeId, report, userJwt } = req.body;
const place = await placeModel.places.findOne({ _id: placeId });
if (!place) {
throw 'Sorry invalid id of place, try later';
}
const userId = await tokenService.verify(userJwt);
await reportModel.create({
placeId: placeId,
userId: userId,
comment: report,
isSolved: false,
});
await res.status(200).send({ message: 'Thanks, we added your report' });
} catch (e) {
res.status(500).send({ message: 'Wrong id of place or invalid JWT' });
}
};