Changes to handle site id in logging
MODIFY
src/server/rest/v1/service/LoggingService.ts
src/server/rest/v1/service/LoggingService.ts
@@ -103,27 +103,7 @@ export default class LoggingService {
// Filter
const filteredRequest = LoggingSecurity.filterLogsRequest(req.query);
// Add filter for Site Admins
- if (Utils.isComponentActiveFromToken(req.user, TenantComponents.ORGANIZATION) && Authorizations.isSiteAdmin(req.user)) {
- // Optimization: Retrieve Charging Stations to get the logs only for the Site Admin user
- const chargingStations = await ChargingStationStorage.getChargingStations(req.tenant,
- { siteIDs: req.user.sitesAdmin, withSiteArea: true }, Constants.DB_PARAMS_MAX_LIMIT);
- // Check if Charging Station is already filtered
- if (chargingStations.count === 0) {
- filteredRequest.Source = '';
- } else if (filteredRequest.Source && filteredRequest.Source.length > 0) {
- // Filter only Site Admin Chargers
- const sources = [];
- for (const chargingStation of chargingStations.result) {
- if (filteredRequest.Source.includes(chargingStation.id)) {
- sources.push(chargingStation.id);
- }
- }
- filteredRequest.Source = sources.join('|');
- } else {
- // Add all Site Admin Chargers in filter
- filteredRequest.Source = chargingStations.result.map((chargingStation) => chargingStation.id).join('|');
- }
- }
+
// Get logs
const loggings = await LoggingStorage.getLogs(req.user.tenantID, {
search: filteredRequest.Search,
@@ -133,6 +113,7 @@ export default class LoggingService {
hosts: filteredRequest.Host ? filteredRequest.Host.split('|') : null,
levels: filteredRequest.Level ? filteredRequest.Level.split('|') : null,
type: filteredRequest.Type,
+ siteIDs: req.user.sites,
sources: filteredRequest.Source ? filteredRequest.Source.split('|') : null,
actions: filteredRequest.Action ? filteredRequest.Action.split('|') : null,
}, {

MODIFY
src/storage/mongodb/LoggingStorage.ts
src/storage/mongodb/LoggingStorage.ts
@@ -59,6 +59,7 @@ export default class LoggingStorage {
userID: logToSave.user ? DatabaseUtils.convertUserToObjectID(logToSave.user) : null,
actionOnUserID: DatabaseUtils.convertUserToObjectID(logToSave.actionOnUser),
level: logToSave.level,
+ siteID: logToSave.siteID,
source: logToSave.source,
host: logToSave.host ? logToSave.host : Utils.getHostname(),
process: logToSave.process ? logToSave.process : (cluster.isWorker ? 'worker ' + cluster.worker.id.toString() : 'master'),
@@ -85,7 +86,7 @@ export default class LoggingStorage {
}
public static async getLogs(tenantID: string, params: {
- startDateTime?: Date; endDateTime?: Date; levels?: string[]; sources?: string[]; sitesIDs?: string[]; type?: string; actions?: string[];
+ startDateTime?: Date; endDateTime?: Date; levels?: string[]; sources?: string[]; siteIDs?: string[]; type?: string; actions?: string[];
hosts?: string[]; userIDs?: string[]; search?: string; logIDs?: string[];
} = {}, dbParams: DbParams, projectFields: string[]): Promise<DataResult<Log>> {
// Check Tenant
@@ -119,13 +120,13 @@ export default class LoggingStorage {
filters.level = { $in: params.levels };
}
// Filter on site ID
- if (params.sitesIDs && params.sitesIDs.length > 0) {
- filters.siteID = { $in: params.sitesIDs };
+ if (params.siteIDs && params.siteIDs.length > 0) {
+ filters.siteID = { $in: params.siteIDs };
}
// Filter on charging Stations
- // if (params.sources && params.sources.length > 0) {
- // filters.source = { $in: params.sources };
- // }
+ if (params.sources && params.sources.length > 0) {
+ filters.source = { $in: params.sources };
+ }
// Type
if (params.type) {
filters.type = params.type;

Changes to handle site id in logging