Add default property to tag
MODIFY
src/migration/MigrationHandler.ts
src/migration/MigrationHandler.ts
import AddActivePropertyToTagsTask from './tasks/AddActivePropertyToTagsTask';
import AddConsumptionAmpsToConsumptionsTask from './tasks/AddConsumptionAmpsToConsumptionsTask';
import AddCreatedPropertiesToTagTask from './tasks/AddCreatedPropertiesToTagTask';
+import AddDefaultPropertyToTagsTask from './tasks/AddDefaultPropertyToTagsTask';
import AddDescriptionToTagsTask from './tasks/AddDescriptionToTagsTask';
import AddInactivityStatusInTransactionsTask from './tasks/AddInactivityStatusInTransactionsTask';
import AddIssuerFieldTask from './tasks/AddIssuerFieldTask';
@@ -93,6 +94,7 @@ export default class MigrationHandler {
currentMigrationTasks.push(new LogicallyDeleteTagsOfDeletedUsersTask());
currentMigrationTasks.push(new AddCreatedPropertiesToTagTask());
currentMigrationTasks.push(new AddDescriptionToTagsTask());
+ currentMigrationTasks.push(new AddDefaultPropertyToTagsTask());
// Get the already done migrations from the DB
const migrationTasksDone = await MigrationStorage.getMigrations();
// Check

ADD
src/migration/tasks/AddDefaultPropertyToTagsTask.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 = 'AddDefaultPropertyToTagsTask';
+
+export default class AddDefaultPropertyToTagsTask 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 Tags with no Description
+ const tagsMDB = await global.database.getCollection<any>(tenant.id, 'tags')
+ .find({
+ default: null ,
+ issuer: true,
+ }).toArray();
+ if (!Utils.isEmptyArray(tagsMDB)) {
+ for (const tagMDB of tagsMDB) {
+ await global.database.getCollection<any>(tenant.id, 'tags').findOneAndUpdate(
+ { _id: tagMDB._id },
+ { $set: { default: false } });
+ 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) default property have been updated in Tenant '${tenant.name}'`
+ });
+ }
+ }
+
+ getVersion(): string {
+ return '1.0';
+ }
+
+ getName(): string {
+ return 'AddDefaultPropertyToTagsTask';
+ }
+
+ isAsynchronous(): boolean {
+ return true;
+ }
+}

MODIFY
src/server/rest/service/UserService.ts
src/server/rest/service/UserService.ts
@@ -1218,6 +1218,10 @@ export default class UserService {
action: action
});
}
+ // Clear default tag
+ if(filteredRequest.default){
+ await UserStorage.clearTagUserDefault(req.user.tenantID,filteredRequest.userID);
+ }
// Create
const newTag: Tag = {
id: filteredRequest.id.toUpperCase(),
@@ -1226,7 +1230,8 @@ export default class UserService {
active: filteredRequest.active,
createdBy: { id: req.user.id },
createdOn: new Date(),
- userID: filteredRequest.userID
+ userID: filteredRequest.userID,
+ default: filteredRequest.default
} as Tag;
// Save
await UserStorage.saveTag(req.user.tenantID, newTag);
@@ -1328,10 +1333,14 @@ export default class UserService {
}
formerTagOwnerID = tag.userID;
}
+ if(filteredRequest.default && (tag.default !== filteredRequest.default)){
+ await UserStorage.clearTagUserDefault(req.user.tenantID,filteredRequest.userID);
+ }
// Update
tag.description = filteredRequest.description;
tag.active = filteredRequest.active;
tag.userID = filteredRequest.userID;
+ tag.default = filteredRequest.default;
tag.lastChangedBy = { id: req.user.id };
tag.lastChangedOn = new Date();
// Save

MODIFY
src/server/rest/service/security/UserSecurity.ts
src/server/rest/service/security/UserSecurity.ts
@@ -251,6 +251,7 @@ export default class UserSecurity {
description: sanitize(tag.description),
active: UtilsSecurity.filterBoolean(tag.active),
issuer: UtilsSecurity.filterBoolean(tag.issuer),
+ default: UtilsSecurity.filterBoolean(tag.default),
userID: sanitize(tag.userID)
} as Tag;
}
@@ -270,6 +271,7 @@ export default class UserSecurity {
filteredTag.active = tag.active;
filteredTag.transactionsCount = tag.transactionsCount;
filteredTag.userID = tag.userID;
+ filteredTag.default = tag.default;
if (tag.user) {
filteredTag.user = UserSecurity.filterMinimalUserResponse(tag.user, loggedUser);
}

MODIFY
src/storage/mongodb/UserStorage.ts
src/storage/mongodb/UserStorage.ts
@@ -315,6 +315,7 @@ export default class UserStorage {
userID: Utils.convertToObjectID(tag.userID),
issuer: Utils.convertToBoolean(tag.issuer),
active: Utils.convertToBoolean(tag.active),
+ default: Utils.convertToBoolean(tag.default),
ocpiToken: tag.ocpiToken,
description: tag.description,
deleted: Utils.objectHasProperty(tag, 'deleted') ? tag.deleted : false
@@ -332,6 +333,20 @@ export default class UserStorage {
Logging.traceEnd(MODULE_NAME, 'saveTag', uniqueTimerID, { id: tag.id });
}
+ public static async clearTagUserDefault(tenantID: string, userID: string): Promise<void> {
+ const uniqueTimerID = Logging.traceStart(MODULE_NAME, 'clearTagUserDefault');
+ await Utils.checkTenant(tenantID);
+ await global.database.getCollection<any>(tenantID, 'tags').updateMany(
+ {
+ userID: Utils.convertToObjectID(userID),
+ default: true
+ },
+ {
+ $set: { default: false }
+ });
+ Logging.traceEnd(MODULE_NAME, 'clearTagUserDefault', uniqueTimerID, { userID });
+ }
+
public static async deleteTag(tenantID: string, userID: string, tag: Tag): Promise<void> {
// Debug
const uniqueTimerID = Logging.traceStart(MODULE_NAME, 'deleteTag');

MODIFY
src/types/Tag.ts
src/types/Tag.ts
@@ -12,5 +12,6 @@ export default interface Tag {
transactionsCount?: number;
ocpiToken?: OCPIToken;
user?: User;
+ default?: boolean
deleted?: boolean
}

Add default property to tag