Deleted Tags for deleted users
MODIFY
src/migration/MigrationHandler.ts
src/migration/MigrationHandler.ts
@@ -16,6 +16,7 @@ import CleanupMeterValuesTask from './tasks/CleanupMeterValuesTask';
import CleanupOrphanBadgeTask from './tasks/CleanupOrphanBadgeTask';
import CleanupSiteAreasTask from './tasks/CleanupSiteAreasTask';
import Constants from '../utils/Constants';
+import DeleteTagsForDeletedUsersTask from './tasks/DeleteTagsForDeletedUsersTask';
import InitialCarImportTask from './tasks/InitialCarImportTask';
import { LockEntity } from '../types/Locking';
import LockingManager from '../locking/LockingManager';
@@ -88,6 +89,7 @@ export default class MigrationHandler {
currentMigrationTasks.push(new UnmarkTransactionExtraInactivitiesTask());
currentMigrationTasks.push(new RecomputeAllTransactionsConsumptionsTask());
currentMigrationTasks.push(new AddUserInTransactionsTask());
+ currentMigrationTasks.push(new DeleteTagsForDeletedUsersTask());
currentMigrationTasks.push(new AddCreatedPropertiesToBadgeTask());
currentMigrationTasks.push(new AddDescriptionToTagsTask());
currentMigrationTasks.push(new AlignTagsWithUsersIssuerTask());

ADD
src/migration/tasks/DeleteTagsForDeletedUsersTask.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 = 'DeleteTagsForDeletedUsersTask';
+
+export default class DeleteTagsForDeletedUsersTask 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;
+ const users = await global.database.getCollection<any>(tenant.id, 'users').find({
+ 'deleted': true
+ }).toArray();
+ if (!Utils.isEmptyArray(users)) {
+ for (const user of users) {
+ // Update
+ result = await global.database.getCollection(tenant.id, 'tags').updateMany(
+ {
+ userID: user._id
+ },
+ {
+ $set: {
+ deleted: true,
+ }
+ }
+ );
+ updated += result.modifiedCount;
+ }
+ }
+ // Log in the default tenant
+ if (updated > 0) {
+ Logging.logDebug({
+ tenantID: Constants.DEFAULT_TENANT,
+ action: ServerAction.MIGRATION,
+ module: MODULE_NAME, method: 'migrateTenant',
+ message: `${updated} Tags(s) have been updated in Tenant '${tenant.name}'`
+ });
+ }
+ }
+
+ getVersion(): string {
+ return '1.0';
+ }
+
+ getName(): string {
+ return 'DeleteTagsForDeletedUsersTask';
+ }
+
+ isAsynchronous(): boolean {
+ return true;
+ }
+}
+
+

MODIFY
src/server/rest/service/UserService.ts
src/server/rest/service/UserService.ts
@@ -279,6 +279,7 @@ export default class UserService {
for (const tag of user.tags) {
if (tag.transactionsCount > 0) {
tag.active = false;
+ tag.deleted = true;
tag.lastChangedOn = new Date();
tag.lastChangedBy = { id: req.user.id };
tag.userID = user.id;

MODIFY
src/storage/mongodb/UserStorage.ts
src/storage/mongodb/UserStorage.ts
@@ -315,7 +315,8 @@ export default class UserStorage {
issuer: Utils.convertToBoolean(tag.issuer),
active: Utils.convertToBoolean(tag.active),
ocpiToken: tag.ocpiToken,
- description: tag.description
+ description: tag.description,
+ deleted: Utils.objectHasProperty(tag, 'deleted') ? tag.deleted : false
};
// Check Created/Last Changed By
DatabaseUtils.addLastChangedCreatedProps(tagMDB, tag);
@@ -757,7 +758,9 @@ export default class UserStorage {
dbParams.skip = Utils.checkRecordSkip(dbParams.skip);
// Create Aggregation
const aggregation = [];
- const filters: FilterParams = {};
+ const filters: FilterParams = {
+ '$or': DatabaseUtils.getNotDeletedFilter()
+ };
// Filter by other properties
if (params.search) {
const searchRegex = Utils.escapeSpecialCharsInRegex(params.search);

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

Deleted Tags for deleted users