diff --git a/src/controllers/GroupsController.ts b/src/controllers/GroupsController.ts index de7e273..d083ea3 100644 --- a/src/controllers/GroupsController.ts +++ b/src/controllers/GroupsController.ts @@ -458,6 +458,27 @@ export class GroupsController { h.success("User membership has been successfully updated!"); } + /** + * Respond to a membership request + * + * @param h Request handler + */ + public static async RespondRequest(h: RequestHandler) { + const groupID = await h.postGroupIDWithAccess("groupID", GroupsAccessLevel.MODERATOR_ACCESS); + const userID = await h.postUserId("userID"); + const accept = h.postBool("accept"); + + if(await GroupsHelper.GetMembershipLevel(groupID, userID) != GroupMembershipLevels.PENDING) + h.error(401, "This user has not requested a membership for this group!"); + + // Respond to the request + await GroupsHelper.RespondRequest(groupID, userID, accept); + + // TODO : create a notification + + h.success("The response to the request has been successfully saved!"); + } + /** * Turn a GroupInfo object into a valid API object * diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 1dbcfb7..9209dff 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -118,4 +118,6 @@ export const Routes : Route[] = [ {path: "/groups/delete_member", cb: (h) => GroupsController.DeleteMember(h)}, {path: "/groups/update_membership_level", cb: (h) => GroupsController.UpdateMembership(h)}, + + {path: "/groups/respond_request", cb: (h) => GroupsController.RespondRequest(h)}, ] \ No newline at end of file diff --git a/src/helpers/GroupsHelper.ts b/src/helpers/GroupsHelper.ts index d70f0f5..253cfc4 100644 --- a/src/helpers/GroupsHelper.ts +++ b/src/helpers/GroupsHelper.ts @@ -206,6 +206,20 @@ export class GroupsHelper { await this.UpdateMembershipLevel(groupID, userID, GroupMembershipLevels.MEMBER); } + /** + * Respond to a group membership request + * + * @param groupID Target groupID + * @param userID Target user ID + * @param accept TRUE to accept / FALSE else + */ + public static async RespondRequest(groupID: number, userID: number, accept: boolean) { + if(!accept) + await this.DeleteMember(groupID, userID); + else + await this.UpdateMembershipLevel(groupID, userID, GroupMembershipLevels.MEMBER); + } + /** * Insert a new group member *