Badge Migration Tasks
MODIFY
src/migration/MigrationHandler.ts
src/migration/MigrationHandler.ts
import AddActivePropertyToTagsTask from './tasks/AddActivePropertyToTagsTask';
import AddConsumptionAmpsToConsumptionsTask from './tasks/AddConsumptionAmpsToConsumptionsTask';
+import AddCreatedPropertiesToBadgeTask from './tasks/AddCreatedPropertiesToBadgeTask';
+import AddDescriptionToTagsTask from './tasks/AddDescriptionToTagsTask';
import AddInactivityStatusInTransactionsTask from './tasks/AddInactivityStatusInTransactionsTask';
import AddIssuerFieldTask from './tasks/AddIssuerFieldTask';
import AddLastChangePropertiesToBadgeTask from './tasks/AddLastChangePropertiesToBadgeTask';
@@ -9,6 +11,7 @@ import AddSiteAreaLimitToConsumptionsTask from './tasks/AddSiteAreaLimitToConsum
import AddTagTypeTask from './tasks/AddTagTypeTask';
import AddTransactionRefundStatusTask from './tasks/AddTransactionRefundStatusTask';
import AddUserInTransactionsTask from './tasks/AddUserInTransactionsTask';
+import AlignTagsWithUsersIssuerTask from './tasks/AlignTagsWithUsersIssuerTask';
import CleanupMeterValuesTask from './tasks/CleanupMeterValuesTask';
import CleanupOrphanBadgeTask from './tasks/CleanupOrphanBadgeTask';
import CleanupSiteAreasTask from './tasks/CleanupSiteAreasTask';
@@ -85,6 +88,9 @@ export default class MigrationHandler {
currentMigrationTasks.push(new UnmarkTransactionExtraInactivitiesTask());
currentMigrationTasks.push(new RecomputeAllTransactionsConsumptionsTask());
currentMigrationTasks.push(new AddUserInTransactionsTask());
+ currentMigrationTasks.push(new AddCreatedPropertiesToBadgeTask());
+ currentMigrationTasks.push(new AddDescriptionToTagsTask());
+ currentMigrationTasks.push(new AlignTagsWithUsersIssuerTask());
// Get the already done migrations from the DB
const migrationTasksDone = await MigrationStorage.getMigrations();
// Check

ADD
src/migration/tasks/AddCreatedPropertiesToBadgeTask.ts
+import Constants from '../../utils/Constants';
+import Logging from '../../utils/Logging';
+import MigrationTask from '../MigrationTask';
+import { ServerAction } from '../../types/Server';
+import Tenant from '../../types/Tenant';
+import TenantStorage from '../../storage/mongodb/TenantStorage';
+import UserStorage from '../../storage/mongodb/UserStorage';
+import Utils from '../../utils/Utils';
+import global from '../../types/GlobalType';
+
+const MODULE_NAME = 'AddCreatedPropertiesToBadgeTask';
+
+export default class AddCreatedPropertiesToBadgeTask extends MigrationTask {
+ async migrate(): Promise<void> {
+ const tenants = await TenantStorage.getTenants({}, Constants.DB_PARAMS_MAX_LIMIT);
+ for (const tenant of tenants.result) {
+ await this.migrateTenant(tenant);
+ }
+ }
+
+ async migrateTenant(tenant: Tenant): Promise<void> {
+ const users = await UserStorage.getUsers(tenant.id, {}, Constants.DB_PARAMS_MAX_LIMIT);
+ const tagCollection = global.database.getCollection<any>(tenant.id, 'tags');
+ let counter = 0;
+ for (const user of users.result) {
+ const tags = await tagCollection.find({ userID: Utils.convertToObjectID(user.id), createdOn: null }).toArray();
+ if (!Utils.isEmptyArray(tags) && user.createdOn) {
+ for (const tag of tags) {
+ await tagCollection.updateOne(
+ {
+ _id: tag._id
+ },
+ {
+ $set: {
+ createdOn: user.createdOn,
+ createdBy: user.createdBy ? Utils.convertToObjectID(user.createdBy.id) : null
+ }
+ },
+ { upsert: false }
+ );
+ counter++;
+ }
+ }
+ }
+ // Log in the default tenant
+ if (counter > 0) {
+ Logging.logDebug({
+ tenantID: Constants.DEFAULT_TENANT,
+ action: ServerAction.MIGRATION,
+ module: MODULE_NAME, method: 'migrateTenant',
+ message: `${counter} Tags(s) have been updated in Tenant '${tenant.name}'`
+ });
+ }
+ }
+
+ getVersion(): string {
+ return '1.0';
+ }
+
+ getName(): string {
+ return 'AddCreatedPropertiesToBadgeTask';
+ }
+
+ isAsynchronous(): boolean {
+ return true;
+ }
+}

ADD
src/migration/tasks/AddDescriptionToTagsTask.ts
+import Constants from '../../utils/Constants';
+import Logging from '../../utils/Logging';
+import MigrationTask from '../MigrationTask';
+import { ServerAction } from '../../types/Server';
+import Tenant from '../../types/Tenant';
+import TenantStorage from '../../storage/mongodb/TenantStorage';
+import Utils from '../../utils/Utils';
+import global from '../../types/GlobalType';
+
+const MODULE_NAME = 'AddDescriptionToTagsTask';
+
+export default class AddDescriptionToTagsTask extends MigrationTask {
+ async migrate(): Promise<void> {
+ const tenants = await TenantStorage.getTenants({}, Constants.DB_PARAMS_MAX_LIMIT);
+ for (const tenant of tenants.result) {
+ await this.migrateTenant(tenant);
+ }
+ }
+
+ async migrateTenant(tenant: Tenant): Promise<void> {
+ // Add the active property to tags
+ let updated = 0;
+ // Get users
+ const tagsMDB = await global.database.getCollection<any>(tenant.id, 'tags')
+ .find({ description: null }).toArray();
+ if (!Utils.isEmptyArray(tagsMDB)) {
+ for (const tagMDB of tagsMDB) {
+ await global.database.getCollection<any>(tenant.id, 'tags').findOneAndUpdate(
+ { _id: tagMDB._id },
+ { $set: { description: `Tag ID '${tagMDB._id}'` } });
+ updated++
+ }
+ }
+ // Log in the default tenant
+ if (updated > 0) {
+ Logging.logDebug({
+ tenantID: Constants.DEFAULT_TENANT,
+ module: MODULE_NAME, method: 'migrateTenant',
+ action: ServerAction.MIGRATION,
+ message: `${updated} Tag(s) have been updated in Tenant '${tenant.name}'`
+ });
+ }
+ }
+
+ getVersion(): string {
+ return '1.0';
+ }
+
+ getName(): string {
+ return 'AddDescriptionToTagsTask';
+ }
+
+ isAsynchronous(): boolean {
+ return true;
+ }
+}

MODIFY
src/migration/tasks/AddLastChangePropertiesToBadgeTask.ts
src/migration/tasks/AddLastChangePropertiesToBadgeTask.ts
@@ -23,14 +23,14 @@ export default class AddLastChangePropertiesToBadgeTask extends MigrationTask {
const tagCollection = global.database.getCollection<any>(tenant.id, 'tags');
let counter = 0;
for (const user of users.result) {
- if (user.tags) {
- for (const tag of user.tags) {
- if (!tag.lastChangedOn) {
+ const tags = await tagCollection.find({ 'userID': Utils.convertToObjectID(user.id), lastChangedOn: null }).toArray();
+ if (!Utils.isEmptyArray(tags)) {
+ for (const tag of tags) {
const lastChangedOn = user.lastChangedOn ? user.lastChangedOn : user.createdOn;
const lastChangedBy = user.lastChangedBy ? user.lastChangedBy : user.createdBy;
await tagCollection.updateOne(
{
- '_id': tag.id
+ '_id': tag._id
},
{
$set: {
@@ -44,7 +44,6 @@ export default class AddLastChangePropertiesToBadgeTask extends MigrationTask {
}
}
}
- }
// Log in the default tenant
if (counter > 0) {
Logging.logDebug({
@@ -57,10 +56,14 @@ export default class AddLastChangePropertiesToBadgeTask extends MigrationTask {
}
getVersion(): string {
- return '1.0';
+ return '1.1';
}
getName(): string {
return 'AddLastChangePropertiesToBadgeTask';
}
+
+ isAsynchronous(): boolean {
+ return true;
+ }
}

ADD
src/migration/tasks/AlignTagsWithUsersIssuerTask.ts
+import Constants from '../../utils/Constants';
+import Logging from '../../utils/Logging';
+import MigrationTask from '../MigrationTask';
+import { ServerAction } from '../../types/Server';
+import Tenant from '../../types/Tenant';
+import TenantStorage from '../../storage/mongodb/TenantStorage';
+import { UpdateWriteOpResult } from 'mongodb';
+import Utils from '../../utils/Utils';
+import global from '../../types/GlobalType';
+
+const MODULE_NAME = 'AlignTagsWithUsersIssuerTask';
+
+export default class AlignTagsWithUsersIssuerTask extends MigrationTask {
+ async migrate(): Promise<void> {
+ const tenants = await TenantStorage.getTenants({}, Constants.DB_PARAMS_MAX_LIMIT);
+ for (const tenant of tenants.result) {
+ await this.migrateTenant(tenant);
+ }
+ }
+
+ async migrateTenant(tenant: Tenant): Promise<void> {
+ let updated = 0;
+ let result: UpdateWriteOpResult;
+ // Get users
+ const usersMDB = await global.database.getCollection<any>(tenant.id, 'users')
+ .find({}).toArray();
+ if (!Utils.isEmptyArray(usersMDB)) {
+ // Update tags
+ for (const userMDB of usersMDB) {
+ result = await global.database.getCollection(tenant.id, 'tags').updateMany(
+ {
+ userID: userMDB._id
+ },
+ {
+ $set: {
+ issuer: userMDB.issuer,
+ }
+ }
+ );
+ updated += result.modifiedCount;
+ }
+ Logging.logInfo({
+ tenantID: Constants.DEFAULT_TENANT,
+ action: ServerAction.MIGRATION,
+ module: MODULE_NAME, method: 'migrateTenant',
+ message: `${updated} Tag(s) have been updated in Tenant '${tenant.name}'`,
+ });
+ }
+ }
+
+ getVersion(): string {
+ return '1.0';
+ }
+
+ getName(): string {
+ return 'AlignTagsWithUsersIssuerTask';
+ }
+
+ isAsynchronous(): boolean {
+ return true;
+ }
+}

MODIFY
src/types/HTTPError.ts
src/types/HTTPError.ts
@@ -21,7 +21,6 @@ export enum HTTPError {
USER_EMAIL_ALREADY_EXIST_ERROR = 510,
USER_EULA_ERROR = 520,
- USER_TAG_ID_ALREADY_USED_ERROR = 540,
USER_NO_BADGE_ERROR = 570,
TAG_HAS_TRANSACTIONS = 575,

Badge Migration Tasks