public async Task UpdateNotificationStatisticsAsync(Notification notification)
        {
            if (notification == null)
            {
                throw new ArgumentNullException(nameof(notification));
            }

            var stats = await _context.NotificationStatistics
                        .SingleOrDefaultAsync(s => s.NotificationId == notification.NotificationId);

            if (stats == null)
            {
                stats = new NotificationStatistics(notification);
                await _context.AddAsync(stats);
            }

            stats.RecipientsTotal = await _context.NotificationRecipients
                                    .CountAsync(r => r.NotificationId == notification.NotificationId);

            stats.SentTotal = await _context.NotificationRecipients
                              .CountAsync(r => r.NotificationId == notification.NotificationId && r.Sent.HasValue);

            stats.ErrorsTotal = await _context.NotificationRecipients
                                .CountAsync(r => r.NotificationId == notification.NotificationId && r.Errors != null);

            await _context.SaveChangesAsync();
        }
Ejemplo n.º 2
0
        public static async Task <IDisposableEntity <SmsNotification> > CreateSmsNotificationAsync(
            this TestServiceScope scope,
            string message                = TestingConstants.Placeholder,
            EventInfo eventInfo           = null,
            Product product               = null,
            Organization organization     = null,
            ApplicationUser createdByUser = null,
            NotificationStatus?status     = null,
            int?totalSent               = null,
            int?totalErrors             = null,
            IEnumerable <string> phones = null,
            IEnumerable <ApplicationUser> recipientUsers = null,
            IEnumerable <Registration> registrations     = null)
        {
            if (TestingConstants.Placeholder.Equals(message))
            {
                message = $"Test SMS message {Guid.NewGuid()}";
            }

            var recipients = new List <NotificationRecipient>();

            if (phones != null)
            {
                recipients.AddRange(phones
                                    .Select(NotificationRecipient.Sms));
            }

            if (recipientUsers != null)
            {
                recipients.AddRange(recipientUsers
                                    .Select(NotificationRecipient.Sms));
            }

            if (registrations != null)
            {
                recipients.AddRange(registrations
                                    .Select(NotificationRecipient.Sms));
            }

            var disposables = new List <IDisposable>();

            if (createdByUser == null)
            {
                var disposableUser = await scope.CreateUserAsync();

                createdByUser = disposableUser.Entity;
                disposables.Add(disposableUser);
            }

            NotificationStatistics stats = null;

            if (totalSent.HasValue || totalErrors.HasValue)
            {
                stats = new NotificationStatistics
                {
                    SentTotal   = totalSent ?? 0,
                    ErrorsTotal = totalErrors ?? 0
                };
            }

            eventInfo ??= product?.EventInfo;
            organization ??= eventInfo?.Organization;

            var notification = new SmsNotification(message)
            {
                EventInfo     = eventInfo,
                Product       = product,
                Organization  = organization,
                CreatedByUser = createdByUser,
                Status        = status ?? NotificationStatus.New,
                Recipients    = recipients,
                Statistics    = stats
            };

            await scope.Db.Notifications.AddAsync(notification);

            await scope.Db.SaveChangesAsync();

            return(new DisposableEntity <SmsNotification>(notification, scope.Db, disposables.ToArray()));
        }
Ejemplo n.º 3
0
 public NotificationStatisticsDto(NotificationStatistics stats)
 {
     Sent       = stats.SentTotal;
     Errors     = stats.ErrorsTotal;
     Recipients = stats.RecipientsTotal;
 }