Skip to content

Commit

Permalink
Add role migration check in user initialization
Browse files Browse the repository at this point in the history
The commit includes adding a new query for role migration and incorporating it into the user initialization process to prevent user creation before migration finishes. Also, a custom error, MigrationNotFinishedException, has been added for better error handling regarding the migration completion status. This will help in ensuring accurate role assignment to new users.
  • Loading branch information
ludeknovy committed Apr 19, 2024
1 parent c9f100d commit b4a384a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/server/controllers/auth/init-user-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,29 @@ import boom = require("boom")
import { Request, Response, NextFunction } from "express"
import { db } from "../../../db/db"
import { AllowedRoles } from "../../middleware/authorization-middleware"
import { getUsers } from "../../queries/auth"
import { getUsers, getRoleMigration } from "../../queries/auth"
import { createNewUserController } from "../users/create-new-user-controller"
import { MigrationNotFinishedException } from "../../errors/migration-not-finished-exception"

export const initUserController = async (req: Request, res: Response, next: NextFunction) => {
try {
const users = await db.manyOrNone(getUsers())
if (users && users.length > 0) {
return next(boom.forbidden("User was already initialized"))
}
req.body.role = AllowedRoles.Admin
await createNewUserController(req, res, next)

try {
const roleMigration = await db.oneOrNone(getRoleMigration())
console.log(roleMigration)
if (!roleMigration) {
throw MigrationNotFinishedException
}
const users = await db.manyOrNone(getUsers())
if (users && users.length > 0) {
return next(boom.forbidden("User was already initialized"))
}
req.body.role = AllowedRoles.Admin
await createNewUserController(req, res, next)

} catch(error) {
return next(error)
}
} catch(error) {
if (error.code === "42P01" || error instanceof MigrationNotFinishedException) {
return next(boom.preconditionRequired("Migrations were not finished, please try it again later."))
}
return next(error)
}

}
9 changes: 9 additions & 0 deletions src/server/errors/migration-not-finished-exception.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class MigrationNotFinishedException extends Error {
constructor (message) {
super(message)

this.name = this.constructor.name

Error.captureStackTrace(this, this.constructor)
}
}
7 changes: 7 additions & 0 deletions src/server/queries/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,10 @@ export const updatePassword = (id, password) => {
values: [id, password],
}
}

export const getRoleMigration = () => {
return {
text: "SELECT * FROM pgmigrations WHERE name = $1",
values: ["1643273224321_role"],
}
}

0 comments on commit b4a384a

Please sign in to comment.