Change Tenant ID to Object Registration Token Storage
MODIFY
src/server/ocpp/utils/OCPPUtils.ts
src/server/ocpp/utils/OCPPUtils.ts
@@ -64,7 +64,7 @@ export default class OCPPUtils {
});
}
// Get the Token
- const token = await RegistrationTokenStorage.getRegistrationToken(tenant.id, tokenID);
+ const token = await RegistrationTokenStorage.getRegistrationToken(tenant, tokenID);
if (!token) {
throw new BackendError({
source: chargingStationID,

MODIFY
src/server/rest/v1/service/RegistrationTokenService.ts
src/server/rest/v1/service/RegistrationTokenService.ts
@@ -79,7 +79,7 @@ export default class RegistrationTokenService {
createdOn: new Date()
};
// Save
- registrationToken.id = await RegistrationTokenStorage.saveRegistrationToken(req.user.tenantID, registrationToken);
+ registrationToken.id = await RegistrationTokenStorage.saveRegistrationToken(req.tenant, registrationToken);
// Build OCPP URLs
registrationToken.ocpp15SOAPUrl = Utils.buildOCPPServerURL(req.user.tenantID, OCPPVersion.VERSION_15, OCPPProtocol.SOAP, registrationToken.id);
registrationToken.ocpp16SOAPUrl = Utils.buildOCPPServerURL(req.user.tenantID, OCPPVersion.VERSION_16, OCPPProtocol.SOAP, registrationToken.id);
@@ -107,7 +107,7 @@ export default class RegistrationTokenService {
});
}
// Get Token
- const registrationToken = await RegistrationTokenStorage.getRegistrationToken(req.user.tenantID, filteredRequest.id);
+ const registrationToken = await RegistrationTokenStorage.getRegistrationToken(req.tenant, filteredRequest.id);
UtilsService.assertObjectExists(action, registrationToken, `Token ID '${filteredRequest.id}' does not exist`,
MODULE_NAME, 'handleUpdateRegistrationToken', req.user);
if (Utils.isComponentActiveFromToken(req.user, TenantComponents.ORGANIZATION)) {
@@ -136,7 +136,7 @@ export default class RegistrationTokenService {
registrationToken.lastChangedOn = new Date();
registrationToken.revocationDate = null;
// Save
- registrationToken.id = await RegistrationTokenStorage.saveRegistrationToken(req.user.tenantID, registrationToken);
+ registrationToken.id = await RegistrationTokenStorage.saveRegistrationToken(req.tenant, registrationToken);
// Build OCPP URLs
registrationToken.ocpp15SOAPUrl = Utils.buildOCPPServerURL(req.user.tenantID, OCPPVersion.VERSION_15, OCPPProtocol.SOAP, registrationToken.id);
registrationToken.ocpp16SOAPUrl = Utils.buildOCPPServerURL(req.user.tenantID, OCPPVersion.VERSION_16, OCPPProtocol.SOAP, registrationToken.id);
@@ -153,7 +153,7 @@ export default class RegistrationTokenService {
const tokenID = RegistrationTokenSecurity.filterRegistrationTokenByIDRequest(req.query);
UtilsService.assertIdIsProvided(action, tokenID, MODULE_NAME, 'handleDeleteRegistrationToken', req.user);
// Get Token
- const registrationToken = await RegistrationTokenStorage.getRegistrationToken(req.user.tenantID, tokenID);
+ const registrationToken = await RegistrationTokenStorage.getRegistrationToken(req.tenant, tokenID);
UtilsService.assertObjectExists(action, registrationToken, `Registration Token ID '${tokenID}' does not exist`,
MODULE_NAME, 'handleDeleteRegistrationToken', req.user);
// Check auth
@@ -167,7 +167,7 @@ export default class RegistrationTokenService {
});
}
// Delete
- await RegistrationTokenStorage.deleteRegistrationToken(req.user.tenantID, tokenID);
+ await RegistrationTokenStorage.deleteRegistrationToken(req.tenant, tokenID);
// Log
await Logging.logSecurityInfo({
tenantID: req.user.tenantID,
@@ -185,7 +185,7 @@ export default class RegistrationTokenService {
const tokenID = RegistrationTokenSecurity.filterRegistrationTokenByIDRequest(req.query);
UtilsService.assertIdIsProvided(action, tokenID, MODULE_NAME, 'handleDeleteRegistrationToken', req.user);
// Get Token
- const registrationToken = await RegistrationTokenStorage.getRegistrationToken(req.user.tenantID, tokenID);
+ const registrationToken = await RegistrationTokenStorage.getRegistrationToken(req.tenant, tokenID);
UtilsService.assertObjectExists(action, registrationToken, `Registration Token ID '${tokenID}' does not exist`,
MODULE_NAME, 'handleRevokeRegistrationToken', req.user);
// Check auth
@@ -212,7 +212,7 @@ export default class RegistrationTokenService {
registrationToken.revocationDate = new Date();
registrationToken.lastChangedBy = { 'id': req.user.id };
registrationToken.lastChangedOn = new Date();
- await RegistrationTokenStorage.saveRegistrationToken(req.user.tenantID, registrationToken);
+ await RegistrationTokenStorage.saveRegistrationToken(req.tenant, registrationToken);
// Log
await Logging.logSecurityInfo({
tenantID: req.user.tenantID,
@@ -244,7 +244,7 @@ export default class RegistrationTokenService {
userProject = [ 'createdBy.name', 'createdBy.firstName', 'lastChangedBy.name', 'lastChangedBy.firstName' ];
}
// Get the tokens
- const registrationTokens = await RegistrationTokenStorage.getRegistrationTokens(req.user.tenantID,
+ const registrationTokens = await RegistrationTokenStorage.getRegistrationTokens(req.tenant,
{
siteAreaID: filteredRequest.SiteAreaID,
siteIDs: Authorizations.getAuthorizedSiteAdminIDs(req.user, null),
@@ -283,7 +283,7 @@ export default class RegistrationTokenService {
userProject = [ 'createdBy.name', 'createdBy.firstName', 'lastChangedBy.name', 'lastChangedBy.firstName' ];
}
// Get the token
- const registrationToken = await RegistrationTokenStorage.getRegistrationToken(req.user.tenantID,
+ const registrationToken = await RegistrationTokenStorage.getRegistrationToken(req.tenant,
filteredRequest,
[
'id', 'status', 'description', 'createdOn', 'lastChangedOn', 'expirationDate', 'revocationDate',

MODIFY
src/storage/mongodb/RegistrationTokenStorage.ts
src/storage/mongodb/RegistrationTokenStorage.ts
@@ -7,16 +7,17 @@ import DbParams from '../../types/database/DbParams';
import Logging from '../../utils/Logging';
import { ObjectId } from 'mongodb';
import RegistrationToken from '../../types/RegistrationToken';
+import Tenant from '../../types/Tenant';
import Utils from '../../utils/Utils';
const MODULE_NAME = 'RegistrationTokenStorage';
export default class RegistrationTokenStorage {
- static async saveRegistrationToken(tenantID: string, registrationToken: RegistrationToken): Promise<string> {
+ static async saveRegistrationToken(tenant: Tenant, registrationToken: RegistrationToken): Promise<string> {
// Debug
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'saveRegistrationToken');
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'saveRegistrationToken');
// Check Tenant
- await DatabaseUtils.checkTenant(tenantID);
+ DatabaseUtils.checkTenantObject(tenant);
// Set
const registrationTokenMDB = {
_id: registrationToken.id ? DatabaseUtils.convertToObjectID(registrationToken.id) : new ObjectId(),
@@ -28,23 +29,23 @@ export default class RegistrationTokenStorage {
// Add Last Changed/Created props
DatabaseUtils.addLastChangedCreatedProps(registrationTokenMDB, registrationToken);
// Modify
- await global.database.getCollection(tenantID, 'registrationtokens').findOneAndUpdate(
+ await global.database.getCollection(tenant.id, 'registrationtokens').findOneAndUpdate(
{ _id: registrationTokenMDB._id },
{ $set: registrationTokenMDB },
{ upsert: true, returnDocument: 'after' }
);
// Debug
- await Logging.traceEnd(tenantID, MODULE_NAME, 'saveRegistrationToken', uniqueTimerID, registrationTokenMDB);
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'saveRegistrationToken', uniqueTimerID, registrationTokenMDB);
return registrationTokenMDB._id.toString();
}
- static async getRegistrationTokens(tenantID: string,
+ static async getRegistrationTokens(tenant: Tenant,
params: { tokenIDs?: string[]; siteIDs?: string[]; siteAreaID?: string } = {}, dbParams: DbParams, projectFields?: string[]):
Promise<DataResult<RegistrationToken>> {
// Debug
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'getRegistrationTokens');
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'getRegistrationTokens');
// Check Tenant
- await DatabaseUtils.checkTenant(tenantID);
+ DatabaseUtils.checkTenantObject(tenant);
// Clone before updating the values
dbParams = Utils.cloneObject(dbParams);
// Check Limit
@@ -55,7 +56,7 @@ export default class RegistrationTokenStorage {
const aggregation = [];
// Add Site Area
DatabaseUtils.pushSiteAreaLookupInAggregation({
- tenantID, aggregation, localField: 'siteAreaID', foreignField: '_id',
+ tenantID: tenant.id, aggregation, localField: 'siteAreaID', foreignField: '_id',
asField: 'siteArea', oneToOneCardinality: true
});
// Set the filters
@@ -88,13 +89,13 @@ export default class RegistrationTokenStorage {
aggregation.push({ $limit: Constants.DB_RECORD_COUNT_CEIL });
}
// Count Records
- const registrationTokensCountMDB = await global.database.getCollection<DataResult<RegistrationToken>>(tenantID, 'registrationtokens')
+ const registrationTokensCountMDB = await global.database.getCollection<DataResult<RegistrationToken>>(tenant.id, 'registrationtokens')
.aggregate([...aggregation, { $count: 'count' }], { allowDiskUse: true })
.toArray();
// Check if only the total count is requested
if (dbParams.onlyRecordCount) {
// Return only the count
- await Logging.traceEnd(tenantID, MODULE_NAME, 'getRegistrationTokens', uniqueTimerID, registrationTokensCountMDB);
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'getRegistrationTokens', uniqueTimerID, registrationTokensCountMDB);
return {
count: (registrationTokensCountMDB.length > 0 ? registrationTokensCountMDB[0].count : 0),
result: []
@@ -120,17 +121,17 @@ export default class RegistrationTokenStorage {
$limit: (dbParams.limit > 0 && dbParams.limit < Constants.DB_RECORD_COUNT_CEIL) ? dbParams.limit : Constants.DB_RECORD_COUNT_CEIL
});
// Add Created By / Last Changed By
- DatabaseUtils.pushCreatedLastChangedInAggregation(tenantID, aggregation);
+ DatabaseUtils.pushCreatedLastChangedInAggregation(tenant.id, aggregation);
// Project
DatabaseUtils.projectFields(aggregation, projectFields);
// Read DB
- const registrationTokens = await global.database.getCollection<any>(tenantID, 'registrationtokens')
+ const registrationTokens = await global.database.getCollection<any>(tenant.id, 'registrationtokens')
.aggregate(aggregation, {
allowDiskUse: true
})
.toArray();
// Debug
- await Logging.traceEnd(tenantID, MODULE_NAME, 'getRegistrationTokens', uniqueTimerID, registrationTokens);
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'getRegistrationTokens', uniqueTimerID, registrationTokens);
// Ok
return {
count: (registrationTokensCountMDB.length > 0 ?
@@ -139,20 +140,20 @@ export default class RegistrationTokenStorage {
};
}
- static async getRegistrationToken(tenantID: string, id: string = Constants.UNKNOWN_OBJECT_ID,
+ static async getRegistrationToken(tenant: Tenant, id: string = Constants.UNKNOWN_OBJECT_ID,
projectFields?: string[]): Promise<RegistrationToken> {
- const registrationTokensMDB = await RegistrationTokenStorage.getRegistrationTokens(tenantID, {
+ const registrationTokensMDB = await RegistrationTokenStorage.getRegistrationTokens(tenant, {
tokenIDs: [id]
}, Constants.DB_PARAMS_SINGLE_RECORD, projectFields);
return registrationTokensMDB.count === 1 ? registrationTokensMDB.result[0] : null;
}
- static async deleteRegistrationToken(tenantID: string, id: string): Promise<void> {
+ static async deleteRegistrationToken(tenant: Tenant, id: string): Promise<void> {
// Debug
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'deleteRegistrationToken');
- await global.database.getCollection<any>(tenantID, 'registrationtokens')
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'deleteRegistrationToken');
+ await global.database.getCollection<any>(tenant.id, 'registrationtokens')
.findOneAndDelete({ '_id': DatabaseUtils.convertToObjectID(id) });
// Debug
- await Logging.traceEnd(tenantID, MODULE_NAME, 'deleteRegistrationToken', uniqueTimerID, { id });
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'deleteRegistrationToken', uniqueTimerID, { id });
}
}

Change Tenant ID to Object Registration Token Storage