Esempio n. 1
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);
            }
        }