Align endpoints:
Company service
Site service
MODIFY
src/server/rest/v1/service/CompanyService.ts
src/server/rest/v1/service/CompanyService.ts
@@ -48,10 +48,8 @@ export default class CompanyService {
// Filter
const filteredRequest = CompanyValidator.getInstance().validateCompanyGetReq(req.query);
// Check and Get Company
- const company = await UtilsService.checkAndGetCompanyAuthorization(
- req.tenant, req.user, filteredRequest.ID, Action.READ, action, null, {
- withLogo: true
- }, true);
+ const company = await UtilsService.checkAndGetCompanyAuthorization(req.tenant, req.user, filteredRequest.ID, Action.READ, action, null,
+ { withLogo: true }, true);
res.json(company);
next();
}

MODIFY
src/server/rest/v1/service/SiteService.ts
src/server/rest/v1/service/SiteService.ts
-import { Action, DynamicAuthorizationDataSourceName, Entity, SitesAdminDynamicAuthorizationDataSourceData } from '../../../../types/Authorization';
+import { Action, DynamicAuthorizationDataSourceName, Entity } from '../../../../types/Authorization';
import { HTTPAuthError, HTTPError } from '../../../../types/HTTPError';
import { NextFunction, Request, Response } from 'express';
@@ -9,6 +9,7 @@ import ChargingStationStorage from '../../../../storage/mongodb/ChargingStationS
import Constants from '../../../../utils/Constants';
import DynamicAuthorizationFactory from '../../../../authorization/DynamicAuthorizationFactory';
import Logging from '../../../../utils/Logging';
+import LoggingHelper from '../../../../utils/LoggingHelper';
import { ServerAction } from '../../../../types/Server';
import Site from '../../../../types/Site';
import { SiteDataResult } from '../../../../types/DataResult';
@@ -29,31 +30,6 @@ export default class SiteService {
Action.UPDATE, Entity.SITE, MODULE_NAME, 'handleUpdateSiteUserAdmin');
// Filter request
const filteredRequest = SiteValidator.getInstance().validateSiteAdminReq(req.body);
- // Check mandatory fields
- if (!filteredRequest.userID) {
- throw new AppError({
- errorCode: HTTPError.GENERAL_ERROR,
- message: 'The User ID must be provided',
- module: MODULE_NAME, method: 'handleUpdateSiteUserAdmin',
- user: req.user
- });
- }
- if (!filteredRequest.siteID) {
- throw new AppError({
- errorCode: HTTPError.GENERAL_ERROR,
- message: 'The Site ID must be provided',
- module: MODULE_NAME, method: 'handleUpdateSiteUserAdmin',
- user: req.user
- });
- }
- if (!Utils.objectHasProperty(filteredRequest, 'siteAdmin')) {
- throw new AppError({
- errorCode: HTTPError.GENERAL_ERROR,
- message: 'The Site Admin value must be provided',
- module: MODULE_NAME, method: 'handleUpdateSiteUserAdmin',
- user: req.user
- });
- }
if (req.user.id === filteredRequest.userID) {
throw new AppError({
errorCode: HTTPError.GENERAL_ERROR,
@@ -71,7 +47,9 @@ export default class SiteService {
req.tenant, req.user, filteredRequest.userID, Action.READ, action);
// Update
await SiteStorage.updateSiteUserAdmin(req.tenant, filteredRequest.siteID, filteredRequest.userID, filteredRequest.siteAdmin);
+ // Log
await Logging.logInfo({
+ ...LoggingHelper.getSiteProperties(site),
tenantID: req.user.tenantID,
user: req.user, actionOnUser: user,
module: MODULE_NAME, method: 'handleUpdateSiteUserAdmin',
@@ -88,31 +66,6 @@ export default class SiteService {
Action.UPDATE, Entity.SITE, MODULE_NAME, 'handleUpdateSiteOwner');
// Filter request
const filteredRequest = SiteValidator.getInstance().validateSiteOwnerReq(req.body);
- // Check mandatory fields
- if (!filteredRequest.userID) {
- throw new AppError({
- errorCode: HTTPError.GENERAL_ERROR,
- message: 'The User ID must be provided',
- module: MODULE_NAME, method: 'handleUpdateSiteOwner',
- user: req.user
- });
- }
- if (!filteredRequest.siteID) {
- throw new AppError({
- errorCode: HTTPError.GENERAL_ERROR,
- message: 'The Site ID must be provided',
- module: MODULE_NAME, method: 'handleUpdateSiteOwner',
- user: req.user
- });
- }
- if (!Utils.objectHasProperty(filteredRequest, 'siteOwner')) {
- throw new AppError({
- errorCode: HTTPError.GENERAL_ERROR,
- message: 'The Site Owner value must be provided',
- module: MODULE_NAME, method: 'handleUpdateSiteOwner',
- user: req.user
- });
- }
// Check and Get Site
const site = await UtilsService.checkAndGetSiteAuthorization(
req.tenant, req.user, filteredRequest.siteID, Action.UPDATE, action);
@@ -121,7 +74,9 @@ export default class SiteService {
req.tenant, req.user, filteredRequest.userID, Action.READ, action);
// Update
await SiteStorage.updateSiteOwner(req.tenant, filteredRequest.siteID, filteredRequest.userID, filteredRequest.siteOwner);
+ // Log
await Logging.logInfo({
+ ...LoggingHelper.getSiteProperties(site),
tenantID: req.user.tenantID,
user: req.user, actionOnUser: user,
module: MODULE_NAME, method: 'handleUpdateSiteOwner',
@@ -150,7 +105,9 @@ export default class SiteService {
} else {
await SiteStorage.removeUsersFromSite(req.tenant, site.id, users.map((user) => user.id));
}
+ // Log
await Logging.logInfo({
+ ...LoggingHelper.getSiteProperties(site),
tenantID: req.user.tenantID,
user: req.user,
module: MODULE_NAME,
@@ -168,14 +125,8 @@ export default class SiteService {
Action.UPDATE, Entity.SITE, MODULE_NAME, 'handleGetUsers');
// Filter
const filteredRequest = SiteValidator.getInstance().validateSiteGetUsersReq(req.query);
- // Check Site
- try {
- await UtilsService.checkAndGetSiteAuthorization(
- req.tenant, req.user, filteredRequest.SiteID, Action.READ, action);
- } catch (error) {
- UtilsService.sendEmptyDataResult(res, next);
- return;
- }
+ // Check Site Auth
+ await UtilsService.checkAndGetSiteAuthorization(req.tenant, req.user, filteredRequest.SiteID, Action.READ, action);
// Check dynamic auth for reading Users
const authorizationSiteUsersFilter = await AuthorizationService.checkAndGetSiteUsersAuthorizations(req.tenant,
req.user, filteredRequest);
@@ -213,7 +164,9 @@ export default class SiteService {
req.tenant, req.user, siteID, Action.DELETE, action);
// Delete
await SiteStorage.deleteSite(req.tenant, site.id);
+ // Log
await Logging.logInfo({
+ ...LoggingHelper.getSiteProperties(site),
tenantID: req.user.tenantID,
user: req.user, module: MODULE_NAME, method: 'handleDeleteSite',
message: `Site '${site.name}' has been deleted successfully`,
@@ -302,7 +255,6 @@ export default class SiteService {
public static async handleGetSiteImage(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
// This endpoint is not protected, so no need to check user's access
- // Filter
const filteredRequest = SiteValidator.getInstance().validateSiteGetImageReq(req.query);
UtilsService.assertIdIsProvided(action, filteredRequest.ID, MODULE_NAME, 'handleGetSiteImage', req.user);
if (!filteredRequest.TenantID) {
@@ -352,7 +304,7 @@ export default class SiteService {
throw new AppAuthError({
errorCode: HTTPAuthError.FORBIDDEN,
user: req.user,
- action: Action.READ, entity: Entity.SITE,
+ action: Action.CREATE, entity: Entity.SITE,
module: MODULE_NAME, method: 'handleCreateSite'
});
}
@@ -373,7 +325,9 @@ export default class SiteService {
}
// Save
site.id = await SiteStorage.saveSite(req.tenant, site);
+ // Log
await Logging.logInfo({
+ ...LoggingHelper.getSiteProperties(site),
tenantID: req.user.tenantID,
user: req.user, module: MODULE_NAME, method: 'handleCreateSite',
message: `Site '${site.name}' has been created successfully`,
@@ -440,7 +394,9 @@ export default class SiteService {
site.lastChangedOn = new Date();
// Save
await SiteStorage.saveSite(req.tenant, site, Utils.objectHasProperty(filteredRequest, 'image') ? true : false);
+ // Log
await Logging.logInfo({
+ ...LoggingHelper.getSiteProperties(site),
tenantID: req.user.tenantID,
user: req.user, module: MODULE_NAME, method: 'handleUpdateSite',
message: `Site '${site.name}' has been updated successfully`,

Align endpoints:

- Company service
- Site service