Example #1
0
        public async Task AddStopAsync(Common.Models.Stop stop)
        {
            DateTime      now = DateTime.Now;
            StringBuilder sb  = new StringBuilder();

            sb.Append(stop.OfficerId.Substring(5, 4));
            sb.Append(BASE36_CHARS[now.Year - 2021]);
            sb.Append(BASE36_CHARS[now.Month]);
            sb.Append(BASE36_CHARS[now.Day]);
            sb.Append(BASE36_CHARS[now.Hour]);
            sb.Append(now.ToString("mmss"));

            stop.Id = sb.ToString();

            await _container.CreateItemAsync <Common.Models.Stop>(stop, new PartitionKey(stop.Id));
        }
Example #2
0
 public async Task UpdateStopAuditAsync(string id, Common.Models.Stop stop)
 {
     await _container.UpsertItemAsync <Common.Models.Stop>(stop, new PartitionKey(id));
 }
        public async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "put", Route = "PutStop/{Id}")] Common.Models.Stop stop, HttpRequest req, string Id, ILogger log)
        {
            log.LogInformation($"PUT - Put Stop requested, ID: {Id}, OID: {stop.OfficerId}, DATE: {stop.Date}, TIME: {stop.Time}");

            try
            {
                if (!RIPAAuthorization.ValidateUserOrAdministratorRole(req, log).ConfigureAwait(false).GetAwaiter().GetResult())
                {
                    return(new UnauthorizedResult());
                }
            }
            catch (Exception ex)
            {
                log.LogError(ex.Message);

                return(new UnauthorizedResult());
            }

            try
            {
                var objectId = await RIPAAuthorization.GetUserId(req, log);

                stop.EditStopOfficerId = (await _userProfileCosmosDbService.GetUserProfileAsync(objectId)).OfficerId;
            }
            catch (Exception ex)
            {
                log.LogError(ex.Message);

                return(new BadRequestObjectResult("User profile was not found"));
            }

            if (string.IsNullOrEmpty(Id))
            {
                return(new BadRequestObjectResult("Stop Id cannot be empty or null"));
            }

            if (stop.OfficerId.Length != 9)
            {
                return(new BadRequestObjectResult("Officer ID must be 9 char"));
            }

            if (stop.Location.City == null)
            {
                return(new BadRequestObjectResult("City is required"));
            }

            if (stop.Status == null)
            {
                stop.Status = SubmissionStatus.Unsubmitted.ToString();
            }

            stop.Ori = Environment.GetEnvironmentVariable("ORI"); //What is an Originating Agency Identification (ORI) Number? A nine-character identifier assigned to an agency. Agencies must identify their ORI Number...

            bool isDuplicate = await _stopCosmosDbService.CheckForDuplicateStop(stop.Id, stop.Ori, stop.OfficerId, stop.Date, stop.Time);

            if (isDuplicate)
            {
                log.LogError($"This appears to be a duplicate Stop: ID: {Id}, SID: {stop.Id}, OID: {stop.OfficerId}, DATE: {stop.Date}, TIME: {stop.Time}");

                return(new BadRequestObjectResult("This appears to be a duplicate Stop"));
            }

            stop.IsEdited = Id == "0" ? false : true;

            try
            {
                if (Id != "0")
                {
                    //Protect the submission history
                    Common.Models.Stop editingStop = await _stopCosmosDbService.GetStopAsync(Id);

                    editingStop.Id = $"{stop.Id}-{DateTime.UtcNow:yyyyMMddHHmmss}";
                    await _stopAuditCosmosDbService.UpdateStopAuditAsync(editingStop.Id, editingStop);

                    log.LogInformation($"Saving stop audit ID: {editingStop.Id}");
                    stop.ListSubmission = editingStop.ListSubmission;
                }
            }
            catch (Exception ex)
            {
                log.LogError($"Failed getting stop to protect submission history, ID: {Id}, SID: {stop.Id}, OID: {stop.OfficerId}, DATE: {stop.Date}, TIME: {stop.Time}");

                return(new BadRequestObjectResult("Failed getting stop submission history"));
            }

            int retryAttemps = GetRetrySetting("RetryAttemps", 3);
            int retrywait    = GetRetrySetting("RetryWait", 1000);

            while (retryAttemps > 0)
            {
                try
                {
                    stop.Id = Id;
                    if (stop.Id == "0")
                    {
                        await _stopCosmosDbService.AddStopAsync(stop);
                    }
                    else
                    {
                        if (!RIPAAuthorization.ValidateAdministratorRole(req, log).ConfigureAwait(false).GetAwaiter().GetResult())
                        {
                            return(new UnauthorizedResult());
                        }

                        await _stopCosmosDbService.UpdateStopAsync(stop);
                    }

                    log.LogInformation($"PUT - saved stop, ID: {Id}, SID: {stop.Id}, OID: {stop.OfficerId}, DATE: {stop.Date}, TIME: {stop.Time}");

                    return(new OkObjectResult(stop));
                }
                catch (Exception exception)
                {
                    log.LogError($"Failed to insert/update stop, attempt counter: {retryAttemps}, ID: {Id}, SID: {stop.Id}, OID: {stop.OfficerId}, DATE: {stop.Date}, TIME: {stop.Time}", exception.GetBaseException());
                }

                await Task.Delay(retrywait);

                retryAttemps--;
            }

            return(new BadRequestObjectResult("The maximum number of attemps to save the STOP was exceeded"));
        }
Example #4
0
 public async Task UpdateStopAsync(Common.Models.Stop stop)
 {
     await _container.UpsertItemAsync <Common.Models.Stop>(stop, new PartitionKey(stop.Id));
 }