Beispiel #1
0
        public async Task <Customer> ImportCustomerAsync(Customer customer)
        {
            bool success;
            var  importedCustomer = default(Customer);

            #region Validate
            var validationResult = await _customerIsValidToImportValidator.Validate(customer);

            if (validationResult.HasErrorMessage)
            {
                await SendDomainNotifications(validationResult);

                return(_customerFactory.Create(validationResult));
            }
            #endregion

            // Check if customer exists
            var getCustomerByCodeQuery = new GetCustomerByCodeQuery(UserSessionInfo, GlobalizationInfo, validationResult, customer.Code);
            _ = await InMemoryBus.SendQueryAsync(getCustomerByCodeQuery);

            var customerAlreadyExists = getCustomerByCodeQuery.QueryResult != null;
            if (!customerAlreadyExists)
            {
                #region Process
                importedCustomer = _customerFactory.Create(validationResult);
                try
                {
                    importedCustomer.ImportCustomer(
                        new TenantInfoValueObject(UserSessionInfo.TenantInfo.TenantCode),
                        _systemUserFactory.Create(UserSessionInfo),
                        customer.Code,
                        customer.Name,
                        customer.BirthDate
                        );

                    success = importedCustomer?.Id != Guid.Empty;
                }
                catch (Exception ex)
                {
                    _ = await InMemoryBus.SendRaisedExceptionEventAsync(UserSessionInfo, GlobalizationInfo, ex);

                    _ = await InMemoryBus.SendEventAsync(new CustomerImportFailedEvent(
                                                             UserSessionInfo,
                                                             GlobalizationInfo,
                                                             validationResult,
                                                             customer));

                    success = false;
                }
                #endregion

                #region Notify
                if (!customerAlreadyExists)
                {
                    if (success)
                    {
                        _ = await InMemoryBus.SendEventAsync(new CustomerWasImportedEvent(
                                                                 UserSessionInfo,
                                                                 GlobalizationInfo,
                                                                 validationResult,
                                                                 importedCustomer
                                                                 ));
                    }
                    else
                    {
                        _ = await InMemoryBus.SendEventAsync(new CustomerImportFailedEvent(
                                                                 UserSessionInfo,
                                                                 GlobalizationInfo,
                                                                 validationResult,
                                                                 importedCustomer
                                                                 ));
                    }
                }
                #endregion
            }

            return(importedCustomer);
        }
Beispiel #2
0
        public async Task <Product> ImportProductAsync(Product product)
        {
            bool success         = false;
            var  importedProduct = default(Product);

            #region Validate
            var validationResult = await _productIsValidToImportValidator.Validate(product);

            if (validationResult.HasErrorMessage)
            {
                await SendDomainNotifications(validationResult);

                return(_productFactory.Create(validationResult));
            }
            #endregion

            // Check if product exists
            var getProductByCodeQuery = new GetProductByCodeQuery(UserSessionInfo, GlobalizationInfo, validationResult, product.Code);
            _ = await InMemoryBus.SendQueryAsync(getProductByCodeQuery);

            var productAlreadyExists = getProductByCodeQuery.QueryResult != null;
            if (!productAlreadyExists)
            {
                #region Process
                importedProduct = _productFactory.Create(validationResult);

                try
                {
                    importedProduct.ImportProduct(
                        new TenantInfoValueObject(UserSessionInfo.TenantInfo.TenantCode),
                        _systemUserFactory.Create(UserSessionInfo),
                        product.Code,
                        product.Name,
                        product.Description,
                        product.ActivationInfo.IsActive
                        );

                    success = importedProduct?.Id != Guid.Empty;
                }
                catch (Exception ex)
                {
                    _ = await InMemoryBus.SendRaisedExceptionEventAsync(UserSessionInfo, GlobalizationInfo, ex);

                    _ = await InMemoryBus.SendEventAsync(new ProductImportFailedEvent(
                                                             UserSessionInfo,
                                                             GlobalizationInfo,
                                                             validationResult,
                                                             product));

                    success = false;
                }
                #endregion
            }

            #region Notify
            if (!productAlreadyExists)
            {
                if (success)
                {
                    _ = await InMemoryBus.SendEventAsync(new ProductWasImportedEvent(
                                                             UserSessionInfo,
                                                             GlobalizationInfo,
                                                             validationResult,
                                                             importedProduct
                                                             ));
                }
                else
                {
                    _ = await InMemoryBus.SendEventAsync(new ProductImportFailedEvent(
                                                             UserSessionInfo,
                                                             GlobalizationInfo,
                                                             validationResult,
                                                             importedProduct
                                                             ));
                }
            }
            #endregion

            return(importedProduct);
        }
Beispiel #3
0
        public async Task <Order> ImportOrderAsync(Order order)
        {
            bool success;

            #region Validate
            var validationResult = await _orderIsValidToImportValidator.Validate(order);

            if (validationResult.HasErrorMessage)
            {
                await SendDomainNotifications(validationResult);

                return(_orderFactory.Create(validationResult));
            }
            #endregion

            #region Process
            var importedOrder = _orderFactory.Create(validationResult);
            try
            {
                importedOrder.ImportOrder(
                    new TenantInfoValueObject(UserSessionInfo.TenantInfo.TenantCode),
                    _systemUserFactory.Create(UserSessionInfo),
                    order.OrderDate,
                    order.Code,
                    order.Customer,
                    order.OrderShipping,
                    order.OrderItemCollection
                    );

                success = importedOrder?.Id != Guid.Empty;
            }
            catch (Exception ex)
            {
                _ = await InMemoryBus.SendRaisedExceptionEventAsync(UserSessionInfo, GlobalizationInfo, ex);

                success = false;
            }
            #endregion

            #region Notify
            if (success)
            {
                _ = await InMemoryBus.SendEventAsync(
                    new OrderWasImportedEvent(
                        UserSessionInfo,
                        GlobalizationInfo,
                        validationResult,
                        importedOrder
                        )
                    ).ConfigureAwait(false);
            }
            else
            {
                _ = await InMemoryBus.SendEventAsync(
                    new OrderImportFailedEvent(
                        UserSessionInfo,
                        GlobalizationInfo,
                        validationResult,
                        order
                        )
                    ).ConfigureAwait(false);
            }
            #endregion

            return(importedOrder);
        }