Added import users in async task
RENAME
src/scheduler/tasks/ImportUsersTask.ts
src/async-task/tasks/UsersImportAsyncTask.ts
import { ActionsResponse, ImportStatus } from '../../types/GlobalType';
import User, { ImportedUser, UserRole, UserStatus } from '../../types/User';
+import AbstractAsyncTask from '../AsyncTask';
import Constants from '../../utils/Constants';
import { DataResult } from '../../types/DataResult';
import DbParams from '../../types/database/DbParams';
import LockingHelper from '../../locking/LockingHelper';
import LockingManager from '../../locking/LockingManager';
import Logging from '../../utils/Logging';
-import SchedulerTask from '../SchedulerTask';
import { ServerAction } from '../../types/Server';
-import { TaskConfig } from '../../types/TaskConfig';
-import Tenant from '../../types/Tenant';
+import TenantStorage from '../../storage/mongodb/TenantStorage';
import UserStorage from '../../storage/mongodb/UserStorage';
import Utils from '../../utils/Utils';
-const MODULE_NAME = 'SynchronizeUsersImportTask';
+const MODULE_NAME = 'UsersImportAsyncTask';
-export default class ImportUsersTask extends SchedulerTask {
- public async processTenant(tenant: Tenant, config?: TaskConfig): Promise<void> {
- const importUsersLock = await LockingHelper.createImportUsersLock(tenant.id);
+export default class UsersImportAsyncTask extends AbstractAsyncTask {
+ protected async executeAsyncTask(): Promise<void> {
+ const importUsersLock = await LockingHelper.createImportUsersLock(this.asyncTask.tenantID);
if (importUsersLock) {
+ const tenant = await TenantStorage.getTenant(this.asyncTask.tenantID);
try {
const dbParams: DbParams = { limit: Constants.IMPORT_PAGE_SIZE, skip: 0 };
let importedUsers: DataResult<ImportedUser>;

MODIFY
src/scheduler/SchedulerManager.ts
src/scheduler/SchedulerManager.ts
@@ -7,7 +7,6 @@ import CheckSessionNotStartedAfterAuthorizeTask from './tasks/CheckSessionNotSta
import CheckUserAccountInactivityTask from './tasks/CheckUserAccountInactivityTask';
import Configuration from '../utils/Configuration';
import Constants from '../utils/Constants';
-import ImportUsersTask from './tasks/ImportUsersTask';
import Logging from '../utils/Logging';
import LoggingDatabaseTableCleanupTask from './tasks/LoggingDatabaseTableCleanupTask';
import MigrateSensitiveDataTask from './tasks/MigrateSensitiveDataTask';
@@ -132,9 +131,6 @@ export default class SchedulerManager {
case 'CheckChargingStationTemplateTask':
schedulerTask = new CheckChargingStationTemplateTask();
break;
- case 'ImportUsersTask':
- schedulerTask = new ImportUsersTask();
- break;
case 'MigrateSensitiveDataTask':
schedulerTask = new MigrateSensitiveDataTask();
break;

MODIFY
src/server/rest/v1/service/UserService.ts
src/server/rest/v1/service/UserService.ts
import { Action, Entity } from '../../../../types/Authorization';
import { ActionsResponse, ImportStatus } from '../../../../types/GlobalType';
+import { AsyncTaskType, AsyncTasks } from '../../../../types/AsyncTask';
import { HTTPAuthError, HTTPError } from '../../../../types/HTTPError';
import { NextFunction, Request, Response } from 'express';
import { OCPITokenType, OCPITokenWhitelist } from '../../../../types/ocpi/OCPIToken';
@@ -8,6 +9,7 @@ import User, { ImportedUser, UserRequiredImportProperties, UserStatus } from '..
import Address from '../../../../types/Address';
import AppAuthError from '../../../../exception/AppAuthError';
import AppError from '../../../../exception/AppError';
+import AsyncTaskManager from '../../../../async-task/AsyncTaskManager';
import AuthorizationService from './AuthorizationService';
import Authorizations from '../../../../authorization/Authorizations';
import BillingFactory from '../../../../integration/billing/BillingFactory';
@@ -21,7 +23,6 @@ import Cypher from '../../../../utils/Cypher';
import { DataResult } from '../../../../types/DataResult';
import EmspOCPIClient from '../../../../client/ocpi/EmspOCPIClient';
import I18nManager from '../../../../utils/I18nManager';
-import ImportUsersTask from '../../../../scheduler/tasks/ImportUsersTask';
import JSONStream from 'JSONStream';
import LockingHelper from '../../../../locking/LockingHelper';
import LockingManager from '../../../../locking/LockingManager';
@@ -980,8 +981,15 @@ export default class UserService {
`{{inSuccess}} User(s) were successfully uploaded in ${executionDurationSecs}s and ready for asynchronous import and {{inError}} failed to be uploaded`,
`No User have been uploaded in ${executionDurationSecs}s`, req.user
);
- // Trigger manually and asynchronously the job
- void new ImportUsersTask().processTenant(req.tenant);
+ // Create and Save async task
+ AsyncTaskManager.createAndSaveAsyncTasks({
+ name: AsyncTasks.USERS_IMPORT,
+ action: ServerAction.USERS_IMPORT,
+ type: AsyncTaskType.TASK,
+ tenantID: req.tenant.id,
+ module: MODULE_NAME,
+ method: 'handleImportUsers',
+ }, req.user);
// Respond
res.json({ ...result, ...Constants.REST_RESPONSE_SUCCESS });
next();

MODIFY
src/types/AsyncTask.ts
src/types/AsyncTask.ts
@@ -28,7 +28,9 @@ export enum AsyncTaskStatus {
ERROR = 'E',
}
+
export enum AsyncTasks {
TAGS_IMPORT = 'TagsImportAsyncTask',
+ USERS_IMPORT = 'UsersImportAsyncTask',
}

Added import users in async task