public static MedicalCertificateViewModel GetViewModel(MedicalCertificate model, Func<DateTime, DateTime> toLocal)
        {
            var viewModel = new MedicalCertificateViewModel
                {
                    Id = model.Id,
                    PatientId = model.PatientId,
                    ModelName = model.ModelMedicalCertificate != null ? model.ModelMedicalCertificate.Name : null,
                    IssuanceDate = toLocal(model.IssuanceDate),
                    Fields = (from f in model.Fields
                              select new MedicalCertificateFieldViewModel()
                                  {
                                      Id = f.Id,
                                      Name = f.Name,
                                      Value = f.Value
                                  }).ToList()
                };

            return viewModel;
        }
        public void Delete_HappyPath()
        {
            // obtains a valid patient
            Firestarter.CreateFakePatients(this.db.Doctors.First(), this.db);
            this.db.SaveChanges();
            var patientId = this.db.Patients.First().Id;

            // obtains a valid certificate model
            ModelMedicalCertificateViewModel certificateModelFormModel = new ModelMedicalCertificateViewModel()
            {
                Name = "My Model",
                Text = "This is a reference: <%FIELD_1%>"
            };

            var mr = new MockRepository(true);
            var certificateModelController = mr.CreateController<ModelMedicalCertificatesController>();
            var certificateModelControllerResult = certificateModelController.Edit(certificateModelFormModel);
            var certificateModel = this.db.ModelMedicalCertificates.First();

            // tries to save a certificate based on that model
            MedicalCertificateViewModel formModel = new MedicalCertificateViewModel()
            {
                ModelId = certificateModel.Id,
                PatientId = patientId,
                Fields = new List<MedicalCertificateFieldViewModel>()
                {
                     new MedicalCertificateFieldViewModel() { Name = "field_1", Value = "value 1" }
                }
            };

            var certificateController = mr.CreateController<MedicalCertificatesController>();
            var certificateControllerResult = certificateController.Edit(new[] { formModel });
            var certificate = this.db.MedicalCertificates.First();

            // tries to delete the certificate
            var result = certificateController.Delete(certificate.Id);
            JsonDeleteMessage deleteMessage = (JsonDeleteMessage)result.Data;

            Assert.AreEqual(true, deleteMessage.success, "deleteMessage.success must be true");
            Assert.AreEqual(0, this.db.MedicalCertificates.Count());
        }
        public void Edit_2_CannotSaveWithInvalidPatient()
        {
            var mr = new MockRepository(true);
            int modelId = this.db.ModelMedicalCertificates.First().Id;

            // tries to save
            var formModel = new MedicalCertificateViewModel()
            {
                ModelId = modelId,
                // this probably doesn't exist
                PatientId = 9999
            };

            var controller = mr.CreateController<MedicalCertificatesController>();
            var controllerResult = controller.Edit(new[] { formModel });

            Assert.IsInstanceOfType(controllerResult, typeof(ViewResult));
            Assert.AreEqual(false, controller.ModelState.IsValid);
            Assert.AreEqual(1, controller.ModelState.Count);
        }
        public void Edit_1_CannotSaveWithInvalidModelId()
        {
            // obtains a valid patient
            Firestarter.CreateFakePatients(this.db.Doctors.First(), this.db);
            this.db.SaveChanges();
            var patientId = this.db.Patients.First().Id;

            MedicalCertificateViewModel formModel = new MedicalCertificateViewModel()
            {
                // this probably doesn't exist
                ModelId = 9999,
                PatientId = patientId
            };

            var mr = new MockRepository(true);
            var controller = mr.CreateController<MedicalCertificatesController>();
            var controllerResult = controller.Edit(new[] { formModel });

            Assert.IsInstanceOfType(controllerResult, typeof(ViewResult));
            Assert.AreEqual(false, controller.ModelState.IsValid);
            Assert.AreEqual(1, controller.ModelState.Count);
        }
        public void MedicalCertificateFieldsEditor_4_BothParametersAreSuppliedAndTheyDontMatch()
        {
            // create a model
            ModelMedicalCertificateViewModel certificateModelFormModel = new ModelMedicalCertificateViewModel()
            {
                Name = "My Model",
                Text = "This is a reference: <%PROP_1%>"
            };

            var mr = new MockRepository(true);
            var certificateModelcontroller = mr.CreateController<ModelMedicalCertificatesController>();
            certificateModelcontroller.Edit(certificateModelFormModel);
            var modelId = this.db.ModelMedicalCertificates.Select(m => m.Id).First();

            // create a certificate
            // obtains a valid patient
            Firestarter.CreateFakePatients(this.db.Doctors.First(), this.db);
            this.db.SaveChanges();

            var patientId = this.db.Patients.First().Id;

            var controller = mr.CreateController<MedicalCertificatesController>();
            MedicalCertificateViewModel formModel = new MedicalCertificateViewModel()
            {
                // both EXISTING
                ModelId = modelId,
                PatientId = patientId,
                Fields = new List<MedicalCertificateFieldViewModel>()
                {
                     new MedicalCertificateFieldViewModel() { Name = "prop_1", Value ="Este é o valor" }
                }
            };

            // save the certificate
            controller.Edit(new[] { formModel });
            var certificateId = this.db.MedicalCertificates.Select(c => c.Id).First();

            // at this point we have 2 certificate models, "modelId" and "anotherModelId" and we have a certificate using "modelId". The point is
            // to call MedicalCertificateFieldsEditor passing "anotherModelId"

            var controllerResult = controller.MedicalCertificateFieldsEditor(modelId, certificateId, null);

            // obtaining the view-model
            ViewResult view = (ViewResult)controllerResult;
            var viewModel = (MedicalCertificateViewModel)view.Model;

            Assert.AreEqual(1, viewModel.Fields.Count);
            Assert.AreEqual("Este é o valor", viewModel.Fields[0].Value);
        }
        public void GetCertificateText_HappyPath()
        {
            // obtains a valid patient
            Firestarter.CreateFakePatients(this.db.Doctors.First(), this.db);
            this.db.SaveChanges();

            var patient = this.db.Patients.First();
            var patientId = patient.Id;
            var patientName = patient.Person.FullName;

            // obtains a valid certificate model
            ModelMedicalCertificateViewModel certificateModelFormModel = new ModelMedicalCertificateViewModel()
            {
                Name = "My Model",
                Text = "This is a reference: <%FIELD_1%>. This is the patient name: <%paCIENTE%>"
            };
            var mr = new MockRepository(true);
            var certificateModelController = mr.CreateController<ModelMedicalCertificatesController>();
            var certificateModelControllerResult = certificateModelController.Edit(certificateModelFormModel);
            var modelId = this.db.ModelMedicalCertificates.First().Id;

            // tries to save
            MedicalCertificateViewModel formModel = new MedicalCertificateViewModel()
            {
                // both EXISTING
                ModelId = modelId,
                PatientId = patientId,
                Fields = new List<MedicalCertificateFieldViewModel>()
                {
                     new MedicalCertificateFieldViewModel() { Name = "field_1", Value = "This is a value" }
                }
            };

            var certificateController = mr.CreateController<MedicalCertificatesController>();
            var certificateControllerResult = certificateController.Edit(new[] { formModel });

            Assert.IsInstanceOfType(certificateControllerResult, typeof(ViewResult));
            Assert.AreEqual(true, certificateController.ModelState.IsValid);

            // Now verifies whether the result is the expected
            var newlyCreatedCertificate = this.db.MedicalCertificates.First();

            var certificateText = certificateController.GetCertificateText(newlyCreatedCertificate.Id);

            Assert.AreEqual("This is a reference: This is a value. This is the patient name: " + patientName, certificateText);
        }
        public void Edit_HappyPath()
        {
            // obtains a valid patient
            Firestarter.CreateFakePatients(this.db.Doctors.First(), this.db);
            this.db.SaveChanges();
            var patientId = this.db.Patients.First().Id;

            // obtains a valid certificate model
            var certificateModelFormModel = new ModelMedicalCertificateViewModel()
            {
                Name = "My Model",
                Text = "This is a reference: <%FIELD_1%>"
            };
            var mr = new MockRepository(true);
            var certificateModelTarget = mr.CreateController<ModelMedicalCertificatesController>();
            var certificateModelResult = certificateModelTarget.Edit(certificateModelFormModel);
            var modelId = this.db.ModelMedicalCertificates.First().Id;

            // tries to save
            MedicalCertificateViewModel formModel = new MedicalCertificateViewModel()
            {
                // both EXISTING
                ModelId = modelId,
                PatientId = patientId,
                Fields = new List<MedicalCertificateFieldViewModel>()
                {
                     new MedicalCertificateFieldViewModel() { Name = "field_1", Value ="Este é o valor" }
                }
            };

            var target = mr.CreateController<MedicalCertificatesController>();
            var result = target.Edit(new[] { formModel });

            Assert.IsInstanceOfType(result, typeof(ViewResult));
            Assert.AreEqual(true, target.ModelState.IsValid);
        }
        public void Edit_5_CanSaveAllFieldsWhenModelDoesNotExist()
        {
            // obtains a valid patient
            Firestarter.CreateFakePatients(this.db.Doctors.First(), this.db);
            this.db.SaveChanges();
            var patientId = this.db.Patients.First().Id;

            // obtains a valid certificate model
            ModelMedicalCertificateViewModel certificateModelFormModel = new ModelMedicalCertificateViewModel()
            {
                Name = "My Model",
                Text = "This is a reference: <%FIELD_1%>"
            };
            var mr = new MockRepository(true);
            var certificateModelTarget = mr.CreateController<ModelMedicalCertificatesController>();
            var certificateModelResult = certificateModelTarget.Edit(certificateModelFormModel);
            var modelId = this.db.ModelMedicalCertificates.First().Id;

            // tries to save
            MedicalCertificateViewModel formModel = new MedicalCertificateViewModel()
            {
                // both EXISTING
                ModelId = modelId,
                PatientId = patientId,
                Fields = new List<MedicalCertificateFieldViewModel>()
                {
                     new MedicalCertificateFieldViewModel() { Name = "field_1", Value = "value 1" }
                }
            };

            var target = mr.CreateController<MedicalCertificatesController>();
            var result = target.Edit(new[] { formModel });

            Assert.IsInstanceOfType(result, typeof(ViewResult));
            Assert.AreEqual(true, target.ModelState.IsValid);

            // now, edit to remove the model and add a field

            var medicalCerticate = this.db.MedicalCertificates.First();
            formModel = new MedicalCertificateViewModel()
            {
                // both EXISTING
                Id = medicalCerticate.Id,
                ModelId = null,
                PatientId = patientId,
                Fields = new List<MedicalCertificateFieldViewModel>()
                {
                     new MedicalCertificateFieldViewModel() { Name = "field_1" },
                     new MedicalCertificateFieldViewModel() { Name = "field_2" },
                }
            };

            target = mr.CreateController<MedicalCertificatesController>();
            result = target.Edit(new[] { formModel });

            Assert.IsInstanceOfType(result, typeof(ViewResult));
            Assert.AreEqual(true, target.ModelState.IsValid);
        }
        public void Edit_4_CannotSaveACertificateWithInvalidFields()
        {
            // obtains a valid patient
            Firestarter.CreateFakePatients(this.db.Doctors.First(), this.db);
            this.db.SaveChanges();
            var patientId = this.db.Patients.First().Id;

            // obtains a valid certificate model
            ModelMedicalCertificateViewModel certificateModelFormModel = new ModelMedicalCertificateViewModel()
            {
                Name = "My Model",
                Text = "This is a reference: <%FIELD_1%>"
            };
            var mr = new MockRepository(true);
            var certificateModelTarget = mr.CreateController<ModelMedicalCertificatesController>();
            var certificateModelResult = certificateModelTarget.Edit(certificateModelFormModel);
            var modelId = this.db.ModelMedicalCertificates.First().Id;

            // tries to save
            MedicalCertificateViewModel formModel = new MedicalCertificateViewModel()
            {
                // both EXISTING
                ModelId = modelId,
                PatientId = patientId
            };

            var target = mr.CreateController<MedicalCertificatesController>();
            var result = target.Edit(new[] { formModel });

            Assert.IsInstanceOfType(result, typeof(ViewResult));
            Assert.AreEqual(false, target.ModelState.IsValid);
            Assert.AreEqual(1, target.ModelState.Count);
        }
        public void Edit_3_CannotSaveWithInvalidModelIdAndId()
        {
            // obtains a valid patient
            Firestarter.CreateFakePatients(this.db.Doctors.First(), this.db);
            this.db.SaveChanges();
            var patientId = this.db.Patients.First().Id;

            // tries to save
            var formModel = new MedicalCertificateViewModel()
            {
                // both EXISTING
                ModelId = null,
                Id = null,
                PatientId = patientId,
                Fields = new List<MedicalCertificateFieldViewModel>()
                {
                     new MedicalCertificateFieldViewModel() { Name = "field_1" },
                     new MedicalCertificateFieldViewModel() { Name = "field_2" }
                }
            };

            var mr = new MockRepository(true);
            var controller = mr.CreateController<MedicalCertificatesController>();
            var controllerResult = controller.Edit(new[] { formModel });

            Assert.IsInstanceOfType(controllerResult, typeof(ViewResult));
            Assert.AreEqual(false, controller.ModelState.IsValid);
            Assert.AreEqual(1, controller.ModelState.Count);
        }
 public ActionResult Create(MedicalCertificateViewModel[] medicalCertificates)
 {
     return this.Edit(medicalCertificates);
 }
        public ActionResult MedicalCertificateFieldsEditor(int? modelId, int? certificateId, string htmlFieldPrefix)
        {
            var viewModel = new MedicalCertificateViewModel() { Fields = new List<MedicalCertificateFieldViewModel>() };
            var model = this.db.ModelMedicalCertificates.FirstOrDefault(mmc => mmc.Id == modelId);

            if (model != null)
            {
                foreach (var field in model.Fields)
                    viewModel.Fields.Add(new MedicalCertificateFieldViewModel()
                    {
                        Name = field.Name,
                        Value = null
                    });

                var certificate = this.db.MedicalCertificates.FirstOrDefault(mc => mc.Id == certificateId && mc.ModelMedicalCertificateId == modelId);
                if (certificate != null)
                {
                    foreach (var field in certificate.Fields)
                    {
                        var matchingField = viewModel.Fields.FirstOrDefault(f => f.Name == field.Name);
                        if (matchingField != null)
                            matchingField.Value = field.Value;
                    }
                }
            }

            this.ViewData.TemplateInfo.HtmlFieldPrefix = htmlFieldPrefix;
            return this.View(viewModel);
        }
        public ActionResult Edit(MedicalCertificateViewModel[] medicalCertificates)
        {
            var formModel = medicalCertificates.Single();

            ModelMedicalCertificate certificateModel = null;

            // validates the existence and the compliance of the certificate model
            if (formModel.ModelId.HasValue)
            {
                certificateModel = this.db.ModelMedicalCertificates.FirstOrDefault(mmc => mmc.Id == formModel.ModelId);
                if (certificateModel == null)
                    this.ModelState.AddModelError<MedicalCertificateViewModel>(m => m.ModelId, "O modelo de atestado informado não é válido");
                else
                {
                    // for each field in the model, all must exist in the formModel
                    if (certificateModel.Fields.Any(field => formModel.Fields.All(f => f.Name.ToLower() != field.Name.ToLower())))
                        this.ModelState.AddModelError<MedicalCertificateViewModel>(m => m.Fields, "Dados inválidos. As informações recebidas não condizem com o modelo de atestado especificado");

                    // #KNOWN ISSUE# The next statements shouldn't exist. The REQUIRED attribute should work :(
                    // for all fields existing in the formModel, all must have a value
                    for (var i = 0; i < formModel.Fields.Count; i++)
                    {
                        var field = formModel.Fields[i];
                        if (string.IsNullOrEmpty(field.Value))
                            this.ModelState.AddModelError("Fields[" + i + "]", "O valor do campo é requerido");
                    }
                }
            }

            if (!formModel.ModelId.HasValue && !formModel.Id.HasValue)
                this.ModelState.AddModelError<MedicalCertificateViewModel>(m => m.ModelId, "É necessário informar o modelo do atestado");

            // validates the existence of the patient
            if (formModel.PatientId.HasValue && !this.db.Patients.Any(m => m.Id == formModel.PatientId))
                this.ModelState.AddModelError<MedicalCertificateViewModel>(m => m.ModelId, "O paciente informado não é válido");

            if (this.ModelState.IsValid)
            {
                MedicalCertificate certificate = null;
                if (formModel.Id == null)
                {
                    certificate = new MedicalCertificate()
                    {
                        CreatedOn = this.GetUtcNow(),
                        PatientId = formModel.PatientId.Value,
                        PracticeId = this.DbUser.PracticeId,
                    };
                    this.db.MedicalCertificates.AddObject(certificate);
                }
                else
                {
                    certificate = this.db.MedicalCertificates.FirstOrDefault(r => r.Id == formModel.Id);
                    if (certificate == null)
                        return this.ObjectNotFound();
                }

                if (certificateModel != null)
                {
                    certificate.ModelMedicalCertificateId = certificateModel.Id;
                    certificate.Text = certificateModel.Text;
                }
                else
                    certificate.ModelMedicalCertificateId = null;

                certificate.Patient.IsBackedUp = false;
                certificate.Fields.Update(
                        formModel.Fields,
                        (vm, m) => vm.Name == m.Name,
                        (vm, m) =>
                        {
                            m.Name = vm.Name;
                            m.Value = vm.Value;
                            m.PracticeId = this.DbUser.PracticeId;
                        },
                        (m) => this.db.MedicalCertificateFields.DeleteObject(m));

                certificate.IssuanceDate = this.ConvertToUtcDateTime(formModel.IssuanceDate.Value);
                this.db.SaveChanges();

                // todo: use GetViewModel method:
                //var viewModel = GetViewModel(certificate);

                var viewModel = new MedicalCertificateViewModel
                {
                    Id = certificate.Id,
                    ModelId = certificate.ModelMedicalCertificateId,
                    // the only situation in which ModelName will be null is when the model certificate has been removed
                    ModelName = certificate.ModelMedicalCertificate != null ? certificate.ModelMedicalCertificate.Name : null,
                    PatientId = certificate.PatientId,
                    IssuanceDate = this.ConvertToLocalDateTime(certificate.IssuanceDate),
                    Fields = (from f in certificate.Fields
                              select new MedicalCertificateFieldViewModel()
                              {
                                  Id = f.Id,
                                  Name = f.Name,
                                  Value = f.Value,
                              }).ToList(),
                };

                return this.View("Details", viewModel);
            }

            formModel.ModelOptions = this.db.ModelMedicalCertificates.ToList().Select(mmc => new SelectListItem() { Text = mmc.Name, Value = mmc.Id.ToString() }).ToList();
            return this.View("Edit", formModel);
        }
        public ActionResult Edit(int? id, int? patientId, int? y, int? m, int? d)
        {
            MedicalCertificateViewModel viewModel = null;

            if (id.HasValue)
            {
                var certificate = this.db.MedicalCertificates.FirstOrDefault(mc => mc.Id == id.Value);
                // todo: use GetViewModel method
                //viewModel = GetViewModel(certificate);
                viewModel = new MedicalCertificateViewModel
                {
                    Id = certificate.Id,
                    PatientId = certificate.PatientId,
                    ModelName = certificate.ModelMedicalCertificate.Name,
                    ModelId = certificate.ModelMedicalCertificateId,
                    IssuanceDate = this.ConvertToLocalDateTime(certificate.IssuanceDate),
                    Fields = (from f in certificate.Fields
                              select new MedicalCertificateFieldViewModel
                              {
                                  Id = f.Id,
                                  Name = f.Name,
                                  Value = f.Value,
                              }).ToList(),
                };
            }
            else
            {
                viewModel = new MedicalCertificateViewModel
                {
                    Id = id,
                    PatientId = patientId,
                    IssuanceDate = DateTimeHelper.CreateDate(y, m, d) ?? this.GetPracticeLocalNow(),
                };
            }

            return this.View("Edit", viewModel);
        }