Skip to content

Commit

Permalink
VKT(Backend): Provide unused free enrollment slots to frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
lket committed Jun 6, 2024
1 parent 7748a8d commit bca3e8f
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public record PublicEnrollmentInitialisationDTO(
@NonNull PublicExamEventDTO examEvent,
@NonNull PublicPersonDTO person,
PublicReservationDTO reservation,
PublicEnrollmentDTO enrollment
PublicEnrollmentDTO enrollment,
PublicFreeEnrollmentDetails freeEnrollmentDetails
) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package fi.oph.vkt.api.dto;

import lombok.NonNull;

public record PublicFreeEnrollmentDetails(
@NonNull int freeTextualSkillExamsLeft,
@NonNull int freeOralSkillExamsLeft
) {}
2 changes: 1 addition & 1 deletion backend/vkt/src/main/java/fi/oph/vkt/model/Enrollment.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public class Enrollment extends BaseEntity {
@OneToMany(mappedBy = "enrollment")
private List<Payment> payments = new ArrayList<>();

@ManyToOne(fetch = FetchType.LAZY)
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "free_enrollment", referencedColumnName = "free_enrollment_id")
private FreeEnrollment freeEnrollment;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -39,4 +41,7 @@ public class FreeEnrollment extends BaseEntity {

@Column(name = "comment")
String comment;

@OneToOne(mappedBy = "freeEnrollment", fetch = FetchType.LAZY, optional = false)
Enrollment enrollment;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
package fi.oph.vkt.repository;

import fi.oph.vkt.model.FreeEnrollment;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface FreeEnrollmentRepository extends BaseRepository<FreeEnrollment> {}
public interface FreeEnrollmentRepository extends BaseRepository<FreeEnrollment> {
@Query(
"SELECT count(f)" +
" FROM FreeEnrollment f" +
" JOIN f.enrollment e" +
" WHERE f.person.id = ?1" +
" AND e.textualSkill = true"
)
int findUsedTextualSkillFreeEnrollmentsForPerson(final long personId);

@Query(
"SELECT count(f)" +
" FROM FreeEnrollment f" +
" JOIN f.enrollment e" +
" WHERE f.person.id = ?1" +
" AND e.oralSkill = true"
)
int findUsedOralSkillFreeEnrollmentsForPerson(final long personId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import fi.oph.vkt.api.dto.PublicEnrollmentDTO;
import fi.oph.vkt.api.dto.PublicEnrollmentInitialisationDTO;
import fi.oph.vkt.api.dto.PublicExamEventDTO;
import fi.oph.vkt.api.dto.PublicFreeEnrollmentDetails;
import fi.oph.vkt.api.dto.PublicPersonDTO;
import fi.oph.vkt.api.dto.PublicReservationDTO;
import fi.oph.vkt.model.Enrollment;
import fi.oph.vkt.model.ExamEvent;
import fi.oph.vkt.model.FeatureFlag;
import fi.oph.vkt.model.FreeEnrollment;
import fi.oph.vkt.model.Person;
import fi.oph.vkt.model.Reservation;
Expand All @@ -22,6 +24,7 @@
import fi.oph.vkt.util.exception.APIException;
import fi.oph.vkt.util.exception.APIExceptionType;
import fi.oph.vkt.util.exception.NotFoundException;
import jakarta.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Optional;
Expand All @@ -40,7 +43,7 @@ public class PublicEnrollmentService extends AbstractEnrollmentService {
private final PublicReservationService publicReservationService;
private final ReservationRepository reservationRepository;
private final FreeEnrollmentRepository freeEnrollmentRepository;
private final Environment environment;
private final FeatureFlagService featureFlagService;

@Transactional
public PublicEnrollmentInitialisationDTO initialiseEnrollment(final long examEventId, final Person person) {
Expand Down Expand Up @@ -70,6 +73,7 @@ public PublicEnrollmentInitialisationDTO initialiseEnrollment(final long examEve
person,
openings,
Optional.of(reservationDTO),
Optional.empty(),
Optional.empty()
);
}
Expand All @@ -95,12 +99,28 @@ public PublicEnrollmentInitialisationDTO getEnrollmentInitialisationDTO(final lo
final Optional<PublicEnrollmentDTO> optionalEnrollmentDTO = findEnrollment(examEvent, person, enrollmentRepository)
.map(this::createEnrollmentDTO);

Optional<PublicFreeEnrollmentDetails> optionalPublicFreeEnrollmentDetails;
if (featureFlagService.isEnabled(FeatureFlag.FREE_ENROLLMENT_FOR_HIGHEST_LEVEL_ALLOWED)) {
final int freeTextualSkillExamsLeft =
3 - freeEnrollmentRepository.findUsedTextualSkillFreeEnrollmentsForPerson(person.getId());
final int freeOralSkillExamsLeft =
3 - freeEnrollmentRepository.findUsedOralSkillFreeEnrollmentsForPerson(person.getId());
final PublicFreeEnrollmentDetails freeEnrollmentDetails = new PublicFreeEnrollmentDetails(
freeTextualSkillExamsLeft,
freeOralSkillExamsLeft
);
optionalPublicFreeEnrollmentDetails = Optional.of(freeEnrollmentDetails);
} else {
optionalPublicFreeEnrollmentDetails = Optional.empty();
}

return createEnrollmentInitialisationDTO(
examEvent,
person,
openings,
optionalReservationDTO,
optionalEnrollmentDTO
optionalEnrollmentDTO,
optionalPublicFreeEnrollmentDetails
);
}

Expand Down Expand Up @@ -133,7 +153,8 @@ private PublicEnrollmentInitialisationDTO createEnrollmentInitialisationDTO(
final Person person,
final long openings,
final Optional<PublicReservationDTO> optionalReservationDTO,
final Optional<PublicEnrollmentDTO> optionalEnrollmentDTO
final Optional<PublicEnrollmentDTO> optionalEnrollmentDTO,
final Optional<PublicFreeEnrollmentDetails> optionalPublicFreeEnrollmentDetails
) {
final PublicExamEventDTO examEventDTO = PublicExamEventDTO
.builder()
Expand All @@ -153,6 +174,7 @@ private PublicEnrollmentInitialisationDTO createEnrollmentInitialisationDTO(
.person(personDTO)
.reservation(optionalReservationDTO.orElse(null))
.enrollment(optionalEnrollmentDTO.orElse(null))
.freeEnrollmentDetails(optionalPublicFreeEnrollmentDetails.orElse(null))
.build();
}

Expand All @@ -175,7 +197,14 @@ public PublicEnrollmentInitialisationDTO initialiseEnrollmentToQueue(final long
throw new APIException(APIExceptionType.INITIALISE_ENROLLMENT_DUPLICATE_PERSON);
}

return createEnrollmentInitialisationDTO(examEvent, person, openings, Optional.empty(), Optional.empty());
return createEnrollmentInitialisationDTO(
examEvent,
person,
openings,
Optional.empty(),
Optional.empty(),
Optional.empty()
);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import fi.oph.vkt.api.dto.PublicPersonDTO;
import fi.oph.vkt.model.Enrollment;
import fi.oph.vkt.model.ExamEvent;
import fi.oph.vkt.model.FeatureFlag;
import fi.oph.vkt.model.Person;
import fi.oph.vkt.model.Reservation;
import fi.oph.vkt.model.type.EnrollmentStatus;
Expand Down Expand Up @@ -72,6 +73,7 @@ public class PublicEnrollmentServiceTest {
private TestEntityManager entityManager;

private PublicEnrollmentService publicEnrollmentService;
private FeatureFlagService featureFlagService;

@BeforeEach
public void setup() throws IOException, InterruptedException {
Expand All @@ -80,6 +82,9 @@ public void setup() throws IOException, InterruptedException {
final Environment environment = mock(Environment.class);
when(environment.getRequiredProperty("app.reservation.duration")).thenReturn(ONE_MINUTE.toString());

final FeatureFlagService featureFlagService = mock(FeatureFlagService.class);
when(featureFlagService.isEnabled(any(FeatureFlag.class))).thenReturn(true);

final PublicReservationService publicReservationService = new PublicReservationService(
reservationRepository,
environment
Expand All @@ -92,7 +97,7 @@ public void setup() throws IOException, InterruptedException {
publicReservationService,
reservationRepository,
freeEnrollmentRepository,
environment
featureFlagService
);
}

Expand Down

0 comments on commit bca3e8f

Please sign in to comment.