From 240ee34f0515580c13537ea1e8c480dbc41bf7eb Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Fri, 23 Sep 2022 10:23:49 +0200 Subject: Save UID and display name for managers of the org chart Signed-off-by: Christoph Wurst --- src/components/AppContent/ChartContent.vue | 9 ++++++--- .../ContactDetails/ContactDetailsProperty.vue | 20 ++++++++++++++++++-- src/models/contact.js | 19 ++++--------------- src/utils/chartUtils.js | 20 ++++++++------------ 4 files changed, 36 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/components/AppContent/ChartContent.vue b/src/components/AppContent/ChartContent.vue index b77f61a9..e9c3b82f 100644 --- a/src/components/AppContent/ChartContent.vue +++ b/src/components/AppContent/ChartContent.vue @@ -44,14 +44,17 @@ export default { prev.push(transformNode(contact)) const manager = contactsByUid[contact.addressbook.id][contact.managersName] - if (manager && !manager.managersName && !headManagers.includes(manager.uid)) { + if (manager && !manager.managersName && !headManagers.some(m => m.nodeId === manager.uid)) { prev.push(transformNode(manager)) - headManagers.push(manager.uid) + headManagers.push(transformNode(manager)) } return prev }, []) - return headManagers.map(uid => getChart(tempContacts, uid)) + const charts = headManagers.map(managerNode => getChart(tempContacts, managerNode)) + // Debugging logs to figure out why a graph might not show. Leave this in place until the logic is bulletproof + console.debug('Org charts', charts.map((nodes, index) => nodes.map(n => `list ${index} ${n.nodeId} (${n.fullName}) -> ${n.parentNodeId}`))) + return charts }, }, } diff --git a/src/components/ContactDetails/ContactDetailsProperty.vue b/src/components/ContactDetails/ContactDetailsProperty.vue index 2882853e..feb8c06c 100644 --- a/src/components/ContactDetails/ContactDetailsProperty.vue +++ b/src/components/ContactDetails/ContactDetailsProperty.vue @@ -230,7 +230,7 @@ export default { return { ...prev, [contact.uid]: { - id: contact.uid, + id: contact.key, name: contact.displayName, }, } @@ -333,6 +333,16 @@ export default { ? this.property.getValues()[0] : this.property.getValues() } + if (this.propName === 'x-managersname') { + if (this.property.getParameter('uid')) { + return this.property.getParameter('uid') + '~' + this.contact.addressbook.id + } + // Try to find the matching contact by display name + // TODO: this only *shows* the display name but doesn't assign the missing UID + const displayName = this.property.getFirstValue() + const other = this.otherContacts(this.contact).find(contact => contact.displayName === displayName) + return other?.key + } return this.property.getFirstValue() }, set(data) { @@ -342,7 +352,13 @@ export default { ? this.property.setValues([data]) : this.property.setValues(data) } else { - this.property.setValue(data) + if (this.propName === 'x-managersname') { + const manager = this.$store.getters.getContact(data) + this.property.setValue(manager.displayName) + this.property.setParameter('uid', manager.uid) + } else { + this.property.setValue(data) + } } this.updateContact() }, diff --git a/src/models/contact.js b/src/models/contact.js index f97c6b71..8c8bbdab 100644 --- a/src/models/contact.js +++ b/src/models/contact.js @@ -371,22 +371,11 @@ export default class Contact { * @memberof Contact */ get managersName() { - return this.firstIfArray(this.vCard.getFirstPropertyValue('x-managersname')) - } - - /** - * Set the x-managersname - * - * @param {string} managersName the x-managersname data - * @memberof Contact - */ - set managersName(managersName) { - // delete the org if empty - if (isEmpty(managersName)) { - this.vCard.removeProperty('x-managersname') - return + const prop = this.vCard.getFirstProperty('x-managersname') + if (!prop) { + return null } - this.vCard.updatePropertyWithValue('x-managersname', managersName) + return prop.getFirstParameter('uid') ?? null } /** diff --git a/src/utils/chartUtils.js b/src/utils/chartUtils.js index 66bc75a5..7d48a38a 100644 --- a/src/utils/chartUtils.js +++ b/src/utils/chartUtils.js @@ -1,20 +1,16 @@ import { generateUrl } from '@nextcloud/router' import { GROUP_ALL_CONTACTS } from '../models/constants.ts' -export const getChart = (list, parent, nodes = []) => { - if (!nodes.length) { - nodes.push(list.find(node => node.nodeId === parent)) - } - const children = list.filter(node => { - return node.parentNodeId === parent - }) - - children.forEach(node => { - nodes.push(node) - getChart(list, node.nodeId, nodes) +export const getChart = (allNodes, currentNode) => { + const result = [currentNode] + const children = allNodes.filter(node => { + return node.nodeId !== currentNode.nodeId && node.parentNodeId === currentNode.nodeId }) - return nodes + return [ + ...result, + ...children.flatMap(child => getChart(allNodes, child)), + ] } export const transformNode = (contact) => { -- cgit v1.2.3