protected virtual async Task TryToSendNotificationsAsync(GenericChangedEntry <Subscription> changedEntry)
        {
            //Collection of order notifications
            var notifications = new List <SubscriptionEmailNotificationBase>();

            if (IsSubscriptionCanceled(changedEntry))
            {
                //Resolve SubscriptionCanceledEmailNotification with template defined on store level
                var notification = await _notificationSearchService.GetNotificationAsync <SubscriptionCanceledEmailNotification>(new TenantIdentity(changedEntry.NewEntry.StoreId, nameof(Store)));

                if (notification != null)
                {
                    notifications.Add(notification);
                }
            }

            if (changedEntry.EntryState == EntryState.Added)
            {
                //Resolve NewSubscriptionEmailNotification with template defined on store level
                var notification = await _notificationSearchService.GetNotificationAsync <NewSubscriptionEmailNotification>(new TenantIdentity(changedEntry.NewEntry.StoreId, nameof(Store)));

                if (notification != null)
                {
                    notifications.Add(notification);
                }
            }

            foreach (var notification in notifications)
            {
                await SetNotificationParametersAsync(notification, changedEntry.NewEntry);

                await _notificationSender.SendNotificationAsync(notification, changedEntry.NewEntry.CustomerOrderPrototype.LanguageCode);
            }
        }
Esempio n. 2
0
        public async Task <ActionResult <Notification> > GetNotificationByTypeId(string type, string tenantId = null, string tenantType = null)
        {
            var responseGroup = NotificationResponseGroup.Full.ToString();
            var notification  = await _notificationSearchService.GetNotificationAsync(type, new TenantIdentity(tenantId, tenantType));

            return(Ok(notification));
        }
Esempio n. 3
0
        public virtual async Task TryToSendOrderNotificationsAsync(OrderNotificationJobArgument[] jobArguments)
        {
            var ordersByIdDict = (await _customerOrderService.GetAsync(jobArguments.Select(x => x.CustomerOrderId).Distinct().ToList()))
                                 .ToDictionary(x => x.Id)
                                 .WithDefaultValue(null);

            foreach (var jobArgument in jobArguments)
            {
                var notification = await _notificationSearchService.GetNotificationAsync(jobArgument.NotificationTypeName, new TenantIdentity(jobArgument.StoreId, nameof(Store)));

                if (notification != null)
                {
                    var order = ordersByIdDict[jobArgument.CustomerOrderId];

                    if (order != null && notification is CustomerReviewEmailNotification orderNotification)
                    {
                        var customer = await _memberResolver.ResolveMemberByIdAsync(jobArgument.CustomerId);

                        orderNotification.Item         = order.Items.FirstOrDefault(i => i.ProductId == jobArgument.ProductId);
                        orderNotification.Customer     = customer;
                        orderNotification.RequestId    = jobArgument.RequestId;
                        orderNotification.LanguageCode = order.LanguageCode;

                        await SetNotificationParametersAsync(notification, order, customer);

                        await _notificationSender.ScheduleSendNotificationAsync(notification);
                    }
                }
            }
        }
        public async Task <ActionResult> GetInvoicePdf(string orderNumber)
        {
            var searchCriteria = AbstractTypeFactory <CustomerOrderSearchCriteria> .TryCreateInstance();

            searchCriteria.Number = orderNumber;
            searchCriteria.Take   = 1;
            //ToDo
            //searchCriteria.ResponseGroup = OrderReadPricesPermission.ApplyResponseGroupFiltering(_securityService.GetUserPermissions(User.Identity.Name), null);

            var orders = await _searchService.SearchCustomerOrdersAsync(searchCriteria);

            var order = orders.Results.FirstOrDefault();

            if (order == null)
            {
                throw new InvalidOperationException($"Cannot find order with number {orderNumber}");
            }

            var notification = await _notificationSearchService.GetNotificationAsync <InvoiceEmailNotification>(new TenantIdentity(order.StoreId, nameof(Store)));

            notification.CustomerOrder = order;
            var message = AbstractTypeFactory <EmailNotificationMessage> .TryCreateInstance($"{notification.Kind}Message");

            message.LanguageCode = order.LanguageCode;
            await notification.ToMessageAsync(message, _notificationTemplateRenderer);

            if (message.Body.IsNullOrEmpty())
            {
                throw new InvalidOperationException($"Document could not be rendered because InvoiceEmailNotification template is empty.");
            }

            byte[] result = GeneratePdf(message.Body);

            return(new FileContentResult(result, "application/pdf"));
        }
