create virtual OICP User and default Badges Change
MODIFY
src/server/rest/v1/service/TenantService.ts
src/server/rest/v1/service/TenantService.ts
@@ -3,6 +3,7 @@ import { HTTPAuthError, HTTPError } from '../../../../types/HTTPError';
import { NextFunction, Request, Response } from 'express';
import { SettingDB, SettingDBContent } from '../../../../types/Setting';
import User, { UserRole, UserStatus } from '../../../../types/User';
+
import AppAuthError from '../../../../exception/AppAuthError';
import AppError from '../../../../exception/AppError';
import Authorizations from '../../../../authorization/Authorizations';
@@ -15,6 +16,7 @@ import { ServerAction } from '../../../../types/Server';
import SettingStorage from '../../../../storage/mongodb/SettingStorage';
import SiteAreaStorage from '../../../../storage/mongodb/SiteAreaStorage';
import { StatusCodes } from 'http-status-codes';
+import TagStorage from '../../../../storage/mongodb/TagStorage';
import Tenant from '../../../../types/Tenant';
import TenantStorage from '../../../../storage/mongodb/TenantStorage';
import TenantValidator from '../validator/TenantValidation';
@@ -317,7 +319,7 @@ export default class TenantService {
}
private static async updateSettingsWithComponents(tenant: Partial<Tenant>, req: Request): Promise<void> {
- // Check if OICP component is activated or deactivated and create/activate/deactivate virtual user accordingly
+ // Check if OICP component is activated or deactivated and create/activate/deactivate virtual user (and Badges) accordingly
await this.checkOICPComponent(tenant);
// Create settings
@@ -370,12 +372,30 @@ export default class TenantService {
if (!virtualOICPUser) {
await UserStorage.createOICPVirtualUser(tenant.id);
} else if (virtualOICPUser.status !== UserStatus.ACTIVE) {
- // Save User Status
+ // Activate user and save user status
await UserStorage.saveUserStatus(tenant.id, virtualOICPUser.id, UserStatus.ACTIVE);
+
+ // Activate Default Badge(s)
+ const tags = (await TagStorage.getTags(tenant.id, { userIDs: [virtualOICPUser.id] }, Constants.DB_PARAMS_MAX_LIMIT)).result;
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
+ tags.forEach(async (tag) => {
+ tag.active = true;
+ // Save
+ await TagStorage.saveTag(tenant.id, tag);
+ });
}
} else if (virtualOICPUser && virtualOICPUser?.status === UserStatus.ACTIVE) {
- // Save User Status
+ // Deactivate user and save user status
await UserStorage.saveUserStatus(tenant.id, virtualOICPUser.id, UserStatus.INACTIVE);
+
+ // Deactivate Default Badge(s)
+ const tags = (await TagStorage.getTags(tenant.id, { userIDs: [virtualOICPUser.id] }, Constants.DB_PARAMS_MAX_LIMIT)).result;
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
+ tags.forEach(async (tag) => {
+ tag.active = false;
+ // Save
+ await TagStorage.saveTag(tenant.id, tag);
+ });
}
}
}

MODIFY
src/storage/mongodb/TagStorage.ts
src/storage/mongodb/TagStorage.ts
@@ -5,7 +5,9 @@ import { DataResult } from '../../types/DataResult';
import DatabaseUtils from './DatabaseUtils';
import DbParams from '../../types/database/DbParams';
import Logging from '../../utils/Logging';
+import { OICPDefaultTagId } from '../../types/oicp/OICPIdentification';
import Tag from '../../types/Tag';
+import UserStorage from './UserStorage';
import Utils from '../../utils/Utils';
import moment from 'moment';
@@ -228,4 +230,29 @@ export default class TagStorage {
result: tagsMDB
};
}
+
+ public static async createOICPVirtualUserTags(tenantID: string): Promise<void> {
+ const defaultTagIds = [OICPDefaultTagId.RemoteIdentification, OICPDefaultTagId.QRCodeIdentification, OICPDefaultTagId.PlugAndChargeIdentification];
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
+ defaultTagIds.forEach(async (defaultTagId) => {
+ let tag = await this.getTag(tenantID, defaultTagId);
+ if (!tag) {
+ const virtualOICPUser = await UserStorage.getOICPVirtualUser(tenantID);
+ tag = {
+ id: defaultTagId,
+ userID: virtualOICPUser.id,
+ user: virtualOICPUser,
+ description: 'Badge for OICP Remote Authorization',
+ issuer: false,
+ active: true,
+ createdOn: new Date(),
+ default: false
+ } as Tag;
+ } else {
+ tag.active = true;
+ }
+ // Save
+ await TagStorage.saveTag(tenantID, tag);
+ });
+ }
}

MODIFY
src/storage/mongodb/UserStorage.ts
src/storage/mongodb/UserStorage.ts
@@ -123,6 +123,9 @@ export default class UserStorage {
newVirtualOICPUser.id = await UserStorage.saveUser(tenantID, newVirtualOICPUser);
// Save User Status
await UserStorage.saveUserStatus(tenantID, newVirtualOICPUser.id, UserStatus.ACTIVE);
+
+ // Create default badges for virtual OICP user
+ await TagStorage.createOICPVirtualUserTags(tenantID);
}
public static async getUserByPasswordResetHash(tenantID: string, passwordResetHash: string = Constants.UNKNOWN_STRING_ID): Promise<User> {

create virtual OICP User and default Badges Change