Skip to content
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
Expand Up @@ -26,7 +26,9 @@
import uk.gov.hmcts.reform.preapi.enums.ParticipantType;
import uk.gov.hmcts.reform.preapi.enums.RecordingOrigin;
import uk.gov.hmcts.reform.preapi.enums.RecordingStatus;
import uk.gov.hmcts.reform.preapi.repositories.BookingRepository;
import uk.gov.hmcts.reform.preapi.repositories.PortalAccessRepository;
import uk.gov.hmcts.reform.preapi.services.CaptureSessionService;
import uk.gov.hmcts.reform.preapi.services.RecordingService;
import uk.gov.hmcts.reform.preapi.services.UserService;

Expand All @@ -48,6 +50,8 @@ public class EntityCreationService {
private final RecordingService recordingService;
private final MigrationRecordService migrationRecordService;
private final UserService userService;
private final BookingRepository bookingRepository;
private final CaptureSessionService captureSessionService;
private final PortalAccessRepository portalAccessRepository;

@Value("${vodafone-user-email}")
Expand Down Expand Up @@ -96,7 +100,13 @@ public CreateBookingDTO createBooking(ProcessedRecording cleansedData, CreateCas
CreateBookingDTO bookingDTO = new CreateBookingDTO();
bookingDTO.setId(bookingId);
bookingDTO.setCaseId(aCase.getId());
bookingDTO.setScheduledFor(cleansedData.getRecordingTimestamp());
bookingDTO.setScheduledFor(
version != null && version.equalsIgnoreCase("COPY")
? bookingRepository.findByIdAndDeletedAtIsNull(bookingId)
.map(b -> b.getScheduledFor())
.orElse(cleansedData.getRecordingTimestamp())
: cleansedData.getRecordingTimestamp()
);
Set<CreateParticipantDTO> filteredParticipants = aCase.getParticipants().stream()
.filter(p ->
(p.getFirstName() != null && p.getFirstName().equalsIgnoreCase(cleansedData.getWitnessFirstName()))
Expand Down Expand Up @@ -127,15 +137,26 @@ public CreateCaptureSessionDTO createCaptureSession(ProcessedRecording cleansedD
captureSessionId = UUID.randomUUID();
}

boolean isCopy = version != null && version.equalsIgnoreCase("COPY");

CreateCaptureSessionDTO captureSessionDTO = new CreateCaptureSessionDTO();
captureSessionDTO.setId(captureSessionId);
captureSessionDTO.setBookingId(booking.getId());
captureSessionDTO.setStartedAt(cleansedData.getRecordingTimestamp());

UUID vodafoneUser = getUserByEmail(vodafoneUserEmail);
captureSessionDTO.setStartedByUserId(vodafoneUser);
captureSessionDTO.setFinishedAt(cleansedData.getFinishedAt());
captureSessionDTO.setFinishedByUserId(vodafoneUser);

if (isCopy) {
var existing = captureSessionService.findById(captureSessionId);
captureSessionDTO.setStartedAt(existing.getStartedAt());
captureSessionDTO.setStartedByUserId(existing.getStartedByUserId());
captureSessionDTO.setFinishedAt(existing.getFinishedAt());
captureSessionDTO.setFinishedByUserId(existing.getFinishedByUserId());
} else {
captureSessionDTO.setStartedAt(cleansedData.getRecordingTimestamp());
captureSessionDTO.setStartedByUserId(vodafoneUser);
captureSessionDTO.setFinishedAt(cleansedData.getFinishedAt());
captureSessionDTO.setFinishedByUserId(vodafoneUser);
}
captureSessionDTO.setStatus(RecordingStatus.NO_RECORDING);
captureSessionDTO.setOrigin(RecordingOrigin.VODAFONE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import uk.gov.hmcts.reform.preapi.batch.entities.ProcessedRecording;
import uk.gov.hmcts.reform.preapi.dto.AccessDTO;
import uk.gov.hmcts.reform.preapi.dto.BookingDTO;
import uk.gov.hmcts.reform.preapi.dto.CaptureSessionDTO;
import uk.gov.hmcts.reform.preapi.dto.CaseDTO;
import uk.gov.hmcts.reform.preapi.dto.CreateBookingDTO;
import uk.gov.hmcts.reform.preapi.dto.CreateCaptureSessionDTO;
Expand All @@ -33,7 +34,10 @@
import uk.gov.hmcts.reform.preapi.enums.ParticipantType;
import uk.gov.hmcts.reform.preapi.enums.RecordingOrigin;
import uk.gov.hmcts.reform.preapi.enums.RecordingStatus;
import uk.gov.hmcts.reform.preapi.entities.Booking;
import uk.gov.hmcts.reform.preapi.repositories.BookingRepository;
import uk.gov.hmcts.reform.preapi.repositories.PortalAccessRepository;
import uk.gov.hmcts.reform.preapi.services.CaptureSessionService;
import uk.gov.hmcts.reform.preapi.services.RecordingService;
import uk.gov.hmcts.reform.preapi.services.UserService;

Expand Down Expand Up @@ -77,6 +81,12 @@ public class EntityCreationServiceTest {
@MockitoBean
private UserService userService;

@MockitoBean
private BookingRepository bookingRepository;

@MockitoBean
private CaptureSessionService captureSessionService;

@MockitoBean
private PortalAccessRepository portalAccessRepository;

Expand Down Expand Up @@ -203,13 +213,19 @@ public void createBookingReturnsNullIfCopyHasNoOrigBookingId() {
public void createBookingShouldUseExistingBookingIdForCopy() {
UUID archiveId = UUID.randomUUID();
UUID existingBookingId = UUID.randomUUID();
Timestamp originalScheduledFor = Timestamp.from(Instant.now().minus(Duration.ofDays(30)));

MigrationRecord copyRecord = new MigrationRecord();
copyRecord.setArchiveId(archiveId.toString());

MigrationRecord origRecord = new MigrationRecord();
origRecord.setBookingId(existingBookingId);

Booking existingBooking = new Booking();
existingBooking.setScheduledFor(originalScheduledFor);
when(bookingRepository.findByIdAndDeletedAtIsNull(existingBookingId))
.thenReturn(Optional.of(existingBooking));

when(migrationRecordService.findByArchiveId(archiveId.toString()))
.thenReturn(Optional.of(copyRecord));
when(migrationRecordService.getOrigFromCopy(copyRecord))
Expand All @@ -230,7 +246,7 @@ public void createBookingShouldUseExistingBookingIdForCopy() {
assertThat(result).isNotNull();
assertThat(result.getId()).isEqualTo(existingBookingId);
assertThat(result.getCaseId()).isEqualTo(caseDTO.getId());
assertThat(result.getScheduledFor()).isEqualTo(recording.getRecordingTimestamp());
assertThat(result.getScheduledFor()).isEqualTo(originalScheduledFor);
}

@Test
Expand Down Expand Up @@ -290,7 +306,7 @@ public void createCaptureSessionOrigMissingId() {
public void createCaptureSessionShouldUseExistingCaptureSessionIdForCopy() {
UUID archiveId = UUID.randomUUID();
UUID existingCaptureSessionId = UUID.randomUUID();

MigrationRecord copyRecord = new MigrationRecord();
copyRecord.setArchiveId(archiveId.toString());

Expand All @@ -302,11 +318,17 @@ public void createCaptureSessionShouldUseExistingCaptureSessionIdForCopy() {
when(migrationRecordService.getOrigFromCopy(copyRecord))
.thenReturn(Optional.of(origRecord));

BaseUserDTO user = new UserDTO();
user.setId(UUID.randomUUID());
AccessDTO accessDTO = new AccessDTO();
accessDTO.setUser(user);
when(userService.findByEmail(VODAFONE_EMAIL)).thenReturn(accessDTO);
Timestamp originalStartedAt = Timestamp.from(Instant.now().minus(Duration.ofDays(30)));
Timestamp originalFinishedAt = Timestamp.from(Instant.now().minus(Duration.ofDays(29)));
UUID originalStartedByUserId = UUID.randomUUID();
UUID originalFinishedByUserId = UUID.randomUUID();

CaptureSessionDTO existingCaptureSession = new CaptureSessionDTO();
existingCaptureSession.setStartedAt(originalStartedAt);
existingCaptureSession.setStartedByUserId(originalStartedByUserId);
existingCaptureSession.setFinishedAt(originalFinishedAt);
existingCaptureSession.setFinishedByUserId(originalFinishedByUserId);
when(captureSessionService.findById(existingCaptureSessionId)).thenReturn(existingCaptureSession);

ProcessedRecording processedRecording = ProcessedRecording.builder()
.archiveId(archiveId.toString())
Expand All @@ -322,8 +344,10 @@ public void createCaptureSessionShouldUseExistingCaptureSessionIdForCopy() {
assertThat(result).isNotNull();
assertThat(result.getId()).isEqualTo(existingCaptureSessionId);
assertThat(result.getBookingId()).isEqualTo(booking.getId());
assertThat(result.getStartedAt()).isEqualTo(processedRecording.getRecordingTimestamp());
assertThat(result.getStartedByUserId()).isEqualTo(user.getId());
assertThat(result.getStartedAt()).isEqualTo(originalStartedAt);
assertThat(result.getStartedByUserId()).isEqualTo(originalStartedByUserId);
assertThat(result.getFinishedAt()).isEqualTo(originalFinishedAt);
assertThat(result.getFinishedByUserId()).isEqualTo(originalFinishedByUserId);
assertThat(result.getStatus()).isEqualTo(RecordingStatus.NO_RECORDING);
assertThat(result.getOrigin()).isEqualTo(RecordingOrigin.VODAFONE);
}
Expand Down
Loading