150 lines
3.2 KiB
TypeScript
150 lines
3.2 KiB
TypeScript
import {
|
|
fetchRoleApi,
|
|
createRoleApi,
|
|
updateRoleApi,
|
|
removeRoleApi,
|
|
assignRoleApi,
|
|
unassignRoleApi,
|
|
fetchPermissionsApi,
|
|
IUpdateRoleRequest,
|
|
} from "../../api/role";
|
|
import { deleteRole, setPermissions, setRole } from "../../store/role";
|
|
import { state } from "../../store/state";
|
|
import { fetchUserCommunityRoles } from "../user";
|
|
|
|
const fetchRole = async (id: string) => {
|
|
const data = await fetchRoleApi({
|
|
id: id,
|
|
});
|
|
|
|
if (typeof data.error === "string") {
|
|
return;
|
|
}
|
|
|
|
setRole(data);
|
|
};
|
|
|
|
const createRole = async (name: string, communityId: string) => {
|
|
const data = await createRoleApi({
|
|
name: name,
|
|
communityId: communityId,
|
|
permissions: [],
|
|
});
|
|
|
|
if (typeof data.error === "string") {
|
|
return;
|
|
}
|
|
|
|
setRole(data);
|
|
};
|
|
|
|
const updateRole = async (updateRoleData: IUpdateRoleRequest) => {
|
|
const data = await updateRoleApi(updateRoleData);
|
|
|
|
if (typeof data.error === "string") {
|
|
return;
|
|
}
|
|
|
|
setRole(data);
|
|
};
|
|
|
|
const removeRole = async (id: string) => {
|
|
const data = await removeRoleApi({
|
|
id: id,
|
|
});
|
|
|
|
if (typeof data.error === "string") {
|
|
return;
|
|
}
|
|
|
|
deleteRole(data.id);
|
|
};
|
|
|
|
const assignRole = async (id: string, userId: string) => {
|
|
const data = await assignRoleApi({
|
|
id: id,
|
|
userId: userId,
|
|
});
|
|
|
|
if (typeof data.error === "string") {
|
|
return;
|
|
}
|
|
|
|
fetchUserCommunityRoles(data.userId, data.communityId);
|
|
};
|
|
|
|
const unassignRole = async (id: string, userId: string) => {
|
|
const data = await unassignRoleApi({
|
|
id: id,
|
|
userId: userId,
|
|
});
|
|
|
|
if (typeof data.error === "string") {
|
|
return;
|
|
}
|
|
|
|
fetchUserCommunityRoles(data.userId, data.communityId);
|
|
};
|
|
|
|
const fetchPermissions = async () => {
|
|
const data = await fetchPermissionsApi();
|
|
|
|
if (typeof data.error === "string") {
|
|
return;
|
|
}
|
|
|
|
setPermissions(data.permissions);
|
|
};
|
|
|
|
const getUserBestRoleId = (
|
|
userId: string,
|
|
communityId: string,
|
|
): string | undefined => {
|
|
const user = state.user.users[userId];
|
|
const community = state.community.communities[communityId];
|
|
if (!user || !community) {
|
|
return;
|
|
}
|
|
|
|
const communityRoleIds = community.roles;
|
|
if (!communityRoleIds) {
|
|
return;
|
|
}
|
|
const communityRoles = communityRoleIds
|
|
.map((roleId) => {
|
|
const role = state.role.roles[roleId];
|
|
if (!role) {
|
|
return {
|
|
id: roleId,
|
|
};
|
|
}
|
|
|
|
return role;
|
|
})
|
|
.filter((role) => role.id !== "" && role.showInMembers);
|
|
|
|
let bestRoleOrder = 999999;
|
|
let bestRoleId: string | undefined = undefined;
|
|
communityRoles.forEach((role) => {
|
|
if (
|
|
(role.order ?? 999999) < bestRoleOrder &&
|
|
(user.roles ?? {})[communityId].includes(role.id)
|
|
) {
|
|
bestRoleOrder = role.order ?? 999999;
|
|
bestRoleId = role.id;
|
|
}
|
|
});
|
|
|
|
return bestRoleId;
|
|
};
|
|
|
|
export {
|
|
fetchRole,
|
|
createRole,
|
|
updateRole,
|
|
removeRole,
|
|
assignRole,
|
|
unassignRole,
|
|
fetchPermissions,
|
|
getUserBestRoleId,
|
|
};
|