public ActionResult Add(AddTeamMemberRequestVM model) { // This method adds a user to the team // If user is new to the system, sends an email to user to signup and join, else add him to the team try { if (ModelState.IsValid) { var existingUser = repo.GetUser(model.EmailAddress); if (existingUser != null) { TeamMember teamMember = new TeamMember { MemberID = existingUser.ID, TeamID = TeamID, CreatedByID = UserID }; var result = repo.SaveTeamMember(teamMember); if (result.ID > 0) { return Json(new { Status = "Success",Message="Successfully added user to team" }); } } else { var teamMemberRequest = new TeamMemberRequest { EmailAddress = model.EmailAddress, CreatedByID = UserID }; teamMemberRequest.TeamID = model.TeamID; teamMemberRequest.ActivationCode = model.TeamID + "-" + Guid.NewGuid().ToString("n"); var resultNew = repo.SaveTeamMemberRequest(teamMemberRequest); if (resultNew.Status) { new UserService(repo,SiteBaseURL).SendJoinMyTeamEmail(teamMemberRequest); return Json(new { Status = "Success" }); } else { log.Debug(resultNew); } } } } catch (Exception ex) { log.Error(ex); } return Json(new { Status = "Error", Message = "Error adding user to team" }); }
private async Task SendTeamMemberInvitationEmail(AddTeamMemberRequestVM teamMemberRequest) { try { var emailTemplate = await _emailRepository.GetEmailTemplate("JoinMyTeam"); if (emailTemplate != null) { var emailSubject = emailTemplate.Subject; var emailBody = emailTemplate.EmailBody; var email = new Email(); email.ToAddress.Add(teamMemberRequest.EmailAddress); var joinLink = String.Format("{0}Account/Join?returnurl={1}", this._settings.SiteUrl, teamMemberRequest.ActivationCode); emailBody = emailBody.Replace("@teamName", teamMemberRequest.Team.Name); emailBody = emailBody.Replace("@joinUrl", joinLink); emailBody = emailBody.Replace("@inviter", teamMemberRequest.CreatedBy.Name); email.Body = emailBody; email.Subject = emailSubject; await this._emailManager.Send(email); } } catch (Exception) { // Silently fail. We will log this. But we do not want to show an error to user because of this } }
public async Task AddNewTeamMember(AddTeamMemberRequestVM teamMemberRequest) { var user = await _userRepository.GetUser(teamMemberRequest.EmailAddress); if (user != null) { _teamRepository.SaveTeamMember(this._userSessionHelper.TeamId, user.Id, this._userSessionHelper.UserId); } else { teamMemberRequest.TeamId = this._userSessionHelper.TeamId; teamMemberRequest.CreatedById = this._userSessionHelper.UserId; var id = await _teamRepository.SaveTeamMemberRequest(teamMemberRequest); var requests = await _teamRepository.GetTeamMemberInvitations(this._userSessionHelper.TeamId) ; var r = requests.FirstOrDefault(s => s.Id == id); await SendTeamMemberInvitationEmail(r); } }