36 lines
1.3 KiB
TypeScript
36 lines
1.3 KiB
TypeScript
import {users} from "../../../../../database/schema/users.js";
|
|
import {eq} from "drizzle-orm";
|
|
import {db} from "../../../../../database/dbclient.js";
|
|
import {userRoles} from "../../../../../database/schema/userRoles.js";
|
|
import {modules} from "../../../../../database/schema/modules.js";
|
|
import {roles} from "../../../../../database/schema/roles.js";
|
|
|
|
export const setSysAdmin = async (user: any, roleName: any): Promise<void> => {
|
|
// remove all userRoles to prevent errors
|
|
try {
|
|
const remove = await db.delete(userRoles).where(eq(userRoles.user_id, user[0].user_id));
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
|
|
// now we want to add the user to the system admin.
|
|
const module = await db.select().from(modules);
|
|
const role = await db.select().from(roles).where(eq(roles.name, roleName));
|
|
|
|
for (let i = 0; i < module.length; i++) {
|
|
try {
|
|
const userRole = await db.insert(userRoles).values({
|
|
user_id: user[0].user_id,
|
|
role_id: role[0].role_id,
|
|
module_id: module[i].module_id,
|
|
role: roleName,
|
|
});
|
|
console.log(`${user[0].username} has been granted access to ${module[i].name} with the role ${roleName}`);
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
}
|
|
|
|
return;
|
|
};
|