public void Given_Get_When_Save_Then_Returns_OverviewViewModel() { // Given var target = GetTarget(); var viewModel = new OverviewViewModel(); var viewDocModel = new DocumentsToSaveViewModel(); // When var result = target.Save(_accidentRecordId, _companyId, viewModel, viewDocModel) as ViewResult; // Then Assert.That(result.Model, Is.InstanceOf<OverviewViewModel>()); }
public JsonResult SaveAndNext(OverviewViewModel overviewModel,DocumentsToSaveViewModel documentsToSaveViewModel) { Validate(overviewModel); if (ModelState.IsValid) { UpdateAccidentRecordOverview(overviewModel, documentsToSaveViewModel); return Json(new { Success = true }); } else { return ModelStateErrorsAsJson(); } }
public ActionResult Save(long accidentRecordId, long companyId, OverviewViewModel overviewModel, DocumentsToSaveViewModel documentsToSaveViewModel) { Validate(overviewModel); if (!ModelState.IsValid) { return InvalidAddAccidentDocumentsViewResult(overviewModel); } UpdateAccidentRecordOverview(overviewModel, documentsToSaveViewModel); TempData["Notice"] = "Accident overview successfully updated"; return View("Index", GetViewModel(accidentRecordId, CurrentUser.CompanyId)); }
public OverviewViewModel GetViewModel() { var accidentRecord = _accidentRecordService.GetByIdAndCompanyIdWithAccidentRecordDocuments(_accidentRecordId, _companyId); var viewModel = new OverviewViewModel(); viewModel.AccidentRecordId = _accidentRecordId; viewModel.CompanyId = _companyId; viewModel.DescriptionHowAccidentHappened = accidentRecord.DescriptionHowAccidentHappened; viewModel.IsReportable = accidentRecord.IsReportable; viewModel.DoNotSendEmailNotification = accidentRecord.DoNotSendEmailNotification; viewModel.EmailNotificationSent = accidentRecord.EmailNotificationSent; viewModel.Documents = new DocumentsViewModel() { CompanyId = _companyId, RiskAssessmentId = 0, ExistingDocumentsViewModel = new ExistingDocumentsViewModel() { CanDeleteDocuments = true, CanEditDocumentType = false, DocumentOriginTypeId = 1, DocumentTypeId = (int) DocumentTypeEnum.AccidentRecord } }; viewModel.Documents.ExistingDocumentsViewModel.PreviouslyAddedDocuments = accidentRecord.AccidentRecordDocuments .Select(x => new PreviouslyAddedDocumentGridRowViewModel() { Description = x.Description, DocumentLibraryId = x.DocumentLibraryId, DocumentOriginType = DocumentOriginType.TaskCompleted, DocumentTypeName = x.DocumentType.Name, Filename = x.Filename, Id = x.Id }).ToList(); viewModel.NextStepsVisible = accidentRecord.NextStepsAvailable; if (accidentRecord.SiteWhereHappened != null) { var accidentRecordNotificationMemberEmails = _siteService.GetAccidentRecordNotificationMembers(accidentRecord.SiteWhereHappened.Id).Where(a => a.HasEmail()); viewModel.AccidentRecordNotificationMemberEmails = accidentRecordNotificationMemberEmails .Select( a => new AccidentRecordNotificationMemberEmail(){Email = a.Email(), Name = a.FullName()} ).ToList(); } return viewModel; }
public void Given_get_When_Save_Then_Returns_View() { // Given var target = GetTarget(); var viewModel = new OverviewViewModel() { AccidentRecordId = _accidentRecordId, DescriptionHowAccidentHappened = "desc" }; var viewDocModel = new DocumentsToSaveViewModel(); // When var result = target.Save(_accidentRecordId, _companyId, viewModel, viewDocModel); // Then Assert.IsInstanceOf<ViewResult>(result); }
public void given_description_added_check_saved() { //given var target = GetTarget(); var viewModel = new OverviewViewModel { AccidentRecordId = _accidentRecordId, DescriptionHowAccidentHappened = "desc" }; var viewDocModel = new DocumentsToSaveViewModel(); //when target.Save(_accidentRecordId, _companyId, viewModel, viewDocModel); //Then _accidentRecordService.Verify( x => x.SetAccidentRecordOverviewDetails( It.Is<AccidentRecordOverviewRequest>(y => y.Description == "desc"))); }
public void UpdateAccidentRecordOverview(OverviewViewModel overviewModel,DocumentsToSaveViewModel documentsToSaveViewModel) { if (documentsToSaveViewModel.CreateDocumentRequests.Count > 0) { var request = new AddDocumentToAccidentReportRequest { AccidentRecordId = overviewModel.AccidentRecordId , UserId = CurrentUser.UserId, CompanyId = CurrentUser.CompanyId , DocumentLibraryIds = documentsToSaveViewModel.CreateDocumentRequests.Select(x => new DocumentLibraryFile() { Description = x.Description, Filename = x.Filename, Id = x.DocumentLibraryId }).ToList() }; _accidentRecordService.AddAccidentRecordDocument(request); } if (documentsToSaveViewModel.DeleteDocumentRequests.Count > 0) { var request = new RemoveDocumentsFromAccidentRecordRequest() { AccidentRecordId = overviewModel.AccidentRecordId, UserId = CurrentUser.UserId, CompanyId = CurrentUser.CompanyId, DocumentLibraryIds = documentsToSaveViewModel.DeleteDocumentRequests }; _accidentRecordService.RemoveAccidentRecordDocuments(request); } var accidentRecordOverviewRequest = new AccidentRecordOverviewRequest() { AccidentRecordId = overviewModel.AccidentRecordId, CompanyId = CurrentUser.CompanyId, UserId = CurrentUser.UserId, Description = overviewModel.DescriptionHowAccidentHappened, DoNotSendEmailNotification = overviewModel.DoNotSendEmailNotification }; _accidentRecordService.SetAccidentRecordOverviewDetails(accidentRecordOverviewRequest); if (!overviewModel.DoNotSendEmailNotification) { _accidentRecordService.SendAccidentRecordEmails(overviewModel.AccidentRecordId, CurrentUser.CompanyId, CurrentUser.UserId); } }
private void Validate(OverviewViewModel viewModel) { if (String.IsNullOrEmpty(viewModel.DescriptionHowAccidentHappened)) { ModelState.AddModelError("EventsDescription", "Briefly describe how the accident happened."); } }
private ActionResult InvalidAddAccidentDocumentsViewResult(OverviewViewModel model) { return View("Index", GetViewModel(model.AccidentRecordId, CurrentUser.CompanyId)); }
public void Given_Description_Not_Added_Check_Description_Not_Saved() { //given var target = GetTarget(); var viewModel = new OverviewViewModel(); var viewDocModel = new DocumentsToSaveViewModel(); //when target.Save(_accidentRecordId, _companyId, viewModel, viewDocModel); //Then _accidentRecordService.Verify(x => x.SetAccidentRecordOverviewDetails(It.IsAny<AccidentRecordOverviewRequest>()), Times.Never()); }