Skip to content

Commit

Permalink
Add objective contributors create/remove functions
Browse files Browse the repository at this point in the history
Add create and remove functions for the new objective contributors
collection.
  • Loading branch information
simenheg committed Sep 4, 2023
1 parent 8998cc9 commit 8dfddff
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
31 changes: 30 additions & 1 deletion firestore.rules
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,32 @@ service cloud.firestore {
return userIsMemberOfParent;
}

/**
* Return true if the current user is a member of the parent of the
* document's objective *before* performing the action.
*/
function isMemberOfObjectiveParentBefore(document, type) {
let userRef = /databases/$(database)/documents/users/$(request.auth.token.email);
let doc = get(/databases/$(database)/documents/$(type)/$(document));
let objectiveDoc = get(doc.data.objective);
let parentDoc = get(objectiveDoc.data.parent);
let userIsMemberOfParent = userRef in parentDoc.data.team;
return userIsMemberOfParent;
}
/**
* Return true if the current user is a member of the parent of the
* document's objective *after* performing the action.
*/
function isMemberOfObjectiveParentAfter(document, type) {
let userRef = /databases/$(database)/documents/users/$(request.auth.token.email);
let doc = getAfter(/databases/$(database)/documents/$(type)/$(document));
let objectiveDoc = getAfter(doc.data.objective);
let parentDoc = getAfter(objectiveDoc.data.parent);
let userIsMemberOfParent = userRef in parentDoc.data.team;
return userIsMemberOfParent;
}

function isSelf(document) {
let user = document == request.auth.token.email;
return user;
Expand Down Expand Up @@ -150,10 +176,13 @@ service cloud.firestore {
}

/*
* TODO: Needs to be extended with rules for `create` and `delete`.
* TODO: Should also allow create/delete by organization admins. This isn't
* currently working for other document types either.
*/
match /objectiveContributors/{document} {
allow read: if isSignedIn();
allow create: if isSuperAdmin() || isMemberOfObjectiveParentAfter(document, 'objectiveContributors');
allow delete: if isSuperAdmin() || isMemberOfObjectiveParentBefore(document, 'objectiveContributors');
}
match /periods/{document} {
Expand Down
37 changes: 37 additions & 0 deletions src/db/ObjectiveContributors/ObjectiveContributors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { db } from '@/config/firebaseConfig';
import props from './props';
import { createDocument, validateCreateProps } from '../common';

const collection = db.collection('objectiveContributors');

const create = async (itemRef, objectiveRef) => {
if (await itemRef.get().then(({ exists }) => !exists)) {
throw new Error(`Cannot find item with ID ${itemRef.id}`);
}
if (await objectiveRef.get().then(({ exists }) => !exists)) {
throw new Error(`Cannot find objcetive with ID ${objectiveRef.id}`);
}

const data = { item: itemRef, objective: objectiveRef };

validateCreateProps(props, data);

return createDocument(collection, data);
};

const remove = async (itemRef, objectiveRef) => {
collection
.where('item', '==', itemRef)
.where('objective', '==', objectiveRef)
.get()
.then((snapshot) => {
if (snapshot.docs.length < 1) {
throw new Error(
`ObjectiveContributors for item ${itemRef.id} and objective ${objectiveRef.id} not found; cannot delete`
);
}
snapshot.forEach((doc) => doc.ref.delete());
});
};

export default { create, remove };
3 changes: 3 additions & 0 deletions src/db/ObjectiveContributors/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import ObjectiveContributors from './ObjectiveContributors';

export default ObjectiveContributors;
10 changes: 10 additions & 0 deletions src/db/ObjectiveContributors/props.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
item: {
type: 'reference',
required: true,
},
objective: {
type: 'reference',
required: false,
},
};

0 comments on commit 8dfddff

Please sign in to comment.