From a3e05bd49e1ad30cfb2c3bd6714875ba84164098 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 27 Dec 2019 18:31:31 +0100 Subject: [PATCH] Can delete a group membership --- src/controllers/GroupsController.ts | 21 +++++++++++++++++++++ src/controllers/Routes.ts | 2 ++ 2 files changed, 23 insertions(+) diff --git a/src/controllers/GroupsController.ts b/src/controllers/GroupsController.ts index 5a621f2..1b4c765 100644 --- a/src/controllers/GroupsController.ts +++ b/src/controllers/GroupsController.ts @@ -516,6 +516,27 @@ export class GroupsController { h.success("Membership invitation has been cancelled!"); } + /** + * Delete the membership of a user to a group + * + * @param h Request handler + */ + public static async RemoveMembership(h: RequestHandler) { + const groupID = await h.postGroupIDWithAccess("id", GroupsAccessLevel.LIMITED_ACCESS); + const level = await GroupsHelper.GetMembershipLevel(groupID, h.getUserId()); + + if(level == GroupMembershipLevels.ADMINISTRATOR + && await GroupsHelper.CountMembersAtLevel(groupID, GroupMembershipLevels.ADMINISTRATOR) == 1) + h.error(401, "You are the last administrator of the group!"); + + // Delete mebmership + await GroupsHelper.DeleteMember(groupID, h.getUserId()); + + // TODO : delete group membership notifications + + h.success("Your membership has been successfully deleted!"); + } + /** * Turn a GroupInfo object into a valid API object * diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 965662c..dd7bd21 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -124,4 +124,6 @@ export const Routes : Route[] = [ {path: "/groups/get_membership", cb: (h) => GroupsController.GetMembership(h)}, {path: "/groups/cancel_invitation", cb: (h) => GroupsController.CancelInvitation(h)}, + + {path: "/groups/remove_membership", cb: (h) => GroupsController.RemoveMembership(h)}, ] \ No newline at end of file