From 6d0994ff8533c258068e32ea7a188be21edd4c10 Mon Sep 17 00:00:00 2001 From: Nathanael Coonrod Date: Sun, 3 Mar 2024 23:37:36 -0700 Subject: [PATCH] chore: removed forced seeding from dev builds; --- package.json | 2 +- src/User.ts | 92 +++++++++++++++++++++++++++++++--------------------- 2 files changed, 56 insertions(+), 38 deletions(-) diff --git a/package.json b/package.json index 3248154..aa1110f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@hotstaq/userroute", "description": "A user route for HotStaq. Allows users to be created/edited/deleted securely.", - "version": "0.4.9", + "version": "0.5.0", "main": "build/src/index.js", "scripts": { "test": "hotstaq --dev --env-file .env run --server-type api --api-test", diff --git a/src/User.ts b/src/User.ts index 4aae73d..5f559e5 100644 --- a/src/User.ts +++ b/src/User.ts @@ -297,46 +297,64 @@ export class User implements IUser logOutDate DATETIME DEFAULT NULL, PRIMARY KEY (id) )`); + } + + /** + * Checks if the users table is empty. + * + * @returns Returns true if the users table is empty. + */ + static async checkForEmptyUsers (db: HotDBMySQL): Promise + { + let results: MySQLResults = await db.queryOne (`select COUNT(*) from users;`); + + if (results.results["COUNT(*)"] < 1) + return (true); + + return (false); + } - if (debug == true) + /** + * Seed the users table. + * + * @param testPlayers The test players to seed. If the array is empty, it will use the default test players. + */ + static async seedUsers (db: HotDBMySQL, testPlayers: User[] = []): Promise + { + if (testPlayers.length < 1) { - let results: MySQLResults = await db.queryOne (`select COUNT(*) from users;`); + testPlayers = [ + new User ({ + firstName: "John", + lastName: "Doe", + displayName: "Test1", + email: "test1@freelight.org", + password: "a867h398jdg", + verified: true + }), + new User ({ + firstName: "Jane", + lastName: "Smith", + displayName: "Test2", + email: "test2@freelight.org", + password: "ai97w3a98w3498", + verified: true }), + new User ({ + userType: "admin", + firstName: "Bob", + lastName: "Derp", + displayName: "Admin1", + email: "admin1@freelight.org", + password: "a98j3w987aw3h47u", + verified: true }) + ]; + } - if (results.results["COUNT(*)"] < 1) - { - let testPlayers = [ - new User ({ - firstName: "John", - lastName: "Doe", - displayName: "Test1", - email: "test1@freelight.org", - password: "a867h398jdg", - verified: true - }), - new User ({ - firstName: "Jane", - lastName: "Smith", - displayName: "Test2", - email: "test2@freelight.org", - password: "ai97w3a98w3498", - verified: true }), - new User ({ - userType: "admin", - firstName: "Bob", - lastName: "Derp", - displayName: "Admin1", - email: "admin1@freelight.org", - password: "a98j3w987aw3h47u", - verified: true }) - ]; - - for (let iIdx = 0; iIdx < testPlayers.length; iIdx++) - { - let testPlayer = testPlayers[iIdx]; - - await testPlayer.register (db); - } - } + for (let iIdx = 0; iIdx < testPlayers.length; iIdx++) + { + let testPlayer = testPlayers[iIdx]; + + await testPlayer.register (db); } }