Skip to content

Commit

Permalink
Sync objective contributors on objective lift
Browse files Browse the repository at this point in the history
Synchronize the objective contributor links when an objective is
lifted, as the links may be needed to keep the donor as a contributor
after the lift.
  • Loading branch information
simenheg committed Nov 13, 2023
1 parent 237abf9 commit f1cff29
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 104 deletions.
111 changes: 8 additions & 103 deletions src/components/drawers/EditKeyResult.vue
Original file line number Diff line number Diff line change
Expand Up @@ -163,17 +163,14 @@

<script>
import { mapState, mapGetters } from 'vuex';
import { db } from '@/config/firebaseConfig';
import KeyResult from '@/db/KeyResult';
import ObjectiveContributors from '@/db/ObjectiveContributors';
import { PktButton } from '@oslokommune/punkt-vue2';
import { db } from '@/config/firebaseConfig';
import { FormSection, BtnSave, BtnDelete } from '@/components/generic/form';
import { isDepartment, isOrganization } from '@/util/getActiveItemType';
import ArchivedRestore from '@/components/ArchivedRestore.vue';
import KeyResult from '@/db/KeyResult';
import PagedDrawerWrapper from '@/components/drawers/PagedDrawerWrapper.vue';
import getActiveItemType, {
isDepartment,
isOrganization,
} from '@/util/getActiveItemType';
import syncObjectiveContributors from '@/util/objectiveContributors';
export default {
name: 'EditKeyResult',
Expand Down Expand Up @@ -332,6 +329,7 @@ export default {
methods: {
isDepartment,
isOrganization,
syncObjectiveContributors,
memberOfLevel(level) {
return level.team.map(({ id }) => id).includes(this.user.id);
Expand Down Expand Up @@ -374,7 +372,7 @@ export default {
this.thisKeyResult.objective = objectiveRef;
this.$emit('create', this.thisKeyResult);
}
await this.syncObjectiveContributor();
await syncObjectiveContributors(this.objective.id);
this.$refs.drawer.next();
} catch (error) {
console.log(error);
Expand All @@ -390,7 +388,7 @@ export default {
this.loading = true;
try {
await KeyResult.archive(this.thisKeyResult.id);
await this.syncObjectiveContributor();
await syncObjectiveContributors(this.objective.id);
this.thisKeyResult.archived = true;
this.$emit('archive', this.thisKeyResult);
} catch (error) {
Expand All @@ -404,7 +402,7 @@ export default {
async restore() {
try {
await KeyResult.restore(this.thisKeyResult.id);
await this.syncObjectiveContributor();
await syncObjectiveContributors(this.objective.id);
this.thisKeyResult.archived = false;
this.$emit('restore', this.thisKeyResult);
} catch {
Expand All @@ -414,99 +412,6 @@ export default {
}
},
async getObjectiveContributors() {
const objectiveRef = await db.doc(`objectives/${this.objective.id}`);
const objectiveContributors = await db
.collection('objectiveContributors')
.where('objective', '==', objectiveRef)
.where('archived', '==', false)
.get()
.then((snapshot) => snapshot.docs)
.then((docs) => docs.map((d) => d.data()));
const contributors = await Promise.all(
objectiveContributors.map(async (con) => {
return {
ref: await con.item.get(),
name: await con.item.get().then((snapshot) => snapshot.data().name),
};
})
);
return contributors;
},
async getKeyResultOwners() {
const objectiveRef = await db.doc(`objectives/${this.objective.id}`);
const keyResults = await db
.collection('keyResults')
.where('objective', '==', objectiveRef)
.where('archived', '==', false)
.get()
.then((snapshot) => snapshot.docs)
.then((docs) => docs.map((d) => d.data()));
const keyResultNames = await Promise.all(
keyResults.map(async (owner) => {
return {
ref: await owner.parent.get(),
name: await owner.parent.get().then((snapshot) => snapshot.data().name),
};
})
);
return keyResultNames;
},
async syncObjectiveContributor() {
const keyResultOwners = await this.getKeyResultOwners();
const contributors = await this.getObjectiveContributors();
let redundantContributors = [...contributors];
let keyResWithNoContributor = [...keyResultOwners];
// Filter out already present links
contributors.forEach((c) => {
keyResultOwners.forEach((k) => {
if (c.name === k.name) {
redundantContributors = redundantContributors.filter(
(con) => con.name !== c.name
);
keyResWithNoContributor = keyResWithNoContributor.filter(
(kr) => kr.name !== k.name
);
}
});
});
// We only need one contributor element per unique keyRes parent (here mapped by name)
const uniqueKeyResWithNoContributor = keyResWithNoContributor.filter(
(value, index, self) => index === self.findIndex((t) => t.name === value.name)
);
// Add missing contributor
uniqueKeyResWithNoContributor.forEach((k) => {
this.createObjectiveContributor(k.ref);
});
// Remove redundant contributors
redundantContributors.forEach((c) => {
this.removeObjectiveContributor(c.ref);
});
},
createObjectiveContributor(item) {
const itemType = getActiveItemType(item.data());
const itemRef = db.doc(`${itemType}s/${item.id}`);
const objectiveRef = db.doc(`objectives/${this.objective.id}`);
ObjectiveContributors.create(itemRef, objectiveRef);
},
async removeObjectiveContributor(item) {
const itemType = getActiveItemType(item.data());
const itemRef = db.doc(`${itemType}s/${item.id}`);
const objectiveRef = db.doc(`objectives/${this.objective.id}`);
return ObjectiveContributors.remove(itemRef, objectiveRef);
},
close(e) {
this.$emit('close', e);
},
Expand Down
8 changes: 7 additions & 1 deletion src/components/drawers/EditObjective.vue
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ import { FormSection, BtnSave, BtnDelete, BtnCancel } from '@/components/generic
import ArchivedRestore from '@/components/ArchivedRestore.vue';
import PagedDrawerWrapper from '@/components/drawers/PagedDrawerWrapper.vue';
import PeriodShortcut from '@/components/period/PeriodShortcut.vue';
import syncObjectiveContributors from '@/util/objectiveContributors';
export default {
name: 'EditObjective',
Expand Down Expand Up @@ -331,6 +332,7 @@ export default {
...mapActions('okrs', ['setActiveObjective']),
formattedPeriod,
syncObjectiveContributors,
getCurrentDateRange() {
if (this.thisObjective.startDate && this.thisObjective.endDate) {
Expand Down Expand Up @@ -368,7 +370,8 @@ export default {
} else {
data.period = period;
}
if (this.hasNewOwner) {
const hasNewOwner = this.hasNewOwner;
if (hasNewOwner) {
data.parent = this.parentRef;
if (!this.hasSelfContributor) {
this.setActiveObjective(null);
Expand All @@ -377,6 +380,9 @@ export default {
this.lifted = true;
}
await Objective.update(this.thisObjective.id, data);
if (hasNewOwner) {
await syncObjectiveContributors(this.thisObjective.id);
}
await this.$emit('update', this.thisObjective);
} else {
const { id } = await Objective.create({
Expand Down
98 changes: 98 additions & 0 deletions src/util/objectiveContributors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { db } from '@/config/firebaseConfig';
import getActiveItemType from '@/util/getActiveItemType';
import ObjectiveContributors from '@/db/ObjectiveContributors';

async function getKeyResultOwners(objectiveId) {
const objectiveRef = await db.doc(`objectives/${objectiveId}`);
const keyResults = await db
.collection('keyResults')
.where('objective', '==', objectiveRef)
.where('archived', '==', false)
.get()
.then((snapshot) => snapshot.docs)
.then((docs) => docs.map((d) => d.data()));

const keyResultNames = await Promise.all(
keyResults.map(async (owner) => {
return {
ref: await owner.parent.get(),
name: await owner.parent.get().then((snapshot) => snapshot.data().name),
};
})
);
return keyResultNames;
}

async function getObjectiveContributors(objectiveId) {
const objectiveRef = await db.doc(`objectives/${objectiveId}`);
const objectiveContributors = await db
.collection('objectiveContributors')
.where('objective', '==', objectiveRef)
.where('archived', '==', false)
.get()
.then((snapshot) => snapshot.docs)
.then((docs) => docs.map((d) => d.data()));

const contributors = await Promise.all(
objectiveContributors.map(async (con) => {
return {
ref: await con.item.get(),
name: await con.item.get().then((snapshot) => snapshot.data().name),
};
})
);
return contributors;
}

function createObjectiveContributor(item, objectiveId) {
const itemType = getActiveItemType(item.data());
const itemRef = db.doc(`${itemType}s/${item.id}`);
const objectiveRef = db.doc(`objectives/${objectiveId}`);
ObjectiveContributors.create(itemRef, objectiveRef);
}

function removeObjectiveContributor(item, objectiveId) {
const itemType = getActiveItemType(item.data());
const itemRef = db.doc(`${itemType}s/${item.id}`);
const objectiveRef = db.doc(`objectives/${objectiveId}`);
return ObjectiveContributors.remove(itemRef, objectiveRef);
}

async function syncObjectiveContributors(objectiveId) {
const keyResultOwners = await getKeyResultOwners(objectiveId);
const contributors = await getObjectiveContributors(objectiveId);

let redundantContributors = [...contributors];
let keyResWithNoContributor = [...keyResultOwners];

// Filter out already present links
contributors.forEach((c) => {
keyResultOwners.forEach((k) => {
if (c.name === k.name) {
redundantContributors = redundantContributors.filter(
(con) => con.name !== c.name
);
keyResWithNoContributor = keyResWithNoContributor.filter(
(kr) => kr.name !== k.name
);
}
});
});

// We only need one contributor element per unique keyRes parent (here mapped by name)
const uniqueKeyResWithNoContributor = keyResWithNoContributor.filter(
(value, index, self) => index === self.findIndex((t) => t.name === value.name)
);

// Add missing contributor
uniqueKeyResWithNoContributor.forEach((k) => {
createObjectiveContributor(k.ref, objectiveId);
});

// Remove redundant contributors
redundantContributors.forEach((c) => {
removeObjectiveContributor(c.ref, objectiveId);
});
}

export default syncObjectiveContributors;

0 comments on commit f1cff29

Please sign in to comment.