Esempio n. 5
0
        public static async Task <T> GetNotificationAsync <T>(this INotificationSearchService service, TenantIdentity tenant = null) where T : Notification
        {
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }
            var result = await service.GetNotificationAsync(typeof(T).Name, tenant);

            return(result as T);
        }
        public async Task Do()
        {
            var notification = await _notificationSearchService.GetNotificationAsync <ExtendedSampleEmailNotification>();

            if (notification != null)
            {
                notification.LanguageCode = "en-US";
                notification.SetFromToMembers("*****@*****.**", "*****@*****.**");
                await _notificationSender.SendNotificationAsync(notification);
            }
        }
        public async Task <ActionResult> GetInvoicePdf(string orderNumber)
        {
            var searchCriteria = AbstractTypeFactory <CustomerOrderSearchCriteria> .TryCreateInstance();

            searchCriteria.Number = orderNumber;
            searchCriteria.Take   = 1;

            var orders = await _searchService.SearchCustomerOrdersAsync(searchCriteria);

            var order = orders.Results.FirstOrDefault();

            if (order == null)
            {
                throw new InvalidOperationException($"Cannot find order with number {orderNumber}");
            }

            var notification = await _notificationSearchService.GetNotificationAsync <InvoiceEmailNotification>(new TenantIdentity(order.StoreId, nameof(StoreModule.Core.Model.Store)));

            var message = AbstractTypeFactory <NotificationMessage> .TryCreateInstance($"{notification.Kind}Message");

            message.LanguageCode = order.LanguageCode;
            var emailNotificationMessage = (EmailNotificationMessage)notification.ToMessage(message, _notificationTemplateRenderer);

            var pdf = new HtmlToPdfDocument()
            {
                GlobalSettings = { ColorMode = ColorMode.Color, Orientation = Orientation.Landscape, PaperSize = PaperKind.A4Plus },
                Objects        = { new ObjectSettings {
                                       PagesCount = true, HtmlContent = emailNotificationMessage.Body
                                   } }
            };
            var    converter = new SynchronizedConverter(new PdfTools());
            var    byteArray = converter.Convert(pdf);
            Stream stream    = new MemoryStream(byteArray);

            return(new FileStreamResult(stream, "application/pdf"));
        }
Esempio n. 8
0
        protected virtual async Task TryToSendOrderNotificationsAsync(GenericChangedEntry <CustomerOrder> changedEntry)
        {
            // Collection of order notifications
            var notifications = new List <OrderEmailNotificationBase>();

            if (IsOrderCanceled(changedEntry))
            {
                var notification = await _notificationSearchService.GetNotificationAsync <CancelOrderEmailNotification>(new TenantIdentity(changedEntry.NewEntry.StoreId, nameof(Store)));

                if (notification != null)
                {
                    notifications.Add(notification);
                }
            }

            if (changedEntry.EntryState == EntryState.Added && !changedEntry.NewEntry.IsPrototype)
            {
                var notification = await _notificationSearchService.GetNotificationAsync <OrderCreateEmailNotification>(new TenantIdentity(changedEntry.NewEntry.StoreId, nameof(Store)));

                if (notification != null)
                {
                    notifications.Add(notification);
                }
            }

            if (HasNewStatus(changedEntry))
            {
                var notification = await _notificationSearchService.GetNotificationAsync <NewOrderStatusEmailNotification>(new TenantIdentity(changedEntry.NewEntry.StoreId, nameof(Store)));

                if (notification != null)
                {
                    notification.NewStatus = changedEntry.NewEntry.Status;
                    notification.OldStatus = changedEntry.OldEntry.Status;
                    notifications.Add(notification);
                }
            }

            if (IsOrderPaid(changedEntry))
            {
                var notification = await _notificationSearchService.GetNotificationAsync <OrderPaidEmailNotification>(new TenantIdentity(changedEntry.NewEntry.StoreId, nameof(Store)));

                if (notification != null)
                {
                    notifications.Add(notification);
                }
            }

            if (IsOrderSent(changedEntry))
            {
                var notification = await _notificationSearchService.GetNotificationAsync <OrderSentEmailNotification>(new TenantIdentity(changedEntry.NewEntry.StoreId, nameof(Store)));

                if (notification != null)
                {
                    notifications.Add(notification);
                }
            }

            var customer = await GetCustomerAsync(changedEntry.NewEntry.CustomerId);

            foreach (var notification in notifications)
            {
                notification.CustomerOrder = changedEntry.NewEntry;
                notification.Customer      = customer;
                notification.LanguageCode  = changedEntry.NewEntry.LanguageCode;
                await SetNotificationParametersAsync(notification, changedEntry);

                _notificationSender.ScheduleSendNotification(notification);
            }
        }