soumadipshyamm / test

1 stars 1 forks source link

Dxd #1

Open soumadipshyamm opened 10 months ago

soumadipshyamm commented 10 months ago

const moment = require('moment');

// Assuming you have the User model and middleware from the previous examples...

// Custom function to calculate total login and logout duration for a specific date function calculateDailyTime(userId, date, startTime, endTime) { const startOfDay = moment(date).startOf('day'); // Start of the specified date const endOfDay = moment(date).endOf('day'); // End of the specified date

// Retrieve user information const user = await User.findById(userId);

// Filter login and logout times for the specified date const dailyLoginTimes = user.loginTimes.filter(time => isSameDay(time, startOfDay)); const dailyLogoutTimes = user.logoutTimes.filter(time => isSameDay(time, startOfDay));

// Calculate total login and logout duration const totalLoginDuration = calculateTotalDuration( dailyLoginTimes, dailyLogoutTimes, endOfDay, moment.duration(startTime), // Convert to duration moment.duration(endTime) // Convert to duration );

return { date: startOfDay, totalLoginDuration: totalLoginDuration, }; }

// Example usage const userId = 'yourUserId'; const currentDate = moment(); // Today's date const startTime = { hours: 10, minutes: 0 }; // 10:00 a.m. const endTime = { hours: 17, minutes: 0 }; // 5:00 p.m.

const dailyTimeInfo = calculateDailyTime(userId, currentDate, startTime, endTime); console.log(dailyTimeInfo);

function isSameDay(date1, date2) { return moment(date1).isSame(date2, 'day'); }

function calculateTotalDuration(loginTimes, logoutTimes, endOfDay, startTime, endTime) { let totalDuration = 0;

loginTimes.forEach((loginTime, index) => { const logoutTime = logoutTimes[index] || endOfDay;

const loginMoment = moment(loginTime);
const logoutMoment = moment(logoutTime);

// Only consider times within the specified range
if (loginMoment.isBefore(endTime) && logoutMoment.isAfter(startTime)) {
  const withinRangeLoginTime = moment.max(loginMoment, startTime);
  const withinRangeLogoutTime = moment.min(logoutMoment, endTime);

  totalDuration += withinRangeLogoutTime.diff(withinRangeLoginTime, 'minutes');
}

});

return totalDuration; }