From 5bb65e70bbeae84f2107af8811985be24bacb343 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 26 Dec 2019 18:16:44 +0100 Subject: [PATCH] Can respond to membership invitation --- src/controllers/GroupsController.ts | 21 ++++++++++ src/controllers/Routes.ts | 2 + src/helpers/GroupsHelper.ts | 64 +++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) diff --git a/src/controllers/GroupsController.ts b/src/controllers/GroupsController.ts index 2dcae4f..6fac2a1 100644 --- a/src/controllers/GroupsController.ts +++ b/src/controllers/GroupsController.ts @@ -308,6 +308,27 @@ export class GroupsController { h.success("The user has been successfully invited to join the group!"); } + /** + * Respond to a user invitation + * + * @param h Request handler + */ + public static async RespondInvitation(h: RequestHandler) { + const groupID = await h.postGroupIDWithAccess("id", GroupsAccessLevel.LIMITED_ACCESS); + const accept = h.postBool("accept"); + + // Check if the user really received an invitation to join the group + if(!await GroupsHelper.ReceivedInvitation(groupID, h.getUserId())) + h.error(404, "Invitation not found!"); + + // Respond to the invitation + await GroupsHelper.RespondInvitation(groupID, h.getUserId(), accept); + + // TODO : Create a notification + + h.success("Response to the invitation was successfully saved!"); + } + /** * Turn a GroupInfo object into a valid API object * diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index bd765db..c7928fc 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -108,4 +108,6 @@ export const Routes : Route[] = [ {path: "/groups/get_members", cb: (h) => GroupsController.GetMembers(h)}, {path: "/groups/invite", cb: (h) => GroupsController.InviteUser(h)}, + + {path: "/groups/respond_invitation", cb: (h) => GroupsController.RespondInvitation(h)}, ] \ No newline at end of file diff --git a/src/helpers/GroupsHelper.ts b/src/helpers/GroupsHelper.ts index bb81afa..4dc3219 100644 --- a/src/helpers/GroupsHelper.ts +++ b/src/helpers/GroupsHelper.ts @@ -175,6 +175,37 @@ export class GroupsHelper { })); } + /** + * Check if a user received an invitation to join a group or not + * + * @param groupID Target group ID + * @param userID Target user ID + */ + public static async ReceivedInvitation(groupID: number, userID: number) : Promise { + return await DatabaseHelper.Count({ + table: GROUPS_MEMBERS_TABLE, + where: { + groups_id: groupID, + user_ID: userID, + level: GroupMembershipLevels.INVITED + } + }) > 0; + } + + /** + * Respond to a membership invitation + * + * @param groupID Target group ID + * @param userID Target user ID + * @param accept true to accept invitation / FALSE else + */ + public static async RespondInvitation(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 * @@ -189,6 +220,39 @@ export class GroupsHelper { }); } + /** + * Update a user membership level + * + * @param groupID Target group ID + * @param userID Target user ID + * @param level New membership level + */ + public static async UpdateMembershipLevel(groupID: number, userID: number, level: GroupMembershipLevels) { + await DatabaseHelper.UpdateRows({ + table: GROUPS_MEMBERS_TABLE, + where: { + user_id: userID, + groups_id: groupID + }, + set: { + level: level + } + }); + } + + /** + * Delete completely a user membership + * + * @param groupID Target group ID + * @param userID Target user ID + */ + public static async DeleteMember(groupID: number, userID: number) { + await DatabaseHelper.DeleteRows(GROUPS_MEMBERS_TABLE, { + groups_id: groupID, + user_id: userID + }); + } + /** * Get the membership level of a user for a group