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.
remove post from user progress when it is deleted
- Loading branch information
1 parent
0c24e0c
commit 66e357a
Showing
4 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
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
68 changes: 68 additions & 0 deletions
68
functions/src/posts/onDelete/__tests__/removeFromProgress.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,68 @@ | ||
import functions from 'firebase-functions-test'; | ||
import * as admin from 'firebase-admin'; | ||
|
||
const testEnv = functions(); | ||
const db = admin.firestore(); | ||
|
||
import { onDeletePostRemoveFromProgress } from '../removeFromProgress'; | ||
|
||
const post1 = { ref: { update: jest.fn().mockReturnValue('post1Ref') } }; | ||
const post2 = { ref: { update: jest.fn().mockReturnValue('post2Ref') } }; | ||
|
||
beforeAll(() => { | ||
spyOn(Promise, 'all').and.returnValue('removed'); | ||
}); | ||
|
||
test('return when there is no chapterId', async (done) => { | ||
const data = { chapterId: null }; | ||
const snap = { data: () => data }; | ||
const wrapped = testEnv.wrap(onDeletePostRemoveFromProgress); | ||
const req = await wrapped(snap); | ||
|
||
expect(req).toBe(false); | ||
expect(db.collectionGroup).not.toHaveBeenCalled(); | ||
expect(Promise.all).not.toHaveBeenCalled(); | ||
done(); | ||
}); | ||
|
||
test('remove examples', async (done) => { | ||
spyOn(db.collectionGroup('').where('', '==', ''), 'get').and.returnValue({ | ||
docs: [post1, post2], | ||
}); | ||
|
||
const data = { category: 'examples', chapterId: 'chapterId' }; | ||
const snap = { data: () => data, id: 'exampleId' }; | ||
const wrapped = testEnv.wrap(onDeletePostRemoveFromProgress); | ||
const req = await wrapped(snap); | ||
|
||
expect(req).toBe('removed'); | ||
expect(db.collectionGroup).toHaveBeenCalledWith('progress'); | ||
expect(db.collectionGroup('').where).toHaveBeenCalledWith( | ||
'examples', | ||
'array-contains', | ||
'exampleId', | ||
); | ||
expect(Promise.all).toHaveBeenCalledWith(['post1Ref', 'post2Ref']); | ||
done(); | ||
}); | ||
|
||
test('remove lessons', async (done) => { | ||
spyOn(db.collectionGroup('').where('', '==', ''), 'get').and.returnValue({ | ||
docs: [post1, post2], | ||
}); | ||
|
||
const data = { category: 'lessons', chapterId: 'chapterId' }; | ||
const snap = { data: () => data, id: 'lessonId' }; | ||
const wrapped = testEnv.wrap(onDeletePostRemoveFromProgress); | ||
const req = await wrapped(snap); | ||
|
||
expect(req).toBe('removed'); | ||
expect(db.collectionGroup).toHaveBeenCalledWith('progress'); | ||
expect(db.collectionGroup('').where).toHaveBeenCalledWith( | ||
'lessons', | ||
'array-contains', | ||
'lessonId', | ||
); | ||
expect(Promise.all).toHaveBeenCalledWith(['post1Ref', 'post2Ref']); | ||
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,2 +1,3 @@ | ||
export * from './addToActivity'; | ||
export * from './removeFromProgress'; | ||
export * from './removeFromTimeline'; |
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,33 @@ | ||
import * as functions from 'firebase-functions'; | ||
import * as admin from 'firebase-admin'; | ||
import { Post } from '@zoonk/models'; | ||
|
||
const db = admin.firestore(); | ||
|
||
/** | ||
* When an example or lesson is deleted, we should remove it | ||
* from the chapter progress. | ||
*/ | ||
export const onDeletePostRemoveFromProgress = functions.firestore | ||
.document('posts/{id}') | ||
.onDelete(async (snap) => { | ||
const { category, chapterId } = snap.data() as Post.Response; | ||
|
||
// We only track progress for chapter posts. | ||
if (!chapterId) return false; | ||
|
||
// Get all users who read this post. | ||
const users = await db | ||
.collectionGroup('progress') | ||
.where(category, 'array-contains', snap.id) | ||
.get(); | ||
|
||
// Remove this post from every user. | ||
const requests = users.docs.map((user) => { | ||
return user.ref.update({ | ||
[category]: admin.firestore.FieldValue.arrayRemove(snap.id), | ||
}); | ||
}); | ||
|
||
return Promise.all(requests); | ||
}); |