Migration task for making only one default badge for each user
MODIFY
src/migration/tasks/SetDefaultTagToUserTask.ts
src/migration/tasks/SetDefaultTagToUserTask.ts
@@ -2,6 +2,7 @@ import Constants from '../../utils/Constants';
import Logging from '../../utils/Logging';
import MigrationTask from '../MigrationTask';
import { ServerAction } from '../../types/Server';
+import Tag from '../../types/Tag';
import TagStorage from '../../storage/mongodb/TagStorage';
import Tenant from '../../types/Tenant';
import TenantStorage from '../../storage/mongodb/TenantStorage';
@@ -20,19 +21,40 @@ export default class SetDefaultTagToUserTask extends MigrationTask {
async migrateTenant(tenant: Tenant): Promise<void> {
let modifiedCount = 0;
+ // Get all the Users
const users = await UserStorage.getUsers(tenant.id, {
issuer: true,
}, Constants.DB_PARAMS_MAX_LIMIT);
- if (users.count > 0) {
+ // Process them
+ if (!Utils.isEmptyArray(users.result)) {
for (const user of users.result) {
- const tagsMDB = await TagStorage.getTags(tenant.id, {
+ // Get all the User's Tags
+ const tags = await TagStorage.getTags(tenant.id, {
userIDs: [user.id]
- }, Constants.DB_PARAMS_SINGLE_RECORD);
- if (tagsMDB.count === 1) {
- if (!tagsMDB.result[0].default) {
- tagsMDB.result[0].default = true;
- await TagStorage.saveTag(tenant.id, tagsMDB.result[0]);
- modifiedCount++;
+ }, Constants.DB_PARAMS_MAX_LIMIT);
+ // Process them
+ if (!Utils.isEmptyArray(tags.result)) {
+ let numberOfDefaultTag = 0;
+ let activeTag: Tag;
+ for (const tag of tags.result) {
+ // Count default Tag
+ if (tag.default) {
+ numberOfDefaultTag++;
+ }
+ // Keep an active tag
+ if (tag.active) {
+ activeTag = tag;
+ }
+ }
+ // More than one default Tag or no Tag at all
+ if (numberOfDefaultTag !== 1) {
+ // Clear default User's Tags
+ modifiedCount += numberOfDefaultTag;
+ await TagStorage.clearDefaultUserTag(tenant.id, user.id);
+ if (activeTag) {
+ activeTag.default = true;
+ await TagStorage.saveTag(tenant.id, activeTag);
+ }
}
}
}
@@ -49,7 +71,7 @@ export default class SetDefaultTagToUserTask extends MigrationTask {
}
getVersion(): string {
- return '1.0';
+ return '1.1';
}
getName(): string {

Migration task for making only one default badge for each user