Moved OICPUtils.checkOICPComponent(tenant) in TenantValidation.validateComponentDependencies()
MODIFY
src/server/oicp/OICPUtils.ts
src/server/oicp/OICPUtils.ts
@@ -7,9 +7,7 @@ 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';
@@ -219,33 +217,4 @@ export default class OICPUtils {
oicpSetting.emsp.cert = await sanitize(Cypher.encrypt(tenantID, 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/TenantService.ts
src/server/rest/v1/service/TenantService.ts
@@ -14,7 +14,6 @@ import { LockEntity } from '../../../../types/Locking';
import LockingManager from '../../../../locking/LockingManager';
import Logging from '../../../../utils/Logging';
import NotificationHandler from '../../../../notification/NotificationHandler';
-import OICPUtils from '../../../oicp/OICPUtils';
import { ServerAction } from '../../../../types/Server';
import SettingStorage from '../../../../storage/mongodb/SettingStorage';
import SiteAreaStorage from '../../../../storage/mongodb/SiteAreaStorage';
@@ -190,7 +189,7 @@ export default class TenantService {
public static async handleCreateTenant(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
// Validate
- const filteredRequest = TenantValidator.getInstance().validateTenantCreateRequestSuperAdmin(req.body);
+ const filteredRequest = await TenantValidator.getInstance().validateTenantCreateRequestSuperAdmin(req.body);
// Check auth
if (!Authorizations.canCreateTenant(req.user)) {
throw new AppAuthError({
@@ -292,7 +291,7 @@ export default class TenantService {
public static async handleUpdateTenant(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
// Check
- const filteredRequest = TenantValidator.getInstance().validateTenantUpdateRequestSuperAdmin(req.body);
+ const filteredRequest = await TenantValidator.getInstance().validateTenantUpdateRequestSuperAdmin(req.body);
// Check auth
if (!Authorizations.canUpdateTenant(req.user)) {
throw new AppAuthError({
@@ -353,8 +352,6 @@ 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 OICPUtils.checkOICPComponent(tenant);
// Create settings
for (const componentName in tenant.components) {
// Get the settings

MODIFY
src/server/rest/v1/validator/TenantValidation.ts
src/server/rest/v1/validator/TenantValidation.ts
@@ -3,9 +3,13 @@ import { HttpTenantLogoRequest, HttpTenantsRequest } from '../../../../types/req
import AppError from '../../../../exception/AppError';
import Constants from '../../../../utils/Constants';
import { HTTPError } from '../../../../types/HTTPError';
+import OICPEndpointStorage from '../../../../storage/mongodb/OICPEndpointStorage';
+import { OICPRole } from '../../../../types/oicp/OICPRole';
+import OICPUtils from '../../../oicp/OICPUtils';
import Schema from './Schema';
import SchemaValidator from './SchemaValidator';
import Tenant from '../../../../types/Tenant';
+import UserStorage from '../../../../storage/mongodb/UserStorage';
import fs from 'fs';
import global from '../../../../types/GlobalType';
@@ -35,19 +39,19 @@ export default class TenantValidator extends SchemaValidator {
return TenantValidator._instance;
}
- public validateTenantCreateRequestSuperAdmin(tenant: Tenant): Tenant {
+ public async validateTenantCreateRequestSuperAdmin(tenant: Tenant): Promise<Tenant> {
// Validate schema
this.validate(this.tenantCreateReqSuperAdmin, tenant);
// Validate deps between components
- this.validateComponentDependencies(tenant);
+ await this.validateComponentDependencies(tenant);
return tenant;
}
- public validateTenantUpdateRequestSuperAdmin(tenant: Tenant): Tenant {
+ public async validateTenantUpdateRequestSuperAdmin(tenant: Tenant): Promise<Tenant> {
// Validate schema
this.validate(this.tenantUpdateReqSuperAdmin, tenant);
// Validate deps between components
- this.validateComponentDependencies(tenant);
+ await this.validateComponentDependencies(tenant);
return tenant;
}
@@ -75,7 +79,7 @@ export default class TenantValidator extends SchemaValidator {
return data;
}
- private validateComponentDependencies(tenant: Tenant) {
+ private async validateComponentDependencies(tenant: Tenant): Promise<void> {
if (tenant.components) {
// Smart Charging active: Organization must be active
if (tenant.components.smartCharging && tenant.components.organization &&
@@ -117,6 +121,29 @@ export default class TenantValidator extends SchemaValidator {
module: this.moduleName, method: 'validateTenantUpdateRequestSuperAdmin'
});
}
+ // OICP
+ if (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) {
+ // Create OICP user
+ if (!virtualOICPUser) {
+ await OICPUtils.createOICPVirtualUser(tenant.id);
+ }
+ } else {
+ // Clean up user
+ if (virtualOICPUser) {
+ await UserStorage.deleteUser(tenant.id, virtualOICPUser.id);
+ }
+ // Delete Endpoints if component is inactive
+ const oicpEndpoints = await OICPEndpointStorage.getOicpEndpoints(tenant.id, { role: OICPRole.CPO }, Constants.DB_PARAMS_MAX_LIMIT);
+ oicpEndpoints.result.forEach(async (oicpEndpoint) => {
+ await OICPEndpointStorage.deleteOicpEndpoint(tenant.id, oicpEndpoint.id);
+ });
+ }
+ }
}
}
}

Moved OICPUtils.checkOICPComponent(tenant) in TenantValidation.validateComponentDependencies()