Skip to content

Commit

Permalink
Merge pull request #9 from ezequielramos/main
Browse files Browse the repository at this point in the history
deploy
  • Loading branch information
ezequielramos authored Jul 5, 2024
2 parents 8062c84 + 67994bc commit c3864bd
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 26 deletions.
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"cSpell.words": [
"abcdefghijklmnopqrstuvxyz",
"fullframe",
"medmask",
"traceback"
]
}
6 changes: 3 additions & 3 deletions src/controllers/events.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WatchList, watchLists } from "./watch-lists";
import { WatchList, getWatchLists } from "./watch-lists";

function randomCharacters(length: number) {
let result = '';
Expand Down Expand Up @@ -110,7 +110,7 @@ function createEvent({ created_date, camera }: { created_date?: string; camera:
confidence: (Math.random() * 0.45) + 0.5,
external_detector: true,
looks_like_confidence: null,
matched_lists: [watchLists[0].id],
matched_lists: [getWatchLists()[0].id],
meta: {},
quality: 0.79995,
video_archive: null,
Expand Down Expand Up @@ -154,7 +154,7 @@ function createEvent({ created_date, camera }: { created_date?: string; camera:
id: null
}
},
verbose_matched_lists: [watchLists[0]],
verbose_matched_lists: [getWatchLists()[0]],
// TODO: once camera is implemented, bring it here
verbose_camera: {},
// TODO: once camera group is implemented, bring it here
Expand Down
6 changes: 2 additions & 4 deletions src/controllers/humans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface Human {
let humanId = 0;
const humans: { [humanId: number]: Human | undefined; } = {};

function createHuman({ name, active }: { name: string; active: boolean; }) {
function createHuman({ name, active, watchLists }: { name: string; active: boolean; watchLists: number[]; }) {
humanId++;
const human: Human = {
"id": humanId,
Expand All @@ -34,9 +34,7 @@ function createHuman({ name, active }: { name: string; active: boolean; }) {
"modified_date": new Date(),
"name": name,
"comment": "",
"watch_lists": [
1
],
"watch_lists": watchLists,
"meta": {},
"active_after": null,
"active_before": null,
Expand Down
45 changes: 44 additions & 1 deletion src/controllers/watch-lists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ interface WatchList {
origin: "ffsecurity";
}

let watchListId = 1;

const watchLists: WatchList[] = [
{
"id": -1,
Expand Down Expand Up @@ -68,4 +70,45 @@ const watchLists: WatchList[] = [
}
];

export { watchLists, WatchList };
function getWatchLists() {
return watchLists;
}

function createWatchList(name: string) {
watchListId++;
const watchList: WatchList = {
"id": watchListId,
"created_date": new Date().toISOString(),
"modified_date": new Date().toISOString(),
"active": true,
"name": name,
"comment": "",
"color": "123456",
"notify": false,
"acknowledge": false,
"camera_groups": [],
"face_threshold": null,
"body_threshold": null,
"car_threshold": null,
"ignore_events": false,
"send_events_to_external_vms": false,
"active_after": null,
"active_before": null,
"disable_schedule": {},
"recount_schedule_on": null,
"origin": "ffsecurity"
};
watchLists.push(watchList);

return watchList;
}

function getWatchList(id: number) {
const filteredWatchLists = watchLists.filter(watchList => watchList.id == id);
if (filteredWatchLists.length) {
return filteredWatchLists[0];
}
return null;
}

export { createWatchList, getWatchLists, getWatchList, WatchList };
40 changes: 24 additions & 16 deletions src/routes/cards.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Express, Request, Response } from 'express';
import { validAuthorization } from '../services/route_middlewares';
import { createHuman, deleteHuman, getHuman } from '../controllers/humans';
import { getWatchLists } from '../controllers/watch-lists';

function loadCardRoutes(app: Express) {

Expand Down Expand Up @@ -38,32 +39,39 @@ function loadCardRoutes(app: Express) {
watchLists = [req.body.watch_lists];
}

const watchListsIds = getWatchLists().filter(wl => wl.id != -1).map(wl => wl.id);

if (watchLists != 1 || Number(watchLists[0]) != 1) {
const missingPermissions = watchLists
.filter((v: string | number) => !([1, -1].includes(Number(v))))
.map((v: string | number) => `Watch list(${v}) - view`);
if (watchLists.includes(-1)) {
return res.status(400).json({
"traceback": "",
"code": "BAD_PARAM",
"desc": "You can't add watch list \"Unmatched\" to a card",
"param": "watch_lists"
});
}

if (missingPermissions.length == 0) {
return res.status(400).json({
for (const watchList of watchLists) {

if (!watchListsIds.includes(watchList)) {

const missingPermissions = watchLists
.filter((v: string | number) => !(watchListsIds.includes(Number(v))))
.map((v: string | number) => `Watch list(${v}) - view`);

return res.status(403).json({
"traceback": "",
"code": "BAD_PARAM",
"desc": "You can't add watch list \"Unmatched\" to a card",
"param": "watch_lists"
"code": "PERMISSION_DENIED",
"desc": "Permission denied",
"missing_permissions": missingPermissions
});
}

return res.status(403).json({
"traceback": "",
"code": "PERMISSION_DENIED",
"desc": "Permission denied",
"missing_permissions": missingPermissions
});
}

const human = createHuman({
name: req.body.name,
active: req.body.active ?? true
active: req.body.active ?? true,
watchLists
});

return res.status(200).json(human);
Expand Down
19 changes: 17 additions & 2 deletions src/routes/watch-lists.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import { Express, Request, Response } from 'express';
import { validAuthorization } from '../services/route_middlewares';
import { watchLists } from '../controllers/watch-lists';
import { getWatchLists, createWatchList } from '../controllers/watch-lists';

function loadWatchListsRoutes(app: Express) {

app.get('/watch-lists/', validAuthorization, async (req: Request, res: Response) => {
return res.status(200).json({
"results": watchLists
"results": getWatchLists()
});

});

app.post('/watch-lists/', validAuthorization, async (req: Request, res: Response) => {
if (!req.body.name) {
return res.status(400).json({
"traceback": "",
"code": "BAD_PARAM",
"desc": "This field is required.",
"param": "name"
});
}

const watchList = createWatchList(req.body.name);

return res.status(200).json(watchList);
});

}

export { loadWatchListsRoutes };
18 changes: 18 additions & 0 deletions tests/watch-lists.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect } from 'chai';
import { agent } from 'supertest';

import { webService } from '../src/services/web-service';
import { WatchList } from '../src/controllers/watch-lists';

const request = agent(webService.app);

Expand All @@ -27,4 +28,21 @@ describe('Watch Lists Route Testing', async () => {
const res = await request.get(`/watch-lists/`).set('Authorization', 'Token ' + token);
expect(res.statusCode).to.be.equal(200);
});

it('test create watch lists', async () => {
let res;
res = await request.post(`/watch-lists/`)
.set('Authorization', 'Token ' + token)
.send({
name: 'Company 1'
})
.type('application/json');
expect(res.statusCode).to.be.equal(200);

res = await request.get(`/watch-lists/`).set('Authorization', 'Token ' + token);
expect(res.statusCode).to.be.equal(200);

const names = res.body.results.map((wl: WatchList) => wl.name);
expect(names).to.includes('Company 1');
});
});

0 comments on commit c3864bd

Please sign in to comment.