put poplateTemplates in a better place !!
tested to put template populate at start time otherwise it's always too late
+replaced id: '' to id: null in template creation
MODIFY
src/server/rest/v1/service/ChargingStationTemplateService.ts
src/server/rest/v1/service/ChargingStationTemplateService.ts
@@ -21,7 +21,7 @@ export default class ChargingStationTemplateService {
await AuthorizationService.checkAndGetChargingStationTemplateAuthorizations(
req.tenant, req.user, {}, Action.CREATE, filteredRequest);
const newChargingStationTemplate: ChargingStationTemplate = {
- id: '',
+ id: null,
createdBy: { id: req.user.id },
createdOn: new Date(),
template : {

MODIFY
src/start.ts
src/start.ts
@@ -7,6 +7,7 @@ import CentralSystemRestServiceConfiguration from './types/configuration/Central
import ChargingStationConfiguration from './types/configuration/ChargingStationConfiguration';
import Configuration from './utils/Configuration';
import Constants from './utils/Constants';
+import ContextBuilder from '../test/api/context/ContextBuilder';
import I18nManager from './utils/I18nManager';
import JsonOCPPServer from './server/ocpp/json/JsonOCPPServer';
import LocalCarCatalogBootstrap from './bootstrap/LocalCarCatalogBootstrap';
@@ -167,6 +168,13 @@ export default class Bootstrap {
// Update of manually uploaded data
if (Bootstrap.migrationConfig?.active) {
+ // -------------------------------------------------------------------------
+ // Update Charging Station Templates
+ // -------------------------------------------------------------------------
+ startTimeMillis = await this.logAndGetStartTimeMillis('Charging Station templates is being updated...');
+ // Load and Save the Charging Station templates
+ await ContextBuilder.populateTemplates();
+ await this.logDuration(startTimeMillis, 'Charging Station templates have been updated successfully');
// -------------------------------------------------------------------------
// Import Local Car Catalogs
// -------------------------------------------------------------------------

MODIFY
src/storage/mongodb/ChargingStationTemplateStorage.ts
src/storage/mongodb/ChargingStationTemplateStorage.ts
@@ -16,12 +16,12 @@ export default class ChargingStationTemplateStorage {
public static async saveChargingStationTemplate(chargingStationTemplate: ChargingStationTemplate): Promise<string> {
const startTime = Logging.traceDatabaseRequestStart();
// Validate
+ chargingStationTemplate.hash = Utils.hash(JSON.stringify(chargingStationTemplate));
+ chargingStationTemplate.hashTechnical = Utils.hash(JSON.stringify(chargingStationTemplate.template.technical));
+ chargingStationTemplate.hashCapabilities = Utils.hash(JSON.stringify(chargingStationTemplate.template.capabilities));
+ chargingStationTemplate.hashOcppStandard = Utils.hash(JSON.stringify(chargingStationTemplate.template.ocppStandardParameters));
+ chargingStationTemplate.hashOcppVendor = Utils.hash(JSON.stringify(chargingStationTemplate.template.ocppVendorParameters));
const chargingStationTemplateMDB = ChargingStationValidatorStorage.getInstance().validateChargingStationTemplateSave(chargingStationTemplate);
- chargingStationTemplateMDB.hash = Utils.hash(JSON.stringify(chargingStationTemplateMDB)),
- chargingStationTemplateMDB.hashTechnical = Utils.hash(JSON.stringify(chargingStationTemplateMDB.template.technical)),
- chargingStationTemplateMDB.hashCapabilities = Utils.hash(JSON.stringify(chargingStationTemplateMDB.template.capabilities)),
- chargingStationTemplateMDB.hashOcppStandard = Utils.hash(JSON.stringify(chargingStationTemplateMDB.template.ocppStandardParameters)),
- chargingStationTemplateMDB.hashOcppVendor = Utils.hash(JSON.stringify(chargingStationTemplateMDB.template.ocppVendorParameters)),
DatabaseUtils.switchIDToMongoDBID(chargingStationTemplateMDB);
// Add Last Changed/Created props
DatabaseUtils.addLastChangedCreatedProps(chargingStationTemplateMDB, chargingStationTemplate);

MODIFY
test/api/context/ContextBuilder.ts
test/api/context/ContextBuilder.ts
@@ -69,9 +69,10 @@ export default class ContextBuilder {
public async prepareContexts(): Promise<void> {
// Connect to the DB
await global.database.start();
+ // await this.populateTemplates()
await this.destroyTestTenants();
// Build each tenant context
-
+ // await this.populateTemplates();
let tenantDefinitions = ContextDefinition.TENANT_CONTEXT_LIST;
if (process.env.TENANT_FILTER) {
// Just an optimization allowing to only initialize a single tenant
@@ -250,7 +251,6 @@ export default class ContextBuilder {
}
userListToAssign = [adminUser]; // Default admin is always assigned to site
userList = [adminUser]; // Default admin is always assigned to site
- await this.populateTemplates();
// Prepare users
// Skip first entry as it is the default admin already consider above
for (let index = 1; index < ContextDefinition.TENANT_USER_LIST.length; index++) {
@@ -429,13 +429,18 @@ export default class ContextBuilder {
return newTenantContext;
}
- private async populateTemplates(): Promise<void> {
+ public static async populateTemplates(): Promise<void> {
let created = 0;
+ // Check if there is existing templates
+ const existingTemplates = await global.database.getCollection<any>(Constants.DEFAULT_TENANT_ID, 'chargingstationtemplates').findOne({});
+ if (existingTemplates) {
+ return;
+ }
try {
let chargingStationTemplates: ChargingStationTemplate[] = [
// Template Schneider~EV2S22P44|EVC1S22P4E4E
{
- id: Utils.generateUUID(),
+ id: null,
template: {
chargePointVendor: "Schneider Electric",
extraFilters: {
@@ -565,7 +570,7 @@ export default class ContextBuilder {
},
// Template Schneider~EV2S7P44|EVC1S7P4E4E
{
- id: Utils.generateUUID(),
+ id: null,
template: {
chargePointVendor: "Schneider Electric",
extraFilters: {
@@ -695,7 +700,7 @@ export default class ContextBuilder {
},
// Template Delta~10616
{
- id: Utils.generateUUID(),
+ id: null,
template: {
chargePointVendor: "DELTA",
extraFilters: {
@@ -802,25 +807,15 @@ export default class ContextBuilder {
}
}
];
- // chargingStationTemplates.forEach((template) => {
for (const template of chargingStationTemplates) {
- // Build the hash code
- template.hash = Utils.hash(JSON.stringify(template));
- // Check if the existing car has changed
- const existingTemplate = await ChargingStationTemplateStorage.getChargingStationTemplate(template.id);
- // Ignore
- if (existingTemplate?.hash === template.hash) {
- continue;
- }
- await ChargingStationTemplateStorage.deleteChargingStationTemplate(Constants.DEFAULT_TENANT_OBJECT, template.id)
template.createdOn = new Date();
await ChargingStationTemplateStorage.saveChargingStationTemplate(template);
- };
created++;
+ };
} catch (error) {
- console.log(`Error while importing the charging station templates : ${error.message as string}`);
+ console.log(`>>>> Error while importing the charging station templates : ${error.message as string}`);
}
// Log in the default tenant
- console.log(`${created} charging station template(s) created in the default tenant`);
+ console.log(`>>>> ${created} charging station template(s) created in the default tenant`);
}
}

put poplateTemplates in a better place !!
tested to put template populate at start time otherwise it's always too late
+replaced id: '' to id: null in template creation