-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathreports.ts
55 lines (43 loc) · 1.34 KB
/
reports.ts
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
@module ReportsRoute
*/
import express from 'express';
import { ObjectId } from 'mongodb';
import { COLLECTION_NAMES, addDocument, getDocuments, deleteDocument, deleteDocuments } from '../helpers/mongo';
import { GenericObject } from '../types/types';
const router = express.Router();
/**
* Get all reports
*/
router.get('/', async (req, res) => {
const reports = await getDocuments(COLLECTION_NAMES.REPORTS, {}); // get all reports in collection
res.json(reports);
});
/**
* Add a report
*/
router.post('/', async (req, res) => {
console.log(`Adding Report: ${JSON.stringify(req.body)}`);
await addDocument(COLLECTION_NAMES.REPORTS, req.body);
res.json(req.body);
});
/**
* Delete a report
*/
router.delete('/', async (req, res) => {
let status;
if (req.body.id) {
console.log(`Deleting report ${req.body.id}`);
status = await deleteDocument(COLLECTION_NAMES.REPORTS, {
_id: new ObjectId(req.body.id),
});
} else {
console.log(`Deleting reports with reviewID ${req.body.reviewID}`);
const query: GenericObject = {};
if (req.body.reviewID) query['reviewID'] = req.body.reviewID;
if (Object.keys(query).length === 0) return; // avoid deleting all documents if no filters are specified
status = await deleteDocuments(COLLECTION_NAMES.REPORTS, query);
}
res.json(status);
});
export default router;