Esempio n. 1
0
        public async Task <string> CreateAnomalyReportNotificationAsync(NotificationRequest notificationRequest, InstanceAnomalyReportDto report)
        {
            var receivers     = new List <User>();
            var notifications = new List <NotificationDto>();

            if (notificationRequest.InstanceId == null)
            {
                return(string.Empty);
            }

            var instance = await _uow.InstanceRepository.GetFirstOrDefaultAsync(i => i.GuidId == notificationRequest.InstanceId,
                                                                                include : instances => instances.Include(o => o.Organization)
                                                                                .ThenInclude(uo => uo.UserOrganizations)
                                                                                .ThenInclude(uo => uo.User));

            if (instance == null)
            {
                return(string.Empty);
            }

            var htmlTable  = InstanceAnomalyReportsService.GetHtml(report);
            var htmlDocUrl = await _fileStorageProvider.UploadHtmlFileAsync(htmlTable, report.Id);

            notificationRequest.Text           = $"Anomaly Report was created for instance: {instance.Title} (id: {instance.Id}). Download it <a href=\"{htmlDocUrl}\">here</a>";
            notificationRequest.OrganizationId = instance.OrganizationId;
            receivers.AddRange(instance.Organization.UserOrganizations.Select(userOrganization => userOrganization.User));

            foreach (var receiver in receivers)
            {
                var entity = _mapper.Map <NotificationRequest, Notification>(notificationRequest);
                entity.UserId     = receiver.Id;
                entity.InstanceId = instance.Id;
                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)
                {
                    var htmlLetter = InstanceAnomalyReportsService.GetHtmlForLetter(receiver.DisplayName, instance.Title, htmlDocUrl);
                    await _emailProvider.SendMessageOneToOne(
                        "*****@*****.**",
                        $"{notificationSetting.Type} Notification",
                        receiver.EmailForNotifications,
                        dto.Text, htmlLetter);
                }

                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(htmlDocUrl);
        }