Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
update user progress for topics
Browse files Browse the repository at this point in the history
  • Loading branch information
ceolinwill committed Jun 29, 2020
1 parent 9490d2e commit 0c24e0c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
31 changes: 31 additions & 0 deletions functions/src/progress/onWrite/__tests__/updateTopic.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import functions from 'firebase-functions-test';
import * as admin from 'firebase-admin';

const testEnv = functions();
const db = admin.firestore();

import { onWriteProgressUpdateTopic } from '../updateTopic';

beforeAll(() => {
const chapterData = { topics: ['topicId'] };
spyOn(db.doc(''), 'set').and.returnValue('updated');
spyOn(db.doc(''), 'get').and.returnValue({ data: () => chapterData });
});

test('update the lessons count for a topic', async (done) => {
const data = { examples: new Array(2), lessons: new Array(4) };
const change = {
before: { data: () => undefined },
after: { data: () => data },
};
const params = { chapterId: '123', userId: 'user' };
const wrapped = testEnv.wrap(onWriteProgressUpdateTopic);
const req = await wrapped(change, { params });
const payload = { 123: { examples: 2, lessons: 4, posts: 6 } };

expect(req).toBe('updated');
expect(db.doc).toHaveBeenCalledWith('topics/topicId/progress/user');
expect(db.doc).toHaveBeenCalledWith('chapters/123');
expect(db.doc('').set).toHaveBeenCalledWith(payload, { merge: true });
done();
});
1 change: 1 addition & 0 deletions functions/src/progress/onWrite/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './earnXp';
export * from './updateTopic';
35 changes: 35 additions & 0 deletions functions/src/progress/onWrite/updateTopic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import { Chapter, ChapterProgress, TopicProgress } from '@zoonk/models';

const db = admin.firestore();

/**
* When a chapter progress is updated, we also need to update
* its topic progress. This makes easier to analyze a topic
* progress in the frontend (avoiding the need to have multiple
* calls - one for each chapter - to get the topic progress).
*/
export const onWriteProgressUpdateTopic = functions.firestore
.document('chapters/{chapterId}/progress/{userId}')
.onWrite(async (change, context) => {
const { chapterId, userId } = context.params;
const after = change.after.data() as ChapterProgress.Response | undefined;
const chapter = await db.doc(`chapters/${chapterId}`).get();
const chapterData = chapter.data() as Chapter.Response;
const topicId = chapterData.topics[0];
const examples = after?.examples?.length || 0;
const lessons = after?.lessons?.length || 0;

const payload: TopicProgress = {
[chapterId]: {
examples,
lessons,
posts: examples + lessons,
},
};

return db
.doc(`topics/${topicId}/progress/${userId}`)
.set(payload, { merge: true });
});
4 changes: 4 additions & 0 deletions src/models/progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ export namespace ChapterProgress {
lessons?: string[];
}
}

export interface TopicProgress {
[chapterId: string]: { examples: number; lessons: number; posts: number };
}

0 comments on commit 0c24e0c

Please sign in to comment.