reorganised code
MODIFY
src/async-task/tasks/ImportAsyncTask.ts
src/async-task/tasks/ImportAsyncTask.ts
import User, { ImportedUser, UserRole, UserStatus } from '../../types/User';
import AbstractAsyncTask from '../AsyncTask';
-import { ActionsResponse } from '../../types/GlobalType';
import Constants from '../../utils/Constants';
import { ImportedTag } from '../../types/Tag';
import Logging from '../../utils/Logging';
@@ -23,29 +22,31 @@ export default class ImportAsyncTask extends AbstractAsyncTask {
// usersSite: Map<string, string[]> = new Map();
usersSite: Record<string, string[]> = {};
- protected async checkUserExists(tenant: Tenant, importedUser: ImportedUser|ImportedTag): Promise<User> {
+ protected async processImportedUser(tenant: Tenant, importedUser: ImportedUser|ImportedTag): Promise<User> {
// Existing Users
- const foundUser = await UserStorage.getUserByEmail(tenant.id, importedUser.email);
- if (foundUser) {
+ let user = await UserStorage.getUserByEmail(tenant.id, importedUser.email);
+ if (user) {
// Check tag is already in use
- if (!foundUser.issuer) {
+ if (!user.issuer) {
throw new Error('User is not local to the organization');
}
- if (foundUser.status !== UserStatus.PENDING) {
+ if (user.status !== UserStatus.PENDING) {
throw new Error('User account is already in use');
}
// Update it
- foundUser.name = importedUser.name;
- foundUser.firstName = importedUser.firstName;
- await UserStorage.saveUser(tenant.id, foundUser);
- if (importedUser.siteIDs && Utils.isTenantComponentActive(tenant, TenantComponents.ORGANIZATION)) {
- await this.checkSitesAndAssign(tenant, foundUser, importedUser);
+ user.name = importedUser.name;
+ user.firstName = importedUser.firstName;
+ await UserStorage.saveUser(tenant.id, user);
+ } else {
+ user = await this.createUser(tenant, importedUser);
}
+ if (importedUser.siteIDs && Utils.isTenantComponentActive(tenant, TenantComponents.ORGANIZATION)) {
+ await this.checkSitesAndAssign(tenant, user, importedUser);
}
- return foundUser;
+ return user;
}
- protected async createUser(tenant: Tenant, importedUser: ImportedUser|ImportedTag, result?: ActionsResponse): Promise<User> {
+ protected async createUser(tenant: Tenant, importedUser: ImportedUser|ImportedTag): Promise<User> {
// New User
const newUser = UserStorage.createNewUser() as User;
// Set
@@ -61,9 +62,6 @@ export default class ImportAsyncTask extends AbstractAsyncTask {
await UserStorage.saveUserRole(tenant.id, newUser.id, UserRole.BASIC);
// Status need to be set separately
await UserStorage.saveUserStatus(tenant.id, newUser.id, importedUser.importedData.autoActivateUserAtImport ? UserStatus.ACTIVE : UserStatus.INACTIVE);
- if (importedUser.siteIDs && Utils.isTenantComponentActive(tenant, TenantComponents.ORGANIZATION)) {
- await this.checkSitesAndAssign(tenant, newUser, importedUser);
- }
await this.sendNotifications(tenant, newUser);
return newUser;
}

MODIFY
src/async-task/tasks/TagsImportAsyncTask.ts
src/async-task/tasks/TagsImportAsyncTask.ts
@@ -12,7 +12,6 @@ import { ServerAction } from '../../types/Server';
import TagStorage from '../../storage/mongodb/TagStorage';
import Tenant from '../../types/Tenant';
import TenantStorage from '../../storage/mongodb/TenantStorage';
-import User from '../../types/User';
import Utils from '../../utils/Utils';
const MODULE_NAME = 'TagsImportAsyncTask';
@@ -82,7 +81,7 @@ export default class TagsImportAsyncTask extends ImportAsyncTask {
}
// Save user if any and get the ID to assign tag
if (importedTag.email && importedTag.name && importedTag.firstName) {
- await this.createUserAndAssignTag(tenant, importedTag, tagToSave);
+ await this.processImportedTag(tenant, importedTag, tagToSave);
}
// Save the new Tag
await TagStorage.saveTag(tenant.id, tagToSave);
@@ -135,12 +134,9 @@ export default class TagsImportAsyncTask extends ImportAsyncTask {
}
}
- private async createUserAndAssignTag(tenant: Tenant, importedTag: ImportedTag, tag: Tag) {
- let user: User = null;
+ private async processImportedTag(tenant: Tenant, importedTag: ImportedTag, tag: Tag) {
// if user not found we create one
- if (Utils.isNullOrUndefined(user = await this.checkUserExists(tenant, importedTag))) {
- user = await this.createUser(tenant, importedTag);
- }
+ const user = await this.processImportedUser(tenant, importedTag);
tag.userID = user.id;
await TagStorage.clearDefaultUserTag(tenant.id, user.id);
tag.default = true;

MODIFY
src/async-task/tasks/UsersImportAsyncTask.ts
src/async-task/tasks/UsersImportAsyncTask.ts
@@ -44,9 +44,7 @@ export default class UsersImportAsyncTask extends ImportAsyncTask {
for (const importedUser of importedUsers.result) {
try {
// if user not found we create one
- if (Utils.isNullOrUndefined(await this.checkUserExists(tenant, importedUser))) {
- await this.createUser(tenant, importedUser, result);
- }
+ await this.processImportedUser(tenant, importedUser);
// Remove the imported User either it's found or not
await UserStorage.deleteImportedUser(tenant.id, importedUser.id);
result.inSuccess++;

reorganised code