Skip to content

Commit

Permalink
Bindu | BAH-3117 | get note by voided false and make delete reason as…
Browse files Browse the repository at this point in the history
… optional query parameter
  • Loading branch information
binduak committed Jul 24, 2023
1 parent 047d628 commit 6c35026
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@
import org.bahmni.module.bahmnicore.dao.NoteDao;
import org.bahmni.module.bahmnicore.model.Note;
import org.bahmni.module.bahmnicore.model.NoteType;
import org.hibernate.Criteria;
import org.hibernate.query.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.openmrs.api.APIException;
import org.openmrs.api.db.DAOException;

public class NoteDaoImpl implements NoteDao {

Expand Down Expand Up @@ -75,7 +72,8 @@ public Note getNote(Date noteDate, String noteType) {
List<Note> notes = new ArrayList<>();
StringBuilder query = new StringBuilder("select note from Note note " +
"where note.noteDate = :noteDate " +
"and note.noteType.name = :noteType ");
"and note.noteType.name = :noteType " +
"and note.voided = false");

Query queryToGetNotes = sessionFactory.getCurrentSession().createQuery(query.toString());
queryToGetNotes.setParameter("noteDate", noteDate);
Expand All @@ -92,7 +90,8 @@ public List<Note> getNotes(Date startDate, Date endDate, String noteType) {
Query query = currentSession.createQuery(
"select note from Note note " +
"where note.noteDate between :startDate and :endDate " +
"and note.noteType.name = :noteType");
"and note.noteType.name = :noteType" +
" and note.voided = false");
query.setParameter("startDate", startDate);
query.setParameter("endDate", endDate);
query.setParameter("noteType", noteType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@

import org.bahmni.module.bahmnicore.contract.NoteRequest;
import org.bahmni.module.bahmnicore.dao.NoteDao;
import org.bahmni.module.bahmnicore.model.Note;
import org.openmrs.annotation.Handler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;

@Component
Expand All @@ -36,9 +33,8 @@ public void validate(Object obj, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "noteDate", "Note.noteDate.required");
}

Note note = noteDao.getNote(noteRequest.getNoteDate(), noteRequest.getNoteTypeName());
if(nonNull(note)) {
errors.reject("Note entry exist for notetype and noteDate");
if(nonNull(noteDao.getNote(noteRequest.getNoteDate(), noteRequest.getNoteTypeName()))) {
errors.reject("Note entry exist for noteType and noteDate");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,101 @@
package org.bahmni.module.bahmnicore.validator;

import org.bahmni.module.bahmnicore.contract.NoteRequest;
import org.bahmni.module.bahmnicore.dao.NoteDao;
import org.bahmni.module.bahmnicore.model.Note;
import org.junit.Assert;
import org.bahmni.module.bahmnicore.model.NoteType;
import org.junit.Before;
import org.junit.Test;
import org.openmrs.RelationshipType;
import org.openmrs.test.BaseContextSensitiveTest;
import org.openmrs.validator.RelationshipTypeValidator;
import org.springframework.validation.BindException;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.Errors;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.MockitoAnnotations.initMocks;

public class NoteValidatorTest {

public class NoteValidatorTest extends BaseContextSensitiveTest {
@InjectMocks
private NoteValidator validator;
@Mock
private NoteDao noteDao;

public void validate_shouldFailValidationIfaIsToBIsNullOrEmptyOrWhitespace() throws Exception {
Note type = new Note();
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}

@Test
public void ensureNoteTypeIsNotNull() {
initMocks(this);
NoteRequest noteRequest = new NoteRequest();
Errors noteRequestErrors = new BeanPropertyBindingResult(noteRequest, "noteRequest");
noteRequest.setNoteTypeName(null);
noteRequest.setNoteText("Note Text");
noteRequest.setNoteDate(new Date());
Note note = new Note();
note.setId(1);
Mockito.when(noteDao.getNote(any(Date.class), any(String.class))).thenReturn(note);
validator.validate(noteRequest, noteRequestErrors);
assertEquals(true, "Note.noteType.required".matches(noteRequestErrors.getAllErrors().get(0).getCode().toString()));
}

Errors errors = new BindException(type, "type");
new NoteValidator().validate(type, errors);
Assert.assertTrue(errors.hasFieldErrors("aIsToB"));
@Test
public void ensureNoteDateIsNotNull() {
NoteRequest noteRequest = new NoteRequest();
Errors noteRequestErrors = new BeanPropertyBindingResult(noteRequest, "noteRequest");
noteRequest.setNoteTypeName("OT module");
noteRequest.setNoteText("Note Text");
noteRequest.setNoteDate(null);
Note note = new Note();
note.setId(1);
Mockito.when(noteDao.getNote(any(Date.class), any(String.class))).thenReturn(note);
validator.validate(noteRequest, noteRequestErrors);
assertEquals(true, "Note.noteDate.required".matches(noteRequestErrors.getAllErrors().get(0).getCode().toString()));
}

@Test
public void ensureNoteTextIsNotNull() {
NoteRequest noteRequest = new NoteRequest();
Errors noteRequestErrors = new BeanPropertyBindingResult(noteRequest, "noteRequest");
noteRequest.setNoteTypeName("OT module");
noteRequest.setNoteDate(new Date());
noteRequest.setNoteText(null);
Note note = new Note();
note.setId(1);
Mockito.when(noteDao.getNote(any(Date.class), any(String.class))).thenReturn(note);
validator.validate(noteRequest, noteRequestErrors);
assertEquals(true, "Note.noteText.required".matches(noteRequestErrors.getAllErrors().get(0).getCode().toString()));
}

@Test
public void ensureNoteDoesntExistForSameNoteDateAndNoteType() throws ParseException {
NoteRequest noteRequest = new NoteRequest();
Errors noteRequestErrors = new BeanPropertyBindingResult(noteRequest, "noteRequest");
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date noteDate1 = format.parse("2023-08-16 00:00:00.0");
noteRequest.setNoteTypeName("OT module");
noteRequest.setNoteDate(noteDate1);
noteRequest.setNoteText("Some text");

Note note = new Note();
NoteType noteType = new NoteType();
Date existingNote = format.parse("2023-08-16 00:00:00.0");
noteType.setName("OT module");
note.setId(1);
note.setNoteDate(existingNote);
note.setNoteType(noteType);
Mockito.when(noteDao.getNote(any(Date.class), any(String.class))).thenReturn(note);
validator.validate(noteRequest, noteRequestErrors);
assertEquals(true, "Note entry exist for noteType and noteDate".matches(noteRequestErrors.getAllErrors().get(0).getCode().toString()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


@Controller
@RequestMapping(value = "/rest/" + RestConstants.VERSION_1 + "/note")
@RequestMapping(value = "/rest/" + RestConstants.VERSION_1 + "/notes")
public class BahmniNotesController extends BaseRestController {

private Log log = LogFactory.getLog(this.getClass());
Expand Down Expand Up @@ -81,7 +81,7 @@ public NoteResponse update(@Valid @PathVariable("id") String id, @RequestBody St

@RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
@ResponseBody
public NoteResponse delete(@PathVariable("id") String id, @RequestParam(value = "reason") String reason ) {
public NoteResponse delete(@PathVariable("id") String id, @RequestParam(value = "reason", required = false) String reason ) {
Integer noteId = Integer.valueOf(id);
return noteMapper.map(Context.getService(NoteService.class).voidNote(noteId, reason));
}
Expand Down
15 changes: 13 additions & 2 deletions bahmnicore-omod/src/main/resources/liquibase.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4556,7 +4556,7 @@
insert into order_type_class_map(order_type_id, concept_class_id) values(@radiology_order_type_id,@radiology_imaging);
</sql>
</changeSet>
<changeSet author="Bindu" id="notes-202307091606">
<changeSet author="Bindu" id="notes-202307091607">
<preConditions onFail="MARK_RAN">
<not>
<tableExists tableName="notes"/>
Expand Down Expand Up @@ -4613,7 +4613,7 @@
</createIndex>

</changeSet>
<changeSet author="Bindu" id="note-type-202307101600">
<changeSet author="Bindu" id="note-type-202307101601">
<preConditions onFail="MARK_RAN">
<not>
<tableExists tableName="note_type"/>
Expand All @@ -4640,5 +4640,16 @@
<column name="uuid" />
</createIndex>
</changeSet>
<changeSet id="note-name-202307241600" author="Bindu">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="0">
select count(*) from note_type where name = 'OT module';
</sqlCheck>
</preConditions>
<comment> Adding note type </comment>
<sql>
insert into note_type (name, description, uuid) values ('OT module', 'OT module', uuid());
</sql>
</changeSet>
<include file="addAtomfeedMigrations.xml"/>
</databaseChangeLog>

This file was deleted.

0 comments on commit 6c35026

Please sign in to comment.