Example #1
0
            public void NullValues_NullValues()
            {
                var expected = new GeocodeAddress(item: null);
                var stop     = new LoadStopData
                {
                    Address1   = null,
                    City       = null,
                    State      = null,
                    Country    = null,
                    PostalCode = null
                };
                var actual = _svc.GetValidAddress(stop);

                actual.Should().BeEquivalentTo(expected);
            }
        public virtual void ConvertAndValidateStopData(LoadDetailData load, Guid customerId, OrderAddressValidationEnum validateAddress, bool manuallyCreated, string urnPrefix, BaseServiceResponse response)
        {
            if (load.LoadStops == null || load.LoadStops.Count < 2)
            {
                response.AddError($"{urnPrefix}:{nameof(load.LoadStops)}", _messages.LoadStopsTooFew);
                return;
            }

            //convert the stop type
            var      usedStopNumbers         = new List <int>();
            var      assignStopTypeIfOmitted = load.LoadStops.Count == 2 && !manuallyCreated;
            bool     firstDeliveryStopFound  = false;
            DateTime lastPickupDateTime      = DateTime.MinValue;

            for (int i = 0; i < load.LoadStops.Count; i++)
            {
                var stopUrnPrefix = $"{urnPrefix}:{nameof(load.LoadStops)}:{i}";
                var stop          = load.LoadStops[i];
                if (usedStopNumbers.Contains(stop.StopNbr))
                {
                    response.AddError($"{stopUrnPrefix}:{nameof(stop.StopNbr)}", _messages.StopNumberDuplicated);
                }
                usedStopNumbers.Add(stop.StopNbr);

                if (!string.IsNullOrWhiteSpace(stop.StopType))
                {
                    stop.StopTypeId = StopTypes?.FirstOrDefault(_ => string.Compare(_.Name, stop.StopType, true) == 0)?.StopTypeId;
                    if (stop.StopTypeId == null)
                    {
                        response.AddError($"{stopUrnPrefix}:{nameof(stop.StopType)}", _messages.StopTypeInvalid);
                    }
                }
                else if (assignStopTypeIfOmitted)
                {
                    stop.StopTypeId = i == 0 ? (int?)StopTypeEnum.Pickup : (int?)StopTypeEnum.Delivery;
                }
                else if (manuallyCreated)
                {
                    response.AddError($"{stopUrnPrefix}:{nameof(stop.StopType)}", _messages.StopTypeRequired);
                }

                if (stop.StopTypeId != null)
                {
                    if (stop.StopTypeId == (int)StopTypeEnum.Delivery)
                    {
                        firstDeliveryStopFound = true;
                    }
                    else if (firstDeliveryStopFound)
                    {
                        response.AddError($"{stopUrnPrefix}:{nameof(stop.StopType)}", _messages.StopTypeOutOfOrder);
                    }
                }

                if (manuallyCreated && stop.StopType == StopTypeEnum.Delivery.ToString())
                {
                    var stopLineItems = load.LineItems?.Where(x => x.DeliveryStopNumber == stop.StopNbr);
                    if (stopLineItems == null || stopLineItems.Count() == 0)
                    {
                        response.AddError($"{stopUrnPrefix}:{nameof(load.LineItems)}", _messages.LineItemsTooFew);
                    }
                }

                if (manuallyCreated)
                {
                    if (string.IsNullOrWhiteSpace(stop.LocationName))
                    {
                        response.AddError($"{stopUrnPrefix}:{nameof(stop.LocationName)}", _messages.LocationNameRequired);
                    }
                    if (string.IsNullOrWhiteSpace(stop.Address1))
                    {
                        response.AddError($"{stopUrnPrefix}:{nameof(stop.Address1)}", _messages.Address1Required);
                    }
                    if (string.IsNullOrWhiteSpace(stop.City))
                    {
                        response.AddError($"{stopUrnPrefix}:{nameof(stop.City)}", _messages.CityRequired);
                    }
                    if (string.IsNullOrWhiteSpace(stop.State))
                    {
                        response.AddError($"{stopUrnPrefix}:{nameof(stop.State)}", _messages.StateRequired);
                    }
                    if (string.IsNullOrWhiteSpace(stop.Country))
                    {
                        response.AddError($"{stopUrnPrefix}:{nameof(stop.Country)}", _messages.CountryRequired);
                    }
                    if (string.IsNullOrWhiteSpace(stop.PostalCode))
                    {
                        response.AddError($"{stopUrnPrefix}:{nameof(stop.PostalCode)}", _messages.PostalCodeRequired);
                    }
                }

                if (validateAddress == OrderAddressValidationEnum.Validate)
                {
                    if (!_addressValidationService.IsAddressValid(customerId, stop))
                    {
                        response.AddError($"{stopUrnPrefix}", _messages.AddressInvalid);
                    }
                }
                else if (validateAddress == OrderAddressValidationEnum.Replace)
                {
                    var address = _addressValidationService.GetValidAddress(stop);
                    if (address != null)
                    {
                        stop.Address1   = address.Address1;
                        stop.City       = address.City;
                        stop.State      = address.State;
                        stop.Country    = address.Country;
                        stop.PostalCode = address.PostalCode;
                    }
                }

                if (stop.StopTypeId == (int)StopTypeEnum.Pickup)
                {
                    lastPickupDateTime = stop.LateDtTm;
                }

                ValidateStopTimes(stop, manuallyCreated, lastPickupDateTime, stopUrnPrefix, response);

                if (string.IsNullOrWhiteSpace(stop.ApptType))
                {
                    response.AddError($"{stopUrnPrefix}", _messages.ApptTypeRequired);
                }


                var schedulingCode   = string.IsNullOrWhiteSpace(stop.AppointmentSchedulingCode) ? null : stop.AppointmentSchedulingCode;
                var confirmationCode = string.IsNullOrWhiteSpace(stop.AppointmentConfirmationCode) ? null : stop.AppointmentConfirmationCode;
                if (manuallyCreated)
                {
                    if (schedulingCode == null)
                    {
                        response.AddError($"{stopUrnPrefix}:{nameof(stop.AppointmentSchedulingCode)}", _messages.AppointmentSchedulingCodeRequired);
                    }
                    if (confirmationCode == null)
                    {
                        response.AddError($"{stopUrnPrefix}:{nameof(stop.AppointmentConfirmationCode)}", _messages.AppointmentConfirmationCodeRequired);
                    }
                }
                else
                {
                    //API loads can have both or none
                    if (schedulingCode == null && confirmationCode != null)
                    {
                        response.AddError($"{stopUrnPrefix}:{nameof(stop.AppointmentSchedulingCode)}", _messages.AppointmentSchedulingCodeMissing);
                    }
                    if (schedulingCode != null && confirmationCode == null)
                    {
                        response.AddError($"{stopUrnPrefix}:{nameof(stop.AppointmentConfirmationCode)}", _messages.AppointmentConfirmationCodeMissing);
                    }
                }

                if (schedulingCode != null && confirmationCode != null)
                {
                    if (!(AppointmentSchedulerConfirmationTypes?.Where(_ => string.Compare(_.AppointmentSchedulingCode, schedulingCode, true) == 0).Any() ?? false))
                    {
                        response.AddError($"{stopUrnPrefix}:{nameof(stop.AppointmentSchedulingCode)}", _messages.AppointmentSchedulingCodeInvalid);
                    }
                    if (!(AppointmentSchedulerConfirmationTypes?.Where(_ => string.Compare(_.AppointmentConfirmationCode, confirmationCode, true) == 0).Any() ?? false))
                    {
                        response.AddError($"{stopUrnPrefix}:{nameof(stop.AppointmentConfirmationCode)}", _messages.AppointmentConfirmationCodeInvalid);
                    }

                    stop.AppointmentSchedulerConfirmationTypeId = AppointmentSchedulerConfirmationTypes?
                                                                  .FirstOrDefault(_ => string.Compare(_.AppointmentSchedulingCode, schedulingCode, true) == 0 &&
                                                                                  string.Compare(_.AppointmentConfirmationCode, confirmationCode, true) == 0)
                                                                  ?.AppointmentSchedulerConfirmationTypeId;

                    if (stop.AppointmentSchedulerConfirmationTypeId == null)
                    {
                        response.AddError($"{stopUrnPrefix}:{nameof(stop.AppointmentConfirmationCode)}", _messages.AppointmentCodeMismatch);
                    }
                }

                if (stop.StopTypeId == (int)StopTypeEnum.Pickup)
                {
                    if (manuallyCreated && !(load.LineItems?.Any(_ => _.PickupStopNumber == stop.StopNbr) ?? false))
                    {
                        response.AddError($"{stopUrnPrefix}", _messages.DeliveryItemRequired);
                    }
                }

                ValidateStopContactData(stop, stopUrnPrefix, response);
            }
        }