public static async Task <bool> DeleteItemAsync(string id, InternalDelivery delivery) { using (logger.BeginScope(nameof(DeleteItemAsync))) { logger.LogInformation("id: {Id}", id); logger.LogInformation("Start: updating item's TTL in Redis"); var success = await cache.KeyExpireAsync(id, DateTime.UtcNow.AddDays(7)).ConfigureAwait(continueOnCapturedContext: false); logger.LogInformation("End: updating item's TTL in Redis"); return(success); } }
public async Task <IActionResult> Confirm(string id, [FromBody] Confirmation confirmation) { logger.LogInformation("In Confirm action with id: {Id} and confirmation: {@Confirmation}", id, confirmation.ToLogInfo()); var delivery = await deliveryRepository.GetAsync(id); if (delivery == null) { logger.LogDebug("Delivery id: {Id} not found", id); return(NotFound()); } var confirmedDelivery = new InternalDelivery(delivery.Id, delivery.Owner, delivery.Pickup, confirmation.GeoCoordinates, delivery.Deadline, delivery.Expedited, delivery.ConfirmationRequired, delivery.DroneId); var internalConfirmation = new InternalConfirmation { DateTime = confirmation.DateTime, GeoCoordinates = confirmation.GeoCoordinates, ConfirmationType = confirmation.ConfirmationType, ConfirmationBlob = confirmation.ConfirmationBlob }; // Adds the delivery complete status event await deliveryTrackingRepository.AddAsync( new DeliveryTrackingEvent { DeliveryId = id, Stage = DeliveryStage.Completed }); // sends notifications var notifyMeRequests = await notifyMeRequestRepository.GetAllByDeliveryIdAsync(id); IEnumerable <Task> notificationTasks = notifyMeRequests.Select(nR => notificationService.SendNotificationsAsync(nR)); await Task.WhenAll(notificationTasks); // logical delivery deletion await deliveryRepository.DeleteAsync(id, confirmedDelivery); return(Ok()); }
public async Task Confirm_DeletesDeliveryLogically() { // Arrange InternalDelivery confirmedDelivery = null; DeliveryStatusEvent completeEvent = null; var deliveryRepository = new Mock <IDeliveryRepository>(); deliveryRepository.Setup(r => r.GetAsync("deliveryid")).ReturnsAsync(delivery); deliveryRepository.Setup(r => r.DeleteAsync("deliveryid", It.IsAny <InternalDelivery>())) .Returns(Task.CompletedTask) .Callback <string, InternalDelivery>((i, d) => confirmedDelivery = d); var deliveryStatusEventRepository = new Mock <IDeliveryStatusEventRepository>(); deliveryStatusEventRepository.Setup(r => r.AddAsync(It.IsAny <DeliveryStatusEvent>())) .Returns(Task.CompletedTask) .Callback <DeliveryStatusEvent>(e => completeEvent = e); deliveryStatusEventRepository.Setup(r => r.GetByDeliveryIdAsync("deliveryid")) .ReturnsAsync(new ReadOnlyCollection <DeliveryStatusEvent>(new List <DeliveryStatusEvent>() { completeEvent })); var loggerFactory = new Mock <ILoggerFactory>(); loggerFactory.Setup(f => f.CreateLogger(It.IsAny <string>())).Returns(new Mock <ILogger>().Object); var target = new DeliveriesController(deliveryRepository.Object, new Mock <INotifyMeRequestRepository>().Object, new Mock <INotificationService>().Object, new Mock <IDeliveryHistoryService>().Object, deliveryStatusEventRepository.Object, loggerFactory.Object); var location = new Location(1, 2, 3); var confirmation = new Confirmation(new DateTimeStamp("datetimevalue"), location, ConfirmationType.Picture, "confirmationblob"); // Act var result = await target.Confirm("deliveryid", confirmation) as OkResult; // Assert Assert.IsNotNull(result); deliveryRepository.Verify(r => r.DeleteAsync("deliveryid", confirmedDelivery)); Assert.AreEqual(location, confirmedDelivery.Dropoff); }
public async Task Patch_UpdatesCache() { // Arrange InternalDelivery updatedDelivery = null; var rescheduledDelivery = new RescheduledDelivery(new Location(2, 2, 2), new Location(3, 3, 3), "newdeadline"); var deliveryRepository = new Mock <IDeliveryRepository>(); deliveryRepository.Setup(r => r.GetAsync("deliveryid")).ReturnsAsync(delivery); deliveryRepository.Setup(r => r.UpdateAsync("deliveryid", It.IsAny <InternalDelivery>())) .Returns(Task.CompletedTask) .Callback((string i, InternalDelivery d) => updatedDelivery = d); var loggerFactory = new Mock <ILoggerFactory>(); loggerFactory.Setup(f => f.CreateLogger(It.IsAny <string>())).Returns(new Mock <ILogger>().Object); var target = new DeliveriesController(deliveryRepository.Object, new Mock <INotifyMeRequestRepository>().Object, new Mock <INotificationService>().Object, new Mock <IDeliveryHistoryService>().Object, new Mock <IDeliveryStatusEventRepository>().Object, loggerFactory.Object); // Act await target.Patch("deliveryid", rescheduledDelivery); // Assert //unchanged values Assert.AreEqual("deliveryid", updatedDelivery.Id); Assert.AreEqual("userid", updatedDelivery.Owner.UserId); //updated values Assert.AreEqual(2, updatedDelivery.Pickup.Altitude); Assert.AreEqual(3, updatedDelivery.Dropoff.Altitude); Assert.AreEqual("newdeadline", updatedDelivery.Deadline); deliveryRepository.VerifyAll(); }
public async Task CancelAsync(InternalDelivery delivery, params DeliveryTrackingEvent[] deliveryTrackingEvents) { await EventHubSender <DeliveryHistory> .SendMessageAsync(new DeliveryHistory(delivery.Id, delivery, deliveryTrackingEvents), nameof(DeliveryStage.Cancelled), delivery.Id.Substring(0, Constants.PartitionKeyLength)).ConfigureAwait(continueOnCapturedContext: false); }
public async Task CompleteAsync(InternalDelivery delivery, InternalConfirmation confirmation, params DeliveryTrackingEvent[] deliveryTrackingEvents) { //TODO: shallowing confirmation (TBD) await EventHubSender <DeliveryHistory> .SendMessageAsync(new DeliveryHistory(delivery.Id, delivery, deliveryTrackingEvents), nameof(DeliveryStage.Completed), delivery.Id.Substring(0, Constants.PartitionKeyLength)).ConfigureAwait(continueOnCapturedContext: false); }
public async Task CancelAsync(InternalDelivery delivery, params DeliveryStatusEvent[] events) { await EventHubSender <DeliveryHistory> .SendMessageAsync(new DeliveryHistory(delivery.Id, delivery, events), DeliveryEventType.Cancelled.ToString(), delivery.Id.Substring(0, Constants.PartitionKeyLength)).ConfigureAwait(continueOnCapturedContext: false); }
public async Task Confirm_SendsMessageCompleteToDeliveryHistory() { // Arrange InternalDelivery confirmedDelivery = null; InternalConfirmation sentConfirmation = null; DeliveryStatusEvent completeEvent = null; DeliveryStatusEvent[] allEvents = null; var deliveryRepository = new Mock <IDeliveryRepository>(); deliveryRepository.Setup(r => r.GetAsync("deliveryid")).ReturnsAsync(delivery); var notifyMeRequestRepository = new Mock <INotifyMeRequestRepository>(); notifyMeRequestRepository.Setup(r => r.GetAllByDeliveryIdAsync("deliveryid")) .ReturnsAsync(new List <InternalNotifyMeRequest>()); var deliveryStatusEventRepository = new Mock <IDeliveryStatusEventRepository>(); deliveryStatusEventRepository.Setup(r => r.AddAsync(It.IsAny <DeliveryStatusEvent>())) .Returns(Task.CompletedTask) .Callback <DeliveryStatusEvent>(e => completeEvent = e); deliveryStatusEventRepository.Setup(r => r.GetByDeliveryIdAsync("deliveryid")) .ReturnsAsync(new ReadOnlyCollection <DeliveryStatusEvent>(new List <DeliveryStatusEvent>() { completeEvent })); var deliveryHistoryService = new Mock <IDeliveryHistoryService>(); deliveryHistoryService.Setup(r => r.CompleteAsync(It.IsAny <InternalDelivery>(), It.IsAny <InternalConfirmation>(), It.IsAny <DeliveryStatusEvent[]>())) .Returns(Task.CompletedTask) .Callback <InternalDelivery, InternalConfirmation, DeliveryStatusEvent[]>((d, c, es) => { confirmedDelivery = d; sentConfirmation = c; allEvents = es; }); var loggerFactory = new Mock <ILoggerFactory>(); loggerFactory.Setup(f => f.CreateLogger(It.IsAny <string>())).Returns(new Mock <ILogger>().Object); var target = new DeliveriesController(deliveryRepository.Object, notifyMeRequestRepository.Object, new Mock <INotificationService>().Object, deliveryHistoryService.Object, deliveryStatusEventRepository.Object, loggerFactory.Object); var confirmation = new Confirmation(new DateTimeStamp("datetimevalue"), new Location(1, 2, 3), ConfirmationType.Picture, "confirmationblob"); // Act var result = await target.Confirm("deliveryid", confirmation) as OkResult; // Assert Assert.IsNotNull(result); Assert.AreEqual("datetimevalue", sentConfirmation.DateTime.DateTimeValue); Assert.AreEqual(1, sentConfirmation.GeoCoordinates.Altitude); Assert.AreEqual(2, sentConfirmation.GeoCoordinates.Latitude); Assert.AreEqual(3, sentConfirmation.GeoCoordinates.Longitude); Assert.AreEqual(ConfirmationType.Picture, sentConfirmation.ConfirmationType); Assert.AreEqual("confirmationblob", sentConfirmation.ConfirmationBlob); deliveryHistoryService.Verify(s => s.CompleteAsync(confirmedDelivery, It.IsAny <InternalConfirmation>(), allEvents), Times.Once); }
public async Task DeleteAsync(string id, InternalDelivery delivery) { await RedisCache <InternalDelivery> .DeleteItemAsync(id, delivery).ConfigureAwait(continueOnCapturedContext: false); }
public async Task CreateAsync(InternalDelivery delivery) { await RedisCache <InternalDelivery> .CreateItemAsync(delivery).ConfigureAwait(continueOnCapturedContext: false); }