maintain the session after logout in existing spring mvc project

22 views Asked by At

in my hrms project i add the attendance system in that project. when user login the account and click the checkin button it should showing the checkin time and date. once click the checkin button the timer running to calculate the work hours until user click the check-out button. here is the problem when user logout account and comeback to login the account the check in time, check out time should be reset. how to solve this problem

here is the controller class---> `@Controller public class AttendanceController {

@Autowired
private AttendanceService attendanceService;

@RequestMapping("/attendance")
public String home(Model model) {
    List<UserAttendance> userAttendances = attendanceService.getAllUserAttendances();
    model.addAttribute("userAttendances", userAttendances);
    return "home";
}

@RequestMapping(value = "/checkin", method = RequestMethod.POST)
@ResponseBody
public String checkIn(@RequestParam("username") String username, HttpServletRequest request) {
    HttpSession session = request.getSession();
    // Add logic to store check-in time in session or database as required
    session.setAttribute("checkInTime", System.currentTimeMillis());
    return attendanceService.startAttendance(username);
}


@RequestMapping(value = "/checkout", method = RequestMethod.POST)
@ResponseBody
public String checkOut(@RequestParam("username") String username, HttpServletRequest request) {
    HttpSession session = request.getSession();
    Long checkInTime = (Long) session.getAttribute("checkInTime");
    if (checkInTime != null) {
        LocalDateTime checkInDateTime = LocalDateTime.ofEpochSecond(checkInTime / 1000, 0, ZoneOffset.UTC);
        Duration workDuration = Duration.between(checkInDateTime, LocalDateTime.now());
        String formattedWorkHours = convertDurationToHoursMinutes(workDuration);
        return attendanceService.endAttendance(username, formattedWorkHours);
    } else {
        return "Error: User has not checked in yet.";
    }
}

private String convertDurationToHoursMinutes(Duration duration) {
    long hours = duration.toHours();
    long minutes = duration.toMinutes() % 60;;
    return hours + " hours " + minutes + " minutes";
}

@RequestMapping(value = "/fetch-attendance-history", method = RequestMethod.GET)
@ResponseBody
public List<UserAttendance> getAttendanceHistory() {
    return attendanceService.getAllUserAttendances();
}

} And this is service class--> `@Service public class AttendanceService {

@Autowired
private MongoTemplate mongoTemplate;

public String startAttendance(String username) {
    UserAttendance attendance = new UserAttendance(username, LocalDateTime.now(), null, null);
    mongoTemplate.save(attendance);
    return attendance.getStartTime().toString();
}


public String endAttendance(String username, String workHours) {
    Query query = new Query(Criteria.where("username").is(username).and("endTime").is(null));
    LocalDateTime endTime = LocalDateTime.now();
    Update update = new Update()
            .set("endTime", endTime)
            .set("workHours", workHours);
    mongoTemplate.updateFirst(query, update, UserAttendance.class);
    return endTime.toString();
}



public List<UserAttendance> getAllUserAttendances() {
    return mongoTemplate.findAll(UserAttendance.class);
}

public void resetAttendance(LocalDateTime startOfDay) {
    Query query = new Query(Criteria.where("startTime").lt(startOfDay).and("endTime").is(null));
    List<UserAttendance> pendingAttendances = mongoTemplate.find(query, UserAttendance.class);

    for (UserAttendance attendance : pendingAttendances) {
        attendance.setEndTime(startOfDay);
        // Optionally, handle the case of resetting attendance records
        // when users have not checked out before the start of the day
        // For example, log the occurrences or notify the admin
        // based on the project's requirements.
        mongoTemplate.save(attendance);
    }
}

public List<UserAttendance> getAttendancesForDate(LocalDateTime startDate, LocalDateTime endDate) {
    Query query = new Query(Criteria.where("startTime").gte(startDate).lte(endDate));
    return mongoTemplate.find(query, UserAttendance.class);
}

}``

i want to solve this issue when i logout the account also it should update check-in time and check-out time

0

There are 0 answers