public async Task <IActionResult> CreateInvitation(string inviteeUsername, Guid travelPlanId) { try { var loggedInUserId = HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value; await _planInvitationRepository.InviteUser(new Guid(loggedInUserId), inviteeUsername, travelPlanId); return(Ok()); } catch (InsufficientRightsException insufRights) { return(BadRequest(new { Message = insufRights.Message })); } catch (UserNotFoundException notFoundExc) { return(BadRequest(new { Message = notFoundExc.Message })); } catch (UniqueConstraintException uniqExc) { return(BadRequest(new { Message = uniqExc.Message })); } catch (CommonException commExc) { return(BadRequest(new { Message = commExc.Message })); } catch (Exception exc) { return(BadRequest(new { Message = "An error occurred sending invitation" })); } }
public async Task InviteUser(Guid inviter, string inviteeUsername, Guid travelPlanId) { try { var travelPlan = await _travelPlanRepository.GetAsync(travelPlanId, includeUTP : true); //validate the inviter is the host if (travelPlan.CreatedById != inviter) { //log here throw new InsufficientRightsException("User doesn't have rights to add to travelplan"); } //validate invitee exists var userToInvite = await _userRepository.GetUserAsync(inviteeUsername); if (userToInvite == null) { //log here throw new UserNotFoundException("User to add does not exist"); } //check if user to invite is already part of plan if (travelPlan.UserTravelPlans.Exists((utp) => utp.TravelPlanId == new Guid(userToInvite.Id))) { throw new CommonException("User is already a traveler!"); } var newInvitation = new PlanInvitation { Created = DateTime.UtcNow, Expiration = DateTime.UtcNow.AddDays(7), InvitedById = inviter, InviteeId = new Guid(userToInvite.Id), TravelPlanId = travelPlanId }; await _planInvitationRepository.InviteUser(newInvitation); } catch (Exception) { throw; } }