Skip to content

Extract createStudentCheckin and endStudentCheckin from PR 465 and add to new branch #525

@JP-CC-Projects

Description

@JP-CC-Projects

Extract createStudentCheckin and endStudentCheckin

This issue rescues code from unmergable PR 465

Add the following methods to automate Checkin timestamp and duration creation:

(Code for PR 465's CheckinService available here)


    @Transactional
    public Checkin createStudentCheckin(Long studentId) {
        Instant now = Instant.now();
//      Prevents accidental opening of multiple check-ins
        if(checkinRepo.findByStudentIdAndCheckoutTimeIsNull(studentId).isPresent()){
            System.out.println("Unclosed Checkin Found for Student with ID: " + studentId);
            endStudentCheckin(studentId);
        }

        System.out.println("Creating Checkin for Student with ID: " + studentId);
        Checkin newCheckin = new Checkin();
        Student existingStudent = studentRepo.findById(studentId)
                .orElseThrow(() -> new EntityNotFoundException("Student not found with id: " + studentId));
        newCheckin.setStudent(existingStudent);
        newCheckin.setStartTime(now);
        checkinRepo.save(setCheckinTypeByDayHour(now, newCheckin));
        existingStudent.getCheckin().add(newCheckin);
        studentRepo.save(existingStudent);
        return newCheckin;
    }
    @Transactional
    public Checkin endStudentCheckin(Long studentId) {
        Instant now = Instant.now();
        System.out.println("Creating Checkout for Student with ID: " + studentId);
        Student existingStudent = studentRepo.findById(studentId)
                .orElseThrow(() -> new EntityNotFoundException(
                        "Student not found with id: " + studentId));
        Checkin unclosedCheckin = checkinRepo.findByStudentIdAndCheckoutTimeIsNull(studentId)
                .orElseThrow(() -> new EntityNotFoundException(
                        "Unclosed Check-in not found for student with id: " + studentId));
        unclosedCheckin.setEndTime(now);
        checkinRepo.save(unclosedCheckin);
        BigInteger oneHourInSeconds = BigInteger.valueOf(3600);
        //Sets max checkin time to 1 hour:
        if(unclosedCheckin.getTimeInClassInSeconds().compareTo(oneHourInSeconds) > 0){
            unclosedCheckin.setTimeInClassInSeconds(oneHourInSeconds);
        }
        existingStudent.getCheckin().add(unclosedCheckin);
        System.out.println("Service: Saving finished checkin with ID: " + unclosedCheckin.getId());
        return unclosedCheckin;
    }

Notes:

  • Parameters that accept studentId may have to be changed to studentUid.
  • Following the close of this issue, we may want to add setCheckinTypeByDayHour (also in PR 465's CheckinService), which automates the setting of CodingType.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions