public async Task <Invitation> InviteUserAsync(InviteUserViewModel inviteViewModel, EntityHeader orgEntityHeader, EntityHeader userEntityHeader) { if (await _orgAccountRepo.QueryOrganizationHasAccountByEmailAsync(orgEntityHeader.Id, inviteViewModel.Email)) { var existingUser = await _appUserRepo.FindByEmailAsync(inviteViewModel.Email); var msg = UserManagementResources.InviteUser_AlreadyPartOfOrg.Replace(Tokens.USERS_FULL_NAME, existingUser.Name).Replace(Tokens.EMAIL_ADDR, inviteViewModel.Email); throw new ValidationException("Could not invite user", false, msg); } var invite = await _inviteUserRepo.GetInviteByOrgIdAndEmailAsync(orgEntityHeader.Id, inviteViewModel.Email); if (invite != null) { invite.Status = Invitation.StatusTypes.Replaced; await _inviteUserRepo.UpdateInvitationAsync(invite); } var inviteModel = new Invitation() { RowKey = Guid.NewGuid().ToId(), PartitionKey = orgEntityHeader.Id, OrganizationId = orgEntityHeader.Id, OrganizationName = orgEntityHeader.Text, InvitedById = userEntityHeader.Id, InvitedByName = userEntityHeader.Text, Name = inviteViewModel.Name, Email = inviteViewModel.Email, DateSent = DateTime.Now.ToJSONString(), Status = Invitation.StatusTypes.New, }; await _inviteUserRepo.InsertInvitationAsync(inviteModel); var subject = Resources.UserManagementResources.Invite_Greeting_Subject.Replace(Tokens.APP_NAME, AppConfig.AppName).Replace(Tokens.ORG_NAME, orgEntityHeader.Text); var message = Resources.UserManagementResources.InviteUser_Greeting_Message.Replace(Tokens.USERS_FULL_NAME, userEntityHeader.Text).Replace(Tokens.ORG_NAME, orgEntityHeader.Text).Replace(Tokens.APP_NAME, AppConfig.AppName); message += $"<br /><br />{inviteViewModel.Message}<br /><br />"; var acceptLink = $"{AppConfig.WebAddress}/organization/acceptinvite/{inviteModel.RowKey}"; message += Resources.UserManagementResources.InviteUser_ClickHere.Replace("[ACCEPT_LINK]", acceptLink); await _emailSender.SendAsync(inviteModel.Email, subject, message); inviteModel = await _inviteUserRepo.GetInvitationAsync(inviteModel.RowKey); inviteModel.DateSent = DateTime.Now.ToJSONString(); inviteModel.Status = Invitation.StatusTypes.Sent; await _inviteUserRepo.UpdateInvitationAsync(inviteModel); return(inviteModel); }
public async Task <InvokeResult <Invitation> > InviteUserAsync(Models.DTOs.InviteUser inviteViewModel, EntityHeader org, EntityHeader user) { ValidateAuthParams(org, user); if (inviteViewModel == null) { _adminLogger.AddCustomEvent(Core.PlatformSupport.LogLevel.Error, "OrgManager_InviteUserAsync", UserAdminErrorCodes.InviteIsNull.Message); return(InvokeResult <Invitation> .FromErrors(UserAdminErrorCodes.InviteIsNull.ToErrorMessage())); } if (String.IsNullOrEmpty(inviteViewModel.Email)) { _adminLogger.AddCustomEvent(Core.PlatformSupport.LogLevel.Error, "OrgManager_InviteUserAsync", UserAdminErrorCodes.InviteEmailIsEmpty.Message); return(InvokeResult <Invitation> .FromErrors(UserAdminErrorCodes.InviteEmailIsEmpty.ToErrorMessage())); } var emailRegEx = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"); if (!emailRegEx.Match(inviteViewModel.Email).Success) { _adminLogger.AddCustomEvent(Core.PlatformSupport.LogLevel.Error, "OrgManager_InviteUserAsync", UserAdminErrorCodes.InviteEmailIsInvalid.Message); return(InvokeResult <Invitation> .FromErrors(UserAdminErrorCodes.InviteEmailIsInvalid.ToErrorMessage())); } if (String.IsNullOrEmpty(inviteViewModel.Name)) { _adminLogger.AddCustomEvent(Core.PlatformSupport.LogLevel.Error, "OrgManager_InviteUserAsync", UserAdminErrorCodes.InviteNameIsEmpty.Message); return(InvokeResult <Invitation> .FromErrors(UserAdminErrorCodes.InviteNameIsEmpty.ToErrorMessage())); } if (await _orgUserRepo.QueryOrgHasUserByEmailAsync(org.Id, inviteViewModel.Email)) { var existingUser = await _appUserRepo.FindByEmailAsync(inviteViewModel.Email); if (existingUser != null) { var msg = UserAdminResources.InviteUser_AlreadyPartOfOrg.Replace(Tokens.USERS_FULL_NAME, existingUser.Name).Replace(Tokens.EMAIL_ADDR, inviteViewModel.Email); return(InvokeResult <Invitation> .FromErrors(new ErrorMessage(msg))); } else { _adminLogger.AddError("OrgManager_InviteUserAsync", "User Found in Org Unit XRef Table Storage, but not in User, bad data", new KeyValuePair <string, string>("OrgId", org.Id), new KeyValuePair <string, string>("Email", inviteViewModel.Email)); } } var existingInvite = await _inviteUserRepo.GetInviteByOrgIdAndEmailAsync(org.Id, inviteViewModel.Email); if (existingInvite != null) { existingInvite.Status = Invitation.StatusTypes.Replaced; await _inviteUserRepo.UpdateInvitationAsync(existingInvite); } Organization organization; organization = await _organizationRepo.GetOrganizationAsync(org.Id); if (organization == null) { /* Quick and Dirty Error Checking, should Never Happen */ return(InvokeResult <Invitation> .FromError("Could not Load Org")); } var inviteModel = new Invitation() { RowKey = Guid.NewGuid().ToId(), PartitionKey = org.Id, OrganizationId = org.Id, OrganizationName = org.Text, InvitedById = user.Id, InvitedByName = user.Text, Message = inviteViewModel.Message, Name = inviteViewModel.Name, Email = inviteViewModel.Email, DateSent = DateTime.Now.ToJSONString(), Status = Invitation.StatusTypes.New, }; await AuthorizeAsync(user, org, "InviteUser", inviteModel.RowKey); inviteModel.OrganizationName = organization.Name; await _inviteUserRepo.InsertInvitationAsync(inviteModel); inviteModel = await _inviteUserRepo.GetInvitationAsync(inviteModel.RowKey); await SendInvitationAsync(inviteModel, organization.Name, user); inviteModel.DateSent = DateTime.Now.ToJSONString(); inviteModel.Status = Invitation.StatusTypes.Sent; await _inviteUserRepo.UpdateInvitationAsync(inviteModel); return(InvokeResult <Invitation> .Create(inviteModel)); }