From eb78cb64683c2dbeb0b68c5b713c78098c160634 Mon Sep 17 00:00:00 2001 From: Naor Biton Date: Mon, 24 Jun 2019 16:55:50 +0800 Subject: [PATCH] feat: sorting ALL user lists by role primary, alphabetical secondary (#841) --- imports/api/unit-roles-data.js | 17 +++++++++---- imports/api/units.js | 44 +++++++++++++++++++++++++--------- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/imports/api/unit-roles-data.js b/imports/api/unit-roles-data.js index ff92a276..6d1abe6e 100644 --- a/imports/api/unit-roles-data.js +++ b/imports/api/unit-roles-data.js @@ -15,23 +15,30 @@ import { logger } from '../util/logger' import { getIncrementFor } from './increment-counters' export const collectionName = 'unitRolesData' +export const roleEnum = { + TENANT: 'Tenant', + OWNER_LANDLORD: 'Owner/Landlord', + CONTRACTOR: 'Contractor', + MGT_COMPANY: 'Management Company', + AGENT: 'Agent' +} export const possibleRoles = [ { - name: 'Tenant', + name: roleEnum.TENANT, canBeOccupant: true }, { - name: 'Owner/Landlord', + name: roleEnum.OWNER_LANDLORD, canBeOccupant: true }, { - name: 'Contractor' + name: roleEnum.CONTRACTOR }, { - name: 'Management Company' + name: roleEnum.MGT_COMPANY }, { - name: 'Agent' + name: roleEnum.AGENT } ] diff --git a/imports/api/units.js b/imports/api/units.js index cd32e62b..af337657 100644 --- a/imports/api/units.js +++ b/imports/api/units.js @@ -7,7 +7,7 @@ import _ from 'lodash' import publicationFactory from './base/rest-resource-factory' import { makeAssociationFactory, withUsers, withDocs } from './base/associations-helper' import UnitMetaData, { unitTypes, collectionName as unitMetaCollName } from './unit-meta-data' -import UnitRolesData, { possibleRoles, collectionName as unitRolesCollName } from './unit-roles-data' +import UnitRolesData, { possibleRoles, roleEnum, collectionName as unitRolesCollName } from './unit-roles-data' import PendingInvitations, { REPLACE_DEFAULT, collectionName as pendingInvitationsCollName } from './pending-invitations' import { callAPI } from '../util/bugzilla-api' import { logger } from '../util/logger' @@ -26,14 +26,22 @@ export const factoryOptions = { export let serverHelpers export const defaultRoleVisibility = { - 'Tenant': true, - 'Owner/Landlord': true, - 'Contractor': true, - 'Management Company': true, - 'Agent': true, + [roleEnum.TENANT]: true, + [roleEnum.OWNER_LANDLORD]: true, + [roleEnum.CONTRACTOR]: true, + [roleEnum.MGT_COMPANY]: true, + [roleEnum.AGENT]: true, 'Occupant': true } +const roleSortOrder = [ + roleEnum.TENANT, + roleEnum.OWNER_LANDLORD, + roleEnum.AGENT, + roleEnum.MGT_COMPANY, + roleEnum.CONTRACTOR +] + if (Meteor.isServer) { serverHelpers = { getAPIUnitByName (unitName, apiKey) { @@ -76,7 +84,11 @@ const withRolesData = unitAssocHelper(UnitRolesData, unitRolesCollName, 'unitBzI export const getUnitRoles = (unit, userId) => { // Resolving roles via the newer mongo collection - let roleDocs = UnitRolesData.find({ unitBzId: unit.id }).fetch() + let roleDocs = UnitRolesData.find({ unitBzId: unit.id }).fetch().sort((a, b) => { + const aInd = roleSortOrder.indexOf(a.roleType) + const bInd = roleSortOrder.indexOf(b.roleType) + return aInd - bInd + }) const unitMeta = UnitMetaData.findOne({ bzId: unit.id }) @@ -100,19 +112,21 @@ export const getUnitRoles = (unit, userId) => { const userIds = roleDocs.reduce((all, roleObj) => all.concat( roleObj.members.reduce((mems, mem) => memberVisCheck(mem, roleObj.roleType) ? mems.concat([mem.id]) : mems, []) ), []) - const userDocs = Meteor.users.find({ _id: { $in: userIds } }).fetch() + // Creating a comparator for alphabetical name sort + const nameComparator = new Intl.Collator('en').compare + // Constructing the user role objects array similar to the way it is done from BZ's product components below const roleUsers = roleDocs.reduce((all, roleObj) => { - roleObj.members.forEach(memberDesc => { + const users = roleObj.members.reduce((allUsers, memberDesc) => { if (memberVisCheck(memberDesc, roleObj.roleType)) { // Using the prefetched array to find the user doc const user = userDocs.find(doc => doc._id === memberDesc.id) // Checking in case the role is visible, but the user is not (on the client) if (user) { - all.push({ + allUsers.push({ userId: user._id, login: user.bugzillaCreds.login, email: user.emails[0].address, @@ -124,7 +138,15 @@ export const getUnitRoles = (unit, userId) => { }) } } - }) + return allUsers + }, []) + + // Sorting members alphabetically + all = all.concat(users.sort((a, b) => { + const aName = a.name || (a.login && a.login.split('@')[0]) + const bName = b.name || (b.login && b.login.split('@')[0]) + return nameComparator(aName, bName) + })) return all }, [])