Files
outline/app/stores/GroupUsersStore.ts
Tom Moorandcodegen-sh[bot] 617504d8bb feat: Add group admin role (#10030)
* Add admin role to GroupUser

This change adds an admin role to GroupUser that allows group admins to:
1. Administer other users in the group
2. Change the group name

Changes include:
- Database migration to add isAdmin field to group_users table
- Updated GroupUser model to include isAdmin field
- Added isGroupAdmin policy utility function
- Updated group policy to allow group admins to update groups
- Added API endpoints for managing admin status
- Updated GroupUsersStore to handle admin functionality
- Added tests for the new functionality

* Replace isAdmin with role-based approach for GroupUser

- Added role field to GroupUser model using UserRole.Admin and UserRole.Member
- Created migration to convert isAdmin boolean to role enum
- Updated policies to be synchronous and require pre-loaded relationships
- Updated API endpoints to support both role and legacy isAdmin parameters
- Updated GroupUsersStore to handle role-based functionality
- Updated tests to use role instead of isAdmin
- Maintained backward compatibility with isAdmin in presenters

* Remove isAdmin logic from GroupUser implementation

- Removed isAdmin parameter from GroupUsersStore methods
- Removed isAdmin field from presenter output
- Removed isAdmin from API schemas
- Removed isAdmin parameter handling in API endpoints
- Updated tests to use role instead of isAdmin
- Simplified role handling throughout the codebase

* lint

* tests

* role -> permission

* fe

* test

* Change permission label from 'Admin' to 'Manage'

---------

Co-authored-by: codegen-sh[bot] <131295404+codegen-sh[bot]@users.noreply.github.com>
2025-09-01 05:10:32 -04:00

118 lines
3.1 KiB
TypeScript

import invariant from "invariant";
import filter from "lodash/filter";
import { action, runInAction } from "mobx";
import GroupUser from "~/models/GroupUser";
import { PaginationParams } from "~/types";
import { GroupPermission } from "@shared/types";
import { client } from "~/utils/ApiClient";
import RootStore from "./RootStore";
import Store, {
PaginatedResponse,
PAGINATION_SYMBOL,
RPCAction,
} from "./base/Store";
export default class GroupUsersStore extends Store<GroupUser> {
actions = [RPCAction.Create, RPCAction.Update, RPCAction.Delete];
constructor(rootStore: RootStore) {
super(rootStore, GroupUser);
}
@action
fetchPage = async (
params: PaginationParams | undefined
): Promise<PaginatedResponse<GroupUser>> => {
this.isFetching = true;
try {
const res = await client.post(`/groups.memberships`, params);
invariant(res?.data, "Data not available");
let response: PaginatedResponse<GroupUser> = [];
runInAction(`GroupUsersStore#fetchPage`, () => {
res.data.users.forEach(this.rootStore.users.add);
response = res.data.groupMemberships.map(this.add);
this.isLoaded = true;
});
response[PAGINATION_SYMBOL] = res.pagination;
return response;
} finally {
this.isFetching = false;
}
};
@action
async create({
groupId,
userId,
permission = GroupPermission.Member,
}: {
groupId: string;
userId: string;
permission?: GroupPermission;
}) {
const res = await client.post("/groups.add_user", {
id: groupId,
userId,
permission,
});
invariant(res?.data, "Group Membership data should be available");
res.data.users.forEach(this.rootStore.users.add);
res.data.groups.forEach(this.rootStore.groups.add);
const groupMemberships = res.data.groupMemberships.map(this.add);
return groupMemberships[0];
}
@action
async delete({ groupId, userId }: { groupId: string; userId: string }) {
const res = await client.post("/groups.remove_user", {
id: groupId,
userId,
});
invariant(res?.data, "Group Membership data should be available");
this.remove(`${userId}-${groupId}`);
runInAction(`GroupUsersStore#delete`, () => {
res.data.groups.forEach(this.rootStore.groups.add);
this.isLoaded = true;
});
}
@action
async update({
groupId,
userId,
permission,
}: {
groupId: string;
userId: string;
permission?: GroupPermission;
}) {
const res = await client.post("/groups.update_user", {
id: groupId,
userId,
permission,
});
invariant(res?.data, "Group Membership data should be available");
res.data.users.forEach(this.rootStore.users.add);
res.data.groups.forEach(this.rootStore.groups.add);
const groupMemberships = res.data.groupMemberships.map(this.add);
return groupMemberships[0];
}
@action
removeGroupMemberships = (groupId: string) => {
this.data.forEach((_, key) => {
if (key.includes(groupId)) {
this.remove(key);
}
});
};
inGroup = (groupId: string) =>
filter(this.orderedData, (member) => member.groupId === groupId);
}