Allow to delete OCPI Users (eMSPs)
MODIFY
src/server/rest/v1/service/UserService.ts
src/server/rest/v1/service/UserService.ts
@@ -242,9 +242,6 @@ export default class UserService {
const user = await UserStorage.getUser(
req.user.tenantID, userID, authorizationUserFilters.filters);
UtilsService.assertObjectExists(action, user, `User '${userID}' does not exist`, MODULE_NAME, 'handleDeleteUser', req.user);
- // Get tags
- const tags = (await TagStorage.getTags(req.user.tenantID,
- { userIDs: [user.id], withNbrTransactions: true }, Constants.DB_PARAMS_MAX_LIMIT)).result;
// Deleted
if (user.deleted) {
throw new AppError({
@@ -258,14 +255,22 @@ export default class UserService {
}
// OCPI User
if (!user.issuer) {
- throw new AppError({
- source: Constants.CENTRAL_SERVER,
- errorCode: HTTPError.GENERAL_ERROR,
- message: 'User not issued by the organization',
- module: MODULE_NAME, method: 'handleDeleteUser',
+ // Delete all tags
+ const numberOfDeletedTags = await TagStorage.deleteTagsByUser(req.user.tenantID, user.id);
+ // Delete User
+ await UserStorage.deleteUser(req.user.tenantID, user.id);
+ // Log
+ await Logging.logSecurityInfo({
+ tenantID: req.user.tenantID,
user: req.user, actionOnUser: user,
+ module: MODULE_NAME, method: 'handleDeleteUser',
+ message: `User with ID '${user.id}' has been deleted successfully along '${numberOfDeletedTags}' Tag(s)`,
action: action
});
+ // Ok
+ res.json(Constants.REST_RESPONSE_SUCCESS);
+ next();
+ return;
}
// Check Billing
if (Utils.isComponentActiveFromToken(req.user, TenantComponents.BILLING)) {
@@ -340,6 +345,8 @@ export default class UserService {
await UserStorage.saveUserAccountVerification(req.user.tenantID, user.id,
{ verificationToken: null, verifiedAt: null });
// Disable/Delete Tags
+ const tags = (await TagStorage.getTags(req.user.tenantID,
+ { userIDs: [user.id], withNbrTransactions: true }, Constants.DB_PARAMS_MAX_LIMIT)).result;
for (const tag of tags) {
if (tag.transactionsCount > 0) {
tag.active = false;
@@ -362,6 +369,9 @@ export default class UserService {
const tenant = await TenantStorage.getTenant(req.user.tenantID);
const ocpiClient: EmspOCPIClient = await OCPIClientFactory.getAvailableOcpiClient(tenant, OCPIRole.EMSP) as EmspOCPIClient;
if (ocpiClient) {
+ // Get tags
+ const tags = (await TagStorage.getTags(req.user.tenantID,
+ { userIDs: [user.id], withNbrTransactions: true }, Constants.DB_PARAMS_MAX_LIMIT)).result;
for (const tag of tags) {
await ocpiClient.pushToken({
uid: tag.id,

MODIFY
src/storage/mongodb/TagStorage.ts
src/storage/mongodb/TagStorage.ts
@@ -62,11 +62,28 @@ export default class TagStorage {
await global.database.getCollection<any>(tenantID, 'tags').deleteOne(
{
'_id': tagID,
- });
+ }
+ );
// Debug
await Logging.traceEnd(tenantID, MODULE_NAME, 'deleteTag', uniqueTimerID, { id: tagID });
}
+ public static async deleteTagsByUser(tenantID: string, userID: string): Promise<number> {
+ // Debug
+ const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'deleteTagsByUser');
+ // Check Tenant
+ await DatabaseUtils.checkTenant(tenantID);
+ // Delete
+ const result = await global.database.getCollection<any>(tenantID, 'tags').deleteMany(
+ {
+ 'userID': Utils.convertToObjectID(userID),
+ }
+ );
+ // Debug
+ await Logging.traceEnd(tenantID, MODULE_NAME, 'deleteTagsByUser', uniqueTimerID, { id: userID });
+ return result.deletedCount;
+ }
+
public static async getTag(tenantID: string, id: string,
params: { withUser?: boolean; withNbrTransactions?: boolean } = {}, projectFields?: string[]): Promise<Tag> {
const tagMDB = await TagStorage.getTags(tenantID, {

Allow to delete OCPI Users (eMSPs)