From 962de576e732390dc20cc13a503c360925587f58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simen=20Heggest=C3=B8yl?= Date: Wed, 16 Aug 2023 08:48:40 +0200 Subject: [PATCH] Load external objectives in the timeline view Load external objectives (objectives that the current item are "contributors" to (as per a new `objectiveContributors` table)) in the timeline view. There is currently no way to create `objectiveContributors` entries in the web UI, but this lays the groundwork for it. --- firestore.rules | 7 +++++++ src/store/actions/set_active_period_and_data.js | 15 ++++++++++++++- src/store/index.js | 8 ++++++-- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/firestore.rules b/firestore.rules index 24b3ab06b..87b4622a2 100644 --- a/firestore.rules +++ b/firestore.rules @@ -147,6 +147,13 @@ service cloud.firestore { allow delete: if isSuperAdmin(); } + /* + * TODO: Needs to be extended with rules for `create` and `delete`. + */ + match /objectiveContributors/{document} { + allow read: if isSignedIn(); + } + match /periods/{document} { allow read: if isSignedIn(); allow create: if isSuperAdmin() || isMemberOfParent(document, 'periods') || isAdminOfParent(document, 'periods'); diff --git a/src/store/actions/set_active_period_and_data.js b/src/store/actions/set_active_period_and_data.js index 529704370..3eef24055 100644 --- a/src/store/actions/set_active_period_and_data.js +++ b/src/store/actions/set_active_period_and_data.js @@ -7,6 +7,7 @@ export default firestoreAction( if (!periodId && !item) { unbindFirestoreRef('periods'); unbindFirestoreRef('objectives'); + unbindFirestoreRef('objectiveContributors'); unbindFirestoreRef('activePeriod'); return false; } @@ -28,14 +29,26 @@ export default firestoreAction( .where('parent', '==', parentRef) .orderBy('name'); + const objectiveContributorsRef = db + .collection('objectiveContributors') + .where('item', '==', parentRef); + const activeObjectivesList = await objectivesRef .get() .then((snapshot) => snapshot.docs.map((doc) => doc.ref)); - if (activeObjectivesList.length) { + const objectiveContributorsList = await objectiveContributorsRef + .get() + .then((snapshot) => snapshot.docs.map((doc) => doc.ref)); + + if (activeObjectivesList.length || objectiveContributorsList.length) { await bindFirestoreRef('objectives', objectivesRef, { maxRefDepth: 1 }); + await bindFirestoreRef('objectiveContributors', objectiveContributorsRef, { + maxRefDepth: 1, + }); } else { unbindFirestoreRef('objectives'); + unbindFirestoreRef('objectiveContributors'); } return true; diff --git a/src/store/index.js b/src/store/index.js index 5e550a7b7..bb2438bca 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -144,10 +144,13 @@ export const storeGetters = { }, /** - * Return `state.objectives` enriched with ID. + * Return `state.objectives` and external objectives from + * `state.objectiveContributors` enriched with ID. */ objectivesWithID: (state) => { - return state.objectives.map((o) => ({ + const externalObjectives = state.objectiveContributors.map((oc) => oc.objective); + + return state.objectives.concat(externalObjectives).map((o) => ({ ...o, id: o.id, })); @@ -294,6 +297,7 @@ export default new Vuex.Store({ activeObjective: null, periods: [], objectives: [], + objectiveContributors: [], kpis: [], subKpis: [], loginError: null,