migration task for account activation setting
MODIFY
src/migration/MigrationHandler.ts
src/migration/MigrationHandler.ts
@@ -26,6 +26,7 @@ import { LockEntity } from '../types/Locking';
import LockingManager from '../locking/LockingManager';
import Logging from '../utils/Logging';
import LogicallyDeleteTagsOfDeletedUsersTask from './tasks/LogicallyDeleteTagsOfDeletedUsersTask';
+import MigrateAccountActivationSettingsFromConfigToDBTask from './tasks/MigrateAccountActivationSettingsFromConfigToDBTask';
import MigrateCoordinatesTask from './tasks/MigrateCoordinatesTask';
import MigrateCryptoSettingsFromConfigToDBTask from './tasks/MigrateCryptoSettingsFromConfigToDBTask';
import MigrateOcpiSettingTask from './tasks/MigrateOcpiSettingTask';
@@ -107,6 +108,7 @@ export default class MigrationHandler {
currentMigrationTasks.push(new MigrateCryptoSettingsFromConfigToDBTask());
currentMigrationTasks.push(new ImportLocalCarCatalogTask());
currentMigrationTasks.push(new AddLastChangedOnToCarCatalogTask());
+ currentMigrationTasks.push(new MigrateAccountActivationSettingsFromConfigToDBTask());
// Get the already done migrations from the DB
const migrationTasksDone = await MigrationStorage.getMigrations();
// Check

ADD
src/migration/tasks/MigrateAccountActivationSettingsFromConfigToDBTask.ts
+import { AccountActivationSetting, CryptoKeySetting, CryptoSettingsType, SettingDB } from '../../types/Setting';
+
+import Constants from '../../utils/Constants';
+import MigrationTask from '../MigrationTask';
+import SettingStorage from '../../storage/mongodb/SettingStorage';
+import Tenant from '../../types/Tenant';
+import TenantComponents from '../../types/TenantComponents';
+import TenantStorage from '../../storage/mongodb/TenantStorage';
+import Utils from '../../utils/Utils';
+
+export default class MigrateAccountActivationSettingsFromConfigToDBTask extends MigrationTask {
+ public async migrate(): Promise<void> {
+ const tenants = await TenantStorage.getTenants({}, Constants.DB_PARAMS_MAX_LIMIT);
+ for (const tenant of tenants.result) {
+ await this.migrateTenant(tenant);
+ }
+ }
+
+ public async migrateTenant(tenant: Tenant): Promise<void> {
+ const accountActivationSetting = await SettingStorage.getAccountActivationSettings(tenant.id);
+ // If no account activation setting exists, initialize it
+ if (Utils.isEmptyObject(accountActivationSetting)) {
+ // Create new account activation setting in tenant
+ const settingsToSave = {
+ identifier: TenantComponents.ACCOUNT_ACTIVATION,
+ createdOn: new Date(),
+ doNotActivateByDefault: false
+ } as AccountActivationSetting;
+
+ await SettingStorage.saveSettings(tenant.id, settingsToSave);
+ }
+ }
+
+ public getVersion(): string {
+ return '1.0';
+ }
+
+ public getName(): string {
+ return 'MigrateAccountActivationSettingsFromConfig';
+ }
+}

MODIFY
src/storage/mongodb/SettingStorage.ts
src/storage/mongodb/SettingStorage.ts
-import { AnalyticsSettings, AnalyticsSettingsType, AssetSettings, AssetSettingsType, BillingSettings, BillingSettingsType, CryptoKeySetting, CryptoSetting, CryptoSettingsType, PricingSettings, PricingSettingsType, RefundSettings, RefundSettingsType, RoamingSettings, SettingDB, SmartChargingSettings, SmartChargingSettingsType } from '../../types/Setting';
+import { AccountActivationSetting, AnalyticsSettings, AnalyticsSettingsType, AssetSettings, AssetSettingsType, BillingSettings, BillingSettingsType, CryptoKeySetting, CryptoSetting, CryptoSettingsType, PricingSettings, PricingSettingsType, RefundSettings, RefundSettingsType, RoamingSettings, SettingDB, SmartChargingSettings, SmartChargingSettingsType } from '../../types/Setting';
import global, { FilterParams } from '../../types/GlobalType';
import BackendError from '../../exception/BackendError';
@@ -55,6 +55,7 @@ export default class SettingStorage {
if (settingToSave.identifier === 'accountActivation') {
return {
_id: settingFilter._id,
+ identifier: settingToSave.identifier,
doNotActivateByDefault: settingToSave.doNotActivateByDefault,
};
}
@@ -343,6 +344,19 @@ export default class SettingStorage {
}
}
+ public static async getAccountActivationSettings(tenantID: string): Promise<AccountActivationSetting> {
+ // Get the account activation settings
+ const settings = await SettingStorage.getSettings(tenantID,
+ { identifier: TenantComponents.ACCOUNT_ACTIVATION },
+ Constants.DB_PARAMS_MAX_LIMIT);
+ const accountActivationSettings = {} as AccountActivationSetting;
+ if (settings && settings.count > 0 && settings.result[0]) {
+ accountActivationSettings.identifier = settings.result[0].identifier,
+ accountActivationSettings.doNotActivateByDefault = settings.result[0].doNotActivateByDefault;
+ }
+ return accountActivationSettings;
+ }
+
public static async getSettings(tenantID: string,
params: {identifier?: string; settingID?: string, dateFrom?: Date, dateTo?: Date},
dbParams: DbParams, projectFields?: string[]): Promise<DataResult<SettingDB>> {

MODIFY
src/types/Setting.ts
src/types/Setting.ts
@@ -269,3 +269,6 @@ export interface AssetGreencomConnectionType {
clientId: string;
clientSecret: string;
}
+export interface AccountActivationSetting extends Setting {
+ doNotActivateByDefault: boolean;
+}

migration task for account activation setting