Skip to content

Commit

Permalink
VKT(Backend): WIP support for free enrollment in API
Browse files Browse the repository at this point in the history
  • Loading branch information
lket committed Jun 6, 2024
1 parent 3c25534 commit 7748a8d
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 5 deletions.
10 changes: 10 additions & 0 deletions backend/vkt/src/main/java/fi/oph/vkt/api/PublicController.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ public PublicEnrollmentDTO createEnrollment(
) {
final Person person = publicAuthService.getPersonFromSession(session);

// TODO this might need separate endpoint?
if (dto.isFree() && featureFlagService.isEnabled(FeatureFlag.FREE_ENROLLMENT_FOR_HIGHEST_LEVEL_ALLOWED)) {
return publicEnrollmentService.createFreeEnrollment(dto, reservationId, person);
}

return publicEnrollmentService.createEnrollment(dto, reservationId, person);
}

Expand All @@ -110,6 +115,11 @@ public PublicEnrollmentDTO updateEnrollment(
) {
final Person person = publicAuthService.getPersonFromSession(session);

// TODO this might need separate endpoint?
if (dto.isFree() && featureFlagService.isEnabled(FeatureFlag.FREE_ENROLLMENT_FOR_HIGHEST_LEVEL_ALLOWED)) {
return publicEnrollmentService.updateEnrollmentForFree(dto, examEventId, person);
}

return publicEnrollmentService.updateEnrollmentForPayment(dto, examEventId, person);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fi.oph.vkt.repository;

import fi.oph.vkt.model.FreeEnrollment;
import org.springframework.stereotype.Repository;

@Repository
public interface FreeEnrollmentRepository extends BaseRepository<FreeEnrollment> {}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
import fi.oph.vkt.api.dto.PublicReservationDTO;
import fi.oph.vkt.model.Enrollment;
import fi.oph.vkt.model.ExamEvent;
import fi.oph.vkt.model.FreeEnrollment;
import fi.oph.vkt.model.Person;
import fi.oph.vkt.model.Reservation;
import fi.oph.vkt.model.type.EnrollmentStatus;
import fi.oph.vkt.model.type.FreeEnrollmentSource;
import fi.oph.vkt.repository.EnrollmentRepository;
import fi.oph.vkt.repository.ExamEventRepository;
import fi.oph.vkt.repository.FreeEnrollmentRepository;
import fi.oph.vkt.repository.ReservationRepository;
import fi.oph.vkt.util.ExamEventUtil;
import fi.oph.vkt.util.PersonUtil;
Expand All @@ -23,6 +26,7 @@
import java.time.LocalDateTime;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -35,6 +39,8 @@ public class PublicEnrollmentService extends AbstractEnrollmentService {
private final PublicEnrollmentEmailService publicEnrollmentEmailService;
private final PublicReservationService publicReservationService;
private final ReservationRepository reservationRepository;
private final FreeEnrollmentRepository freeEnrollmentRepository;
private final Environment environment;

@Transactional
public PublicEnrollmentInitialisationDTO initialiseEnrollment(final long examEventId, final Person person) {
Expand Down Expand Up @@ -189,23 +195,65 @@ public PublicEnrollmentDTO createEnrollment(
dto,
examEvent,
person,
EnrollmentStatus.EXPECTING_PAYMENT_UNFINISHED_ENROLLMENT
EnrollmentStatus.EXPECTING_PAYMENT_UNFINISHED_ENROLLMENT,
null
);
reservationRepository.deleteById(reservationId);

return createEnrollmentDTO(enrollment);
}

@Transactional
public PublicEnrollmentDTO createFreeEnrollment(
final PublicEnrollmentCreateDTO dto,
final long reservationId,
final Person person
) {
final Reservation reservation = reservationRepository.getReferenceById(reservationId);
final ExamEvent examEvent = reservation.getExamEvent();

if (person.getId() != reservation.getPerson().getId()) {
throw new APIException(APIExceptionType.RESERVATION_PERSON_SESSION_MISMATCH);
}

// TODO validate that enrollment is actually free
// Either: check Koski/user-provided certificate
// Or: seperate API-endpoint for creating FreeEnrollment entity, that is checked here

// Validate that there are unused free enrollments

FreeEnrollment freeEnrollment = new FreeEnrollment();
freeEnrollment.setApproved(false);
freeEnrollment.setPerson(person);
freeEnrollment.setSource(FreeEnrollmentSource.KOSKI_COMPLETED_DEGREE);
freeEnrollmentRepository.saveAndFlush(freeEnrollment);

final Enrollment enrollment = createOrUpdateExistingEnrollment(
dto,
examEvent,
person,
EnrollmentStatus.AWAITING_APPROVAL,
freeEnrollment
);
reservationRepository.deleteById(reservationId);

// TODO send confirmation email

return createEnrollmentDTO(enrollment);
}

private Enrollment createOrUpdateExistingEnrollment(
final PublicEnrollmentCreateDTO dto,
final ExamEvent examEvent,
final Person person,
final EnrollmentStatus enrollmentStatus
final EnrollmentStatus enrollmentStatus,
final FreeEnrollment freeEnrollment
) {
final Enrollment enrollment = findEnrollment(examEvent, person, enrollmentRepository).orElse(new Enrollment());
enrollment.setExamEvent(examEvent);
enrollment.setPerson(person);
enrollment.setStatus(enrollmentStatus);
enrollment.setFreeEnrollment(freeEnrollment != null ? freeEnrollment : enrollment.getFreeEnrollment());

copyDtoFieldsToEnrollment(enrollment, dto);
if (dto.digitalCertificateConsent()) {
Expand All @@ -229,7 +277,13 @@ public PublicEnrollmentDTO createEnrollmentToQueue(
final Person person
) {
final ExamEvent examEvent = examEventRepository.getReferenceById(examEventId);
final Enrollment enrollment = createOrUpdateExistingEnrollment(dto, examEvent, person, EnrollmentStatus.QUEUED);
final Enrollment enrollment = createOrUpdateExistingEnrollment(
dto,
examEvent,
person,
EnrollmentStatus.QUEUED,
null
);

publicEnrollmentEmailService.sendEnrollmentToQueueConfirmationEmail(enrollment, person);

Expand Down Expand Up @@ -267,9 +321,36 @@ public PublicEnrollmentDTO updateEnrollmentForPayment(
dto,
examEvent,
person,
EnrollmentStatus.EXPECTING_PAYMENT_UNFINISHED_ENROLLMENT
EnrollmentStatus.EXPECTING_PAYMENT_UNFINISHED_ENROLLMENT,
null
);

return createEnrollmentDTO(enrollment);
}

@Transactional
public PublicEnrollmentDTO updateEnrollmentForFree(
final PublicEnrollmentCreateDTO dto,
final long examEventId,
final Person person
) {
final ExamEvent examEvent = examEventRepository.getReferenceById(examEventId);

// TODO check that validations from creation are still valid?

final Enrollment enrollment = createOrUpdateExistingEnrollment(
dto,
examEvent,
person,
EnrollmentStatus.AWAITING_APPROVAL,
null
);

// TODO This needs proper handling
if (enrollment.getFreeEnrollment() == null) {
throw new APIException(APIExceptionType.PAYMENT_VALIDATION_FAIL);
}

return createEnrollmentDTO(enrollment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import fi.oph.vkt.model.type.EnrollmentStatus;
import fi.oph.vkt.repository.EnrollmentRepository;
import fi.oph.vkt.repository.ExamEventRepository;
import fi.oph.vkt.repository.FreeEnrollmentRepository;
import fi.oph.vkt.repository.ReservationRepository;
import fi.oph.vkt.util.exception.APIException;
import fi.oph.vkt.util.exception.APIExceptionType;
Expand Down Expand Up @@ -64,6 +65,9 @@ public class PublicEnrollmentServiceTest {
@Resource
private ReservationRepository reservationRepository;

@Resource
private FreeEnrollmentRepository freeEnrollmentRepository;

@Resource
private TestEntityManager entityManager;

Expand All @@ -86,7 +90,9 @@ public void setup() throws IOException, InterruptedException {
examEventRepository,
publicEnrollmentEmailServiceMock,
publicReservationService,
reservationRepository
reservationRepository,
freeEnrollmentRepository,
environment
);
}

Expand Down

0 comments on commit 7748a8d

Please sign in to comment.