public async Task <IHttpActionResult> Post(InviteModel inviteModel) { var userId = User.Identity.GetUserId(); var account = _AccountRepo.GetAccount(User.Identity.GetAccountId()); // enforce user is owner if (userId != account.Owner) { return(BadRequest("User is not authorized to make this request.")); } // too many users? if (account.Users.Count > 1) { return (BadRequest( string.Format( "There are already {0} users associated with this account. The current limit is two.", account.Users.Count))); } // too many invitations? Limit users to one invitation, revocable. var accountInvitations = _InviteRepo.FindByAccountId(account.Id.ToString()).Where(x => !x.IsDeleted).ToList(); if (accountInvitations.Any(x => !x.IsExpired && !x.IsAccepted)) { // there's already an outstanding invite. Send it back. return(BadRequest(string.Format("There is already an open invitation for this account."))); } // create a new invite var invite = new Invitation(account.Id.ToString(), inviteModel.ToEmail) { CreatedBy = userId, ModifiedBy = userId }; // add to the account and save _InviteRepo.Save(invite); // send an invite email to the recipient // TODO: This link will have to point to Keenan's end where a decision can be made, // depending on the client type (mobile/web), and open the link appropriately in the // native app or the confirmation screen. var acceptInviteLink = Url.Link("PATCHInvite", new { token = invite.Token }); var emailService = new EmailService(); try { await emailService.SendEmailAsync(invite.ToEmail, "You've been invited to Tandem!", string.Format( "{0} has invited you to join their Tandem account. <a href='{1}'>Click here</a> to get started!", User.Identity.Name, acceptInviteLink)); } catch (Exception e) { Log.Error("InvitationsController, Post, SendEmail", new { invite.Id }, e); return (InternalServerError( new Exception("There was an error sending the invitation email. Please contact support."))); } return(Ok()); }