public async override Task <bool> IsSatisfiedBy(Customer instance) { var result = false; var getCustomerByCodeQuery = new GetCustomerByCodeQuery( _userSessionInfo, _globalizarionInfo, null, instance?.Code ); var handlerResult = await _inMemoryBus.SendQueryAsync( getCustomerByCodeQuery ).ConfigureAwait(false); if (handlerResult.success) { result = string.IsNullOrWhiteSpace(getCustomerByCodeQuery.QueryResult?.Code); } return(result); }
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); }