Merge pull request #282 from HaiDang666/276_warn-reason

User warning reason
This commit is contained in:
Ajay Ramachandran 2021-06-30 13:43:08 -04:00 committed by GitHub
commit 7a877a9653
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 7 deletions

View file

@ -104,6 +104,7 @@
| issueTime | INTEGER | not null | | issueTime | INTEGER | not null |
| issuerUserID | TEXT | not null | | issuerUserID | TEXT | not null |
| enabled | INTEGER | not null | | enabled | INTEGER | not null |
| reason | TEXT | not null, default '' |
| index | field | | index | field |
| -- | :--: | | -- | :--: |

View file

@ -0,0 +1,19 @@
BEGIN TRANSACTION;
/* Add reason field */
CREATE TABLE "sqlb_temp_table_17" (
"userID" TEXT NOT NULL,
"issueTime" INTEGER NOT NULL,
"issuerUserID" TEXT NOT NULL,
enabled INTEGER NOT NULL,
"reason" TEXT NOT NULL default ''
);
INSERT INTO sqlb_temp_table_17 SELECT "userID","issueTime","issuerUserID","enabled", '' FROM "warnings";
DROP TABLE warnings;
ALTER TABLE sqlb_temp_table_17 RENAME TO "warnings";
UPDATE "config" SET value = 17 WHERE key = 'version';
COMMIT;

View file

@ -7,10 +7,11 @@ import { HashedUserID, UserID } from '../types/user.model';
export async function postWarning(req: Request, res: Response) { export async function postWarning(req: Request, res: Response) {
// Collect user input data // Collect user input data
let issuerUserID: HashedUserID = getHash(<UserID> req.body.issuerUserID); const issuerUserID: HashedUserID = getHash(<UserID> req.body.issuerUserID);
let userID: UserID = req.body.userID; const userID: UserID = req.body.userID;
let issueTime = new Date().getTime(); const issueTime = new Date().getTime();
let enabled: boolean = req.body.enabled ?? true; const enabled: boolean = req.body.enabled ?? true;
const reason: string = req.body.reason ?? '';
// Ensure user is a VIP // Ensure user is a VIP
if (!await isUserVIP(issuerUserID)) { if (!await isUserVIP(issuerUserID)) {
@ -25,7 +26,11 @@ export async function postWarning(req: Request, res: Response) {
let previousWarning = await db.prepare('get', 'SELECT * FROM "warnings" WHERE "userID" = ? AND "issuerUserID" = ?', [userID, issuerUserID]); let previousWarning = await db.prepare('get', 'SELECT * FROM "warnings" WHERE "userID" = ? AND "issuerUserID" = ?', [userID, issuerUserID]);
if (!previousWarning) { if (!previousWarning) {
await db.prepare('run', 'INSERT INTO "warnings" ("userID", "issueTime", "issuerUserID", "enabled") VALUES (?, ?, ?, 1)', [userID, issueTime, issuerUserID]); await db.prepare(
'run',
'INSERT INTO "warnings" ("userID", "issueTime", "issuerUserID", "enabled", "reason") VALUES (?, ?, ?, 1, ?)',
[userID, issueTime, issuerUserID, reason]
);
resultStatus = "issued to"; resultStatus = "issued to";
} else { } else {
res.status(409).send(); res.status(409).send();

View file

@ -12,6 +12,7 @@ describe('postWarning', () => {
let json = { let json = {
issuerUserID: 'warning-vip', issuerUserID: 'warning-vip',
userID: 'warning-0', userID: 'warning-0',
reason: 'warning-reason-0'
}; };
fetch(getbaseURL() fetch(getbaseURL()
+ "/api/warnUser", { + "/api/warnUser", {
@ -23,8 +24,8 @@ describe('postWarning', () => {
}) })
.then(async res => { .then(async res => {
if (res.status === 200) { if (res.status === 200) {
let row = await db.prepare('get', `SELECT "userID", "issueTime", "issuerUserID", enabled FROM warnings WHERE "userID" = ?`, [json.userID]); let row = await db.prepare('get', `SELECT "userID", "issueTime", "issuerUserID", enabled, "reason" FROM warnings WHERE "userID" = ?`, [json.userID]);
if (row?.enabled == 1 && row?.issuerUserID == getHash(json.issuerUserID)) { if (row?.enabled == 1 && row?.issuerUserID == getHash(json.issuerUserID) && row?.reason === json.reason) {
done(); done();
} else { } else {
done("Warning missing from database"); done("Warning missing from database");