public async void Handle(OrderTimedOut @event) { using (var context = _contextFactory.Invoke()) { var details = context.Find <OrderStatusDetail>(@event.SourceId); if (details == null) { throw new InvalidOperationException("Order Status not found"); } var order = context.Find <OrderDetail>(@event.SourceId); if (order == null) { throw new InvalidOperationException("Order not found"); } var pickUpPosition = new MapCoordinate { Latitude = order.PickupAddress.Latitude, Longitude = order.PickupAddress.Longitude }; NetworkFleetResponse nextDispatchCompany = null; if (@event.Market.HasValue()) { // External market var marketFleet = GetMarketFleets(@event.Market, details.CompanyKey, pickUpPosition.Latitude, pickUpPosition.Longitude); nextDispatchCompany = FindNextDispatchCompany(details.CompanyKey, pickUpPosition, marketFleet, @event.Market); } else { // Local market var taxiHailNetworkSettings = _configurationDao.GetUserTaxiHailNetworkSettings(details.AccountId) ?? new UserTaxiHailNetworkSettings { IsEnabled = true, DisabledFleets = new string[] {} }; if (taxiHailNetworkSettings.IsEnabled) { var networkFleet = await _taxiHailNetworkServiceClient.GetNetworkFleetAsync(details.CompanyKey, pickUpPosition.Latitude, pickUpPosition.Longitude); // Remove fleets that were disabled by the user var userNetworkFleet = FilterNetworkFleet(taxiHailNetworkSettings.DisabledFleets, networkFleet); nextDispatchCompany = FindNextDispatchCompany(details.CompanyKey, pickUpPosition, userNetworkFleet); } } if (nextDispatchCompany != null) { _commandBus.Send(new PrepareOrderForNextDispatch { OrderId = details.OrderId, DispatchCompanyName = nextDispatchCompany.CompanyName, DispatchCompanyKey = nextDispatchCompany.CompanyKey }); } } }
public HttpResponseMessage Get(string companyId, double?latitude, double?longitude) { // Cast mongo repo to list to have access to all the LINQ enumerators without the mongo driver restriction var network = _networkRepository.ToList(); MapCoordinate coordinate = null; if (latitude.HasValue && longitude.HasValue) { coordinate = new MapCoordinate { Latitude = latitude.Value, Longitude = longitude.Value }; } var currentCompanyNetworkSettings = network.FirstOrDefault(n => n.Id == companyId); if (currentCompanyNetworkSettings == null || !currentCompanyNetworkSettings.IsInNetwork) { return(new HttpResponseMessage(HttpStatusCode.NoContent)); } var networkFleetResult = new List <NetworkFleetResponse>(); // Find all companies that are setup to dispatch orders var dispatchableCompanies = currentCompanyNetworkSettings.Preferences.Where(n => n.CanDispatch).OrderBy(n => n.Order); foreach (var companyPreferences in dispatchableCompanies) { var company = _companyRepository.FirstOrDefault(c => c.CompanyKey == companyPreferences.CompanyKey); if (company != null) { var networkSettings = network.FirstOrDefault(n => n.Id == company.CompanyKey); var isAllowed = networkSettings != null && IsFleetAllowed(networkSettings.FleetId, currentCompanyNetworkSettings.WhiteListedFleetIds, currentCompanyNetworkSettings.BlackListedFleetIds); if (!isAllowed) { // Local company is not allowed by the home company continue; } var ibsTimeDifferenceString = company.CompanySettings.FirstOrDefault(s => s.Key == "IBS.TimeDifference"); long ibsTimeDifference = 0; if (ibsTimeDifferenceString != null) { long.TryParse(ibsTimeDifferenceString.Value, out ibsTimeDifference); } var fleet = new NetworkFleetResponse { CompanyKey = company.CompanyKey, CompanyName = company.CompanyName, FleetId = networkSettings.FleetId, IbsPassword = company.IBS.Password, IbsUserName = company.IBS.Username, IbsUrl = company.IBS.ServiceUrl, IbsTimeDifference = ibsTimeDifference }; if (coordinate != null) { var market = _marketRepository.GetMarket(networkSettings.Market); if (market != null && market.Region != null) { var isCompanyInZone = market.Region.Contains(coordinate); if (isCompanyInZone) { // Company coverage is in the network zone. Add it to the fleet. networkFleetResult.Add(fleet); } } } else { networkFleetResult.Add(fleet); } } } return(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(JsonConvert.SerializeObject(networkFleetResult)) }); }