Esempio n. 1
0
 public async Task <IJsonOperationResult <BaseCitationModel> > SendViolationAsync(
     long accountNumber,
     CitationRequestModel citationModel) =>
 await _apiManager.PostAsync <BaseCitationModel, CitationRequestModel>($"{ApiConstants.API_VERSION_PREFIX}{accountNumber}/Citations", citationModel);
        public async Task <IActionResult> Post([FromBody] CitationRequestModel model)
        {
            var response = new APIResponse <CitationResponseModel>()
            {
                Success = true
            };

            try
            {
                //Check Disabled user
                bool activeUser = _commonCtx.UserAccounts.Where(x => x.Disabled == false && x.AccountId == CommonAccount.Id && x.UserId == User.GetJWTLoggedInUserId().Value).Any();
                if (activeUser)
                {
                    if (ModelState.IsValid)
                    {
                        long citatioNextNumber = 1000000;
                        bool citatoinExists    = false;
                        var  citatoinNum       = _accountCtx.Counters.ForAccount(CommonAccount.Id).Select(x => x).Where(x => x.Name == "Citations").SingleOrDefault();
                        if (citatoinNum != null)
                        {
                            citatioNextNumber = citatoinNum.NextValue;
                        }
                        var timeSpan = DateTime.UtcNow.AddMinutes(_appSettings.CitationTimeSpan);

                        //Only check to see if a citation exists if there is License Plan and VIN

                        if (!string.IsNullOrWhiteSpace(model.LicensePlate) && !string.IsNullOrWhiteSpace(model.VinNumber))
                        {
                            citatoinExists = _accountCtx.Citations.ForAccount(CommonAccount.Id)
                                             .Where(x => x.ViolationId == model.ViolationId)
                                             .Where(x => (x.LicensePlate == model.LicensePlate || x.VinNumber == model.VinNumber) && x.CreateUtc >= timeSpan).Any();
                        }
                        else if (string.IsNullOrWhiteSpace(model.LicensePlate) && !string.IsNullOrWhiteSpace(model.VinNumber))
                        {
                            citatoinExists = _accountCtx.Citations.ForAccount(CommonAccount.Id)
                                             .Where(x => x.ViolationId == model.ViolationId)
                                             .Where(x => x.VinNumber == model.VinNumber && x.CreateUtc >= timeSpan).Any();
                        }
                        else if (!string.IsNullOrWhiteSpace(model.LicensePlate) && string.IsNullOrWhiteSpace(model.VinNumber))
                        {
                            citatoinExists = _accountCtx.Citations.ForAccount(CommonAccount.Id)
                                             .Where(x => x.ViolationId == model.ViolationId)
                                             .Where(x => x.LicensePlate == model.LicensePlate && x.CreateUtc >= timeSpan).Any();
                        }

                        if (!citatoinExists)
                        {
                            var citation = Mapper.Map <Citation>(model);
                            if (model.ViolationId != null || model.ViolationId != Guid.Empty)
                            {
                                //get Violation that are disabled or that is related to passed accountid
                                var violations = await _accountCtx.Violations
                                                 .ForAccount(CommonAccount.Id)
                                                 .Where(m => m.Id == model.ViolationId).SingleOrDefaultAsync();

                                if (violations != null)
                                {
                                    citation.FineAmount = violations.Fee;
                                }
                            }



                            citation.Status = await SetStatus();

                            citation.AccountId      = CommonAccount.Id;
                            citation.CitationNumber = citatioNextNumber;
                            citation.CreateUtc      = DateTime.UtcNow;
                            citation.CreateUserId   = User.GetJWTLoggedInUserId().Value;
                            citation.UpdateUserId   = User.GetJWTLoggedInUserId().Value;

                            _accountCtx.Citations.Add(citation);

                            if (citatoinNum == null)
                            {
                                Counter count = new Counter()
                                {
                                    AccountId    = CommonAccount.Id,
                                    Name         = "Citations",
                                    NextValue    = citatioNextNumber + 1,
                                    CreateUserId = User.GetJWTLoggedInUserId().Value,
                                    UpdateUserId = User.GetJWTLoggedInUserId().Value
                                };
                                _accountCtx.Counters.Add(count);
                            }
                            else
                            {
                                citatoinNum.NextValue    = citatioNextNumber + 1;
                                citatoinNum.UpdateUserId = User.GetJWTLoggedInUserId().Value;
                                _accountCtx.Counters.Update(citatoinNum);
                            }

                            await _accountCtx.SaveChangesAsync();

                            var citationResponse = Mapper.Map <CitationResponseModel>(citation);
                            response.Data = citationResponse;


                            //Create Audit Log
                            //TODO, this should also be created in the background.
                            await _citationSvc.CreateAuditEvent(citation.AccountId, citation.Id, "Citation Created from mobile device.", citation.CreateUserId, CitationAuditEvent.NewCitation);

                            //Reverse Geo Code
                            //We want to to run this in the background.  We don't have to use await here.
                            _citationSvc.ReverseGeoCodeCitation(citation.AccountId, citation.Id);

                            //Create a citation reminder in the background
                            _citationSvc.OverrideCitationFee(citation.AccountId, citation.Id);

                            //Override Fee
                        }
                        else
                        {
                            response.Success = false;
                            response.Message = "A citation for this vehicle has already been submitted";
                        }
                    }
                    else
                    {
                        response.Success = false;
                        response.Errors.AddRange(ModelState.ToErrors());
                    }
                }
                else
                {
                    response.Success = false;
                    response.Message = "This submission was not created because you have been disabled from this account.";
                }

                return(Ok(response));
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
                return(Ok(response));
            }
        }