Chaning tenant ID to tenant object for Connection Storage
MODIFY
src/server/rest/v1/service/ConnectionService.ts
src/server/rest/v1/service/ConnectionService.ts
@@ -47,7 +47,7 @@ export default class ConnectionService {
userProject = [ 'createdBy.name', 'createdBy.firstName', 'lastChangedBy.name', 'lastChangedBy.firstName' ];
}
// Get it
- const connection = await ConnectionStorage.getConnection(req.user.tenantID, connectionID,
+ const connection = await ConnectionStorage.getConnection(req.tenant, connectionID,
[ 'id', 'connectorId', 'createdAt', 'validUntil', 'lastChangedOn', 'createdOn', ...userProject ]);
UtilsService.assertObjectExists(action, connection, `Connection ID '${connectionID}' does not exist`,
MODULE_NAME, 'handleGetConnection', req.user);
@@ -73,7 +73,7 @@ export default class ConnectionService {
userProject = [ 'createdBy.name', 'createdBy.firstName', 'lastChangedBy.name', 'lastChangedBy.firstName' ];
}
// Get
- const connections = await ConnectionStorage.getConnectionsByUserId(req.user.tenantID, filteredRequest.UserID,
+ const connections = await ConnectionStorage.getConnectionsByUserId(req.tenant, filteredRequest.UserID,
[ 'id', 'connectorId', 'createdAt', 'validUntil', 'lastChangedOn', 'createdOn', ...userProject ]);
res.json(connections);
next();
@@ -123,11 +123,11 @@ export default class ConnectionService {
});
}
// Get connection
- const connection = await ConnectionStorage.getConnection(req.user.tenantID, connectionID);
+ const connection = await ConnectionStorage.getConnection(req.tenant, connectionID);
UtilsService.assertObjectExists(action, connection, `Connection ID '${connectionID}' does not exist`,
MODULE_NAME, 'handleDeleteConnection', req.user);
// Delete
- await ConnectionStorage.deleteConnectionById(req.user.tenantID, connection.id);
+ await ConnectionStorage.deleteConnectionById(req.tenant, connection.id);
// Log
await Logging.logSecurityInfo({
tenantID: req.user.tenantID,

MODIFY
src/storage/mongodb/ConnectionStorage.ts
src/storage/mongodb/ConnectionStorage.ts
@@ -4,6 +4,7 @@ import { DataResult } from '../../types/DataResult';
import DatabaseUtils from './DatabaseUtils';
import Logging from '../../utils/Logging';
import { ObjectID } from 'mongodb';
+import Tenant from '../../types/Tenant';
import Utils from '../../utils/Utils';
import global from '../../types/GlobalType';
@@ -11,9 +12,9 @@ const MODULE_NAME = 'ConnectionStorage';
export default class ConnectionStorage {
- static async saveConnection(tenantID: string, connectionToSave: Connection): Promise<string> {
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'saveConnection');
- await DatabaseUtils.checkTenant(tenantID);
+ static async saveConnection(tenant: Tenant, connectionToSave: Connection): Promise<string> {
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'saveConnection');
+ DatabaseUtils.checkTenantObject(tenant);
// Create
const connectionMDB: any = {
_id: !connectionToSave.id ? new ObjectID() : Utils.convertToObjectID(connectionToSave.id),
@@ -25,17 +26,17 @@ export default class ConnectionStorage {
data: connectionToSave.data
};
// Update
- const result = await global.database.getCollection<any>(tenantID, 'connections').findOneAndUpdate(
+ const result = await global.database.getCollection<any>(tenant.id, 'connections').findOneAndUpdate(
{ _id: connectionMDB._id },
{ $set: connectionMDB },
{ upsert: true, returnDocument: 'after' });
- await Logging.traceEnd(tenantID, MODULE_NAME, 'saveConnection', uniqueTimerID, connectionMDB);
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'saveConnection', uniqueTimerID, connectionMDB);
return result.value._id.toHexString();
}
- static async getConnectionByConnectorIdAndUserId(tenantID: string, connectorId: string, userId: string, projectFields?: string[]): Promise<Connection> {
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'getConnectionByConnectorIdAndUserId');
- await DatabaseUtils.checkTenant(tenantID);
+ static async getConnectionByConnectorIdAndUserId(tenant: Tenant, connectorId: string, userId: string, projectFields?: string[]): Promise<Connection> {
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'getConnectionByConnectorIdAndUserId');
+ DatabaseUtils.checkTenantObject(tenant);
const aggregation = [];
aggregation.push({
$match: { connectorId: connectorId, userId: Utils.convertToObjectID(userId) }
@@ -47,20 +48,20 @@ export default class ConnectionStorage {
// Project
DatabaseUtils.projectFields(aggregation, projectFields);
// Exec
- const connections = await global.database.getCollection<any>(tenantID, 'connections')
+ const connections = await global.database.getCollection<any>(tenant.id, 'connections')
.aggregate(aggregation)
.toArray();
let connection: Connection;
if (connections && connections.length > 0) {
connection = connections[0];
}
- await Logging.traceEnd(tenantID, MODULE_NAME, 'getConnectionByConnectorIdAndUserId', uniqueTimerID, connections);
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'getConnectionByConnectorIdAndUserId', uniqueTimerID, connections);
return connection;
}
- static async getConnectionsByUserId(tenantID: string, userID: string, projectFields?: string[]): Promise<DataResult<Connection>> {
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'getConnectionsByUserId');
- await DatabaseUtils.checkTenant(tenantID);
+ static async getConnectionsByUserId(tenant: Tenant, userID: string, projectFields?: string[]): Promise<DataResult<Connection>> {
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'getConnectionsByUserId');
+ DatabaseUtils.checkTenantObject(tenant);
const aggregation = [];
aggregation.push({
$match: { userId: Utils.convertToObjectID(userID) }
@@ -72,21 +73,21 @@ export default class ConnectionStorage {
// Project
DatabaseUtils.projectFields(aggregation, projectFields);
// Get connections
- const connectionsMDB = await global.database.getCollection<Connection>(tenantID, 'connections')
+ const connectionsMDB = await global.database.getCollection<Connection>(tenant.id, 'connections')
.aggregate(aggregation, {
allowDiskUse: true
})
.toArray();
- await Logging.traceEnd(tenantID, MODULE_NAME, 'getConnectionByUserId', uniqueTimerID, connectionsMDB);
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'getConnectionByUserId', uniqueTimerID, connectionsMDB);
return {
count: connectionsMDB.length,
result: connectionsMDB
};
}
- static async getConnection(tenantID: string, id: string = Constants.UNKNOWN_OBJECT_ID, projectFields?: string[]): Promise<Connection> {
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'getConnection');
- await DatabaseUtils.checkTenant(tenantID);
+ static async getConnection(tenant: Tenant, id: string = Constants.UNKNOWN_OBJECT_ID, projectFields?: string[]): Promise<Connection> {
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'getConnection');
+ DatabaseUtils.checkTenantObject(tenant);
const aggregation = [];
// Filters
aggregation.push({
@@ -99,38 +100,38 @@ export default class ConnectionStorage {
// Project
DatabaseUtils.projectFields(aggregation, projectFields);
// Exec
- const connections = await global.database.getCollection<Connection>(tenantID, 'connections')
+ const connections = await global.database.getCollection<Connection>(tenant.id, 'connections')
.aggregate(aggregation)
.toArray();
let connection: Connection;
if (connections && connections.length > 0) {
connection = connections[0];
}
- await Logging.traceEnd(tenantID, MODULE_NAME, 'getConnection', uniqueTimerID, connections);
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'getConnection', uniqueTimerID, connections);
return connection;
}
- static async deleteConnectionById(tenantID: string, id: string): Promise<void> {
+ static async deleteConnectionById(tenant: Tenant, id: string): Promise<void> {
// Debug
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'deleteConnection');
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'deleteConnection');
// Check
- await DatabaseUtils.checkTenant(tenantID);
+ DatabaseUtils.checkTenantObject(tenant);
// Delete
- await global.database.getCollection<Connection>(tenantID, 'connections')
+ await global.database.getCollection<Connection>(tenant.id, 'connections')
.findOneAndDelete({ '_id': Utils.convertToObjectID(id) });
// Debug
- await Logging.traceEnd(tenantID, MODULE_NAME, 'deleteConnection', uniqueTimerID, { id });
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'deleteConnection', uniqueTimerID, { id });
}
- static async deleteConnectionByUserId(tenantID: string, userID: string): Promise<void> {
+ static async deleteConnectionByUserId(tenant: Tenant, userID: string): Promise<void> {
// Debug
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'deleteConnectionByUser');
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'deleteConnectionByUser');
// Check
- await DatabaseUtils.checkTenant(tenantID);
+ DatabaseUtils.checkTenantObject(tenant);
// Delete
- await global.database.getCollection<any>(tenantID, 'connections')
+ await global.database.getCollection<any>(tenant.id, 'connections')
.deleteMany({ 'userId': Utils.convertToObjectID(userID) });
// Debug
- await Logging.traceEnd(tenantID, MODULE_NAME, 'deleteConnectionByUser', uniqueTimerID, { userID });
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'deleteConnectionByUser', uniqueTimerID, { userID });
}
}

Chaning tenant ID to tenant object for Connection Storage