Rename errorCode to status and add an ImportStatus enum
MODIFY
src/scheduler/tasks/SynchronizeTagsImportTask.ts
src/scheduler/tasks/SynchronizeTagsImportTask.ts
@@ -3,7 +3,7 @@ import Tag, { ImportedTag } from '../../types/Tag';
import Constants from '../../utils/Constants';
import { DataResult } from '../../types/DataResult';
import DbParams from '../../types/database/DbParams';
-import { HTTPError } from '../../types/HTTPError';
+import { ImportStatus } from '../../types/GlobalType';
import { LockEntity } from '../../types/Locking';
import LockingManager from '../../locking/LockingManager';
import Logging from '../../utils/Logging';
@@ -25,7 +25,7 @@ export default class SynchronizeTagsImportTask extends SchedulerTask {
let importedTags: DataResult<ImportedTag>;
do {
// Get the imported tags
- importedTags = await TagStorage.getImportedTags(tenant.id, { withNoError: true }, dbParams);
+ importedTags = await TagStorage.getImportedTags(tenant.id, { status: ImportStatus.READY }, dbParams);
for (const importedTag of importedTags.result) {
try {
// Existing tags
@@ -67,7 +67,7 @@ export default class SynchronizeTagsImportTask extends SchedulerTask {
});
} catch (error) {
// Update the imported Tag
- importedTag.errorCode = HTTPError.GENERAL_ERROR;
+ importedTag.status = ImportStatus.ERROR;
importedTag.errorDescription = error.message;
// Update it
await TagStorage.saveImportedTag(tenant.id, importedTag);

MODIFY
src/scheduler/tasks/SynchronizeUsersImportTask.ts
src/scheduler/tasks/SynchronizeUsersImportTask.ts
@@ -2,7 +2,7 @@ import User, { UserRole, UserStatus } from '../../types/User';
import Constants from '../../utils/Constants';
import DbParams from '../../types/database/DbParams';
-import { HTTPError } from '../../types/HTTPError';
+import { ImportStatus } from '../../types/GlobalType';
import { LockEntity } from '../../types/Locking';
import LockingManager from '../../locking/LockingManager';
import Logging from '../../utils/Logging';
@@ -21,12 +21,12 @@ export default class SynchronizeUsersImportTask extends SchedulerTask {
if (await LockingManager.acquire(synchronizeUsersImport)) {
try {
const dbParams: DbParams = { limit: Constants.EXPORT_PAGE_SIZE, skip: 0 };
- let importedUsers = await UserStorage.getImportedUsers(tenant.id, { withNoError: true }, dbParams);
+ let importedUsers = await UserStorage.getImportedUsers(tenant.id, { status: ImportStatus.READY }, dbParams);
while (importedUsers && !Utils.isEmptyArray(importedUsers.result)) {
for (const importedUser of importedUsers.result) {
const foundUser = await UserStorage.getUserByEmail(tenant.id, importedUser.email);
if (foundUser) {
- importedUser.errorCode = HTTPError.USER_EMAIL_ALREADY_EXIST_ERROR;
+ importedUser.status = ImportStatus.ERROR;
importedUser.errorDescription = 'Email already exists';
await UserStorage.saveImportedUser(tenant.id, importedUser);
} else {
@@ -57,7 +57,7 @@ export default class SynchronizeUsersImportTask extends SchedulerTask {
message: `User with email: ${importedUser.email} have been created in Tenant ${Utils.buildTenantName(tenant)}`
});
} catch (error) {
- importedUser.errorCode = HTTPError.GENERAL_ERROR;
+ importedUser.status = ImportStatus.ERROR;
importedUser.errorDescription = error.message;
await UserStorage.saveImportedUser(tenant.id, importedUser);
// Error
@@ -71,7 +71,7 @@ export default class SynchronizeUsersImportTask extends SchedulerTask {
}
}
}
- importedUsers = await UserStorage.getImportedUsers(tenant.id, { withNoError: true }, dbParams);
+ importedUsers = await UserStorage.getImportedUsers(tenant.id, { status: ImportStatus.READY }, dbParams);
}
} catch (error) {
// Log error

MODIFY
src/storage/mongodb/TagStorage.ts
src/storage/mongodb/TagStorage.ts
import Tag, { ImportedTag } from '../../types/Tag';
-import global, { FilterParams } from '../../types/GlobalType';
+import global, { FilterParams, ImportStatus } from '../../types/GlobalType';
import Constants from '../../utils/Constants';
import { DataResult } from '../../types/DataResult';
@@ -46,7 +46,7 @@ export default class TagStorage {
description: importedTagToSave.description,
importedOn: Utils.convertToDate(importedTagToSave.importedOn),
importedBy: Utils.convertToObjectID(importedTagToSave.importedBy),
- errorCode: importedTagToSave.errorCode,
+ status: importedTagToSave.status,
errorDescription: importedTagToSave.errorDescription
};
await global.database.getCollection<any>(tenantID, 'tagsImport').findOneAndUpdate(
@@ -74,9 +74,7 @@ export default class TagStorage {
}
public static async getImportedTags(tenantID: string,
- params: {
- withNoError?: boolean; search?: string
- },
+ params: { status?: ImportStatus; search?: string },
dbParams: DbParams, projectFields?: string[]): Promise<DataResult<ImportedTag>> {
// Debug
const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'getImportedTags');
@@ -98,12 +96,9 @@ export default class TagStorage {
{ 'description': { $regex: params.search, $options: 'i' } }
];
}
- // Only entries with no error
- if (params.withNoError) {
- filters.$or = [
- { 'errorCode': { '$exists': false } },
- { 'errorCode': null }
- ];
+ // Status
+ if (params.status) {
+ filters.status = params.status;
}
// Add filters
aggregation.push({

MODIFY
src/storage/mongodb/UserStorage.ts
src/storage/mongodb/UserStorage.ts
import Site, { SiteUser } from '../../types/Site';
import User, { ImportedUser, UserRole, UserStatus } from '../../types/User';
import { UserInError, UserInErrorType } from '../../types/InError';
-import global, { FilterParams, Image } from '../../types/GlobalType';
+import global, { FilterParams, Image, ImportStatus } from '../../types/GlobalType';
import BackendError from '../../exception/BackendError';
import { BillingUserData } from '../../types/Billing';
@@ -280,7 +280,7 @@ export default class UserStorage {
email: importedUserToSave.email,
firstName: importedUserToSave.firstName,
name: importedUserToSave.name,
- errorCode: importedUserToSave.errorCode,
+ status: importedUserToSave.status,
errorDescription: importedUserToSave.errorDescription,
importedOn:importedUserToSave.importedOn ? importedUserToSave.importedOn : new Date(),
importedBy: Utils.convertToObjectID(importedUserToSave.importedBy)
@@ -683,9 +683,7 @@ export default class UserStorage {
}
public static async getImportedUsers(tenantID: string,
- params: {
- withNoError?: boolean; search?: string
- },
+ params: { status?: ImportStatus; search?: string },
dbParams: DbParams, projectFields?: string[]): Promise<DataResult<ImportedUser>> {
// Debug
const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'getImportedUsers');
@@ -708,12 +706,9 @@ export default class UserStorage {
{ 'email': { $regex: params.search, $options: 'i' } }
];
}
- // Only entries with no error
- if (params.withNoError) {
- filters.$or = [
- { 'errorCode': { '$exists': false } },
- { 'errorCode': null }
- ];
+ // Status
+ if (params.status) {
+ filters.status = params.status;
}
// Remove deleted
filters.deleted = { '$ne': true };

MODIFY
src/types/GlobalType.ts
src/types/GlobalType.ts
@@ -38,6 +38,12 @@ export enum DocumentEncoding {
BASE64 = 'base64',
}
+export enum ImportStatus {
+ READY = 'R',
+ ERROR = 'E',
+}
+
+
interface TSGlobal extends Global {
database: MongoDBStorage;
appRoot: string;
@@ -58,4 +64,6 @@ if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
console.error(`Unknown NODE_ENV '${process.env.NODE_ENV}' defined, exiting`);
process.exit();
}
+
export default global;
+

MODIFY
src/types/Tag.ts
src/types/Tag.ts
import CreatedUpdatedProps from './CreatedUpdatedProps';
-import { HTTPError } from './HTTPError';
+import { ImportStatus } from './GlobalType';
import { OCPIToken } from './ocpi/OCPIToken';
-import { StatusCodes } from 'http-status-codes';
import User from './User';
export default interface Tag extends CreatedUpdatedProps {
@@ -22,6 +21,6 @@ export interface ImportedTag {
description: string;
importedBy?: string;
importedOn?: Date;
- errorCode?: HTTPError | StatusCodes;
+ status?: ImportStatus;
errorDescription?: string;
}

MODIFY
src/types/User.ts
src/types/User.ts
@@ -2,8 +2,7 @@ import Address from './Address';
import { AuthorizationActions } from './Authorization';
import { BillingUserData } from './Billing';
import CreatedUpdatedProps from './CreatedUpdatedProps';
-import { HTTPError } from './HTTPError';
-import { StatusCodes } from 'http-status-codes';
+import { ImportStatus } from './GlobalType';
import UserNotifications from './UserNotifications';
export default interface User extends CreatedUpdatedProps, AuthorizationActions {
@@ -65,7 +64,7 @@ export interface ImportedUser {
email: string;
importedBy?: string;
importedOn?: Date;
- errorCode?: HTTPError | StatusCodes;
+ status?: ImportStatus
errorDescription?: string;
}

Rename errorCode to status and add an ImportStatus enum