addUsersToCar: User must be checked when assigned to car + renamed error messages + added missing traces in storage + added log of errors in Storage
MODIFY
src/integration/billing/BillingIntegration.ts
src/integration/billing/BillingIntegration.ts
@@ -278,7 +278,7 @@ export default abstract class BillingIntegration<T extends BillingSetting> {
source: Constants.CENTRAL_SERVER,
action: ServerAction.BILLING_FORCE_SYNCHRONIZE_USER_INVOICES,
module: MODULE_NAME, method: 'forceSynchronizeUserInvoices',
- message: `Billing invoice with ID '${invoiceIDInBilling}' does not exist anymore`
+ message: `Billing invoice with ID '${invoiceIDInBilling}' does not exist`
});
continue;
}
@@ -396,7 +396,7 @@ export default abstract class BillingIntegration<T extends BillingSetting> {
source: Constants.CENTRAL_SERVER,
action: ServerAction.BILLING_SYNCHRONIZE_INVOICES,
module: MODULE_NAME, method: 'synchronizeInvoices',
- message: `Billing invoice with ID '${invoiceIDInBilling}' does not exist anymore`
+ message: `Billing invoice with ID '${invoiceIDInBilling}' does not exist`
});
continue;
}

MODIFY
src/server/rest/service/BillingService.ts
src/server/rest/service/BillingService.ts
@@ -123,7 +123,7 @@ export default class BillingService {
}
// Get user
const userToSynchronize = await UserStorage.getUser(tenant.id, filteredRequest.id);
- UtilsService.assertObjectExists(action, userToSynchronize, `User '${filteredRequest.id}' does not exist anymore.`,
+ UtilsService.assertObjectExists(action, userToSynchronize, `User '${filteredRequest.id}' does not exist`,
MODULE_NAME, 'handleSynchronizeUser', req.user);
// Sync user
await billingImpl.synchronizeUser(userToSynchronize, tenant.id);
@@ -159,7 +159,7 @@ export default class BillingService {
}
// Get user
const userToSynchronize = await UserStorage.getUser(tenant.id, filteredRequest.id);
- UtilsService.assertObjectExists(action, userToSynchronize, `User '${filteredRequest.id}' does not exist anymore.`,
+ UtilsService.assertObjectExists(action, userToSynchronize, `User '${filteredRequest.id}' does not exist`,
MODULE_NAME, 'handleSynchronizeUser', req.user);
// Sync user
await billingImpl.forceSynchronizeUser(userToSynchronize, tenant.id);
@@ -229,7 +229,7 @@ export default class BillingService {
const filteredRequest = BillingSecurity.filterGetUserInvoicesRequest(req.query);
// Get user
const billingUser = await billingImpl.getUserByEmail(req.user.email);
- UtilsService.assertObjectExists(action, billingUser, `Billing user with email '${req.user.email}' does not exist anymore.`,
+ UtilsService.assertObjectExists(action, billingUser, `Billing user with email '${req.user.email}' does not exist`,
MODULE_NAME, 'handleGetUserInvoices', req.user);
if (Authorizations.isBasic(req.user)) {
filteredRequest.UserID = req.user.id;
@@ -285,7 +285,7 @@ export default class BillingService {
if (Authorizations.isBasic(req.user)) {
// Get the User
user = await UserStorage.getUser(req.user.tenantID, req.user.id);
- UtilsService.assertObjectExists(action, user, `User '${req.user.id}' does not exist anymore.`,
+ UtilsService.assertObjectExists(action, user, `User '${req.user.id}' does not exist`,
MODULE_NAME, 'handleSynchronizeUserInvoices', req.user);
}
// Sync invoices
@@ -322,7 +322,7 @@ export default class BillingService {
const filteredRequest = BillingSecurity.filterForceSynchronizeUserInvoicesRequest(req.body);
// Get the User
const user = await UserStorage.getUser(req.user.tenantID, filteredRequest.userID);
- UtilsService.assertObjectExists(action, user, `User '${filteredRequest.userID}' does not exist anymore.`,
+ UtilsService.assertObjectExists(action, user, `User '${filteredRequest.userID}' does not exist`,
MODULE_NAME, 'handleForceSynchronizeUserInvoices', req.user);
// Sync user invoices
const synchronizeAction = await billingImpl.forceSynchronizeUserInvoices(req.user.tenantID, user);

MODIFY
src/server/rest/service/CarService.ts
src/server/rest/service/CarService.ts
@@ -17,6 +17,7 @@ import TenantComponents from '../../../types/TenantComponents';
import User from '../../../types/User';
import Utils from '../../../utils/Utils';
import UtilsService from './UtilsService';
+import UserStorage from '../../../storage/mongodb/UserStorage';
const MODULE_NAME = 'CarService';
@@ -495,7 +496,32 @@ export default class CarService {
const filteredRequest = CarSecurity.filterUsersAssignRequest(req.body);
// CarID is mandatory
UtilsService.assertIdIsProvided(action, filteredRequest.carID, 'CarSecurity', 'filterUsersAssignRequest', req.user);
+ // Check Users
+ for (const userCar of filteredRequest.usersCar) {
+ // Check the user
+ const user = await UserStorage.getUser(req.user.tenantID, userCar.userID);
+ UtilsService.assertObjectExists(action, user, `User '${userCar.userID}' does not exist`,
+ MODULE_NAME, 'handleAssignUsersToSite', req.user);
+ // Auth
+ if (!Authorizations.canReadUser(req.user, userCar.userID)) {
+ throw new AppAuthError({
+ errorCode: HTTPAuthError.ERROR,
+ user: req.user,
+ action: Action.READ, entity: Entity.USER,
+ module: MODULE_NAME, method: 'handleAssignUsersToSite',
+ value: userCar.userID
+ });
+ }
+ }
const result = await CarStorage.addUsersToCar(req.user.tenantID, filteredRequest.carID, filteredRequest.usersCar);
+ // Log
+ Utils.logActionsResponse(req.user.tenantID, ServerAction.ADD_USERS_TO_CAR,
+ MODULE_NAME, 'AssignUsersCar', result,
+ '{{inSuccess}} user(s) were successfully assigned',
+ '{{inError}} user(s) failed to be assigned',
+ '{{inSuccess}} user(s) were successfully assigned and {{inError}} failed to be assigned',
+ 'All the users are up to date'
+ );
// Return
res.json({ ...result, ...Constants.REST_RESPONSE_SUCCESS });
next();

MODIFY
src/server/rest/service/ChargingStationService.ts
src/server/rest/service/ChargingStationService.ts
@@ -323,7 +323,7 @@ export default class ChargingStationService {
UtilsService.assertIdIsProvided(action, filteredRequest.siteAreaID, MODULE_NAME, 'handleTriggerSmartCharging', req.user);
// Get Site Area
const siteArea = await SiteAreaStorage.getSiteArea(req.user.tenantID, filteredRequest.siteAreaID);
- UtilsService.assertObjectExists(action, siteArea, `Site Area '${filteredRequest.siteAreaID}' does not exist anymore.`,
+ UtilsService.assertObjectExists(action, siteArea, `Site Area '${filteredRequest.siteAreaID}' does not exist`,
MODULE_NAME, 'handleTriggerSmartCharging', req.user);
// Check auth
if (!Authorizations.canUpdateSiteArea(req.user, siteArea.siteID)) {
@@ -478,7 +478,7 @@ export default class ChargingStationService {
// Get the Charging Station`
const chargingStation = await ChargingStationStorage.getChargingStation(req.user.tenantID, filteredRequest.ChargeBoxID);
// Found?
- UtilsService.assertObjectExists(action, chargingStation, `ChargingStation '${filteredRequest.ChargeBoxID}' does not exist anymore.`,
+ UtilsService.assertObjectExists(action, chargingStation, `ChargingStation '${filteredRequest.ChargeBoxID}' does not exist`,
MODULE_NAME, 'handleAssignChargingStationsToSiteArea', req.user);
// Check auth
if (!Authorizations.canReadChargingStation(req.user)) {
@@ -518,7 +518,7 @@ export default class ChargingStationService {
// Get the Charging Station
const chargingStation = await ChargingStationStorage.getChargingStation(req.user.tenantID, filteredRequest.chargeBoxID);
// Found?
- UtilsService.assertObjectExists(action, chargingStation, `ChargingStation '${filteredRequest.chargeBoxID}' does not exist anymore.`,
+ UtilsService.assertObjectExists(action, chargingStation, `ChargingStation '${filteredRequest.chargeBoxID}' does not exist`,
MODULE_NAME, 'handleRequestChargingStationOcppParameters', req.user);
// Get the Config
const result = await OCPPUtils.requestAndSaveChargingStationOcppParameters(

MODIFY
src/server/rest/service/ConnectionService.ts
src/server/rest/service/ConnectionService.ts
@@ -46,7 +46,7 @@ export default class ConnectionService {
}
// Get it
const connection = await ConnectionStorage.getConnection(req.user.tenantID, connectionID);
- UtilsService.assertObjectExists(action, connection, `Connection ID '${connectionID}' does not exist anymore.`,
+ UtilsService.assertObjectExists(action, connection, `Connection ID '${connectionID}' does not exist`,
MODULE_NAME, 'handleGetConnection', req.user);
// Return
res.json(
@@ -125,7 +125,7 @@ export default class ConnectionService {
}
// Get connection
const connection = await ConnectionStorage.getConnection(req.user.tenantID, connectionID);
- UtilsService.assertObjectExists(action, connection, `Connection ID '${connectionID}' does not exist anymore.`,
+ UtilsService.assertObjectExists(action, connection, `Connection ID '${connectionID}' does not exist`,
MODULE_NAME, 'handleDeleteConnection', req.user);
// Delete
await ConnectionStorage.deleteConnectionById(req.user.tenantID, connection.id);

MODIFY
src/server/rest/service/RegistrationTokenService.ts
src/server/rest/service/RegistrationTokenService.ts
@@ -29,7 +29,7 @@ export default class RegistrationTokenService {
if (Utils.isComponentActiveFromToken(req.user, TenantComponents.ORGANIZATION) && filteredRequest.siteAreaID) {
// Get the Site Area
const siteArea = await SiteAreaStorage.getSiteArea(req.user.tenantID, filteredRequest.siteAreaID);
- UtilsService.assertObjectExists(action, siteArea, `Site Area '${filteredRequest.siteAreaID}' does not exist anymore.`,
+ UtilsService.assertObjectExists(action, siteArea, `Site Area '${filteredRequest.siteAreaID}' does not exist`,
MODULE_NAME, 'handleCreateRegistrationToken', req.user);
if (!Authorizations.canCreateRegistrationToken(req.user, siteArea.siteID)) {
// Not Authorized!
@@ -114,7 +114,7 @@ export default class RegistrationTokenService {
}
// Check user
const registrationToken = await RegistrationTokenStorage.getRegistrationToken(req.user.tenantID, tokenID);
- UtilsService.assertObjectExists(action, registrationToken, `Registration Token '${tokenID}' does not exist anymore.`,
+ UtilsService.assertObjectExists(action, registrationToken, `Registration Token '${tokenID}' does not exist`,
MODULE_NAME, 'handleDeleteRegistrationToken', req.user);
await RegistrationTokenStorage.deleteRegistrationToken(req.user.tenantID, tokenID);
// Log
@@ -162,7 +162,7 @@ export default class RegistrationTokenService {
}
// Check user
const registrationToken = await RegistrationTokenStorage.getRegistrationToken(req.user.tenantID, tokenID);
- UtilsService.assertObjectExists(action, registrationToken, `Registration Token '${tokenID}' does not exist anymore.`,
+ UtilsService.assertObjectExists(action, registrationToken, `Registration Token '${tokenID}' does not exist`,
MODULE_NAME, 'handleRevokeRegistrationToken', req.user);
registrationToken.revocationDate = new Date();
registrationToken.lastChangedBy = { 'id': req.user.id };

MODIFY
src/server/rest/service/SettingService.ts
src/server/rest/service/SettingService.ts
@@ -167,7 +167,7 @@ export default class SettingService {
}
// Get Setting
const setting = await SettingStorage.getSetting(req.user.tenantID, settingUpdate.id);
- UtilsService.assertObjectExists(action, setting, `Setting with ID '${settingUpdate.id}' does not exist anymore`,
+ UtilsService.assertObjectExists(action, setting, `Setting with ID '${settingUpdate.id}' does not exist`,
MODULE_NAME, 'handleUpdateSetting', req.user);
// Process the sensitive data if any
// Preprocess the data to take care of updated values

MODIFY
src/server/rest/service/SiteAreaService.ts
src/server/rest/service/SiteAreaService.ts
@@ -48,7 +48,7 @@ export default class SiteAreaService {
}
// Get the Site Area
const siteArea = await SiteAreaStorage.getSiteArea(req.user.tenantID, filteredRequest.siteAreaID);
- UtilsService.assertObjectExists(action, siteArea, `Site Area '${filteredRequest.siteAreaID}' does not exist anymore.`,
+ UtilsService.assertObjectExists(action, siteArea, `Site Area '${filteredRequest.siteAreaID}' does not exist`,
MODULE_NAME, 'handleAssignAssetsToSiteArea', req.user);
// Check auth
if (!Authorizations.canUpdateSiteArea(req.user, siteArea.siteID)) {
@@ -66,7 +66,7 @@ export default class SiteAreaService {
for (const assetID of filteredRequest.assetIDs) {
// Check the asset
const asset = await AssetStorage.getAsset(req.user.tenantID, assetID);
- UtilsService.assertObjectExists(action, asset, `Asset '${assetID}' does not exist anymore.`,
+ UtilsService.assertObjectExists(action, asset, `Asset '${assetID}' does not exist`,
MODULE_NAME, 'handleAssignAssetsToSiteArea', req.user);
// Check auth
if (!Authorizations.canReadAsset(req.user)) {
@@ -122,7 +122,7 @@ export default class SiteAreaService {
}
// Get the Site Area (before auth to get siteID)
const siteArea = await SiteAreaStorage.getSiteArea(req.user.tenantID, filteredRequest.siteAreaID);
- UtilsService.assertObjectExists(action, siteArea, `Site Area '${filteredRequest.siteAreaID}' does not exist anymore.`,
+ UtilsService.assertObjectExists(action, siteArea, `Site Area '${filteredRequest.siteAreaID}' does not exist`,
MODULE_NAME, 'handleAssignChargingStationsToSiteArea', req.user);
// Check auth
if (!Authorizations.canUpdateSiteArea(req.user, siteArea.siteID)) {
@@ -140,7 +140,7 @@ export default class SiteAreaService {
for (const chargingStationID of filteredRequest.chargingStationIDs) {
// Check the charging station
const chargingStation = await ChargingStationStorage.getChargingStation(req.user.tenantID, chargingStationID);
- UtilsService.assertObjectExists(action, chargingStation, `ChargingStation '${chargingStationID}' does not exist anymore.`,
+ UtilsService.assertObjectExists(action, chargingStation, `ChargingStation '${chargingStationID}' does not exist`,
MODULE_NAME, 'handleAssignChargingStationsToSiteArea', req.user);
// Check auth
if (!Authorizations.canReadChargingStation(req.user)) {

MODIFY
src/server/rest/service/SiteService.ts
src/server/rest/service/SiteService.ts
@@ -214,13 +214,13 @@ export default class SiteService {
}
// Get the Site
const site = await SiteStorage.getSite(req.user.tenantID, filteredRequest.siteID);
- UtilsService.assertObjectExists(action, site, `Site '${filteredRequest.siteID}' does not exist anymore.`,
+ UtilsService.assertObjectExists(action, site, `Site '${filteredRequest.siteID}' does not exist`,
MODULE_NAME, 'handleAssignUsersToSite', req.user);
// Get Users
for (const userID of filteredRequest.userIDs) {
// Check the user
const user = await UserStorage.getUser(req.user.tenantID, userID);
- UtilsService.assertObjectExists(action, user, `User '${userID}' does not exist anymore.`,
+ UtilsService.assertObjectExists(action, user, `User '${userID}' does not exist`,
MODULE_NAME, 'handleAssignUsersToSite', req.user);
// Auth
if (!Authorizations.canReadUser(req.user, userID)) {

MODIFY
src/server/rest/service/UserService.ts
src/server/rest/service/UserService.ts
@@ -75,7 +75,7 @@ export default class UserService {
}
// Get the User
const user = await UserStorage.getUser(req.user.tenantID, filteredRequest.userID);
- UtilsService.assertObjectExists(action, user, `User '${filteredRequest.userID}' does not exist anymore.`,
+ UtilsService.assertObjectExists(action, user, `User '${filteredRequest.userID}' does not exist`,
MODULE_NAME, 'handleAssignSitesToUser', req.user);
// Get Sites
for (const siteID of filteredRequest.siteIDs) {
@@ -153,7 +153,7 @@ export default class UserService {
}
// Check user
const user = await UserStorage.getUser(req.user.tenantID, id);
- UtilsService.assertObjectExists(action, user, `User '${id}' does not exist anymore.`,
+ UtilsService.assertObjectExists(action, user, `User '${id}' does not exist`,
MODULE_NAME, 'handleDeleteUser', req.user);
// Deleted
if (user.deleted) {
@@ -346,7 +346,7 @@ export default class UserService {
}
// Get User
let user = await UserStorage.getUser(req.user.tenantID, filteredRequest.id);
- UtilsService.assertObjectExists(action, user, `User '${filteredRequest.id}' does not exist anymore.`,
+ UtilsService.assertObjectExists(action, user, `User '${filteredRequest.id}' does not exist`,
MODULE_NAME, 'handleUpdateUser', req.user);
// Deleted?
if (user.deleted) {
@@ -581,7 +581,7 @@ export default class UserService {
}
// Get User
const user = await UserStorage.getUser(req.user.tenantID, filteredRequest.id);
- UtilsService.assertObjectExists(action, user, `User '${filteredRequest.id}' does not exist anymore.`,
+ UtilsService.assertObjectExists(action, user, `User '${filteredRequest.id}' does not exist`,
MODULE_NAME, 'handleUpdateUserMobileToken', req.user);
// Deleted?
if (user.deleted) {
@@ -647,7 +647,7 @@ export default class UserService {
}
// Get the user
const user = await UserStorage.getUser(req.user.tenantID, id);
- UtilsService.assertObjectExists(action, user, `User '${id}' does not exist anymore.`,
+ UtilsService.assertObjectExists(action, user, `User '${id}' does not exist`,
MODULE_NAME, 'handleGetUser', req.user);
// Deleted?
if (user.deleted) {
@@ -698,7 +698,7 @@ export default class UserService {
}
// Get the logged user
const user = await UserStorage.getUser(req.user.tenantID, filteredRequest.ID);
- UtilsService.assertObjectExists(action, user, `User '${filteredRequest.ID}' does not exist anymore.`,
+ UtilsService.assertObjectExists(action, user, `User '${filteredRequest.ID}' does not exist`,
MODULE_NAME, 'handleGetUserImage', req.user);
// Deleted?
if (user.deleted) {
@@ -1062,7 +1062,7 @@ export default class UserService {
}
// Get the user
const user = await UserStorage.getUser(req.user.tenantID, id);
- UtilsService.assertObjectExists(action, user, `User '${id}' does not exist anymore.`,
+ UtilsService.assertObjectExists(action, user, `User '${id}' does not exist`,
MODULE_NAME, 'handleGetUserInvoice', req.user);
// Deleted?
if (user.deleted) {

MODIFY
src/storage/mongodb/CarStorage.ts
src/storage/mongodb/CarStorage.ts
@@ -530,17 +530,20 @@ export default class CarStorage {
await CarStorage.saveUserCar(tenantID, userCar);
actionsDone.inSuccess++;
} catch (error) {
+ Logging.logError({
+ tenantID,
+ user: userCar.userID,
+ source: Constants.CENTRAL_SERVER,
+ module: MODULE_NAME, method: 'addUsersToCar',
+ action: ServerAction.USERS_CAR_UPDATE,
+ message: 'An error occurred while trying to assign the user to the car',
+ detailedMessages: { error: error.message, stack: error.stack, userCar }
+ });
actionsDone.inError++;
}
}
- // Log
- Utils.logActionsResponse(tenantID, ServerAction.ADD_USERS_TO_CAR,
- MODULE_NAME, 'AssignUsersCar', actionsDone,
- '{{inSuccess}} user(s) were successfully assigned',
- '{{inError}} user(s) failed to be assigned',
- '{{inSuccess}} user(s) were successfully assigned and {{inError}} failed to be assigned',
- 'All the users are up to date'
- );
+ // Debug
+ Logging.traceEnd(MODULE_NAME, 'addUsersToCar', uniqueTimerID, { usersCarToSave });
return actionsDone;
}

addUsersToCar: User must be checked when assigned to car + renamed error messages + added missing traces in storage + added log of errors in Storage