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 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 attendanceScope = queryParams.attendanceScope;
|
||||||
const filter = buildFilterQuery(queryParams, ['topic', 'place', 'note'], [
|
const filter = buildFilterQuery(queryParams, ['topic', 'place', 'note'], [
|
||||||
'page',
|
'page',
|
||||||
@@ -272,10 +272,9 @@ const getAllSessions = async (queryParams) => {
|
|||||||
|
|
||||||
const includeClassStudents = Boolean(attendanceScope);
|
const includeClassStudents = Boolean(attendanceScope);
|
||||||
|
|
||||||
const matched = await populateSessionList(Session.find(filter), { includeClassStudents }).lean();
|
|
||||||
|
|
||||||
let sessions = sortByClosestAttendance(matched);
|
|
||||||
if (attendanceScope) {
|
if (attendanceScope) {
|
||||||
|
const matched = await populateSessionList(Session.find(filter), { includeClassStudents }).lean();
|
||||||
|
let sessions = sortByClosestAttendance(matched);
|
||||||
const studentsByClassId = await buildStudentsByClassId(matched);
|
const studentsByClassId = await buildStudentsByClassId(matched);
|
||||||
sessions = filterByAttendanceScope(sessions, attendanceScope, studentsByClassId)
|
sessions = filterByAttendanceScope(sessions, attendanceScope, studentsByClassId)
|
||||||
.map((session) => enrichSessionAttendance(session, studentsByClassId));
|
.map((session) => enrichSessionAttendance(session, studentsByClassId));
|
||||||
@@ -285,8 +284,16 @@ const getAllSessions = async (queryParams) => {
|
|||||||
return { data: sessions, meta };
|
return { data: sessions, meta };
|
||||||
}
|
}
|
||||||
|
|
||||||
const totalCount = await Session.countDocuments(filter);
|
const effectiveSort = { ...sort };
|
||||||
sessions = sessions.slice(skip, skip + limit);
|
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);
|
const meta = calculateMeta(totalCount, page, limit);
|
||||||
return { data: sessions, meta };
|
return { data: sessions, meta };
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user