renamed userSites / siteUsers to fit return type
MODIFY
src/authorization/Authorizations.ts
src/authorization/Authorizations.ts
@@ -132,9 +132,9 @@ export default class Authorizations {
const siteAdminIDs = [];
const siteOwnerIDs = [];
// Get User's site
- const sites = (await UserStorage.getUserSites(tenant, { userIDs: [user.id] },
+ const siteUsers = (await UserStorage.getSiteUsers(tenant, { userIDs: [user.id] },
Constants.DB_PARAMS_MAX_LIMIT)).result;
- for (const siteUser of sites) {
+ for (const siteUser of siteUsers) {
if (!Authorizations.isAdmin(user)) {
siteIDs.push(siteUser.site.id);
if (siteUser.siteAdmin) {

MODIFY
src/authorization/dynamic-data-source/SitesAdminDynamicAuthorizationDataSource.ts
src/authorization/dynamic-data-source/SitesAdminDynamicAuthorizationDataSource.ts
@@ -21,13 +21,13 @@ export default class SitesAdminDynamicAuthorizationDataSource
private async getSitesAdminSiteIDs(): Promise<string[]> {
// Get the Site IDs of the Sites for which the user is Site Admin
- const sites = await UserStorage.getUserSites(this.tenant,
+ const siteUsers = await UserStorage.getSiteUsers(this.tenant,
{
userIDs: [this.userToken.id],
siteAdmin: true
}, Constants.DB_PARAMS_MAX_LIMIT,
['siteID']
);
- return sites.result.map((userSite) => userSite.siteID);
+ return siteUsers.result.map((siteUser) => siteUser.siteID);
}
}

MODIFY
src/authorization/dynamic-data-source/SitesOwnerDynamicAuthorizationDataSource.ts
src/authorization/dynamic-data-source/SitesOwnerDynamicAuthorizationDataSource.ts
@@ -21,13 +21,13 @@ export default class SitesOwnerDynamicAuthorizationDataSource
private async getSitesOwnerSiteIDs(): Promise<string[]> {
// Get the Site IDs of the Sites for which the user is Site Owner
- const sites = await UserStorage.getUserSites(this.tenant,
+ const siteUsers = await UserStorage.getSiteUsers(this.tenant,
{
userIDs: [this.userToken.id],
siteOwner: true
}, Constants.DB_PARAMS_MAX_LIMIT,
['siteID']
);
- return sites.result.map((userSite) => userSite.siteID);
+ return siteUsers.result.map((siteUser) => siteUser.siteID);
}
}

MODIFY
src/scheduler/tasks/CheckPreparingSessionNotStartedTask.ts
src/scheduler/tasks/CheckPreparingSessionNotStartedTask.ts
@@ -29,7 +29,7 @@ export default class CheckPreparingSessionNotStartedTask extends TenantScheduler
// Get site owner and then send notification
if (chargingStation.siteArea && chargingStation.siteArea.siteID) {
// Get Site Owners
- const siteOwners = await SiteStorage.getSiteUsers(tenant,
+ const siteOwners = await SiteStorage.getUserSites(tenant,
{ siteIDs: [ chargingStation.siteArea.siteID ], siteOwnerOnly: true }, Constants.DB_PARAMS_MAX_LIMIT);
if (siteOwners && siteOwners.count > 0) {
// Send notification

MODIFY
src/server/rest/v1/service/SiteService.ts
src/server/rest/v1/service/SiteService.ts
@@ -130,7 +130,7 @@ export default class SiteService {
return;
}
// Get Users
- const users = await SiteStorage.getSiteUsers(req.tenant,
+ const userSites = await SiteStorage.getUserSites(req.tenant,
{
search: filteredRequest.Search,
siteIDs: [filteredRequest.SiteID],
@@ -144,9 +144,16 @@ export default class SiteService {
},
authorizations.projectFields
);
+ // Filter
+ userSites.result = userSites.result.map((userSite) => ({
+ siteID: userSite.siteID,
+ siteAdmin: userSite.siteAdmin,
+ siteOwner: userSite.siteOwner,
+ user: userSite.user
+ }));
// add user auth
- await AuthorizationService.addUserSitesAuthorizations(req.tenant, req.user, users as UserSiteDataResult, authorizations);
- res.json(users);
+ await AuthorizationService.addUserSitesAuthorizations(req.tenant, req.user, userSites as UserSiteDataResult, authorizations);
+ res.json(userSites);
next();
}

MODIFY
src/server/rest/v1/service/UserService.ts
src/server/rest/v1/service/UserService.ts
@@ -3,7 +3,7 @@ import { ActionsResponse, ImportStatus } from '../../../../types/GlobalType';
import { AsyncTaskType, AsyncTasks } from '../../../../types/AsyncTask';
import Busboy, { FileInfo } from 'busboy';
import { Car, CarType } from '../../../../types/Car';
-import { DataResult, SiteUserDataResult, UserDataResult } from '../../../../types/DataResult';
+import { DataResult, SiteDataResult, SiteUserDataResult, UserDataResult } from '../../../../types/DataResult';
import { HTTPAuthError, HTTPError } from '../../../../types/HTTPError';
import { NextFunction, Request, Response } from 'express';
import Tenant, { TenantComponents } from '../../../../types/Tenant';
@@ -341,12 +341,14 @@ export default class UserService {
// Check dynamic auth for reading Sites
const authorizations = await AuthorizationService.checkAndGetUserSitesAuthorizations(req.tenant,
req.user, filteredRequest);
+ // const authorizations = await AuthorizationService.checkAndGetSitesAuthorizations(req.tenant,
+ // req.user, filteredRequest);
if (!authorizations.authorized) {
UtilsService.sendEmptyDataResult(res, next);
return;
}
// Get Sites
- const sites = await UserStorage.getUserSites(req.tenant,
+ const siteUsers = await UserStorage.getSiteUsers(req.tenant,
{
search: filteredRequest.Search,
userIDs: [filteredRequest.UserID],
@@ -361,15 +363,16 @@ export default class UserService {
authorizations.projectFields
);
// Filter
- sites.result = sites.result.map((userSite) => ({
- userID: userSite.userID,
- siteAdmin: userSite.siteAdmin,
- siteOwner: userSite.siteOwner,
- site: userSite.site
+ siteUsers.result = siteUsers.result.map((siteUser) => ({
+ userID: siteUser.userID,
+ siteAdmin: siteUser.siteAdmin,
+ siteOwner: siteUser.siteOwner,
+ site: siteUser.site
}));
// Add Auth flags
- await AuthorizationService.addSiteUsersAuthorizations(req.tenant, req.user, sites as SiteUserDataResult , authorizations);
- res.json(sites);
+ await AuthorizationService.addSiteUsersAuthorizations(req.tenant, req.user, siteUsers as SiteUserDataResult , authorizations);
+ // await AuthorizationService.addSiteAuthorizations(req.tenant, req.user, siteUsers.result. as SiteDataResult , authorizations);
+ res.json(siteUsers);
next();
}

MODIFY
src/storage/mongodb/SiteStorage.ts
src/storage/mongodb/SiteStorage.ts
@@ -96,7 +96,7 @@ export default class SiteStorage {
await Logging.traceDatabaseRequestEnd(tenant, MODULE_NAME, 'addUsersToSite', startTime, userIDs);
}
- public static async getSiteUsers(tenant: Tenant,
+ public static async getUserSites(tenant: Tenant,
params: { search?: string; siteIDs: string[]; siteOwnerOnly?: boolean },
dbParams: DbParams, projectFields?: string[]): Promise<DataResult<UserSite>> {
const startTime = Logging.traceDatabaseRequestStart();
@@ -155,14 +155,14 @@ export default class SiteStorage {
aggregation.push({ $limit: Constants.DB_RECORD_COUNT_CEIL });
}
// Count Records
- const usersCountMDB = await global.database.getCollection<any>(tenant.id, 'siteusers')
+ const userSitesCountMDB = await global.database.getCollection<any>(tenant.id, 'siteusers')
.aggregate([...aggregation, { $count: 'count' }], DatabaseUtils.buildAggregateOptions())
.toArray() as DatabaseCount[];
// Check if only the total count is requested
if (dbParams.onlyRecordCount) {
- await Logging.traceDatabaseRequestEnd(tenant, MODULE_NAME, 'getSitesUsers', startTime, aggregation, usersCountMDB);
+ await Logging.traceDatabaseRequestEnd(tenant, MODULE_NAME, 'getSitesUsers', startTime, aggregation, userSitesCountMDB);
return {
- count: (usersCountMDB.length > 0 ? usersCountMDB[0].count : 0),
+ count: (userSitesCountMDB.length > 0 ? userSitesCountMDB[0].count : 0),
result: []
};
}
@@ -191,13 +191,13 @@ export default class SiteStorage {
// Project
DatabaseUtils.projectFields(aggregation, projectFields);
// Read DB
- const siteUsersMDB = await global.database.getCollection<any>(tenant.id, 'siteusers')
+ const userSitesMDB = await global.database.getCollection<any>(tenant.id, 'siteusers')
.aggregate<any>(aggregation, DatabaseUtils.buildAggregateOptions())
.toArray() as UserSite[];
- await Logging.traceDatabaseRequestEnd(tenant, MODULE_NAME, 'getSitesUsers', startTime, aggregation, siteUsersMDB);
+ await Logging.traceDatabaseRequestEnd(tenant, MODULE_NAME, 'getSitesUsers', startTime, aggregation, userSitesCountMDB);
return {
- count: DatabaseUtils.getCountFromDatabaseCount(usersCountMDB[0]),
- result: siteUsersMDB
+ count: DatabaseUtils.getCountFromDatabaseCount(userSitesCountMDB[0]),
+ result: userSitesMDB
};
}
@@ -537,16 +537,16 @@ export default class SiteStorage {
await Logging.traceDatabaseRequestEnd(tenant, MODULE_NAME, 'deleteCompanySites', startTime, { companyID });
}
- public static async siteHasUser(tenant: Tenant, siteID: string, userID: string): Promise<boolean> {
- const startTime = Logging.traceDatabaseRequestStart();
- DatabaseUtils.checkTenantObject(tenant);
- // Exec
- const result = await global.database.getCollection<any>(tenant.id, 'siteusers').findOne(
- { siteID: DatabaseUtils.convertToObjectID(siteID), userID: DatabaseUtils.convertToObjectID(userID) });
- await Logging.traceDatabaseRequestEnd(tenant, MODULE_NAME, 'siteHasUser', startTime, { siteID });
- if (!result) {
- return false;
- }
- return true;
- }
+ // public static async siteHasUser(tenant: Tenant, siteID: string, userID: string): Promise<boolean> {
+ // const startTime = Logging.traceDatabaseRequestStart();
+ // DatabaseUtils.checkTenantObject(tenant);
+ // // Exec
+ // const result = await global.database.getCollection<any>(tenant.id, 'siteusers').findOne(
+ // { siteID: DatabaseUtils.convertToObjectID(siteID), userID: DatabaseUtils.convertToObjectID(userID) });
+ // await Logging.traceDatabaseRequestEnd(tenant, MODULE_NAME, 'siteHasUser', startTime, { siteID });
+ // if (!result) {
+ // return false;
+ // }
+ // return true;
+ // }
}

MODIFY
src/storage/mongodb/UserStorage.ts
src/storage/mongodb/UserStorage.ts
@@ -910,7 +910,7 @@ export default class UserStorage {
await Logging.traceDatabaseRequestEnd(tenant, MODULE_NAME, 'deleteUser', startTime, { id });
}
- public static async getUserSites(tenant: Tenant,
+ public static async getSiteUsers(tenant: Tenant,
params: { search?: string; userIDs: string[]; siteAdmin?: boolean; siteOwner?: boolean },
dbParams: DbParams, projectFields?: string[]): Promise<DataResult<SiteUser>> {
const startTime = Logging.traceDatabaseRequestStart();
@@ -960,14 +960,14 @@ export default class UserStorage {
aggregation.push({ $limit: Constants.DB_RECORD_COUNT_CEIL });
}
// Count Records
- const sitesCountMDB = await global.database.getCollection<any>(tenant.id, 'siteusers')
+ const siteUsersCountMDB = await global.database.getCollection<any>(tenant.id, 'siteusers')
.aggregate([...aggregation, { $count: 'count' }], DatabaseUtils.buildAggregateOptions())
.toArray() as DatabaseCount[];
// Check if only the total count is requested
if (dbParams.onlyRecordCount) {
- await Logging.traceDatabaseRequestEnd(tenant, MODULE_NAME, 'getUserSites', startTime, sitesCountMDB);
+ await Logging.traceDatabaseRequestEnd(tenant, MODULE_NAME, 'getUserSites', startTime, siteUsersCountMDB);
return {
- count: (!Utils.isEmptyArray(sitesCountMDB) ? sitesCountMDB[0].count : 0),
+ count: (!Utils.isEmptyArray(siteUsersCountMDB) ? siteUsersCountMDB[0].count : 0),
result: []
};
}
@@ -1001,7 +1001,7 @@ export default class UserStorage {
.toArray() as SiteUser[];
await Logging.traceDatabaseRequestEnd(tenant, MODULE_NAME, 'getUserSites', startTime, siteUsersMDB);
return {
- count: DatabaseUtils.getCountFromDatabaseCount(sitesCountMDB[0]),
+ count: DatabaseUtils.getCountFromDatabaseCount(siteUsersCountMDB[0]),
result: siteUsersMDB,
projectFields: projectFields
};

MODIFY
src/types/Authorization.ts
src/types/Authorization.ts
@@ -72,6 +72,7 @@ export enum Entity {
REPORT = 'Report',
USER = 'User',
USERS_SITES = 'UsersSites',
+ SITES_USERS = 'SiteUsers',
LOGGING = 'Logging',
PRICING = 'Pricing',
PRICING_DEFINITION = 'PricingDefinition',

renamed userSites / siteUsers to fit return type