Fixed tenant creation
MODIFY
src/assets/server/rest/v1/schemas/tenant/tenant-create.json
src/assets/server/rest/v1/schemas/tenant/tenant-create.json
"title": "Create one Tenant",
"type": "object",
"properties": {
- "id": {
- "$ref": "common.json#/definitions/id"
- },
"name": {
"$ref": "common.json#/definitions/name"
},

MODIFY
src/server/rest/v1/service/TenantService.ts
src/server/rest/v1/service/TenantService.ts
@@ -13,6 +13,9 @@ 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';
import SiteAreaStorage from '../../../../storage/mongodb/SiteAreaStorage';
@@ -257,6 +260,18 @@ export default class TenantService {
filteredRequest.createdOn = new Date();
// Save
filteredRequest.id = await TenantStorage.saveTenant(filteredRequest);
+ // OICP
+ if (filteredRequest.components && filteredRequest.components.oicp) {
+ // Virtual user needed for unknown roaming user
+ const virtualOICPUser = await UserStorage.getUserByEmail(filteredRequest.id, Constants.OICP_VIRTUAL_USER_EMAIL);
+ // Activate virtual user
+ if (filteredRequest.components.oicp.active) {
+ // Create OICP user
+ if (!virtualOICPUser) {
+ await OICPUtils.createOICPVirtualUser(filteredRequest.id);
+ }
+ }
+ }
// Update with components
await TenantService.updateSettingsWithComponents(filteredRequest, req);
// Create DB collections
@@ -365,6 +380,28 @@ export default class TenantService {
tenant.lastChangedOn = new Date();
// Update Tenant
await TenantStorage.saveTenant(tenant, Utils.objectHasProperty(filteredRequest, 'logo') ? true : false);
+ // OICP
+ if (tenant.components && 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 (tenant.components.oicp.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);
+ });
+ }
+ }
// Update with components
await TenantService.updateSettingsWithComponents(filteredRequest, req);
// Log

MODIFY
src/server/rest/v1/validator/TenantValidation.ts
src/server/rest/v1/validator/TenantValidation.ts
@@ -131,29 +131,6 @@ 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);
- });
- }
- }
}
}
}

MODIFY
test/api/TenantTest.ts
test/api/TenantTest.ts
@@ -2,6 +2,7 @@ import chai, { expect } from 'chai';
import CentralServerService from '../api/client/CentralServerService';
import Factory from '../factories/Factory';
+import Tenant from '../types/Tenant';
import TestUtils from './TestUtils';
import chaiSubset from 'chai-subset';
import faker from 'faker';
@@ -28,6 +29,58 @@ describe('Tenant tests', function() {
CentralServerService.defaultInstance.tenantApi, Factory.tenant.build());
});
+ it('Should be possible to create a valid tenant with components explicitly disabled', async () => {
+ // Create
+ const tenant = Factory.tenant.build() as Tenant;
+ tenant.components = {
+ 'analytics': {
+ 'active': false,
+ 'type': null
+ },
+ 'asset': {
+ 'active': false,
+ 'type': null
+ },
+ 'billing': {
+ 'active': false,
+ 'type': null
+ },
+ 'car': {
+ 'active': false,
+ 'type': null
+ },
+ 'ocpi': {
+ 'active': false,
+ 'type': null
+ },
+ 'oicp': {
+ 'active': false,
+ 'type': null
+ },
+ 'organization': {
+ 'active': false
+ },
+ 'pricing': {
+ 'active': false,
+ 'type': null
+ },
+ 'refund': {
+ 'active': false,
+ 'type': null
+ },
+ 'smartCharging': {
+ 'active': false,
+ 'type': null
+ },
+ 'statistics': {
+ 'active': false,
+ 'type': null
+ }
+ };
+ await CentralServerService.defaultInstance.createEntity(
+ CentralServerService.defaultInstance.tenantApi, tenant);
+ });
+
it('Should find the created tenant by id', async () => {
// Check if the created entity can be retrieved with its id
await CentralServerService.defaultInstance.getEntityById(

Fixed tenant creation