Skip to content

Commit

Permalink
Fix notifications order
Browse files Browse the repository at this point in the history
Commit updates to state sorted by timestamp.
Fixes learningequality#11302
where "Completed" update was commited before "Started" update.
  • Loading branch information
MisRob committed Oct 9, 2023
1 parent 0001015 commit ad24679
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { NotificationObjects } from '../../../constants/notificationsConstants';
import { updateWithNotifications } from '../actions';

const { QUIZ } = NotificationObjects;

describe('classSummary/actions', () => {
describe('updateWithNotifications', () => {
it('commits quiz notifications updates in a correct order (sorted by their datetime)', () => {
const state = {
learnerMap: { 'user-id': {} },
examMap: { 'quiz-id': {} },
contentNodeMap: {},
lessonMap: {},
};
const commit = jest.fn();
const dispatch = jest.fn();

const quizNotifications = [
{
id: 1,
user_id: 'user-id',
object: QUIZ,
event: 'Completed',
timestamp: '2023-10-05T13:35:00+02:00',
quiz_num_correct: 1,
quiz_num_answered: 2,
quiz_id: 'quiz-id',
},
{
id: 2,
user_id: 'user-id',
object: QUIZ,
event: 'Started',
timestamp: '2023-10-05T13:30:00+02:00',
quiz_num_correct: 2,
quiz_num_answered: 3,
quiz_id: 'quiz-id',
},
];

updateWithNotifications({ state, commit, dispatch }, quizNotifications);

expect(commit).toHaveBeenCalledTimes(1);
expect(commit.mock.calls[0][0]).toBe('APPLY_NOTIFICATION_UPDATES');
expect(commit.mock.calls[0][1].examLearnerStatusMapUpdates).toEqual([
{
learner_id: 'user-id',
status: 'Started',
last_activity: new Date('2023-10-05T11:30:00.000Z'),
num_correct: 2,
num_answered: 3,
exam_id: 'quiz-id',
},
{
learner_id: 'user-id',
status: 'Completed',
last_activity: new Date('2023-10-05T11:35:00.000Z'),
num_correct: 1,
num_answered: 2,
exam_id: 'quiz-id',
},
]);
});
});
});
11 changes: 9 additions & 2 deletions kolibri/plugins/coach/assets/src/modules/classSummary/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ export function updateWithNotifications(store, notifications) {
const contentLearnerStatusMapUpdates = [];
const { learnerMap, examMap, contentNodeMap, lessonMap } = store.state;

for (const nIdx in notifications) {
const notification = notifications[nIdx];
if (!notifications || !notifications.length) {
return
}
const sortedNotifications = notifications.sort((n1, n2) => {
return new Date(n1.timestamp) - new Date(n2.timestamp);
});

for (const nIdx in sortedNotifications) {
const notification = sortedNotifications[nIdx];
const { object } = notification;

// Short-circuit the update if there are missing learners, exams, lessons,
Expand Down

0 comments on commit ad24679

Please sign in to comment.