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

Commit

Permalink
remove post from user progress when it is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
ceolinwill committed Jun 29, 2020
1 parent 0c24e0c commit 66e357a
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
44 changes: 44 additions & 0 deletions database/firestore.indexes.json
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,50 @@
}
]
},
{
"collectionGroup": "progress",
"fieldPath": "examples",
"indexes": [
{
"order": "ASCENDING",
"queryScope": "COLLECTION"
},
{
"order": "DESCENDING",
"queryScope": "COLLECTION"
},
{
"arrayConfig": "CONTAINS",
"queryScope": "COLLECTION"
},
{
"arrayConfig": "CONTAINS",
"queryScope": "COLLECTION_GROUP"
}
]
},
{
"collectionGroup": "progress",
"fieldPath": "lessons",
"indexes": [
{
"order": "ASCENDING",
"queryScope": "COLLECTION"
},
{
"order": "DESCENDING",
"queryScope": "COLLECTION"
},
{
"arrayConfig": "CONTAINS",
"queryScope": "COLLECTION"
},
{
"arrayConfig": "CONTAINS",
"queryScope": "COLLECTION_GROUP"
}
]
},
{
"collectionGroup": "timeline",
"fieldPath": "id",
Expand Down
68 changes: 68 additions & 0 deletions functions/src/posts/onDelete/__tests__/removeFromProgress.spec.ts
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();
});
1 change: 1 addition & 0 deletions functions/src/posts/onDelete/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './addToActivity';
export * from './removeFromProgress';
export * from './removeFromTimeline';
33 changes: 33 additions & 0 deletions functions/src/posts/onDelete/removeFromProgress.ts
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);
});

0 comments on commit 66e357a

Please sign in to comment.