public PostWarningNoteResponse PostWarningNote(PostWarningNoteRequest request) { var person = _databaseContext.Persons.FirstOrDefault(x => x.Id == request.PersonId); if (person == null) { throw new PersonNotFoundException($"Person with given id ({request.PersonId}) not found"); } var warningNote = request.ToDatabaseEntity(); _databaseContext.WarningNotes.Add(warningNote); _databaseContext.SaveChanges(); var response = new PostWarningNoteResponse { WarningNoteId = warningNote.Id }; // try // { var dt = DateTime.Now; var note = new WarningNoteCaseNote { FirstName = person.FirstName, LastName = person.LastName, MosaicId = person.Id.ToString(), Timestamp = dt.ToString("dd/MM/yyyy H:mm:ss"), Note = $"{dt.ToShortDateString()} | Warning Note | Warning note created against this person", FormNameOverall = "API_WarningNote", FormName = "Warning Note Created", WarningNoteId = warningNote.Id.ToString(), WorkerEmail = request.CreatedBy }; var caseNotesDocument = new CaseNotesDocument { CaseFormData = JsonConvert.SerializeObject(note) }; response.CaseNoteId = _processDataGateway.InsertCaseNoteDocument(caseNotesDocument).Result; // } // catch (Exception ex) // { // _databaseContext.WarningNotes.Remove(warningNote); // _databaseContext.SaveChanges(); // throw new PostWarningNoteException($"Unable to create a case note. Warning Note not created: {ex.Message}"); // } return(response); }
public void PatchWarningNote(PatchWarningNoteRequest request) { WarningNote warningNote = _databaseContext.WarningNotes.FirstOrDefault(x => x.Id == request.WarningNoteId); if (warningNote == null) { throw new PatchWarningNoteException($"Warning Note with given id ({request.WarningNoteId}) not found"); } if (warningNote.Status == "closed") { throw new PatchWarningNoteException( $"Warning Note with given id ({request.WarningNoteId}) has already been closed"); } Person person = _databaseContext.Persons.FirstOrDefault(x => x.Id == warningNote.PersonId); if (person == null) { throw new PatchWarningNoteException("Person not found"); } Worker worker = _databaseContext.Workers.FirstOrDefault(x => x.Email == request.ReviewedBy); if (worker == null) { throw new PatchWarningNoteException($"Worker ({request.ReviewedBy}) not found"); } warningNote.LastReviewDate = request.ReviewDate; warningNote.NextReviewDate = request.NextReviewDate; if (request.Status?.ToLower() == "closed") { warningNote.Status = "closed"; warningNote.EndDate = _systemTime.Now; warningNote.NextReviewDate = null; } warningNote.LastModifiedBy = request.ReviewedBy; var review = PostWarningNoteReview(request); _databaseContext.WarningNoteReview.Add(review); _databaseContext.SaveChanges(); var dt = DateTime.Now; var note = new WarningNoteCaseNote { FirstName = person.FirstName, LastName = person.LastName, MosaicId = person.Id.ToString(), Timestamp = dt.ToString("dd/MM/yyyy H:mm:ss"), Note = $"{dt.ToShortDateString()} | Warning Note | {((request.Status == "closed") ? "Warning note against this person ended" : "Warning note against this person reviewed")}", FormNameOverall = "API_WarningNote", FormName = (request.Status == "closed") ? "Warning Note Ended" : "Warning Note Reviewed", WarningNoteId = warningNote.Id.ToString(), WorkerEmail = request.ReviewedBy }; var caseNotesDocument = new CaseNotesDocument { CaseFormData = JsonConvert.SerializeObject(note) }; _ = _processDataGateway.InsertCaseNoteDocument(caseNotesDocument).Result; }