public async Task <HttpResponseMessage> PatchDeliveryStatus( [HttpTrigger(AuthorizationLevel.Function, "patch", Route = "Delivery/{id}/Status")] HttpRequest req, string id) { string token = req.Headers["Authorization"].ToString().Replace("Bearer ", ""); if (_authorizationsService.IsTokenValid(token, false)) { Deliverer deliverer = await _deliverersService.GetDelivererByToken(token); if (deliverer == null) { return(new HttpResponseMessage(HttpStatusCode.Unauthorized)); } StreamReader r = new StreamReader(req.Body); string requestBody = await r.ReadToEndAsync(); r.Dispose(); // Dispose StreamReader so it cannot be used anymore dynamic data = JsonConvert.DeserializeObject <dynamic>(requestBody); //Get info and fron data and parameter DeliveryStatus status = (DeliveryStatus)Enum.ToObject(typeof(DeliveryStatus), (int)data?.status); LatLng latLng = new LatLng((double)data?.latitude, (double)data?.longitude); Guid deliveryId = Guid.Parse(id); // Get Google API key dynamic config = JsonConvert.DeserializeObject(File.ReadAllText("../../../../BezorgDirect.BezorgersApplicatie.API/variables.json")); string apiKey = config["LocationsService"]["GoogleMapsApiKey"]; // Creating location object from LatLng object and AddressData object AddressData addressData = await _locationsService.GetAddressFromLatLong(latLng, apiKey); Location l = new Location(Guid.NewGuid(), latLng.Latitude, latLng.Longitude, addressData.Address, addressData.PostalCode, addressData.Place, false); // If Location exists, replace it with existing one if (await _locationsService.DoesLocationExist(latLng.Latitude, latLng.Longitude, false)) { l = await _locationsService.GetLocationByLatLong(latLng.Latitude, latLng.Longitude, false); } // Getting delivery with new Current Location before calculating ETA's and setting status Delivery deliveryWithLocation = await _deliveriesService.PatchDeliveryLocation(await _deliveriesService.GetDelivery(deliveryId), l); // Patch the delivery status Delivery result = await _deliveriesService.PatchDeliveryStatus(deliveryWithLocation, status, deliverer.Id); //If status is reset to Aangekondigd, create a new Notification if (status == DeliveryStatus.Afgemeld) { bool success = await _notificationsService.CreateNotification(result.Id, result.Vehicle, result.DueDate, new LatLng(result.Warehouse.Latitude, result.Warehouse.Longitude), new LatLng(result.Customer.Latitude, result.Customer.Longitude)); if (!success) { return(new HttpResponseMessage(HttpStatusCode.FailedDependency)); } var expirationInSeconds = ((int)config["NotificationsService"]["ExpirationOfNotificationInMinutes"] * 60); // Create task in scheduler to check for an expired Notification record Scheduler.Scheduler.Instance().ScheduleJob("CheckNotificationContextForExpiration", "trigger_CheckNotificationContextForExpiration", DateTime.Now, (expirationInSeconds + 60), 0); } return(result != null ? new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(JsonConvert.SerializeObject(result), Encoding.UTF8, "application/json") } : new HttpResponseMessage(HttpStatusCode.BadRequest)); } // Authorized access only return(new HttpResponseMessage(HttpStatusCode.Unauthorized)); }