public override async Task <bool> IsSatisfiedByAsync(Customer entity)
        {
            var query = await _getCustomerByGovernamentalDocumentNumberQueryAdapter.AdapteeAsync(
                entity,
                await _getCustomerByGovernamentalDocumentNumberQueryFactory.CreateAsync());

            await Bus.SendQueryAsync(query);

            return((query.QueryReturn?.Id ?? Guid.Empty) == Guid.Empty);
        }
예제 #2
0
        public async Task <Customer> ImportCustomerAsync(string tenantCode, string creationUser, Customer customerToImport)
        {
            /* Validate */
            if (await ValidateAsync(customerToImport, _customerIsValidForImportValidation) == false)
            {
                return(customerToImport);
            }

            /* Process */

            // 1º Step - Import customer
            var importedCustomer = (await Factory.CreateAsync()).ImportCustomer <Customer>(
                tenantCode,
                customerToImport.Name,
                customerToImport.GovernamentalDocumentNumber,
                creationUser);

            // 2º Step - Check and get a existing customer by governamental document number
            var getCustomerByGovernamentalDocumentNumberQuery = await _getCustomerByGovernamentalDocumentNumberQueryAdapter.AdapteeAsync(
                importedCustomer,
                await _getCustomerByGovernamentalDocumentNumberQueryFactory.CreateAsync());

            var existingCustomer = await Bus.SendQueryAsync(getCustomerByGovernamentalDocumentNumberQuery);

            // 3º Step - Change Id if customer existing
            var hasExistingCustomer = (existingCustomer?.Id ?? Guid.Empty) != Guid.Empty;

            if (hasExistingCustomer)
            {
                importedCustomer.ChangeId(existingCustomer.Id);
            }

            /* Notify */
            if (!hasExistingCustomer)
            {
                _ = await Bus.SendEventAsync(await _customerWasImportedEventFactory.CreateAsync(importedCustomer));
            }
            else
            {
                _ = await Bus.SendEventAsync(await _customerWasUpdatedEventFactory.CreateAsync(importedCustomer));
            }

            return(importedCustomer);
        }