-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add objective contributors create/remove functions
Add create and remove functions for the new objective contributors collection.
- Loading branch information
Showing
4 changed files
with
80 additions
and
1 deletion.
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
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,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 }; |
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,3 @@ | ||
import ObjectiveContributors from './ObjectiveContributors'; | ||
|
||
export default ObjectiveContributors; |
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,10 @@ | ||
export default { | ||
item: { | ||
type: 'reference', | ||
required: true, | ||
}, | ||
objective: { | ||
type: 'reference', | ||
required: false, | ||
}, | ||
}; |