From b9ff0a4138843e191351f2bcec75e8305a78d6f2 Mon Sep 17 00:00:00 2001 From: Blake Matthes Date: Fri, 20 Jun 2025 11:15:39 -0500 Subject: [PATCH] feat(default accounts): added in a default account run this will create a bunch of system admins --- server/services/auth/authService.ts | 5 ++ .../auth/utils/DefaultAccountCreation.ts | 56 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 server/services/auth/utils/DefaultAccountCreation.ts diff --git a/server/services/auth/authService.ts b/server/services/auth/authService.ts index ce49995..c6a394a 100644 --- a/server/services/auth/authService.ts +++ b/server/services/auth/authService.ts @@ -10,6 +10,7 @@ import createUser from "./routes/userAdmin/createUser.js"; import allUsers from "./routes/userAdmin/getUsers.js"; import updateUser from "./routes/userAdmin/updateUser.js"; import allUserRoles from "./routes/userAdmin/getAllUserRoles.js"; +import { massAccountCreation } from "./utils/DefaultAccountCreation.js"; const app = new OpenAPIHono(); @@ -36,4 +37,8 @@ const appRoutes = routes.forEach((route) => { app.route("/auth", route); }); +// setTimeout(() => { +// massAccountCreation(); +// }, 1000 * 60); + export default app; diff --git a/server/services/auth/utils/DefaultAccountCreation.ts b/server/services/auth/utils/DefaultAccountCreation.ts new file mode 100644 index 0000000..e44b623 --- /dev/null +++ b/server/services/auth/utils/DefaultAccountCreation.ts @@ -0,0 +1,56 @@ +import { db } from "../../../../database/dbclient.js"; +import { users } from "../../../../database/schema/users.js"; +import { tryCatch } from "../../../globalUtils/tryCatch.js"; +import { createLog } from "../../logger/logger.js"; +import { setSysAdmin } from "../controllers/userRoles/setSysAdmin.js"; +import { createPassword } from "./createPassword.js"; + +export const massAccountCreation = async () => { + /** + * This will create a new account for all users before if they are already in there it will update just there password. + * + */ + + const user: any = [ + // { + // username: "landa002", + // email: "Oscar.Landa@alpla.com", + // password: "Frostlike-Petri5-Ungreased!", + // }, + ]; + + for (let i = 0; i < user.length; i++) { + const updatedUser = { + username: user[i].username, + email: user[i].email, + password: await createPassword(user[i].password), + }; + const { data, error } = await tryCatch( + db + .insert(users) + .values(updatedUser) + .onConflictDoUpdate({ + target: users.username, + set: { + password: updatedUser.password, + email: updatedUser.email, + }, + }) + .returning({ + user_id: users.user_id, + username: users.username, + }) + ); + + await setSysAdmin(data, "systemAdmin"); + + if (error) { + createLog( + "error", + "lst", + "auth", + `There was an error creating ${user[i].username}` + ); + } + } +};