public async Task <List <SpecialInstructionData> > GetSpecialInstructionsForLoadAsync(LoadEntity load) { if (load == null) { throw new Exception($"Load cannot be null"); } GuardCustomer(load.CustomerId); var orderedStops = load.LoadStops?.OrderBy(_ => _.StopNbr).ToList(); var origin = orderedStops?.FirstOrDefault(); var destination = orderedStops?.LastOrDefault(); if (origin == null || destination == null) { return(new List <SpecialInstructionData>()); } var matchingGroups = await _context.SpecialInstructions.AsNoTracking() .Include(x => x.SpecialInstructionEquipment) .Where(_ => _.CustomerId == load.CustomerId && (_.OriginCity == null || _.OriginCity == origin.City) && (_.OriginState == null || _.OriginState == origin.State) && (_.OriginPostalCode == null || _.OriginPostalCode == origin.PostalCode) && (_.OriginCountry == null || _.OriginCountry == origin.Country) && (_.DestinationCity == null || _.DestinationCity == destination.City) && (_.DestinationState == null || _.DestinationState == destination.State) && (_.DestinationPostalCode == null || _.DestinationPostalCode == destination.PostalCode) && (_.DestinationCountry == null || _.DestinationCountry == destination.Country) && (!_.SpecialInstructionEquipment.Any() || _.SpecialInstructionEquipment.Any(equipment => equipment.EquipmentId == load.EquipmentId)) ) .ToListAsync(); // Address standardization has to happen after initial query matchingGroups = matchingGroups.Where(_ => (_.OriginAddress1 == null || AddressValidationService.StandardizeAddress(origin.Address1, true) .Equals(AddressValidationService.StandardizeAddress(_.OriginAddress1, true), StringComparison.OrdinalIgnoreCase)) && (_.DestinationAddress1 == null || AddressValidationService.StandardizeAddress(destination.Address1, true) .Equals(AddressValidationService.StandardizeAddress(_.DestinationAddress1, true), StringComparison.OrdinalIgnoreCase)) ).ToList(); return(RankInstructions(matchingGroups)); }
public IList <ShippingLoadCarrierGroupData> GetLoadCarrierGroupsForLoad(Guid loadId) { //TODO: Add validation that user belongs to customer var load = _context.Loads .Include(_ => _.LoadStops) .SingleOrDefault(_ => _.LoadId == loadId); if (load == null) { throw new Exception($"Could not find load: {loadId}"); } GuardCustomer(load.CustomerId); var orderedStops = load.LoadStops?.OrderBy(_ => _.StopNbr).ToList(); var origin = orderedStops?.FirstOrDefault(); var destination = orderedStops?.LastOrDefault(); if (origin == null || destination == null) { return(null); } var carriers = _securityService.GetContractedCarriersByPrimaryCustomerIdAsync(); var matchingGroups = _context.LoadCarrierGroups .Include(_ => _.LoadCarrierGroupType) .Include(_ => _.LoadCarrierGroupCarriers) .ThenInclude(_ => _.Carrier) .Where(_ => _.CustomerId == load.CustomerId && (_.OriginCity == null || _.OriginCity == origin.City) && (_.OriginState == null || _.OriginState == origin.State) && (_.OriginPostalCode == null || _.OriginPostalCode == origin.PostalCode) && (_.OriginCountry == null || _.OriginCountry == origin.Country) && (_.DestinationCity == null || _.DestinationCity == destination.City) && (_.DestinationState == null || _.DestinationState == destination.State) && (_.DestinationPostalCode == null || _.DestinationPostalCode == destination.PostalCode) && (_.DestinationCountry == null || _.DestinationCountry == destination.Country) && (!_.LoadCarrierGroupEquipment.Any() || _.LoadCarrierGroupEquipment.Any(loadCarrierGroupEquipment => loadCarrierGroupEquipment.EquipmentId == load.EquipmentId)) ) .ToListAsync(); Task.WaitAll(carriers, matchingGroups); // Address standardization has to happen after initial query var groups = matchingGroups.Result.Where(_ => (_.OriginAddress1 == null || AddressValidationService.StandardizeAddress(origin.Address1, true) .Equals(AddressValidationService.StandardizeAddress(_.OriginAddress1, true), StringComparison.OrdinalIgnoreCase)) && (_.DestinationAddress1 == null || AddressValidationService.StandardizeAddress(destination.Address1, true) .Equals(AddressValidationService.StandardizeAddress(_.DestinationAddress1, true), StringComparison.OrdinalIgnoreCase)) ).ToList(); var matchingGroupData = _mapper.Map <IList <ShippingLoadCarrierGroupData> >(groups); foreach (var group in matchingGroupData) { var carrierGroupType = (LoadCarrierGroupTypeEnum)group.LoadCarrierGroupTypeId; if (carrierGroupType == LoadCarrierGroupTypeEnum.Exclude) { var excludeIds = group.Carriers.Select(carrier => carrier.CarrierId).ToArray(); var carrierList = carriers.Result .Where(x => !excludeIds.Contains(x.CarrierId)) .ToList(); group.Carriers = carrierList; } //Make sure we are returning only carriers scacs that the customer has a contract with else { var includeIds = group.Carriers.Select(carrier => carrier.CarrierId).ToArray(); var carrierList = carriers.Result .Where(x => includeIds.Contains(x.CarrierId)) .ToList(); group.Carriers = carrierList; } } return(matchingGroupData.Where(group => group.Carriers.Any()).ToList()); }