Esempio n. 1
0
        public void SetForm(FormForCreationDto formForCreation)
        {
            _formForCreation = formForCreation;
            var formControlValues = formForCreation.FormValues.Select(fv => fv.FormControlValue).ToList();

            foreach (var formValue in formControlValues)
            {
                if (formValue.StartsWith("["))
                {
                    // Handle array
                    _formArrayValues.Add(JsonConvert.DeserializeObject <Dictionary <string, string>[]>(formValue));
                }
                else
                {
                    // Handle object
                    _formValues.Add(JsonConvert.DeserializeObject <Dictionary <string, string> >(formValue));
                }
            }

            _patientDetailForCreation = null;
            _patientDetailForUpdate   = null;
        }
Esempio n. 2
0
        public IActionResult CreateForm(int accountId,
                                        [FromBody] FormForCreationDto form)
        {
            if (form == null)
            {
                return(BadRequest());
            }

            if (form.Description == form.Title)
            {
                ModelState.AddModelError("Description", "The provided description should be different from the title.");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_accountRepository.AccountExists(accountId))
            {
                return(NotFound());
            }

            var finalForm = Mapper.Map <Entities.Form>(form);

            _accountRepository.AddFormForAccount(accountId, finalForm);

            if (!_accountRepository.Save())
            {
                return(StatusCode(500, "A problem happened while handling your request."));
            }

            var createdFormToReturn = Mapper.Map <Models.FormDto>(finalForm);

            return(CreatedAtRoute("GetForm", new
                                  { accountId = accountId, id = createdFormToReturn.Id }, createdFormToReturn));
        }
Esempio n. 3
0
        public async Task <IActionResult> CreateForm([FromBody] FormForCreationDto formForCreation)
        {
            if (formForCreation == null)
            {
                ModelState.AddModelError("Message", "Unable to locate payload for new form");
                return(BadRequest(ModelState));
            }

            if (formForCreation.HasAttachment == false)
            {
                ModelState.AddModelError("Message", "Unable to process form as no attachment has been submitted");
                return(BadRequest(ModelState));
            }

            // Meta form for template extraction
            var metaFormFromRepo = await _metaFormRepository.GetAsync(f => f.ActionName.ToLower().Replace(" ", "") == formForCreation.FormType.ToLower().Replace(" ", ""));

            if (metaFormFromRepo == null)
            {
                ModelState.AddModelError("Message", "Unable to locate meta form for template extraction");
                return(BadRequest(ModelState));
            }

            // Store user for audit log generation purposes
            var      userName     = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;
            var      userFromRepo = _userRepository.Get(u => u.UserName == userName);
            AuditLog auditLog     = null;

            // TODO Use meta form to identify context handler (add/update encounter/patient)

            //var handler = new FormValueHandler(formForCreation.FormValues.Where(fv => fv.FormControlKey == "Object").Select(fv => fv.FormControlValue).ToList());
            _formHandler.SetForm(formForCreation);

            // Validation of the source entity
            _formHandler.ValidateSourceIdentifier();
            if (_formHandler.GetValidationErrors().Count > 0)
            {
                foreach (string message in _formHandler.GetValidationErrors())
                {
                    ModelState.AddModelError("Message", message);
                }
            }

            if (ModelState.IsValid)
            {
                _formHandler.PreparePatientAndClinicalDetail();
                if (_formHandler.GetValidationErrors().Count > 0)
                {
                    foreach (string message in _formHandler.GetValidationErrors())
                    {
                        ModelState.AddModelError("Message", message);
                    }
                }
            }

            if (ModelState.IsValid)
            {
                try
                {
                    auditLog = new AuditLog()
                    {
                        AuditType  = AuditType.SynchronisationForm,
                        User       = userFromRepo,
                        ActionDate = DateTime.Now,
                        Details    = $"Form submission successful {formForCreation.FormIdentifier}",
                        Log        = JsonConvert.SerializeObject(formForCreation, Formatting.Indented)
                    };
                    await _auditLogRepository.SaveAsync(auditLog);

                    await _formHandler.ProcessFormForCreationOrUpdateAsync();

                    await _unitOfWork.CompleteAsync();

                    return(Ok());
                }
                catch (Exception ex)
                {
                    var error = new Dictionary <string, string>
                    {
                        { "Type", ex.GetType().ToString() },
                        { "Message", ex.Message },
                        { "StackTrace", ex.StackTrace }
                    };
                    auditLog = new AuditLog()
                    {
                        AuditType  = AuditType.SynchronisationError,
                        User       = userFromRepo,
                        ActionDate = DateTime.Now,
                        Details    = $"Error on form {formForCreation.FormIdentifier}",
                        Log        = JsonConvert.SerializeObject(error, Formatting.Indented)
                    };
                    _auditLogRepository.Save(auditLog);

                    return(StatusCode(500, ex.Message + " " + ex.InnerException?.Message));
                }
            }

            var audit = new AuditLog()
            {
                AuditType  = AuditType.SynchronisationError,
                User       = userFromRepo,
                ActionDate = DateTime.Now,
                Details    = $"Form submission with model error {formForCreation.FormIdentifier}",
                Log        = JsonConvert.SerializeObject(formForCreation, Formatting.Indented)
            };
            await _auditLogRepository.SaveAsync(audit);

            return(BadRequest(ModelState));
        }