public void ValidateParcelHeight(int height, bool expected) { //Arrange var model = new Parcel() { Height = height }; var sut = new ParcelValidation(new ParcelTypeDictionary(), new ParcelConfiguration()); //Act, Assert Assert.That(sut.ValidateHeight(model), Is.EqualTo(expected)); }
public void ValidateParcelBreadth(int breadth, bool expected) { //Arrange var model = new Parcel() { Breadth = breadth }; var sut = new ParcelValidation(new ParcelTypeDictionary(), new ParcelConfiguration()); //Act, Assert Assert.That(sut.ValidateBreadth(model), Is.EqualTo(expected)); }
public bool ReportDeliveryFinal(string trackingId) { try { var parcelDal = _sqlRepoParcel.GetByTrackingID(trackingId); if (parcelDal == null || parcelDal.FutureHops.Count != 1) { return(false); } var parcelBL = _mapper.Map <Parcel>(parcelDal); var validator = new ParcelValidation(); var checkParcel = validator.Validate(parcelBL); if (!checkParcel.IsValid) { return(false); } parcelDal.FutureHops.RemoveAt(0); parcelDal.State = DAL.Parcel.StateEnum.DeliveredEnum; _sqlRepoParcel.Update(parcelDal); //contact Webhook Subscriber //Find Webhook IDs with the same trackingID List <DAL.Webhook> deleteList = _webrep.GetWebhooksByTrackingID(trackingId); foreach (var hook in deleteList) { _webrep.Delete(hook.Id); } return(true); } catch (DAL.DALException exc) { _logger.LogError(exc.ToString()); throw new BLException($"{exc.GetType()} Exception in {System.Reflection.MethodBase.GetCurrentMethod().Name}", exc); } catch (Exception exc) { _logger.LogError(exc.ToString()); throw new BLException($"{exc.GetType()} Exception in {System.Reflection.MethodBase.GetCurrentMethod().Name}", exc); } }
public bool StaffReportHop(string trackingId, string code) { try { //Get Data from DB and check if exists var parcelDal = _sqlRepoParcel.GetByTrackingID(trackingId); var hopDal = _sqlRepoHop.GetByCode(code); if (parcelDal == null || hopDal == null || parcelDal.FutureHops.Count == 1 || parcelDal.FutureHops[0].Code != code) { return(false); } //Validation var parcelBL = _mapper.Map <Parcel>(parcelDal); var hopBL = new HopArrival() { Code = code, DateTime = DateTime.Now }; var validatorParcel = new ParcelValidation(); var validatorHopArrival = new HopArrivalValidation(); var checkParcel = validatorParcel.Validate(parcelBL); var checkHop = validatorHopArrival.Validate(hopBL); //check if Parcel and Hop is Valid and the next Hop is the same as the incoming Hop if (!checkParcel.IsValid && !checkHop.IsValid) { return(false); } //Change State var hopArrival = _mapper.Map <DAL.HopArrival>(hopBL); if (hopDal.HopType == "Truck") { parcelDal.State = DAL.Parcel.StateEnum.InTruckDeliveryEnum; } if (hopDal.HopType == "Transferwarehouse") { DAL.Transferwarehouse post = (DAL.Transferwarehouse)hopDal; string url = $"{post.LogisticsPartnerUrl}/parcel/{trackingId}"; HttpResponseMessage msg = SendParcelToPartner(url, parcelBL); if (msg.StatusCode != HttpStatusCode.OK) { throw new Exception($"Could not POST Parcel to Partner {post.LogisticsPartnerUrl}"); } parcelDal.State = DAL.Parcel.StateEnum.DeliveredEnum; } if (hopDal.HopType == "Warehouse") { parcelDal.State = DAL.Parcel.StateEnum.InTransportEnum; } // Update Visited/Future Hops parcelDal.FutureHops.RemoveAt(0); parcelDal.VisitedHops.Add(hopArrival); _sqlRepoParcel.Update(parcelDal); //Contact Webhook Subscriber List <DAL.Webhook> contactList = _webrep.GetWebhooksByTrackingID(trackingId); var webhookparcel = _mapper.Map <Parcel>(parcelDal); foreach (var hook in contactList) { HttpResponseMessage msg = SendWebhookResponse(hook.Url, webhookparcel); } return(true); } catch (DAL.DALException exc) { _logger.LogError(exc.ToString()); throw new BLException($"{exc.GetType()} Exception in {System.Reflection.MethodBase.GetCurrentMethod().Name}", exc); } catch (Exception exc) { _logger.LogError(exc.ToString()); throw new BLException($"{exc.GetType()} Exception in {System.Reflection.MethodBase.GetCurrentMethod().Name}", exc); } }
public DTO.NewParcelInfo SubmitParcelIntoBL(Parcel parcel) { try { var validator = new ParcelValidation(); var checkParcel = validator.Validate(parcel); if (!checkParcel.IsValid) { throw new BLException("BL: Parcel Validation failed"); } //check if parcel.trackingId is set -> only happens if it is from a Logistic Partner if (parcel.TrackingId == null || _sqlRepoParcel.GetByTrackingID(parcel.TrackingId) != null) { //generate new trackingId until it is a unique do { parcel.TrackingId = GenerateTrackingId(); } while (_sqlRepoParcel.GetByTrackingID(parcel.TrackingId) != null); } //Set State to InTransport parcel.State = Parcel.StateEnum.InTransportEnum; //Encode Sender and Receipient into a Location Location recLocation = _agent.EncodeGeocodeAsync(GenerateAddress(parcel.Receipient)); Location sendLocation = _agent.EncodeGeocodeAsync(GenerateAddress(parcel.Sender)); //Find responsible Truck for Sender - START //currently only for austria -> NO Transferwarehouse var senderTruck = _mapper.Map <Truck>(_sqlRepoHop.GetTruckByLocation(sendLocation)); HopArrival firstHop = new HopArrival() { Code = senderTruck.Code, DateTime = DateTime.Now }; parcel.VisitedHops.Add(firstHop); //Find responsible Truck for Recipient - FINISH var recTruck = _mapper.Map <Truck>(_sqlRepoHop.GetTruckByLocation(recLocation)); //Calculate Route var rcptHopList = GetRouteToRoot(recTruck); var senderHopList = GetRouteToRoot(senderTruck); //var intersect = rcptHopList.Intersect(senderHopList,EqualityComparerFactory.Create<WarehouseNextHops>((a) => a.HopACode.GetHashCode(), (a, b) =)) bool found = false; Hop intersection = null; foreach (var senderStep in senderHopList) { if (found == false) { foreach (var step in rcptHopList) { if (senderStep.Code == step.Code) { intersection = senderStep; found = true; break; } } } } //Get path to intersection var rcptJourney = rcptHopList.TakeWhile(p => p.Code != intersection.Code).ToList(); var senderJourney = senderHopList.TakeWhile(p => p.Code != intersection.Code).ToList(); senderJourney.Add(intersection); //reverse rcptJourney rcptJourney.Reverse(); senderJourney.AddRange(rcptJourney); senderJourney.Add(recTruck); foreach (var step in senderJourney) { HopArrival futureHop = new HopArrival() { Code = step.Code, DateTime = DateTime.Now }; parcel.FutureHops.Add(futureHop); } var parcelDAL = _mapper.Map <DAL.Parcel>(parcel); _sqlRepoParcel.Create(parcelDAL); var NPInfoMapped = _mapper.Map <DTO.NewParcelInfo>(parcel); DTO.NewParcelInfo NPInfo = (DTO.NewParcelInfo)NPInfoMapped; return(NPInfo); } catch (DAL.DALException exc) { _logger.LogError(exc.ToString()); throw new BLException($"{exc.GetType()} Exception in {System.Reflection.MethodBase.GetCurrentMethod().Name}", exc); } catch (Exception exc) { _logger.LogError(exc.ToString()); throw new BLException($"{exc.GetType()} Exception in {System.Reflection.MethodBase.GetCurrentMethod().Name}", exc); } }