Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S28 250: Edit Requests Via Portal #836

Open
wants to merge 16 commits into
base: S28-3367/edited-recording-notification
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package uk.gov.hmcts.reform.preapi.controllers;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import uk.gov.hmcts.reform.preapi.controllers.params.TestingSupportRoles;
import uk.gov.hmcts.reform.preapi.dto.EditRequestDTO;
import uk.gov.hmcts.reform.preapi.enums.EditRequestStatus;
import uk.gov.hmcts.reform.preapi.media.edit.EditInstructions;
import uk.gov.hmcts.reform.preapi.util.FunctionalTestBase;

import java.util.Map;
Expand All @@ -24,7 +21,7 @@ public class EditControllerFT extends FunctionalTestBase {

@Test
@DisplayName("Should create an edit request from a csv")
void editFromCsvSuccess() throws JsonProcessingException {
void editFromCsvSuccess() {
var recordingDetails = createRecording();
assertRecordingExists(recordingDetails.recordingId(), true);

Expand All @@ -40,8 +37,7 @@ void editFromCsvSuccess() throws JsonProcessingException {
assertThat(postResponse.getStatus()).isEqualTo(EditRequestStatus.PENDING);

assertThat(postResponse.getEditInstruction()).isNotNull();
var instructions = OBJECT_MAPPER.readValue(postResponse.getEditInstruction(),
new TypeReference<EditInstructions>() {});
var instructions = postResponse.getEditInstruction();
assertThat(instructions).isNotNull();

var requestedInstructions = instructions.getRequestedInstructions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,18 @@ public void testSaveAndRetrieveEditRequest() {
var user = HelperFactory.createDefaultTestUser();
entityManager.persist(user);

var editRequest = HelperFactory.createEditRequest(recording, "{}", EditRequestStatus.PENDING, user, null, null);
var editRequest = HelperFactory.createEditRequest(
recording,
"{}",
EditRequestStatus.PENDING,
user,
null,
null,
null,
null,
null,
null
);
entityManager.persist(editRequest);
entityManager.flush();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package uk.gov.hmcts.reform.preapi.services;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import uk.gov.hmcts.reform.preapi.enums.CourtType;
import uk.gov.hmcts.reform.preapi.enums.EditRequestStatus;
import uk.gov.hmcts.reform.preapi.enums.RecordingOrigin;
import uk.gov.hmcts.reform.preapi.util.HelperFactory;
import uk.gov.hmcts.reform.preapi.utils.IntegrationTestBase;

import java.sql.Timestamp;
import java.time.Instant;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;

public class EditRequestServiceIT extends IntegrationTestBase {
@Autowired
private EditRequestService editRequestService;

@Test
@Transactional
public void searchEditRequests() {
mockAdminUser();

var court = HelperFactory.createCourt(CourtType.CROWN, "Example Court", "1234");
entityManager.persist(court);

var caseEntity = HelperFactory.createCase(court, "CASE12345", true, null);
entityManager.persist(caseEntity);

var booking = HelperFactory.createBooking(caseEntity, Timestamp.from(Instant.now()), null);
entityManager.persist(booking);

var captureSession = HelperFactory.createCaptureSession(
booking,
RecordingOrigin.PRE,
null,
null,
null,
null,
null,
null,
null,
null
);
entityManager.persist(captureSession);

var recording = HelperFactory.createRecording(captureSession, null, 1, "filename", null);
entityManager.persist(recording);

var user = HelperFactory.createDefaultTestUser();
entityManager.persist(user);

var editRequest = HelperFactory.createEditRequest(
recording,
"{}",
EditRequestStatus.PENDING,
user,
null,
null,
null,
null,
null,
null
);
entityManager.persist(editRequest);

var requests1 = editRequestService.findAll(recording.getId(), Pageable.unpaged()).toList();
assertThat(requests1).hasSize(1);
assertThat(requests1.getFirst().getId()).isEqualTo(editRequest.getId());

var requests2 = editRequestService.findAll(UUID.randomUUID(), Pageable.unpaged()).toList();
assertThat(requests2).isEmpty();

var requests3 = editRequestService.findAll(null, Pageable.unpaged()).toList();
assertThat(requests3).hasSize(1);
assertThat(requests3.getFirst().getId()).isEqualTo(editRequest.getId());
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
package uk.gov.hmcts.reform.preapi.controllers;

import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PagedResourcesAssembler;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.PagedModel;
import org.springframework.http.HttpEntity;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import uk.gov.hmcts.reform.preapi.controllers.base.PreApiController;
import uk.gov.hmcts.reform.preapi.dto.CreateEditRequestDTO;
import uk.gov.hmcts.reform.preapi.dto.EditRequestDTO;
import uk.gov.hmcts.reform.preapi.exception.BadRequestException;
import uk.gov.hmcts.reform.preapi.exception.PathPayloadMismatchException;
import uk.gov.hmcts.reform.preapi.exception.RequestedPageOutOfRangeException;
import uk.gov.hmcts.reform.preapi.exception.UnsupportedMediaTypeException;
import uk.gov.hmcts.reform.preapi.services.EditRequestService;

Expand All @@ -22,7 +36,7 @@
@Slf4j
@RestController
@RequestMapping("/edits")
public class EditController {
public class EditController extends PreApiController {
private final EditRequestService editRequestService;

public static final String CSV_FILE_TYPE = "text/csv";
Expand All @@ -32,13 +46,56 @@ public EditController(EditRequestService editRequestService) {
this.editRequestService = editRequestService;
}

// todo add more roles when start working with UI
@GetMapping("/{id}")
@PreAuthorize("hasAnyRole('ROLE_SUPER_USER')")
@PreAuthorize("hasAnyRole('ROLE_SUPER_USER', 'ROLE_LEVEL_3')")
public ResponseEntity<EditRequestDTO> getEditRequestById(@PathVariable("id") UUID id) {
return ResponseEntity.ok(editRequestService.findById(id));
}

@GetMapping
@Parameter(
name = "page",
description = "The page number of search result to return",
schema = @Schema(implementation = Integer.class),
example = "0"
)
@Parameter(
name = "size",
description = "The number of search results to return per page",
schema = @Schema(implementation = Integer.class),
example = "10"
)
@PreAuthorize("hasAnyRole('ROLE_SUPER_USER', 'ROLE_LEVEL_3')")
public HttpEntity<PagedModel<EntityModel<EditRequestDTO>>> searchRecordings(
@Parameter(schema = @Schema(implementation = UUID.class)) UUID sourceRecordingId,
@Parameter(hidden = true) Pageable pageable,
@Parameter(hidden = true) PagedResourcesAssembler<EditRequestDTO> assembler
) {
var resultPage = editRequestService.findAll(
sourceRecordingId,
pageable
);

if (pageable.getPageNumber() > resultPage.getTotalPages()) {
throw new RequestedPageOutOfRangeException(pageable.getPageNumber(), resultPage.getTotalPages());
}

return ResponseEntity.ok(assembler.toModel(resultPage));
}

@PutMapping("/{id}")
@PreAuthorize("hasAnyRole('ROLE_SUPER_USER', 'ROLE_LEVEL_1', 'ROLE_LEVEL_3')")
public ResponseEntity<Void> upsertEditRequest(
@PathVariable("id") UUID id,
@Valid @RequestBody CreateEditRequestDTO createEditRequestDTO
) {
if (!id.equals(createEditRequestDTO.getId())) {
throw new PathPayloadMismatchException("editRequestId", "createEditRequestDTO.id");
}

return getUpsertResponse(editRequestService.upsert(createEditRequestDTO), id);
}

@PreAuthorize("hasAnyRole('ROLE_SUPER_USER')")
@PostMapping(value = "/from-csv/{sourceRecordingId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<EditRequestDTO> createEditFromCsv(@PathVariable UUID sourceRecordingId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import jakarta.validation.constraints.Size;
import lombok.Data;
import lombok.NoArgsConstructor;
import uk.gov.hmcts.reform.preapi.dto.validators.CreateEditRequestStatusConstraint;
import uk.gov.hmcts.reform.preapi.enums.EditRequestStatus;

import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@CreateEditRequestStatusConstraint
@Schema(description = "CreateEditRequestDTO")
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class CreateEditRequestDTO {
Expand All @@ -35,8 +35,21 @@ public class CreateEditRequestDTO {
private EditRequestStatus status;

@Valid
@Min(1)
@NotNull
@NotEmpty
@Schema(description = "CreateEditRequestInstructions")
private List<EditCutInstructionDTO> editInstructions;

@Schema(description = "CreateEditRequestJointlyAgreed")
private Boolean jointlyAgreed;

@Size(max = 512)
@Schema(description = "CreateEditRequestRejectionReason")
private String rejectionReason;

@Schema(description = "CreateEditRequestApprovedAt")
private Timestamp approvedAt;

@Size(max = 100)
@Schema(description = "CreateEditRequestApprovedBy")
private String approvedBy;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package uk.gov.hmcts.reform.preapi.dto;


import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import io.swagger.v3.oas.annotations.media.Schema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.opencsv.bean.CsvBindByName;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -25,17 +26,22 @@ public class EditCutInstructionDTO {
@NotNull
@CsvBindByName(column = "Start time of cut")
@Schema(description = "EditInstructionStart")
@Pattern(regexp = "^([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d$", message = "must be in format HH:MM:SS")
private String startOfCut;

@NotNull
@CsvBindByName(column = "End time of cut")
@Schema(description = "EditInstructionEnd")
@Pattern(regexp = "^([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d$", message = "must be in format HH:MM:SS")
private String endOfCut;

@CsvBindByName(column = "Reason")
private String reason;

@Schema(hidden = true)
private Long start;

@Schema(hidden = true)
private Long end;

public long getStart() {
Expand All @@ -55,6 +61,9 @@ public long getEnd() {
}

private static long parseTime(String time) {
if (time == null) {
return -1;
}
try {
var units = time.split(":");
int hours = Integer.parseInt(units[0]);
Expand Down
Loading