diff --git a/src/database/queries/sessions.js b/src/database/queries/sessions.js index dbc2db95d..83d7783ae 100644 --- a/src/database/queries/sessions.js +++ b/src/database/queries/sessions.js @@ -377,6 +377,45 @@ exports.getCreatedSessionsCountInDateRange = async (mentorId, startDate, endDate created_at: { [Op.between]: [startDate, endDate], }, + mentor_id: mentorId, // Check mentor_id + created_by: mentorId, // Check created_by + }, + }) + return count + } catch (error) { + throw error + } +} + +/** + * Get the count of mentoring sessions within a date range for a specific mentor. + * @param {number} mentorId - The ID of the mentor. + * @param {Date} startDate - The start date of the date range. + * @param {Date} endDate - The end date of the date range. + * @returns {Promise} - The count of mentoring sessions. + * @throws {Error} - If an error occurs during the process. + */ + +exports.getAssignedSessionsCountInDateRange = async (mentorId, startDate, endDate) => { + try { + const filter = { + user_id: mentorId, + type: common.SESSION_OWNERSHIP_TYPE.MENTOR, + } + + const option = { + attributes: ['session_id'], + } + const sessionIds = await sessionOwnership.findAll(filter, option, true) + + const count = await Session.count({ + where: { + id: { [Op.in]: sessionIds }, + created_at: { + [Op.between]: [startDate, endDate], + }, + mentor_id: mentorId, + created_by: { [Op.ne]: mentorId }, }, }) return count diff --git a/src/services/mentors.js b/src/services/mentors.js index 4445f15dd..249d26530 100644 --- a/src/services/mentors.js +++ b/src/services/mentors.js @@ -179,13 +179,23 @@ module.exports = class MentorsHelper { filterEndDate.toISOString() ) + const totalSessionsAssigned = await sessionQueries.getAssignedSessionsCountInDateRange( + userId, + filterStartDate.toISOString(), + filterEndDate.toISOString() + ) + const totalSessionsHosted = await sessionQueries.getHostedSessionsCountInDateRange( userId, Date.parse(filterStartDate) / 1000, // Converts milliseconds to seconds Date.parse(filterEndDate) / 1000 ) - const result = { total_session_created: totalSessionsCreated, total_session_hosted: totalSessionsHosted } + const result = { + total_session_created: totalSessionsCreated, + total_session_hosted: totalSessionsHosted, + total_session_assigned: totalSessionsAssigned, + } return responses.successResponse({ statusCode: httpStatusCode.ok, message: 'MENTORS_REPORT_FETCHED_SUCCESSFULLY',