コード例 #1
0
        public async Task <Notification> PushNotification(string body, int notificationTypeId, List <byte[]> attachments, List <string> attachmentNames = null)
        {
            Notification newNotification = null;
            var          now             = DateTime.Now;

            using (var context = ContextFactory.GetNewContext(_securityContext.CurrentUser, _localizationManager))
            {
                var notificationType = await context.NotificationTypes
                                       .AsNoTracking()
                                       .Include(nameof(NotificationType.NotificationTypeSetting))
                                       .SingleOrDefaultAsync(_ => _.NotificationCategory == (NotificationCategory)notificationTypeId);

                if (notificationType == null)
                {
                    return(null);
                }
                newNotification = new Notification
                {
                    NotificationTypeId   = notificationType.Id,
                    CreatedAt            = now,
                    IsProcessed          = false,
                    RecipientTo          = notificationType.NotificationTypeSetting.Recipients,
                    RecipientCc          = notificationType.NotificationTypeSetting.RecipientCcs,
                    RecipientBcc         = notificationType.NotificationTypeSetting.RecipientBccs,
                    ScheduledSendingDate = now,
                    Subject = $"{notificationType.Label} - {now.ToShortDateString()}",
                    Body    = body
                };
                if (newNotification != null)
                {
                    context.Notifications.ApplyChanges(newNotification);

                    await context.SaveChangesAsync();
                }
                if (attachments.Count != 0)
                {
                    int count = 0;
                    foreach (var attachment in attachments)
                    {
                        var newAttachment = new NotificationAttachment();
                        if (attachmentNames != null)
                        {
                            newAttachment.Name = attachmentNames[count];
                        }
                        else
                        {
                            newAttachment.Name = newNotification.Subject;
                        }
                        newAttachment.Attachment     = attachment;
                        newAttachment.NotificationId = newNotification.NotificationId;

                        context.NotificationAttachments.ApplyChanges(newAttachment);
                        await context.SaveChangesAsync();

                        count++;
                    }
                }
            }
            return(newNotification);
        }
コード例 #2
0
 private List <NotificationEmailData> MapNotificationEmails(IEnumerable <NotificationEmail> notificationEmails)
 {
     return(notificationEmails.Select(m => new NotificationEmailData
     {
         Id = m.Id,
         Subject = m.Subject,
         BodyHtml = m.Body,
         ToEmailAddresses = MapEmailAddresses(m.ToEmailAddresses),
         ToCcEmailAddresses = MapEmailAddresses(m.ToCcEmailAddresses),
         ToBccEmailAddresses = MapEmailAddresses(m.ToBccEmailAddresses),
         Attachments = m.NotificationEmailAttachments.Select(t
                                                             => NotificationAttachment.Create(t.Attachment.FileName, _fileFactoryService.Attachments.GetFilePath(t.Attachment.GenFileName)))
     }).ToList());
 }