Use Type ImportedUser
MODIFY
src/assets/server/rest/v1/schemas/user/user-create-req.json
src/assets/server/rest/v1/schemas/user/user-create-req.json
"type": "object",
"properties": {
"email": {
- "type": "string",
- "format": "email"
+ "$ref": "common.json#/definitions/email"
},
"name": {
- "type": "string",
- "minLength": 1,
- "maxLength": 100
+ "$ref": "common.json#/definitions/name"
},
"firstName": {
- "type": "string",
- "minLength": 1,
- "maxLength": 100
+ "$ref": "common.json#/definitions/firstName"
},
"password": {
"type": "string",
"format": "date"
},
"createdBy": {
- "$ref": "common.json#/definitions/id"
+ "type": "object"
},
"locale": {
"type": "string"

MODIFY
src/scheduler/tasks/SynchronizeUsersImportTask.ts
src/scheduler/tasks/SynchronizeUsersImportTask.ts
@@ -30,46 +30,24 @@ export default class SynchronizeUsersImportTask extends SchedulerTask {
} else {
const user: Partial<User> = {
firstName: importedUser.firstName,
- name: importedUser.name.toUpperCase(),
- email: importedUser.email.toLowerCase(),
+ name: importedUser.name,
+ email: importedUser.email,
locale: null, // Defaults to the browser locale
issuer: true,
deleted: false,
role: UserRole.BASIC,
status: UserStatus.ACTIVE,
- createdBy: importedUser.importedBy,
+ createdBy: { id: importedUser.importedBy },
createdOn: new Date(),
- passwordWrongNbrTrials: 0,
- notificationsActive: true,
- notifications: {
- sendSessionStarted: true,
- sendOptimalChargeReached: true,
- sendEndOfCharge: true,
- sendEndOfSession: true,
- sendUserAccountStatusChanged: true,
- sendPreparingSessionNotStarted: false,
- sendUserAccountInactivity: true,
- sendSessionNotStarted: true,
- sendNewRegisteredUser: false,
- sendUnknownUserBadged: false,
- sendChargingStationStatusError: false,
- sendChargingStationRegistered: false,
- sendOcpiPatchStatusError: false,
- sendOfflineChargingStations: false,
- sendBillingSynchronizationFailed: false,
- sendCarCatalogSynchronizationFailed: false,
- sendEndUserErrorNotification: false,
- sendSmtpError: false,
- sendBillingNewInvoice: false,
- sendComputeAndApplyChargingProfilesFailed: false,
- sendAccountVerificationNotification: false,
- sendAdminAccountVerificationNotification: false
- },
+ notificationsActive: true
};
try {
UserValidator.getInstance().validateUserCreation(user);
- await global.database.getCollection<any>(tenant.id, 'users')
- .insertOne(user);
+ user.id = await UserStorage.saveUser(tenant.id, user);
+ // Role need to be set separately
+ await UserStorage.saveUserRole(tenant.id, user.id, UserRole.BASIC);
+ // Status need to be set separately
+ await UserStorage.saveUserStatus(tenant.id, user.id, UserStatus.ACTIVE);
importedUser.status = UserImportStatus.IMPORTED;
await UserStorage.saveImportedUser(tenant.id, importedUser);
await Logging.logDebug({
@@ -96,7 +74,7 @@ export default class SynchronizeUsersImportTask extends SchedulerTask {
}
} catch (error) {
// Log error
- Logging.logActionExceptionMessage(tenant.id, ServerAction.SYNCHRONIZE_USERS, error);
+ await Logging.logActionExceptionMessage(tenant.id, ServerAction.SYNCHRONIZE_USERS, error);
} finally {
// Release the lock
await LockingManager.release(synchronizeUsersImport);

MODIFY
src/server/rest/v1/service/UserService.ts
src/server/rest/v1/service/UserService.ts
@@ -2,7 +2,7 @@ import { Action, Entity } from '../../../../types/Authorization';
import { HTTPAuthError, HTTPError } from '../../../../types/HTTPError';
import { NextFunction, Request, Response } from 'express';
import { OCPITokenType, OCPITokenWhitelist } from '../../../../types/ocpi/OCPIToken';
-import User, { UserImportStatus, UserStatus } from '../../../../types/User';
+import User, { ImportedUser, UserImportStatus, UserStatus } from '../../../../types/User';
import Address from '../../../../types/Address';
import AppAuthError from '../../../../exception/AppAuthError';
@@ -1281,10 +1281,9 @@ export default class UserService {
name: user.Name,
firstName: user.First_Name,
email: user.Email,
- role: user.Role,
importedBy: req.user.id,
status: UserImportStatus.UNKNOWN
- };
+ } as ImportedUser;
await UserStorage.saveImportedUser(req.user.tenantID, newUploadedUser);
} catch (error) {
await Logging.logError({

MODIFY
src/storage/mongodb/UserStorage.ts
src/storage/mongodb/UserStorage.ts
import Site, { SiteUser } from '../../types/Site';
-import User, { UserImportStatus, UserRole, UserStatus } from '../../types/User';
+import User, { ImportedUser, UserImportStatus, UserRole, UserStatus } from '../../types/User';
import { UserInError, UserInErrorType } from '../../types/InError';
import global, { FilterParams, Image } from '../../types/GlobalType';
@@ -272,18 +272,17 @@ export default class UserStorage {
return userMDB._id.toHexString();
}
- public static async saveImportedUser(tenantID: string, userToSave: any): Promise<void> {
+ public static async saveImportedUser(tenantID: string, importedUserToSave: ImportedUser): Promise<void> {
const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'saveImportedUser');
const userMDB = {
- _id: userToSave.id ? Utils.convertToObjectID(userToSave.id) : new ObjectID(),
- email: userToSave.email,
- firstName: userToSave.firstName,
- name: userToSave.name,
- role: userToSave.role,
- status: userToSave.status,
- error: userToSave.error,
+ _id: importedUserToSave.id ? Utils.convertToObjectID(importedUserToSave.id) : new ObjectID(),
+ email: importedUserToSave.email,
+ firstName: importedUserToSave.firstName,
+ name: importedUserToSave.name,
+ status: importedUserToSave.status,
+ error: importedUserToSave.error,
importedOn: new Date(),
- importedBy: Utils.convertToObjectID(userToSave.importedBy)
+ importedBy: Utils.convertToObjectID(importedUserToSave.importedBy)
};
await global.database.getCollection<any>(tenantID, 'usersImport').findOneAndUpdate(
{ _id: userMDB._id },
@@ -676,7 +675,7 @@ export default class UserStorage {
params: {
statuses?: UserImportStatus[]; search?: string
},
- dbParams: DbParams, projectFields?: string[]): Promise<DataResult<any>> {
+ dbParams: DbParams, projectFields?: string[]): Promise<DataResult<ImportedUser>> {
// Debug
const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'getImportedUsers');
// Check Tenant

MODIFY
src/types/User.ts
src/types/User.ts
@@ -56,6 +56,17 @@ export interface UserCar extends CreatedUpdatedProps {
owner?: boolean;
}
+export interface ImportedUser {
+ id?: string;
+ name: string;
+ firstName: string;
+ email: string;
+ importedBy: string,
+ importedOn?: Date,
+ error?: string,
+ status?: UserImportStatus
+}
+
export enum UserStatus {
PENDING = 'P',
ACTIVE = 'A',

Use Type ImportedUser