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 3356: Schema Migration - Edit Requests #749

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package uk.gov.hmcts.reform.preapi.entities;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;
import uk.gov.hmcts.reform.preapi.Application;
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.enums.RecordingStatus;
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 static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(classes = Application.class)
public class EditRequestTest extends IntegrationTestBase {

@Test
@Transactional
public void testSaveAndRetrieveEditRequest() {
var court = HelperFactory.createCourt(CourtType.CROWN, "Example Court", null);
entityManager.persist(court);

var aCase = HelperFactory.createCase(court, "EXAMPLE123", false, null);
entityManager.persist(aCase);

var booking = HelperFactory.createBooking(aCase, Timestamp.from(Instant.now().plusSeconds(60 * 24)), null);
entityManager.persist(booking);

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

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

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

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

var retrievedEditRequest = entityManager.find(EditRequest.class, editRequest.getId());

assertThat(retrievedEditRequest).isNotNull();
assertThat(retrievedEditRequest.getId()).isEqualTo(editRequest.getId());
assertThat(retrievedEditRequest.getSourceRecording().getId()).isEqualTo(recording.getId());
assertThat(retrievedEditRequest.getStatus()).isEqualTo(EditRequestStatus.PENDING);
assertThat(retrievedEditRequest.getCreatedBy().getId()).isEqualTo(user.getId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package uk.gov.hmcts.reform.preapi.entities;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;
import uk.gov.hmcts.reform.preapi.entities.base.CreatedModifiedAtEntity;
import uk.gov.hmcts.reform.preapi.enums.EditRequestStatus;

import java.sql.Timestamp;
import java.util.HashMap;

@Getter
@Setter
@Entity
@Table(name = "edit_requests")
public class EditRequest extends CreatedModifiedAtEntity {
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "source_recording_id", referencedColumnName = "id", nullable = false)
private Recording sourceRecording;

@Column(name = "edit_instruction", nullable = false)
@JdbcTypeCode(SqlTypes.JSON)
private String editInstruction;

@Enumerated(EnumType.STRING)
@Column(name = "status", nullable = false, columnDefinition = "edit_request_status")
@JdbcTypeCode(SqlTypes.NAMED_ENUM)
private EditRequestStatus status;

@Column(name = "started_at")
private Timestamp startedAt;

@Column(name = "finished_at")
private Timestamp finishedAt;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "created_by", referencedColumnName = "id", nullable = false)
private User createdBy;

@Override
public HashMap<String, Object> getDetailsForAudit() {
var details = new HashMap<String, Object>();
details.put("id", getId());
details.put("sourceRecordingId", sourceRecording.getId());
details.put("status", status);
details.put("editInstruction", editInstruction);
details.put("startedAt", startedAt);
details.put("finishedAt", finishedAt);
details.put("createdBy", createdBy.getId());
return details;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package uk.gov.hmcts.reform.preapi.enums;

public enum EditRequestStatus {
PENDING,
PROCESSING,
COMPLETE,
ERROR
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package uk.gov.hmcts.reform.preapi.repositories;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import uk.gov.hmcts.reform.preapi.entities.EditRequest;

import java.util.UUID;

@Repository
public interface EditRequestRepository extends JpaRepository<EditRequest, UUID> {
}
18 changes: 18 additions & 0 deletions src/main/resources/db/migration/V026__EditRequests.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CREATE TYPE public.EDIT_REQUEST_STATUS AS ENUM (
'PENDING',
'PROCESSING',
'COMPLETE',
'ERROR'
);

CREATE TABLE edit_requests (
id UUID PRIMARY KEY,
source_recording_id UUID REFERENCES recordings(id) NOT NULL,
edit_instruction JSON,
status public.EDIT_REQUEST_STATUS NOT NULL,
started_at TIMESTAMPTZ,
finished_at TIMESTAMPTZ,
created_by UUID REFERENCES users(id) NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
modified_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import uk.gov.hmcts.reform.preapi.entities.CaptureSession;
import uk.gov.hmcts.reform.preapi.entities.Case;
import uk.gov.hmcts.reform.preapi.entities.Court;
import uk.gov.hmcts.reform.preapi.entities.EditRequest;
import uk.gov.hmcts.reform.preapi.entities.Participant;
import uk.gov.hmcts.reform.preapi.entities.PortalAccess;
import uk.gov.hmcts.reform.preapi.entities.Recording;
Expand All @@ -25,6 +26,7 @@
import uk.gov.hmcts.reform.preapi.enums.AccessStatus;
import uk.gov.hmcts.reform.preapi.enums.CaseState;
import uk.gov.hmcts.reform.preapi.enums.CourtType;
import uk.gov.hmcts.reform.preapi.enums.EditRequestStatus;
import uk.gov.hmcts.reform.preapi.enums.ParticipantType;
import uk.gov.hmcts.reform.preapi.enums.RecordingOrigin;
import uk.gov.hmcts.reform.preapi.enums.RecordingStatus;
Expand Down Expand Up @@ -230,6 +232,23 @@ public static UserTermsAccepted createUserTermsAccepted(User user,
return termsAccepted;
}

public static EditRequest createEditRequest(Recording sourceRecording,
String editInstructions,
EditRequestStatus status,
User createdBy,
@Nullable Timestamp startedAt,
@Nullable Timestamp finishedAt) {
var editRequest = new EditRequest();
editRequest.setId(UUID.randomUUID());
editRequest.setSourceRecording(sourceRecording);
editRequest.setEditInstruction(editInstructions);
editRequest.setStatus(status);
editRequest.setCreatedBy(createdBy);
editRequest.setStartedAt(startedAt);
editRequest.setFinishedAt(finishedAt);
return editRequest;
}

public static CreateParticipantDTO createParticipantDTO(String firstName,
String lastName,
ParticipantType participantType) {
Expand Down
Loading