This repository has been archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9490d2e
commit 0c24e0c
Showing
4 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
functions/src/progress/onWrite/__tests__/updateTopic.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './earnXp'; | ||
export * from './updateTopic'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters