Don't allow duplicate warnings

This commit is contained in:
Ajay Ramachandran 2020-12-29 13:31:15 -05:00
parent baa4e73ba5
commit 432cc42cba
2 changed files with 33 additions and 2 deletions

View file

@ -22,8 +22,15 @@ export function postWarning(req: Request, res: Response) {
let resultStatus = "";
if (enabled) {
db.prepare('run', 'INSERT INTO warnings (userID, issueTime, issuerUserID, enabled) VALUES (?, ?, ?, 1)', [userID, issueTime, issuerUserID]);
resultStatus = "issued to";
let previousWarning = db.prepare('get', 'SELECT * FROM warnings WHERE userID = ? AND issuerUserID = ?', [userID, issuerUserID]);
if (!previousWarning) {
db.prepare('run', 'INSERT INTO warnings (userID, issueTime, issuerUserID, enabled) VALUES (?, ?, ?, 1)', [userID, issueTime, issuerUserID]);
resultStatus = "issued to";
} else {
res.status(409).send();
return;
}
} else {
db.prepare('run', 'UPDATE warnings SET enabled = 0 WHERE userID = ? AND issuerUserID = ?', [userID, issuerUserID]);
resultStatus = "removed from";

View file

@ -32,6 +32,30 @@ describe('postWarning', () => {
});
});
it('Should be not be able to create a duplicate warning if vip', (done: Done) => {
let json = {
issuerUserID: 'warning-vip',
userID: 'warning-0',
};
request.post(getbaseURL()
+ "/api/warnUser", {json},
(err, res, body) => {
if (err) done(err);
else if (res.statusCode === 409) {
let row = db.prepare('get', "SELECT userID, issueTime, issuerUserID, enabled FROM warnings WHERE userID = ?", [json.userID]);
if (row?.enabled == 1 && row?.issuerUserID == getHash(json.issuerUserID)) {
done();
} else {
done("Warning missing from database");
}
} else {
console.log(body);
done("Status code was " + res.statusCode);
}
});
});
it('Should be able to remove warning if vip', (done: Done) => {
let json = {
issuerUserID: 'warning-vip',