From 6fab4bf8e346cbfa239e17313568c080a916aed7 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 27 Dec 2019 11:04:49 +0100 Subject: [PATCH] Can respond to a membership request --- src/controllers/GroupsController.ts | 21 +++++++++++++++++++++ src/controllers/Routes.ts | 2 ++ src/helpers/GroupsHelper.ts | 14 ++++++++++++++ 3 files changed, 37 insertions(+) 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 *