Optimized get users with tag ID
MODIFY
src/server/rest/v1/service/UserService.ts
src/server/rest/v1/service/UserService.ts
@@ -40,6 +40,7 @@ import UserToken from '../../../../types/UserToken';
import UserValidator from '../validator/UserValidator';
import Utils from '../../../../utils/Utils';
import UtilsService from './UtilsService';
+import _ from 'lodash';
import csvToJson from 'csvtojson/v2';
import moment from 'moment';
@@ -776,6 +777,14 @@ export default class UserService {
if (!authorizationUsersFilters.authorized) {
return Constants.DB_EMPTY_DATA_RESULT;
}
+ // Get Tag IDs from Visual IDs
+ if (filteredRequest.VisualTagID) {
+ const tagIDs = await TagStorage.getTags(req.tenant.id, { visualIDs: filteredRequest.VisualTagID.split('|') }, Constants.DB_PARAMS_MAX_LIMIT, ['userID']);
+ if (!Utils.isEmptyArray(tagIDs.result)) {
+ const userIDs = _.uniq(tagIDs.result.map((tag) => tag.userID));
+ filteredRequest.UserID = userIDs.join('|');
+ }
+ }
// Get users
const users = await UserStorage.getUsers(req.user.tenantID,
{
@@ -783,7 +792,6 @@ export default class UserService {
issuer: Utils.isBoolean(filteredRequest.Issuer) || filteredRequest.Issuer ? Utils.convertToBoolean(filteredRequest.Issuer) : null,
siteIDs: (filteredRequest.SiteID ? filteredRequest.SiteID.split('|') : null),
userIDs: (filteredRequest.UserID ? filteredRequest.UserID.split('|') : null),
- visualTagIDs: (filteredRequest.VisualTagID ? filteredRequest.VisualTagID.split('|') : null),
roles: (filteredRequest.Role ? filteredRequest.Role.split('|') : null),
statuses: (filteredRequest.Status ? filteredRequest.Status.split('|') : null),
excludeSiteID: filteredRequest.ExcludeSiteID,

MODIFY
src/storage/mongodb/UserStorage.ts
src/storage/mongodb/UserStorage.ts
@@ -108,12 +108,11 @@ export default class UserStorage {
}
public static async getUser(tenantID: string, id: string = Constants.UNKNOWN_OBJECT_ID,
- params: { withImage?: boolean; siteIDs?: string[]; tagIDs?: string[]; } = {}, projectFields?: string[]): Promise<User> {
+ params: { withImage?: boolean; siteIDs?: string[]; } = {}, projectFields?: string[]): Promise<User> {
const userMDB = await UserStorage.getUsers(tenantID, {
userIDs: [id],
withImage: params.withImage,
siteIDs: params.siteIDs,
- tagIDs: params.tagIDs
}, Constants.DB_PARAMS_SINGLE_RECORD, projectFields);
return userMDB.count === 1 ? userMDB.result[0] : null;
}
@@ -540,7 +539,7 @@ export default class UserStorage {
notificationsActive?: boolean; siteIDs?: string[]; excludeSiteID?: string; search?: string;
userIDs?: string[]; email?: string; issuer?: boolean; passwordResetHash?: string; roles?: string[];
statuses?: string[]; withImage?: boolean; billingUserID?: string; notSynchronizedBillingData?: boolean;
- withTestBillingData?: boolean; notifications?: any; noLoginSince?: Date; visualTagIDs?: string[]; tagIDs?: string[];
+ withTestBillingData?: boolean; notifications?: any; noLoginSince?: Date;
},
dbParams: DbParams, projectFields?: string[]): Promise<DataResult<User>> {
// Debug
@@ -628,22 +627,6 @@ export default class UserStorage {
aggregation.push({
$match: filters
});
- // Add Tags
- if (!Utils.isEmptyArray(params.visualTagIDs) || !Utils.isEmptyArray(params.tagIDs)) {
- DatabaseUtils.pushTagLookupInAggregation({
- tenantID, aggregation, localField: '_id', foreignField: 'userID', asField: 'tag'
- });
- if (!Utils.isEmptyArray(params.tagIDs)) {
- aggregation.push({
- $match: { 'tag.id': { $in: params.tagIDs } }
- });
- }
- if (!Utils.isEmptyArray(params.visualTagIDs)) {
- aggregation.push({
- $match: { 'tag.visualID': { $in: params.visualTagIDs } }
- });
- }
- }
// Add Site
if (params.siteIDs || params.excludeSiteID) {
DatabaseUtils.pushSiteUserLookupInAggregation({

Optimized get users with tag ID