From 214ddc980786c6dc583eb15c8178ed0d2b96aecd Mon Sep 17 00:00:00 2001 From: Haidang666 Date: Tue, 29 Jun 2021 14:56:57 +0700 Subject: [PATCH 1/2] Add warning reason in postWarning --- DatabaseSchema.md | 1 + databases/_upgrade_sponsorTimes_17.sql | 19 +++++++++++++++++++ src/routes/postWarning.ts | 15 ++++++++++----- 3 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 databases/_upgrade_sponsorTimes_17.sql diff --git a/DatabaseSchema.md b/DatabaseSchema.md index 16aa6f8..3d792d2 100644 --- a/DatabaseSchema.md +++ b/DatabaseSchema.md @@ -104,6 +104,7 @@ | issueTime | INTEGER | not null | | issuerUserID | TEXT | not null | | enabled | INTEGER | not null | +| reason | TEXT | not null, default '' | | index | field | | -- | :--: | diff --git a/databases/_upgrade_sponsorTimes_17.sql b/databases/_upgrade_sponsorTimes_17.sql new file mode 100644 index 0000000..7db65c8 --- /dev/null +++ b/databases/_upgrade_sponsorTimes_17.sql @@ -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; \ No newline at end of file diff --git a/src/routes/postWarning.ts b/src/routes/postWarning.ts index 1734012..8805b2b 100644 --- a/src/routes/postWarning.ts +++ b/src/routes/postWarning.ts @@ -7,10 +7,11 @@ import { HashedUserID, UserID } from '../types/user.model'; export async function postWarning(req: Request, res: Response) { // Collect user input data - let issuerUserID: HashedUserID = getHash( req.body.issuerUserID); - let userID: UserID = req.body.userID; - let issueTime = new Date().getTime(); - let enabled: boolean = req.body.enabled ?? true; + const issuerUserID: HashedUserID = getHash( req.body.issuerUserID); + const userID: UserID = req.body.userID; + const issueTime = new Date().getTime(); + const enabled: boolean = req.body.enabled ?? true; + const reason: string = req.body.reason ?? ''; // Ensure user is a VIP 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]); 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"; } else { res.status(409).send(); From 081f2d14b7acbb8a64b58dc1b8955753ef087531 Mon Sep 17 00:00:00 2001 From: Haidang666 Date: Wed, 30 Jun 2021 08:59:20 +0700 Subject: [PATCH 2/2] Add test, Fix sql syntax --- databases/_upgrade_sponsorTimes_17.sql | 4 ++-- test/cases/postWarning.ts | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/databases/_upgrade_sponsorTimes_17.sql b/databases/_upgrade_sponsorTimes_17.sql index 7db65c8..43b5ecf 100644 --- a/databases/_upgrade_sponsorTimes_17.sql +++ b/databases/_upgrade_sponsorTimes_17.sql @@ -9,10 +9,10 @@ CREATE TABLE "sqlb_temp_table_17" ( "reason" TEXT NOT NULL default '' ); -INSERT INTO sqlb_temp_table_17 SELECT "userID","issueTime","issuerUserID","enabled", "" FROM "warnings"; +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";; +ALTER TABLE sqlb_temp_table_17 RENAME TO "warnings"; UPDATE "config" SET value = 17 WHERE key = 'version'; diff --git a/test/cases/postWarning.ts b/test/cases/postWarning.ts index 2c1242f..de15ef1 100644 --- a/test/cases/postWarning.ts +++ b/test/cases/postWarning.ts @@ -12,6 +12,7 @@ describe('postWarning', () => { let json = { issuerUserID: 'warning-vip', userID: 'warning-0', + reason: 'warning-reason-0' }; fetch(getbaseURL() + "/api/warnUser", { @@ -23,8 +24,8 @@ describe('postWarning', () => { }) .then(async res => { if (res.status === 200) { - let row = await db.prepare('get', `SELECT "userID", "issueTime", "issuerUserID", enabled FROM warnings WHERE "userID" = ?`, [json.userID]); - if (row?.enabled == 1 && row?.issuerUserID == getHash(json.issuerUserID)) { + 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) && row?.reason === json.reason) { done(); } else { done("Warning missing from database");