Skip to content

Commit

Permalink
[kbss-cvut/record-manager-ui#186] Change createRecords handler to han…
Browse files Browse the repository at this point in the history
…dle a situation when User is not assigned to any institutuion
  • Loading branch information
palagdan committed Jul 22, 2024
1 parent a2b7cc4 commit ead3ae9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/main/java/cz/cvut/kbss/study/rest/PatientRecordController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import cz.cvut.kbss.study.exception.NotFoundException;
import cz.cvut.kbss.study.model.PatientRecord;
import cz.cvut.kbss.study.model.RecordPhase;
import cz.cvut.kbss.study.model.User;
import cz.cvut.kbss.study.model.export.RawRecord;
import cz.cvut.kbss.study.persistence.dao.util.RecordFilterParams;
import cz.cvut.kbss.study.rest.event.PaginatedResultRetrievedEvent;
Expand All @@ -17,6 +18,7 @@
import cz.cvut.kbss.study.service.ConfigReader;
import cz.cvut.kbss.study.service.ExcelRecordConverter;
import cz.cvut.kbss.study.service.PatientRecordService;
import cz.cvut.kbss.study.service.UserService;
import cz.cvut.kbss.study.util.ConfigParam;
import cz.cvut.kbss.study.util.Constants;
import jakarta.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -52,16 +54,19 @@ public class PatientRecordController extends BaseController {
private final RestTemplate restTemplate;
private final ConfigReader configReader;
private ObjectMapper objectMapper;
private final UserService userService;

public PatientRecordController(PatientRecordService recordService, ApplicationEventPublisher eventPublisher,
ExcelRecordConverter excelRecordConverter, RestTemplate restTemplate,
ConfigReader configReader, ObjectMapper objectMapper) {
ConfigReader configReader, ObjectMapper objectMapper,
UserService userService) {
this.recordService = recordService;
this.eventPublisher = eventPublisher;
this.excelRecordConverter = excelRecordConverter;
this.restTemplate = restTemplate;
this.configReader = configReader;
this.objectMapper = objectMapper;
this.userService = userService;
}

@PreAuthorize("hasRole('" + SecurityConstants.ROLE_ADMIN + "') or @securityUtils.isMemberOfInstitution(#institutionKey)")
Expand Down Expand Up @@ -150,9 +155,15 @@ private PatientRecord findInternal(String key) {
return record;
}



@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public ResponseEntity<Void> createRecord(@RequestBody PatientRecord record) {
public ResponseEntity<String> createRecord(@RequestBody PatientRecord record) {

if(userService.getCurrentUser().getInstitution() == null)
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("User has no relation to the institution");

recordService.persist(record);
if (LOG.isTraceEnabled()) {
LOG.trace("Patient record {} successfully created.", record);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import cz.cvut.kbss.study.rest.event.PaginatedResultRetrievedEvent;
import cz.cvut.kbss.study.rest.util.RestUtils;
import cz.cvut.kbss.study.service.PatientRecordService;
import cz.cvut.kbss.study.service.UserService;
import cz.cvut.kbss.study.util.Constants;
import cz.cvut.kbss.study.util.IdentificationUtils;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -66,6 +67,9 @@ public class PatientRecordControllerTest extends BaseControllerTestRunner {
@Mock
private ApplicationEventPublisher eventPublisherMock;

@Mock
private UserService userService;

@Spy
private ObjectMapper objectMapper = Environment.getObjectMapper();

Expand Down Expand Up @@ -178,6 +182,7 @@ public void findByInstitutionReturnsRecords() throws Exception {
@Test
public void createRecordReturnsResponseStatusCreated() throws Exception {
PatientRecord record = Generator.generatePatientRecord(user);
when(userService.getCurrentUser()).thenReturn(user);

final MvcResult result = mockMvc.perform(post("/records").content(toJson(record))
.contentType(MediaType.APPLICATION_JSON_VALUE))
Expand All @@ -186,6 +191,22 @@ public void createRecordReturnsResponseStatusCreated() throws Exception {
assertEquals(HttpStatus.CREATED, HttpStatus.valueOf(result.getResponse().getStatus()));
}

@Test
public void createRecordWithoutInstitutionReturnsResponseStatusBadRequest() throws Exception {
user.setInstitution(null);

PatientRecord record = Generator.generatePatientRecord(user);

when(userService.getCurrentUser()).thenReturn(user);

final MvcResult result = mockMvc.perform(post("/records").content(toJson(record))
.contentType(MediaType.APPLICATION_JSON_VALUE))
.andReturn();

assertEquals(HttpStatus.BAD_REQUEST, HttpStatus.valueOf(result.getResponse().getStatus()));
}


@Test
public void updateRecordReturnsResponseStatusNoContent() throws Exception {
final String key = "12345";
Expand Down

0 comments on commit ead3ae9

Please sign in to comment.