Esempio n. 1
0
        public async Task <IHttpActionResult> InsertListSosGeoLocation(CreateListSosGeoLocationModel model)
        {
            try
            {
                //validate
                if (!ModelState.IsValid)
                {
                    foreach (var key in ModelState.Keys.Where(key => ModelState[key].Errors.Count > 0))
                    {
                        return(BadRequest(ModelState[key].Errors[0].ErrorMessage));
                    }
                }
                //find LogSOS info
                var yayYoId = model.YayYoId;
                var logSos  = await _logSosService.GetLastestLogSosAsync(yayYoId);

                if (logSos != null)
                {
                    var list = new List <LogSosGeolocation>();
                    foreach (var location in model.Locations)
                    {
                        var createdOnUtc   = DateTimeOffset.FromUnixTimeSeconds(location.Timestamp).DateTime;
                        var sosGeoLocation = new LogSosGeolocation
                        {
                            LogSosId     = logSos.Id,
                            Location     = location.Location,
                            CreatedOnUtc = createdOnUtc
                        };
                        list.Add(sosGeoLocation);
                    }
                    await _sosGeoLocationService.InsertRangeAsync(list);
                }
                return(Ok());
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
        public async Task <IHttpActionResult> DeactiveSos(DeactiveSosModel model)
        {
            //validate model
            if (!ModelState.IsValid)
            {
                foreach (var key in ModelState.Keys.Where(key => ModelState[key].Errors.Count > 0))
                {
                    return(BadRequest(ModelState[key].Errors[0].ErrorMessage));
                }
            }
            //find safety settings
            var safetySetting = _safetySettingService.GetByYayYoId(model.YayYoId);

            try
            {
                if (safetySetting == null)
                {
                    return(BadRequest("User does not exist"));
                }
                if (safetySetting.Active == false)
                {
                    return(BadRequest("SOS is not active"));
                }
                //check pin. If no pin is found, return error
                if (model.CancellationPin == safetySetting.CancellationPin)
                {
                    safetySetting.Active = false;
                    await _safetySettingService.UpdateAsync(safetySetting);

                    var response = new DeactiveCancelSosResponseModel
                    {
                        IsDuressMode = false
                    };
                    var logSos = await _logSosService.GetLastestLogSosAsync(model.YayYoId);

                    if (logSos == null)
                    {
                        return(BadRequest("Log SOS does not exist"));
                    }
                    logSos.UpdatedOnUtc = DateTime.UtcNow;
                    await _logSosService.UpdateAsync(logSos);

                    return(Ok(response));
                }
                if (model.CancellationPin == safetySetting.DuressPin)
                {
                    var response = new DeactiveCancelSosResponseModel
                    {
                        IsDuressMode = true
                    };
                    var logSos = await _logSosService.GetLastestLogSosAsync(model.YayYoId);

                    if (logSos == null)
                    {
                        return(BadRequest("Log SOS does not exist"));
                    }
                    logSos.UpdatedOnUtc = DateTime.UtcNow;
                    await _logSosService.UpdateAsync(logSos);

                    return(Ok(response));
                }
                return(BadRequest("Incorrect Pin"));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }