From fed23994fa5fdb38e2a257873c5617e0e83befdf Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 31 Dec 2019 10:47:55 +0100 Subject: [PATCH] Send (create) a friendship request --- src/controllers/FriendsController.ts | 27 +++++++++++++++++++++++++++ src/controllers/Routes.ts | 2 ++ src/helpers/FriendsHelper.ts | 17 +++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/src/controllers/FriendsController.ts b/src/controllers/FriendsController.ts index ea801f3..88687cf 100644 --- a/src/controllers/FriendsController.ts +++ b/src/controllers/FriendsController.ts @@ -88,6 +88,33 @@ export class FriendsController { h.send(response); } + /** + * Send a friendship request + * + * @param h Request handler + */ + public static async SendRequest(h: RequestHandler) { + const friendID = await h.postUserId("friendID"); + + if(friendID == h.getUserId()) + h.error(401, "You can not become a friend of yourself!"); + + if(await FriendsHelper.AreFriend(h.getUserId(), friendID)) + h.error(401, "You are already friend with this personn!"); + + // Check if a request is already in progress + if(await FriendsHelper.SentRequest(h.getUserId(), friendID) + || await FriendsHelper.SentRequest(friendID, h.getUserId())) + h.error(401, "A friendship request is already in progress!"); + + // Send the request + await FriendsHelper.SendRequest(h.getUserId(), friendID); + + // TODO : create the notification + + h.success("Send (create) the friendship request"); + } + /** * Turn a friend object into an API entry * diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 4d31b55..c5c92bb 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -83,6 +83,8 @@ export const Routes : Route[] = [ {path: "/friends/getStatus", cb: (h) => FriendsController.GetStatus(h)}, + {path: "/friends/sendRequest", cb: (h) => FriendsController.SendRequest(h)}, + // Conversations controller {path: "/conversations/create", cb: (h) => ConversationsController.CreateConversation(h)}, diff --git a/src/helpers/FriendsHelper.ts b/src/helpers/FriendsHelper.ts index be7138d..1e10b05 100644 --- a/src/helpers/FriendsHelper.ts +++ b/src/helpers/FriendsHelper.ts @@ -80,6 +80,23 @@ export class FriendsHelper { return this.DbToFriend(result); } + /** + * Send (create) a new membership request + * + * @param userID The user sending the request + * @param targetUser The ID of the target user + */ + public static async SendRequest(userID: number, targetUser: number) { + await DatabaseHelper.InsertRow( + FRIENDS_TABLE, + { + ID_personne: targetUser, + ID_amis: userID, + actif: 0 + } + ); + } + /** * Check out whether a user sent a request to another friend *