OICP component update check refactoring
MODIFY
src/server/oicp/OICPUtils.ts
src/server/oicp/OICPUtils.ts
@@ -7,7 +7,9 @@ import Constants from '../../utils/Constants';
import Cypher from '../../utils/Cypher';
import OCPPStorage from '../../storage/mongodb/OCPPStorage';
import { OICPAcknowledgment } from '../../types/oicp/OICPAcknowledgment';
+import OICPEndpointStorage from '../../storage/mongodb/OICPEndpointStorage';
import { OICPEvseID } from '../../types/oicp/OICPEvse';
+import { OICPRole } from '../../types/oicp/OICPRole';
import { OICPSession } from '../../types/oicp/OICPSession';
import { OICPStatusCode } from '../../types/oicp/OICPStatusCode';
import { OicpSetting } from '../../types/Setting';
@@ -223,6 +225,34 @@ export default class OICPUtils {
} else {
oicpSetting.emsp.cert = oicpSetting.emsp.cert;
}
+ }
+ public static async checkOICPComponent(tenant: Partial<Tenant>): Promise<void> {
+ if (tenant.components && tenant.components?.oicp) {
+ const checkOICPComponent = tenant.components.oicp;
+ // Virtual user needed for unknown roaming user
+ const virtualOICPUser = await UserStorage.getUserByEmail(tenant.id, Constants.OICP_VIRTUAL_USER_EMAIL);
+ // Activate or deactivate virtual user depending on the oicp component status
+ if (checkOICPComponent.active) {
+ if (!virtualOICPUser) {
+ await OICPUtils.createOICPVirtualUser(tenant.id);
+ } else if (virtualOICPUser.status !== UserStatus.ACTIVE) {
+ // Activate user and save user status
+ await UserStorage.saveUserStatus(tenant.id, virtualOICPUser.id, UserStatus.ACTIVE);
+ }
+ } else if (virtualOICPUser && virtualOICPUser?.status === UserStatus.ACTIVE) {
+ // Deactivate user and save user status
+ await UserStorage.saveUserStatus(tenant.id, virtualOICPUser.id, UserStatus.INACTIVE);
+ }
+ if (!checkOICPComponent.active) {
+ // Delete Endpoints if component is inactive
+ const oicpEndpoints = await OICPEndpointStorage.getOicpEndpoints(tenant.id, { role: OICPRole.CPO }, Constants.DB_PARAMS_MAX_LIMIT);
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
+ oicpEndpoints.result.forEach(async (oicpEndpoint) => {
+ // Delete
+ await OICPEndpointStorage.deleteOicpEndpoint(tenant.id, oicpEndpoint.id);
+ });
+ }
+ }
}
}

MODIFY
src/server/rest/v1/service/ChargingStationService.ts
src/server/rest/v1/service/ChargingStationService.ts
@@ -1022,7 +1022,7 @@ export default class ChargingStationService {
}
// Get Transaction
const transaction = await TransactionStorage.getTransaction(req.user.tenantID, filteredRequest.args.transactionId);
- UtilsService.assertObjectExists(action, transaction, `Transaction ID '${String(filteredRequest.args.transactionId)}' does not exist`,
+ UtilsService.assertObjectExists(action, transaction, `Transaction ID '${filteredRequest.args.transactionId.toString()}' does not exist`,
MODULE_NAME, 'handleAction', req.user);
// Add connector ID
filteredRequest.args.connectorId = transaction.connectorId;

MODIFY
src/server/rest/v1/service/TenantService.ts
src/server/rest/v1/service/TenantService.ts
@@ -13,8 +13,6 @@ import { LockEntity } from '../../../../types/Locking';
import LockingManager from '../../../../locking/LockingManager';
import Logging from '../../../../utils/Logging';
import NotificationHandler from '../../../../notification/NotificationHandler';
-import OICPEndpointStorage from '../../../../storage/mongodb/OICPEndpointStorage';
-import { OICPRole } from '../../../../types/oicp/OICPRole';
import OICPUtils from '../../../oicp/OICPUtils';
import { ServerAction } from '../../../../types/Server';
import SettingStorage from '../../../../storage/mongodb/SettingStorage';
@@ -331,7 +329,7 @@ export default class TenantService {
private static async updateSettingsWithComponents(tenant: Partial<Tenant>, req: Request): Promise<void> {
// Check if OICP component is activated or deactivated and create/activate/deactivate virtual user (and Badges) accordingly
- await this.checkOICPComponent(tenant);
+ OICPUtils.checkOICPComponent(tenant);
// Create settings
for (const componentName in tenant.components) {
// Get the settings
@@ -371,33 +369,4 @@ export default class TenantService {
}
}
}
-
- private static async checkOICPComponent(tenant: Partial<Tenant>): Promise<void> {
- if (tenant.components && tenant.components?.oicp) {
- const checkOICPComponent = tenant.components.oicp;
- // Virtual user needed for unknown roaming user
- const virtualOICPUser = await UserStorage.getUserByEmail(tenant.id, Constants.OICP_VIRTUAL_USER_EMAIL);
- // Activate or deactivate virtual user depending on the oicp component status
- if (checkOICPComponent.active) {
- if (!virtualOICPUser) {
- await OICPUtils.createOICPVirtualUser(tenant.id);
- } else if (virtualOICPUser.status !== UserStatus.ACTIVE) {
- // Activate user and save user status
- await UserStorage.saveUserStatus(tenant.id, virtualOICPUser.id, UserStatus.ACTIVE);
- }
- } else if (virtualOICPUser && virtualOICPUser?.status === UserStatus.ACTIVE) {
- // Deactivate user and save user status
- await UserStorage.saveUserStatus(tenant.id, virtualOICPUser.id, UserStatus.INACTIVE);
- }
- if (!checkOICPComponent.active) {
- // Delete Endpoints if component is inactive
- const oicpEndpoints = await OICPEndpointStorage.getOicpEndpoints(tenant.id, { role: OICPRole.CPO }, Constants.DB_PARAMS_MAX_LIMIT);
- // eslint-disable-next-line @typescript-eslint/no-misused-promises
- oicpEndpoints.result.forEach(async (oicpEndpoint) => {
- // Delete
- await OICPEndpointStorage.deleteOicpEndpoint(tenant.id, oicpEndpoint.id);
- });
- }
- }
- }
}

OICP component update check refactoring