public virtual async Task <ActionResult <ResponseDto> > Create([FromBody] ResponseRequest request) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var dto = await _responseService.CreateEntityAsync(request); if (dto == null) { return(StatusCode(500)); } var userFeedback = request.Feedback.User; if (userFeedback != null) { var notificationRequest = new NotificationRequest { Text = request.Text, CreatedAt = request.CreatedAt, UserId = userFeedback.Id, OrganizationId = userFeedback.LastPickedOrganizationId, Type = NotificationType.Info }; await _notificationService.CreateEntityAsync(notificationRequest); } else { await _emailProvider.SendMessageOneToOne("*****@*****.**", "Thanks for feedback", request.Feedback.Email, request.Text, ""); } return(CreatedAtAction("GetById", new { id = dto.Id }, dto)); }
public virtual async Task <ActionResult <OrganizationInviteDto> > CreatedAndSend([FromBody] OrganizationInviteRequest request) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var dto = await _service.CreateEntityAsync(request); if (dto == null) { return(StatusCode(500)); } if (String.IsNullOrWhiteSpace(request.InviteEmail) != true) { var fromUser = await _usersService.GetEntityByIdAsync(request.CreatedByUserId); var company = await _organizationService.GetEntityByIdAsync(request.OrganizationId); string body = $"User {fromUser.DisplayName} granted you access to the organization {company.Name}. \n "; body += $"Your invite link: https://bsa-watcher.azurewebsites.net/invite/{request.Link}"; await _emailProvider.SendMessageOneToOne("*****@*****.**", "WATCHER Invite", request.InviteEmail, body, ""); } return(Ok(dto)); }
public virtual ActionResult <InstanceAnomalyReport> GetReport([FromRoute] Guid instanceId) { var report = InstanceAnomalyReportsService.GetAnomalyReport(instanceId); var dto = _mapper.Map <InstanceAnomalyReport, InstanceAnomalyReportDto>(report); var html = InstanceAnomalyReportsService.GetHtml(dto); var htmlLetter = InstanceAnomalyReportsService.GetHtmlForLetter("userName", "instanceName", "bsa-watcher.azurewebsites.net"); _emailProvider.SendMessageOneToOne("*****@*****.**", "Analyze", "target.com", "", htmlLetter); return(Ok(report)); }
private async Task SendToEmailIfNeeded(UserDto userDto, MessageDto messageDto) { if (userDto.Id != messageDto.User.Id) { var settings = await _chatsService.GetSettingsForUserIdAsync(userDto.Id, messageDto.ChatId); if (settings != null && settings.IsEmailable) { await _emailProvider.SendMessageOneToOne("*****@*****.**", $"Chat message from {messageDto.User.DisplayName}", userDto.Email, messageDto.Text, ""); } } }
public virtual async Task <ActionResult <FeedbackDto> > Create([FromBody] FeedbackRequest request) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var dto = await _feedbackService.CreateEntityAsync(request); if (dto == null) { return(StatusCode(500)); } if (!string.IsNullOrEmpty(request.Email)) { await _emailProvider.SendMessageOneToOne("*****@*****.**", "Thanks for feedback", request.Email, "Hello, " + request.Name + ". Thank you for taking the time to type feedback. " + "Best regards, Watcher.", ""); } return(CreatedAtAction("GetById", new { id = dto.Id }, dto)); }
public async Task <bool> UpdateEntityByIdAsync(OrganizationInviteRequest request, int id) { bool result = false; if (request.CreatedByUserId == request.InvitedUserId) { return(false); } var entity = _mapper.Map <OrganizationInviteRequest, OrganizationInvite>(request); entity.Id = id; var inviteFromDb = await _uow.OrganizationInvitesRepository.GetFirstOrDefaultAsync(x => x.Id == entity.Id); if (inviteFromDb == null) { return(false); } if (inviteFromDb.State != OrganizationInviteState.Pending) { return(false); } if (entity.State == OrganizationInviteState.Accepted) { var invitedUser = await _uow.UsersRepository.GetFirstOrDefaultAsync(x => x.Id == entity.InvitedUserId); await _uow.BeginTransaction(); if (invitedUser.UserOrganizations == null) { invitedUser.UserOrganizations = new List <UserOrganization>(); } //TODO: organization role in request //id 2 is member invitedUser.UserOrganizations.Add(new UserOrganization { UserId = invitedUser.Id, OrganizationId = entity.OrganizationId, OrganizationRoleId = 2 }); invitedUser.LastPickedOrganizationId = entity.OrganizationId; await _uow.UsersRepository.UpdateAsync(invitedUser); var updated = await _uow.OrganizationInvitesRepository.UpdateAsync(entity); result = await _uow.SaveAsync(); _uow.CommitTransaction(); } else { if (entity.State == OrganizationInviteState.Pending) { if (String.IsNullOrWhiteSpace(entity.InviteEmail) != true) // send Email { OrganizationInvite inviteFromBd = await _uow.OrganizationInvitesRepository.GetFirstOrDefaultAsync(x => x.Id == entity.Id, include : invite => invite.Include(i => i.Organization) .Include(i => i.CreatedByUser)); string body = $"User {inviteFromBd.CreatedByUser.DisplayName} granted you access to the organization {inviteFromBd.Organization.Name}. \n "; body += $"Your invite link: https://bsa-watcher.azurewebsites.net/invite/{entity.Link}"; await _emailProvider.SendMessageOneToOne("*****@*****.**", "WATCHER Invite", entity.InviteEmail, body, ""); } } var updated = await _uow.OrganizationInvitesRepository.UpdateAsync(entity); result = await _uow.SaveAsync(); } var dto = _mapper.Map <OrganizationInvite, OrganizationInviteDto>(await _uow.OrganizationInvitesRepository.GetFirstOrDefaultAsync(i => i.Id == id)); if (InvitesHub.UsersConnections.ContainsKey(dto.CreatedByUserId)) { foreach (string connectionId in InvitesHub.UsersConnections[dto.CreatedByUserId]) { await _invitesHub.Clients.Client(connectionId) .SendAsync("UpdateInvite", dto); } } return(result); }
public async Task <IEnumerable <NotificationDto> > CreateEntityAsync(NotificationRequest notificationRequest) { var receivers = new List <User>(); var notifications = new List <NotificationDto>(); var organizationId = notificationRequest.OrganizationId; int?instanceId = null; if (notificationRequest.InstanceId != null) { var instance = await _uow.InstanceRepository.GetFirstOrDefaultAsync(i => i.GuidId == notificationRequest.InstanceId); if (instance == null) { return(null); } organizationId = instance.OrganizationId; instanceId = instance.Id; } var entityNotification = _mapper.Map <NotificationRequest, Notification>(notificationRequest); entityNotification.InstanceId = instanceId; entityNotification.InstanceGuidId = notificationRequest.InstanceId; if (notificationRequest.UserId != null) { receivers.Add(await _uow.UsersRepository.GetFirstOrDefaultAsync(u => u.Id == notificationRequest.UserId)); } else if (organizationId != null) { var organizationReceiver = await _uow.OrganizationRepository.GetFirstOrDefaultAsync(o => o.Id == organizationId, include : organizations => organizations.Include(o => o.UserOrganizations) .ThenInclude(uo => uo.User)); foreach (var userOrganization in organizationReceiver.UserOrganizations) { receivers.Add(userOrganization.User); } } else { return(null); } foreach (var receiver in receivers) { var entity = _mapper.Map <NotificationRequest, Notification>(notificationRequest); entity.UserId = receiver.Id; entity.InstanceId = instanceId; entity.InstanceGuidId = notificationRequest.InstanceId; var notificationSetting = await _uow.NotificationSettingsRepository.GetFirstOrDefaultAsync( ns => ns.Type == notificationRequest.Type && ns.UserId == entity.UserId); if (notificationSetting == null || notificationSetting.IsDisable) { continue; } entity.NotificationSettingId = notificationSetting.Id; var created = await _uow.NotificationsRepository.CreateAsync(entity); var result = await _uow.SaveAsync(); if (!result) { return(null); } var dto = _mapper.Map <Notification, NotificationDto>(created); dto.NotificationSetting = _mapper.Map <NotificationSetting, NotificationSettingDto>(notificationSetting); if (notificationSetting.IsEmailable) { await _emailProvider.SendMessageOneToOne("*****@*****.**", $"{notificationSetting.Type} Notification", receiver.EmailForNotifications, dto.Text, ""); } notifications.Add(dto); if (!NotificationsHub.UsersConnections.ContainsKey(dto.UserId)) { continue; } foreach (string connectionId in NotificationsHub.UsersConnections[dto.UserId]) { await _notificationsHub.Clients.Client(connectionId) .SendAsync("AddNotification", dto); } } return(notifications); }