fix(sessions): respect database sort order in getAllSessions and getMySessions
This commit is contained in:
@@ -237,7 +237,7 @@ const populateSessionList = (query, { includeClassStudents = false } = {}) => {
|
||||
};
|
||||
|
||||
const getAllSessions = async (queryParams) => {
|
||||
const { page, limit, skip } = parsePaginationAndSort(queryParams, 'day', 'asc');
|
||||
const { page, limit, skip, sort } = parsePaginationAndSort(queryParams, 'day', 'asc');
|
||||
const attendanceScope = queryParams.attendanceScope;
|
||||
const filter = buildFilterQuery(queryParams, ['topic', 'place', 'note'], [
|
||||
'page',
|
||||
@@ -272,10 +272,9 @@ const getAllSessions = async (queryParams) => {
|
||||
|
||||
const includeClassStudents = Boolean(attendanceScope);
|
||||
|
||||
const matched = await populateSessionList(Session.find(filter), { includeClassStudents }).lean();
|
||||
|
||||
let sessions = sortByClosestAttendance(matched);
|
||||
if (attendanceScope) {
|
||||
const matched = await populateSessionList(Session.find(filter), { includeClassStudents }).lean();
|
||||
let sessions = sortByClosestAttendance(matched);
|
||||
const studentsByClassId = await buildStudentsByClassId(matched);
|
||||
sessions = filterByAttendanceScope(sessions, attendanceScope, studentsByClassId)
|
||||
.map((session) => enrichSessionAttendance(session, studentsByClassId));
|
||||
@@ -285,8 +284,16 @@ const getAllSessions = async (queryParams) => {
|
||||
return { data: sessions, meta };
|
||||
}
|
||||
|
||||
const totalCount = await Session.countDocuments(filter);
|
||||
sessions = sessions.slice(skip, skip + limit);
|
||||
const effectiveSort = { ...sort };
|
||||
if (effectiveSort.day) {
|
||||
effectiveSort.startTime = effectiveSort.day;
|
||||
}
|
||||
|
||||
const [sessions, totalCount] = await Promise.all([
|
||||
populateSessionList(Session.find(filter).sort(effectiveSort).skip(skip).limit(limit), { includeClassStudents: false }).lean(),
|
||||
Session.countDocuments(filter)
|
||||
]);
|
||||
|
||||
const meta = calculateMeta(totalCount, page, limit);
|
||||
return { data: sessions, meta };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user