diff --git a/src/controllers/groups_controller.rs b/src/controllers/groups_controller.rs index 934cd28..5dcbd1f 100644 --- a/src/controllers/groups_controller.rs +++ b/src/controllers/groups_controller.rs @@ -13,6 +13,7 @@ use crate::constants::{DEFAULT_GROUP_LOGO, PATH_GROUPS_LOGOS}; use crate::controllers::routes::RequestResult; use crate::data::group::{Group, GroupAccessLevel, GroupPostsCreationLevel, GroupRegistrationLevel, GroupVisibilityLevel}; use crate::data::group_id::GroupID; +use crate::data::group_member::GroupMembershipLevel; use crate::data::http_request_handler::HttpRequestHandler; use crate::data::new_group::NewGroup; use crate::helpers::{groups_helper, virtual_directory_helper}; @@ -159,5 +160,16 @@ pub fn get_members(r: &mut HttpRequestHandler) -> RequestResult { /// Invite a user to join a group pub fn invite_user(r: &mut HttpRequestHandler) -> RequestResult { - r.success("Ready to implement") + let group_id = r.post_group_id_with_access("group_id", GroupAccessLevel::MODERATOR_ACCESS)?; + let user_id = r.post_user_id("userID")?; + + if groups_helper::get_membership_level(&group_id, Some(user_id.clone()))? != GroupMembershipLevel::VISITOR { + r.bad_request("The user is not a visitor of the group!".to_string())?; + } + + groups_helper::send_invitation(&group_id, &user_id)?; + + // TODO : send a notification + + r.success("The user has been successfully invited to join the group!") } \ No newline at end of file diff --git a/src/helpers/groups_helper.rs b/src/helpers/groups_helper.rs index 659782b..ad175a3 100644 --- a/src/helpers/groups_helper.rs +++ b/src/helpers/groups_helper.rs @@ -331,6 +331,18 @@ pub fn get_list_members(g: &GroupID) -> ResultBoxError> { .exec(db_to_group_member) } +/// Send an invitation to a user +pub fn send_invitation(group_id: &GroupID, user_id: &UserID) -> ResultBoxError { + insert_member(&GroupMember { + id: 0, + user_id: user_id.clone(), + group_id: group_id.clone(), + time_create: time(), + level: GroupMembershipLevel::INVITED, + following: true, + }) +} + /// Turn a database entry into a group struct fn db_to_group(row: &database::RowResult) -> ResultBoxError { let group_id = row.get_group_id("id")?;