changed to import helper
RENAME
src/async-task/tasks/ImportAsyncTask.ts
src/async-task/tasks/ImportHelper.ts
import Tag, { ImportedTag } from '../../types/Tag';
import User, { ImportedUser, UserRole, UserStatus } from '../../types/User';
-import AbstractAsyncTask from '../AsyncTask';
import Constants from '../../utils/Constants';
import Logging from '../../utils/Logging';
import NotificationHandler from '../../notification/NotificationHandler';
@@ -14,43 +13,41 @@ import TenantComponents from '../../types/TenantComponents';
import UserStorage from '../../storage/mongodb/UserStorage';
import Utils from '../../utils/Utils';
-const MODULE_NAME = 'ImportAsyncTask';
+const MODULE_NAME = 'ImportHelper';
-export default abstract class ImportAsyncTask extends AbstractAsyncTask {
- // To store existing sites from db to avoid getting the sites erverytime
- private existingAutoAssignableSites: Map<string, Site> = new Map();
- private existingSites: Map<string, Site> = new Map();
-
- protected async processImportedUser(tenant: Tenant, importedUser: ImportedUser): Promise<User> {
+export default class ImportHelper {
+ public async processImportedUser(tenant: Tenant, importedUser: ImportedUser, existingSites: Map<string, Site>): Promise<User> {
// Get User
let user = await UserStorage.getUserByEmail(tenant.id, importedUser.email);
+ // If one found lets update it else create new User
if (user) {
await this.updateUser(tenant, user, importedUser);
} else {
user = await this.createUser(tenant, importedUser);
}
- if (Utils.isTenantComponentActive(tenant, TenantComponents.ORGANIZATION) && !Utils.isEmptyArray(importedUser.siteIDs)) {
- await this.processSiteAssignment(tenant, user, importedUser);
+ if (Utils.isTenantComponentActive(tenant, TenantComponents.ORGANIZATION) && importedUser.siteIDs) {
+ await this.processSiteAssignment(tenant, user, importedUser, existingSites);
}
return user;
}
- protected async processImportedTag(tenant: Tenant, importedTag: ImportedTag): Promise<Tag> {
+ public async processImportedTag(tenant: Tenant, importedTag: ImportedTag, existingSites: Map<string, Site>): Promise<Tag> {
// Get Tag
let tag = await TagStorage.getTag(tenant.id, importedTag.id, { withNbrTransactions: true });
// Try to get Tag with Visual ID
if (!tag && importedTag.visualID) {
tag = await TagStorage.getTagByVisualID(tenant.id, importedTag.visualID, { withNbrTransactions: true });
}
+ // If one found lets update it else create new tag
if (tag) {
this.updateTag(tag, importedTag);
} else {
tag = this.createTag(importedTag);
}
- // Save user if any and get the ID to assign tag
+ // Save user if any and get the ID to assign current tag
if (importedTag.email && importedTag.name && importedTag.firstName) {
// Check & Import the User
- const user = await this.processImportedUser(tenant, importedTag as ImportedUser);
+ const user = await this.processImportedUser(tenant, importedTag as ImportedUser, existingSites);
// Assign
tag.userID = user.id;
// Make this Tag default
@@ -129,36 +126,23 @@ export default abstract class ImportAsyncTask extends AbstractAsyncTask {
return newUser;
}
- private async processSiteAssignment(tenant: Tenant, user: User, importedUser: ImportedUser): Promise<void> {
- // If we never got the sites from db -> construct array of existing sites that are autoassignable
- if (Utils.isEmptyArray(this.existingAutoAssignableSites)) {
+ private async processSiteAssignment(tenant: Tenant, user: User, importedUser: ImportedUser, existingSites: Map<string, Site>): Promise<void> {
+ // If we never got the sites from db -> construct array of existing sites
+ if (existingSites.size === 0) {
// Init Site collections
- const sites = await SiteStorage.getSites(tenant, {}, Constants.DB_PARAMS_MAX_LIMIT, ['id', 'name', 'autoUserSiteAssignment']);
+ const sites = await SiteStorage.getSites(tenant, {}, Constants.DB_PARAMS_MAX_LIMIT, ['id', 'name']);
for (const site of sites.result) {
- if (site.autoUserSiteAssignment) {
- this.existingAutoAssignableSites.set(site.id, site);
- }
- this.existingSites.set(site.id, site);
+ existingSites.set(site.id, site);
}
}
const importedSiteIDs = importedUser.siteIDs.split('|');
for (const importedSiteID of importedSiteIDs) {
- const existingAutoAssignableSite = this.existingAutoAssignableSites.get(importedSiteID);
- const existingSite = this.existingSites.get(importedSiteID);
+ const existingSite = existingSites.get(importedSiteID);
+ if (existingSite) {
// Assign Site
- if (existingAutoAssignableSite) {
await UserStorage.addSiteToUser(tenant.id, user.id, importedSiteID);
- // Site is not auto assignable
- } else if (existingSite) {
- await Logging.logWarning({
- tenantID: tenant.id,
- action: ServerAction.USERS_IMPORT,
- module: MODULE_NAME, method: 'executeAsyncTask',
- user,
- message: `Site '${existingSite.name}' with ID '${existingSite.id}' does not allow auto assignment`
- });
- // Site does not exist
} else {
+ // Site does not exist
await Logging.logError({
tenantID: tenant.id,
action: ServerAction.USERS_IMPORT,

MODIFY
src/async-task/tasks/TagsImportAsyncTask.ts
src/async-task/tasks/TagsImportAsyncTask.ts
import { ActionsResponse, ImportStatus } from '../../types/GlobalType';
+import AbstractAsyncTask from '../AsyncTask';
import Constants from '../../utils/Constants';
import { DataResult } from '../../types/DataResult';
import DbParams from '../../types/database/DbParams';
-import ImportAsyncTask from './ImportAsyncTask';
+import ImportHelper from './ImportHelper';
import { ImportedTag } from '../../types/Tag';
import LockingHelper from '../../locking/LockingHelper';
import LockingManager from '../../locking/LockingManager';
import Logging from '../../utils/Logging';
import { ServerAction } from '../../types/Server';
+import Site from '../../types/Site';
+import SiteStorage from '../../storage/mongodb/SiteStorage';
import TagStorage from '../../storage/mongodb/TagStorage';
import TenantStorage from '../../storage/mongodb/TenantStorage';
import Utils from '../../utils/Utils';
const MODULE_NAME = 'TagsImportAsyncTask';
-export default class TagsImportAsyncTask extends ImportAsyncTask {
+export default class TagsImportAsyncTask extends AbstractAsyncTask {
protected async executeAsyncTask(): Promise<void> {
const importTagsLock = await LockingHelper.acquireImportTagsLock(this.asyncTask.tenantID);
+ const importHelper = new ImportHelper();
+ const existingSites: Map<string, Site> = new Map();
if (importTagsLock) {
const tenant = await TenantStorage.getTenant(this.asyncTask.tenantID);
try {
+ // If we never got the sites from db -> construct array of existing sites
+ if (existingSites.size === 0) {
+ const sites = await SiteStorage.getSites(tenant, {}, Constants.DB_PARAMS_MAX_LIMIT, ['id', 'name']);
+ for (const site of sites.result) {
+ existingSites.set(site.id, site);
+ }
+ }
const dbParams: DbParams = { limit: Constants.IMPORT_PAGE_SIZE, skip: 0 };
let importedTags: DataResult<ImportedTag>;
const result: ActionsResponse = {
@@ -43,8 +55,8 @@ export default class TagsImportAsyncTask extends ImportAsyncTask {
importedTags = await TagStorage.getImportedTags(tenant.id, { status: ImportStatus.READY }, dbParams);
for (const importedTag of importedTags.result) {
try {
- // Check & Import the Tag
- await this.processImportedTag(tenant, importedTag);
+ // Check & Import the Tag (+ User if present)
+ await importHelper.processImportedTag(tenant, importedTag, existingSites);
// Remove the imported Tag
await TagStorage.deleteImportedTag(tenant.id, importedTag.id);
result.inSuccess++;

MODIFY
src/async-task/tasks/UsersImportAsyncTask.ts
src/async-task/tasks/UsersImportAsyncTask.ts
import { ActionsResponse, ImportStatus } from '../../types/GlobalType';
+import AbstractAsyncTask from '../AsyncTask';
import Constants from '../../utils/Constants';
import { DataResult } from '../../types/DataResult';
import DbParams from '../../types/database/DbParams';
-import ImportAsyncTask from './ImportAsyncTask';
+import ImportHelper from './ImportHelper';
import { ImportedUser } from '../../types/User';
import LockingHelper from '../../locking/LockingHelper';
import LockingManager from '../../locking/LockingManager';
import Logging from '../../utils/Logging';
import { ServerAction } from '../../types/Server';
+import Site from '../../types/Site';
+import SiteStorage from '../../storage/mongodb/SiteStorage';
import TenantStorage from '../../storage/mongodb/TenantStorage';
import UserStorage from '../../storage/mongodb/UserStorage';
import Utils from '../../utils/Utils';
const MODULE_NAME = 'UsersImportAsyncTask';
-export default class UsersImportAsyncTask extends ImportAsyncTask {
+export default class UsersImportAsyncTask extends AbstractAsyncTask {
protected async executeAsyncTask(): Promise<void> {
const importUsersLock = await LockingHelper.acquireImportUsersLock(this.asyncTask.tenantID);
+ const importHelper = new ImportHelper();
+ const existingSites: Map<string, Site> = new Map();
if (importUsersLock) {
const tenant = await TenantStorage.getTenant(this.asyncTask.tenantID);
try {
+ if (existingSites.size === 0) {
+ const sites = await SiteStorage.getSites(tenant, {}, Constants.DB_PARAMS_MAX_LIMIT, ['id', 'name']);
+ for (const site of sites.result) {
+ existingSites.set(site.id, site);
+ }
+ }
const dbParams: DbParams = { limit: Constants.IMPORT_PAGE_SIZE, skip: 0 };
let importedUsers: DataResult<ImportedUser>;
const result: ActionsResponse = {
@@ -44,7 +55,7 @@ export default class UsersImportAsyncTask extends ImportAsyncTask {
for (const importedUser of importedUsers.result) {
try {
// Check & Import the User
- await this.processImportedUser(tenant, importedUser);
+ await importHelper.processImportedUser(tenant, importedUser, existingSites);
// Remove the imported User either it's found or not
await UserStorage.deleteImportedUser(tenant.id, importedUser.id);
result.inSuccess++;

MODIFY
src/server/rest/v1/service/TagService.ts
src/server/rest/v1/service/TagService.ts
@@ -647,7 +647,7 @@ export default class TagService {
};
try {
UserValidator.getInstance().validateImportedUserCreation(newImportedUser);
- tagToImport = { ...tagToImport, ...newImportedUser };
+ tagToImport = { ...tagToImport, ...newImportedUser as ImportedTag };
} catch (error) {
await Logging.logWarning({
tenantID: req.user.tenantID,

MODIFY
src/server/rest/v1/service/UserService.ts
src/server/rest/v1/service/UserService.ts
@@ -84,10 +84,6 @@ export default class UserService {
const errorCodes: Array<StartTransactionErrorCode> = [];
// Check Billing errors
await UserService.checkBillingErrorCodes(action, req.tenant, req.user, user, errorCodes);
- // Add error if EULA are not accepted (use case -> at user import)
- if (!user.eulaAcceptedOn) {
- errorCodes.push(StartTransactionErrorCode.EULA_NOT_ACCEPTED);
- }
res.json({
tag, car, errorCodes
});
@@ -487,8 +483,7 @@ export default class UserService {
user.importedBy = importedBy;
user.importedOn = importedOn;
user.importedData = {
- 'autoActivateUserAtImport' : UtilsSecurity.filterBoolean(req.headers.autoactivateuseratimport),
- 'autoActivateTagAtImport' : UtilsSecurity.filterBoolean(req.headers.autoactivatetagatimport)
+ 'autoActivateUserAtImport' : UtilsSecurity.filterBoolean(req.headers.autoactivateuseratimport)
};
// Import
const importSuccess = await UserService.processUser(action, req, user, usersToBeImported);

MODIFY
src/types/User.ts
src/types/User.ts
@@ -40,7 +40,6 @@ export default interface User extends CreatedUpdatedProps, AuthorizationActions
authorizationID?: string;
importedData?: {
autoActivateUserAtImport: boolean;
- autoActivateTagAtImport: boolean;
};
}
@@ -63,7 +62,6 @@ export interface ImportedUser {
errorDescription?: string;
importedData?: {
autoActivateUserAtImport: boolean;
- autoActivateTagAtImport: boolean;
};
siteIDs?: string;
}

changed to import helper