Updated server types + do not update tags/users in import when already used/active
MODIFY
src/scheduler/tasks/ImportTagsTask.ts
src/scheduler/tasks/ImportTagsTask.ts
@@ -31,9 +31,9 @@ export default class ImportTagsTask extends SchedulerTask {
// Get total number of Tags to import
const totalTagsToImport = await TagStorage.getImportedTagsCount(tenant.id);
if (totalTagsToImport > 0) {
- await Logging.logDebug({
+ await Logging.logInfo({
tenantID: tenant.id,
- action: ServerAction.IMPORT_TAGS,
+ action: ServerAction.TAGS_IMPORT,
module: MODULE_NAME, method: 'processTenant',
message: `${totalTagsToImport} Tag(s) are going to be imported...`
});
@@ -46,6 +46,19 @@ export default class ImportTagsTask extends SchedulerTask {
// Existing tags
const foundTag = await TagStorage.getTag(tenant.id, importedTag.id);
if (foundTag) {
+ // Check tag is already in use
+ if (!foundTag.issuer) {
+ throw new Error('Tag is not local to the organization');
+ }
+ if (foundTag.deleted) {
+ throw new Error('Tag is deleted');
+ }
+ if (foundTag.userID) {
+ throw new Error('Tag is already assigned to an user');
+ }
+ if (foundTag.active) {
+ throw new Error('Tag is already active');
+ }
// Update it
await TagStorage.saveTag(tenant.id, { ...foundTag, ...importedTag });
// Remove the imported Tag
@@ -78,10 +91,10 @@ export default class ImportTagsTask extends SchedulerTask {
// Log
await Logging.logError({
tenantID: tenant.id,
- action: ServerAction.IMPORT_TAGS,
+ action: ServerAction.TAGS_IMPORT,
module: MODULE_NAME, method: 'processTenant',
- message: `An error occurred while importing the Tag ID '${importedTag.id}'`,
- detailedMessages: { error: error.message, stack: error.stack }
+ message: `Error when importing Tag ID '${importedTag.id}': ${error.message}`,
+ detailedMessages: { tag: importedTag, error: error.message, stack: error.stack }
});
}
}
@@ -90,7 +103,7 @@ export default class ImportTagsTask extends SchedulerTask {
const intermediateDurationSecs = Math.round((new Date().getTime() - startTime) / 1000);
await Logging.logDebug({
tenantID: tenant.id,
- action: ServerAction.IMPORT_TAGS,
+ action: ServerAction.TAGS_IMPORT,
module: MODULE_NAME, method: 'processTenant',
message: `${result.inError + result.inSuccess}/${totalTagsToImport} Tag(s) have been processed in ${intermediateDurationSecs}s...`
});
@@ -98,7 +111,7 @@ export default class ImportTagsTask extends SchedulerTask {
} while (!Utils.isEmptyArray(importedTags?.result));
// Log final results
const executionDurationSecs = Math.round((new Date().getTime() - startTime) / 1000);
- await Logging.logActionsResponse(tenant.id, ServerAction.IMPORT_TAGS, MODULE_NAME, 'processTenant', result,
+ await Logging.logActionsResponse(tenant.id, ServerAction.TAGS_IMPORT, MODULE_NAME, 'processTenant', result,
`{{inSuccess}} Tag(s) have been imported successfully in ${executionDurationSecs}s in Tenant ${Utils.buildTenantName(tenant)}`,
`{{inError}} Tag(s) failed to be imported in ${executionDurationSecs}s in Tenant ${Utils.buildTenantName(tenant)}`,
`{{inSuccess}} Tag(s) have been imported successfully but {{inError}} failed in ${executionDurationSecs}s in Tenant ${Utils.buildTenantName(tenant)}`,
@@ -106,7 +119,7 @@ export default class ImportTagsTask extends SchedulerTask {
);
} catch (error) {
// Log error
- await Logging.logActionExceptionMessage(tenant.id, ServerAction.IMPORT_TAGS, error);
+ await Logging.logActionExceptionMessage(tenant.id, ServerAction.TAGS_IMPORT, error);
} finally {
// Release the lock
await LockingManager.release(importTagsLock);

MODIFY
src/scheduler/tasks/ImportUsersTask.ts
src/scheduler/tasks/ImportUsersTask.ts
@@ -31,9 +31,9 @@ export default class ImportUsersTask extends SchedulerTask {
// Get total number of Users to import
const totalUsersToImport = await UserStorage.getImportedUsersCount(tenant.id);
if (totalUsersToImport > 0) {
- await Logging.logDebug({
+ await Logging.logInfo({
tenantID: tenant.id,
- action: ServerAction.IMPORT_USERS,
+ action: ServerAction.USERS_IMPORT,
module: MODULE_NAME, method: 'processTenant',
message: `${totalUsersToImport} User(s) are going to be imported...`
});
@@ -46,6 +46,16 @@ export default class ImportUsersTask extends SchedulerTask {
// Existing Users
const foundUser = await UserStorage.getUserByEmail(tenant.id, importedUser.email);
if (foundUser) {
+ // Check tag is already in use
+ if (!foundUser.issuer) {
+ throw new Error('User is not local to the organization');
+ }
+ if (foundUser.deleted) {
+ throw new Error('User is deleted');
+ }
+ if (foundUser.status !== UserStatus.PENDING) {
+ throw new Error('User account is no longer pending');
+ }
// Update it
foundUser.name = importedUser.name;
foundUser.firstName = importedUser.firstName;
@@ -87,10 +97,10 @@ export default class ImportUsersTask extends SchedulerTask {
// Log
await Logging.logError({
tenantID: tenant.id,
- action: ServerAction.IMPORT_USERS,
+ action: ServerAction.USERS_IMPORT,
module: MODULE_NAME, method: 'processTenant',
- message: `An error occurred when importing user with email '${importedUser.email}'`,
- detailedMessages: { error: error.message, stack: error.stack }
+ message: `Error when importing User with email '${importedUser.email}': ${error.message}`,
+ detailedMessages: { user: importedUser, error: error.message, stack: error.stack }
});
}
}
@@ -99,7 +109,7 @@ export default class ImportUsersTask extends SchedulerTask {
const intermediateDurationSecs = Math.round((new Date().getTime() - startTime) / 1000);
await Logging.logDebug({
tenantID: tenant.id,
- action: ServerAction.IMPORT_USERS,
+ action: ServerAction.USERS_IMPORT,
module: MODULE_NAME, method: 'processTenant',
message: `${result.inError + result.inSuccess}/${totalUsersToImport} User(s) have been processed in ${intermediateDurationSecs}s...`
});
@@ -107,7 +117,7 @@ export default class ImportUsersTask extends SchedulerTask {
} while (!Utils.isEmptyArray(importedUsers?.result));
// Log final results
const executionDurationSecs = Math.round((new Date().getTime() - startTime) / 1000);
- await Logging.logActionsResponse(tenant.id, ServerAction.IMPORT_USERS, MODULE_NAME, 'processTenant', result,
+ await Logging.logActionsResponse(tenant.id, ServerAction.USERS_IMPORT, MODULE_NAME, 'processTenant', result,
`{{inSuccess}} User(s) have been imported successfully in ${executionDurationSecs}s in Tenant ${Utils.buildTenantName(tenant)}`,
`{{inError}} User(s) failed to be imported in ${executionDurationSecs}s in Tenant ${Utils.buildTenantName(tenant)}`,
`{{inSuccess}} User(s) have been imported successfully but {{inError}} failed in ${executionDurationSecs}s in Tenant ${Utils.buildTenantName(tenant)}`,
@@ -115,7 +125,7 @@ export default class ImportUsersTask extends SchedulerTask {
);
} catch (error) {
// Log error
- await Logging.logActionExceptionMessage(tenant.id, ServerAction.IMPORT_USERS, error);
+ await Logging.logActionExceptionMessage(tenant.id, ServerAction.USERS_IMPORT, error);
} finally {
// Release the lock
await LockingManager.release(importUsersLock);

MODIFY
src/server/rest/v1/service/TagService.ts
src/server/rest/v1/service/TagService.ts
@@ -650,7 +650,7 @@ export default class TagService {
module: MODULE_NAME, method: 'insertTags',
action: action,
user: user.id,
- message: `Cannot import '${error.writeErrors.length as number}' tags!`,
+ message: `Cannot import ${error.writeErrors.length as number} tags!`,
detailedMessages: { error: error.message, stack: error.stack, tagsError: error.writeErrors }
});
}

MODIFY
src/server/rest/v1/service/UserService.ts
src/server/rest/v1/service/UserService.ts
@@ -1306,7 +1306,7 @@ export default class UserService {
module: MODULE_NAME, method: 'insertUsers',
action: action,
user: user.id,
- message: `Cannot import '${error.writeErrors.length as number}' users!`,
+ message: `Cannot import ${error.writeErrors.length as number} users!`,
detailedMessages: { error: error.message, stack: error.stack, tagsError: error.writeErrors }
});
}

MODIFY
src/types/Server.ts
src/types/Server.ts
@@ -389,8 +389,6 @@ export enum ServerAction {
USER = 'User',
USERS_EXPORT = 'UsersExport',
USERS_IMPORT = 'UsersImport',
- IMPORT_USERS = 'ImportUsers',
- IMPORT_TAGS = 'ImportTags',
NOTIFICATIONS = 'Notifications',

Updated server types + do not update tags/users in import when already used/active