refactor(auth): added module and submodule access to the user
This commit is contained in:
@@ -1,24 +1,42 @@
|
||||
import { db } from "../../../../../database/dbclient.js";
|
||||
import { userRoles } from "../../../../../database/schema/userRoles.js";
|
||||
import { users } from "../../../../../database/schema/users.js";
|
||||
import { returnRes } from "../../../../globalUtils/routeDefs/returnRes.js";
|
||||
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
|
||||
import { createLog } from "../../../logger/logger.js";
|
||||
|
||||
export const getAllUsers = async () => {
|
||||
/**
|
||||
* returns all users that are in lst
|
||||
*/
|
||||
createLog("info", "apiAuthedRoute", "auth", "Get all users");
|
||||
const { data, error } = await tryCatch(db.select().from(users));
|
||||
/**
|
||||
* returns all users that are in lst
|
||||
*/
|
||||
createLog("info", "apiAuthedRoute", "auth", "Get all users");
|
||||
|
||||
if (error) {
|
||||
returnRes(
|
||||
false,
|
||||
"There was an error getting users",
|
||||
new Error("No user exists.")
|
||||
);
|
||||
}
|
||||
// get all users
|
||||
const { data, error } = await tryCatch(db.select().from(users));
|
||||
|
||||
returnRes(true, "All users.", data);
|
||||
return { success: true, message: "All users", data };
|
||||
// add there modules they are in
|
||||
const { data: m, error: em } = await tryCatch(db.select().from(userRoles));
|
||||
|
||||
const user: any = data;
|
||||
const userData = user.map((i: any) => {
|
||||
// module in
|
||||
const module = m?.filter((x: any) => x.user_id === i.user_id);
|
||||
|
||||
if (module) {
|
||||
return { ...i, moduleRoles: module };
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
if (error) {
|
||||
returnRes(
|
||||
false,
|
||||
"There was an error getting users",
|
||||
new Error("No user exists.")
|
||||
);
|
||||
}
|
||||
|
||||
//returnRes(true, "All users.", data);
|
||||
return { success: true, message: "All users", data: userData };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user