-
Notifications
You must be signed in to change notification settings - Fork 1
/
RolesHelpers.ts
38 lines (36 loc) · 1.23 KB
/
RolesHelpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import vmd from "../virtualmodel/VMD";
import { Role } from "../redux/AuthSlice";
import { SchemaRoles } from "../components/settingsPage/Users/RolesTab";
/**
* Function that obtains the schema roles for a given user
* @returns SchemaRoles
*/
export async function getSchemaRoles(user: string): Promise<SchemaRoles> {
const rolePerSchemaDA = vmd.getRowDataAccessor('meta', 'role_per_schema', 'user', user);
const rolePerSchemaResponse = await rolePerSchemaDA.fetchRows();
if (rolePerSchemaResponse) {
const reducedRolePerSchema = rolePerSchemaResponse.reduce((obj, { schema, role }) => {
obj[schema] = role;
return obj;
}, {});
return reducedRolePerSchema;
} else {
console.log('Failed to obtain the schema roles of user ' + user);
return {};
}
}
/**
* Function that obtains the default role for a given user
* @returns Role
*/
export async function getDefaultRole(user: string): Promise<Role | null> {
const defaultRoleDA = vmd.getViewRowDataAccessor('meta', 'user_info', ['email'], [user]);
const response = await defaultRoleDA.fetchRows();
if (response) {
const defaultRole = response[0]['role'];
return defaultRole as Role;
} else {
console.log('Failed to obtain the default role of user ' + user);
return null;
}
}