Changing Tenant ID to Object Consumption Storage
MODIFY
src/client/ocpi/CpoOCPIClient.ts
src/client/ocpi/CpoOCPIClient.ts
@@ -285,7 +285,7 @@ export default class CpoOCPIClient extends OCPIClient {
transaction.ocpiData.session.total_cost = transaction.currentCumulatedPrice > 0 ? transaction.currentCumulatedPrice : 0;
transaction.ocpiData.session.currency = this.settings.currency;
transaction.ocpiData.session.status = OCPISessionStatus.ACTIVE;
- transaction.ocpiData.session.charging_periods = await this.buildChargingPeriods(this.tenant.id, transaction);
+ transaction.ocpiData.session.charging_periods = await this.buildChargingPeriods(this.tenant, transaction);
// Send OCPI information
const session: Partial<OCPISession> = {
kwh: transaction.ocpiData.session.kwh,
@@ -338,7 +338,7 @@ export default class CpoOCPIClient extends OCPIClient {
transaction.ocpiData.session.end_datetime = transaction.stop.timestamp;
transaction.ocpiData.session.last_updated = transaction.stop.timestamp;
transaction.ocpiData.session.status = OCPISessionStatus.COMPLETED;
- transaction.ocpiData.session.charging_periods = await this.buildChargingPeriods(this.tenant.id, transaction);
+ transaction.ocpiData.session.charging_periods = await this.buildChargingPeriods(this.tenant, transaction);
// Call IOP
const response = await this.axiosInstance.put(
tokensUrl,
@@ -391,7 +391,7 @@ export default class CpoOCPIClient extends OCPIClient {
auth_method: transaction.ocpiData.session.auth_method,
location: transaction.ocpiData.session.location,
total_cost: transaction.stop.roundedPrice > 0 ? transaction.stop.roundedPrice : 0,
- charging_periods: await this.buildChargingPeriods(this.tenant.id, transaction),
+ charging_periods: await this.buildChargingPeriods(this.tenant, transaction),
last_updated: transaction.stop.timestamp
};
// Call IOP
@@ -959,13 +959,13 @@ export default class CpoOCPIClient extends OCPIClient {
return result;
}
- private async buildChargingPeriods(tenantID: string, transaction: Transaction): Promise<OCPIChargingPeriod[]> {
+ private async buildChargingPeriods(tenant: Tenant, transaction: Transaction): Promise<OCPIChargingPeriod[]> {
if (!transaction || !transaction.timestamp) {
return [];
}
const chargingPeriods: OCPIChargingPeriod[] = [];
const consumptions = await ConsumptionStorage.getTransactionConsumptions(
- tenantID, { transactionId: transaction.id });
+ tenant, { transactionId: transaction.id });
if (consumptions.result) {
// Build based on consumptions
for (const consumption of consumptions.result) {

MODIFY
src/scheduler/tasks/AssetGetConsumptionTask.ts
src/scheduler/tasks/AssetGetConsumptionTask.ts
@@ -78,7 +78,7 @@ export default class AssetGetConsumptionTask extends SchedulerTask {
smartChargingActive: siteAreaLimitConsumption.smartChargingActive,
};
// Save Consumption
- await ConsumptionStorage.saveConsumption(tenant.id, consumptionToSave);
+ await ConsumptionStorage.saveConsumption(tenant, consumptionToSave);
// Set Consumption to Asset
this.assignAssetConsumption(asset, consumption);
}

MODIFY
src/server/ocpi/ocpi-services-impl/ocpi-2.1.1/OCPIUtilsService.ts
src/server/ocpi/ocpi-services-impl/ocpi-2.1.1/OCPIUtilsService.ts
@@ -1018,7 +1018,7 @@ export default class OCPIUtilsService {
currencyCode: session.currency,
cumulatedAmount: session.total_cost
} as Consumption;
- await ConsumptionStorage.saveConsumption(tenant.id, consumption);
+ await ConsumptionStorage.saveConsumption(tenant, consumption);
}
}

MODIFY
src/server/ocpp/services/OCPPService.ts
src/server/ocpp/services/OCPPService.ts
@@ -258,7 +258,7 @@ export default class OCPPService {
await OCPPUtils.processTransactionBilling(tenant, transaction, TransactionAction.UPDATE);
}
// Save
- await ConsumptionStorage.saveConsumption(tenant.id, consumption);
+ await ConsumptionStorage.saveConsumption(tenant, consumption);
}
// Get the phases really used from Meter Values (for AC single phase charger/car)
if (!transaction.phasesUsed &&
@@ -1282,7 +1282,7 @@ export default class OCPPService {
connector.status === ChargePointStatus.SUSPENDED_EV) {
// Check the last 3 consumptions
const consumptions = await ConsumptionStorage.getTransactionConsumptions(
- tenant.id, { transactionId: transaction.id }, { limit: 3, skip: 0, sort: { startedAt: -1 } });
+ tenant, { transactionId: transaction.id }, { limit: 3, skip: 0, sort: { startedAt: -1 } });
if (consumptions.count === 3) {
// Check the consumptions
const noConsumption = consumptions.result.every((consumption) =>
@@ -2094,7 +2094,7 @@ export default class OCPPService {
await OCPPUtils.processTransactionPricing(tenant, transaction, chargingStation, consumption, TransactionAction.STOP);
}
// Save Consumption
- await ConsumptionStorage.saveConsumption(tenant.id, consumption);
+ await ConsumptionStorage.saveConsumption(tenant, consumption);
}
// Check Inactivity and Consumption between the last Transaction.End and Stop Transaction
} else if (transaction.lastConsumption) {

MODIFY
src/server/ocpp/utils/OCPPUtils.ts
src/server/ocpp/utils/OCPPUtils.ts
@@ -186,7 +186,7 @@ export default class OCPPUtils {
// Extra inactivity
if (transaction.stop.extraInactivitySecs > 0) {
// Get the last Consumption
- const lastConsumption = await ConsumptionStorage.getLastTransactionConsumption(tenant.id, { transactionId: transaction.id });
+ const lastConsumption = await ConsumptionStorage.getLastTransactionConsumption(tenant, { transactionId: transaction.id });
if (lastConsumption) {
delete lastConsumption.id;
// Create the extra consumption with inactivity
@@ -206,7 +206,7 @@ export default class OCPPUtils {
lastConsumption.instantWattsL2 = 0;
lastConsumption.instantWattsL3 = 0;
// Save
- await ConsumptionStorage.saveConsumption(tenant.id, lastConsumption);
+ await ConsumptionStorage.saveConsumption(tenant, lastConsumption);
// Update the Stop transaction data
transaction.stop.timestamp = lastConsumption.endedAt;
transaction.stop.totalDurationSecs = Utils.createDecimal(
@@ -519,7 +519,7 @@ export default class OCPPUtils {
const transactionSimplePricePerkWh = pricePerkWh > 0 ? pricePerkWh : Utils.roundTo(transaction.stop.price / (transaction.stop.totalConsumptionWh / 1000), 2);
// Get the consumptions
const consumptionDataResult = await ConsumptionStorage.getTransactionConsumptions(
- tenant.id, { transactionId: transaction.id });
+ tenant, { transactionId: transaction.id });
transaction.currentCumulatedPrice = 0;
const consumptions = consumptionDataResult.result;
for (const consumption of consumptions) {
@@ -530,9 +530,9 @@ export default class OCPPUtils {
consumption.cumulatedAmount = transaction.currentCumulatedPrice;
}
// Delete consumptions
- await ConsumptionStorage.deleteConsumptions(tenant.id, [transaction.id]);
+ await ConsumptionStorage.deleteConsumptions(tenant, [transaction.id]);
// Save all
- await ConsumptionStorage.saveConsumptions(tenant.id, consumptions);
+ await ConsumptionStorage.saveConsumptions(tenant, consumptions);
// Update transaction
transaction.roundedPrice = Utils.truncTo(transaction.price, 2);
transaction.stop.price = transaction.currentCumulatedPrice;
@@ -646,9 +646,9 @@ export default class OCPPUtils {
}
}
// Delete first all transaction's consumptions
- await ConsumptionStorage.deleteConsumptions(tenant.id, [transaction.id]);
+ await ConsumptionStorage.deleteConsumptions(tenant, [transaction.id]);
// Save all
- await ConsumptionStorage.saveConsumptions(tenant.id, consumptions);
+ await ConsumptionStorage.saveConsumptions(tenant, consumptions);
// Update the Transaction
if (!transaction.refundData) {
transaction.roundedPrice = Utils.truncTo(transaction.price, 2);

MODIFY
src/server/rest/v1/service/AssetService.ts
src/server/rest/v1/service/AssetService.ts
@@ -78,7 +78,7 @@ export default class AssetService {
});
}
// Get the ConsumptionValues
- const consumptions = await ConsumptionStorage.getAssetConsumptions(req.user.tenantID, {
+ const consumptions = await ConsumptionStorage.getAssetConsumptions(req.tenant, {
assetID: filteredRequest.AssetID,
startDate: filteredRequest.StartDate,
endDate: filteredRequest.EndDate
@@ -167,7 +167,7 @@ export default class AssetService {
// Add site limitation
await OCPPUtils.addSiteLimitationToConsumption(req.tenant, asset.siteArea, consumptionToSave);
// Save consumption
- await ConsumptionStorage.saveConsumption(req.user.tenantID, consumptionToSave);
+ await ConsumptionStorage.saveConsumption(req.tenant, consumptionToSave);
// Assign to asset
asset.currentConsumptionWh = filteredRequest.consumptionWh;
asset.currentInstantAmps = filteredRequest.instantAmps;

MODIFY
src/server/rest/v1/service/SiteAreaService.ts
src/server/rest/v1/service/SiteAreaService.ts
@@ -259,7 +259,7 @@ export default class SiteAreaService {
const siteArea = await UtilsService.checkAndGetSiteAreaAuthorization(
req.tenant, req.user, filteredRequest.SiteAreaID, Action.READ, action);
// Get the ConsumptionValues
- const consumptions = await ConsumptionStorage.getSiteAreaConsumptions(req.user.tenantID, {
+ const consumptions = await ConsumptionStorage.getSiteAreaConsumptions(req.tenant, {
siteAreaID: filteredRequest.SiteAreaID,
startDate: filteredRequest.StartDate,
endDate: filteredRequest.EndDate

MODIFY
src/server/rest/v1/service/TransactionService.ts
src/server/rest/v1/service/TransactionService.ts
@@ -609,7 +609,7 @@ export default class TransactionService {
let consumptions: Consumption[];
if (filteredRequest.LoadAllConsumptions) {
const consumptionsMDB = await ConsumptionStorage.getTransactionConsumptions(
- req.user.tenantID, { transactionId: transaction.id }, Constants.DB_PARAMS_MAX_LIMIT, [
+ req.tenant, { transactionId: transaction.id }, Constants.DB_PARAMS_MAX_LIMIT, [
'startedAt', 'endedAt', 'cumulatedConsumptionWh', 'cumulatedConsumptionAmps', 'cumulatedAmount',
'stateOfCharge', 'limitWatts', 'limitAmps',
'instantVoltsDC', 'instantVolts', 'instantVoltsL1', 'instantVoltsL2', 'instantVoltsL3',
@@ -620,7 +620,7 @@ export default class TransactionService {
consumptions = consumptionsMDB.result;
} else {
consumptions = await ConsumptionStorage.getOptimizedTransactionConsumptions(
- req.user.tenantID, { transactionId: transaction.id }, [
+ req.tenant, { transactionId: transaction.id }, [
'consumptions.startedAt', 'consumptions.cumulatedConsumptionWh', 'consumptions.cumulatedConsumptionAmps', 'consumptions.cumulatedAmount',
'consumptions.stateOfCharge', 'consumptions.limitWatts', 'consumptions.limitAmps', 'consumptions.startedAt', 'consumptions.endedAt',
'consumptions.instantVoltsDC', 'consumptions.instantVolts', 'consumptions.instantVoltsL1', 'consumptions.instantVoltsL2', 'consumptions.instantVoltsL3',

MODIFY
src/storage/mongodb/ConsumptionStorage.ts
src/storage/mongodb/ConsumptionStorage.ts
@@ -13,29 +13,29 @@ import Utils from '../../utils/Utils';
const MODULE_NAME = 'ConsumptionStorage';
export default class ConsumptionStorage {
- static async saveConsumption(tenantID: string, consumptionToSave: Consumption): Promise<string> {
+ static async saveConsumption(tenant: Tenant, consumptionToSave: Consumption): Promise<string> {
// Debug
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'saveConsumption');
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'saveConsumption');
// Check
- await DatabaseUtils.checkTenant(tenantID);
+ DatabaseUtils.checkTenantObject(tenant);
// Build
const consumptionMDB = ConsumptionStorage.buildConsumptionMDB(consumptionToSave);
// Modify
- await global.database.getCollection<any>(tenantID, 'consumptions').findOneAndUpdate(
+ await global.database.getCollection<any>(tenant.id, 'consumptions').findOneAndUpdate(
{ '_id': consumptionMDB._id },
{ $set: consumptionMDB },
{ upsert: true });
// Debug
- await Logging.traceEnd(tenantID, MODULE_NAME, 'saveConsumption', uniqueTimerID, consumptionMDB);
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'saveConsumption', uniqueTimerID, consumptionMDB);
// Return
return consumptionMDB._id;
}
- static async saveConsumptions(tenantID: string, consumptionsToSave: Consumption[]): Promise<string[]> {
+ static async saveConsumptions(tenant: Tenant, consumptionsToSave: Consumption[]): Promise<string[]> {
// Debug
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'saveConsumptions');
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'saveConsumptions');
// Check
- await DatabaseUtils.checkTenant(tenantID);
+ DatabaseUtils.checkTenantObject(tenant);
const consumptionsMDB = [];
for (const consumptionToSave of consumptionsToSave) {
// Build
@@ -44,30 +44,30 @@ export default class ConsumptionStorage {
consumptionsMDB.push(consumptionMDB);
}
// Insert
- await global.database.getCollection<any>(tenantID, 'consumptions').insertMany(consumptionsMDB);
+ await global.database.getCollection<any>(tenant.id, 'consumptions').insertMany(consumptionsMDB);
// Debug
- await Logging.traceEnd(tenantID, MODULE_NAME, 'saveConsumptions', uniqueTimerID, consumptionsToSave);
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'saveConsumptions', uniqueTimerID, consumptionsToSave);
// Return
return consumptionsMDB.map((consumptionMDB) => consumptionMDB._id);
}
- static async deleteConsumptions(tenantID: string, transactionIDs: number[]): Promise<void> {
+ static async deleteConsumptions(tenant: Tenant, transactionIDs: number[]): Promise<void> {
// Debug
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'deleteConsumptions');
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'deleteConsumptions');
// Check
- await DatabaseUtils.checkTenant(tenantID);
+ DatabaseUtils.checkTenantObject(tenant);
// DeleFte
- await global.database.getCollection<any>(tenantID, 'consumptions')
+ await global.database.getCollection<any>(tenant.id, 'consumptions')
.deleteMany({ 'transactionId': { $in: transactionIDs } });
// Debug
- await Logging.traceEnd(tenantID, MODULE_NAME, 'deleteConsumptions', uniqueTimerID, { transactionIDs });
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'deleteConsumptions', uniqueTimerID, { transactionIDs });
}
- static async getAssetConsumptions(tenantID: string, params: { assetID: string; startDate: Date; endDate: Date }, projectFields?: string[]): Promise<Consumption[]> {
+ static async getAssetConsumptions(tenant: Tenant, params: { assetID: string; startDate: Date; endDate: Date }, projectFields?: string[]): Promise<Consumption[]> {
// Debug
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'getAssetConsumptions');
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'getAssetConsumptions');
// Check
- await DatabaseUtils.checkTenant(tenantID);
+ DatabaseUtils.checkTenantObject(tenant);
// Create filters
const filters: FilterParams = {};
// ID
@@ -135,11 +135,11 @@ export default class ConsumptionStorage {
// Project
DatabaseUtils.projectFields(aggregation, projectFields);
// Read DB
- const consumptionsMDB = await global.database.getCollection<Consumption>(tenantID, 'consumptions')
+ const consumptionsMDB = await global.database.getCollection<Consumption>(tenant.id, 'consumptions')
.aggregate(aggregation, { allowDiskUse: true })
.toArray();
// Debug
- await Logging.traceEnd(tenantID, MODULE_NAME, 'getAssetConsumptions', uniqueTimerID, consumptionsMDB);
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'getAssetConsumptions', uniqueTimerID, consumptionsMDB);
return consumptionsMDB;
}
@@ -182,13 +182,13 @@ export default class ConsumptionStorage {
return consumption;
}
- static async getSiteAreaConsumptions(tenantID: string,
+ static async getSiteAreaConsumptions(tenant: Tenant,
params: { siteAreaID: string; startDate: Date; endDate: Date },
projectFields?: string[]): Promise<Consumption[]> {
// Debug
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'getSiteAreaConsumptions');
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'getSiteAreaConsumptions');
// Check
- await DatabaseUtils.checkTenant(tenantID);
+ DatabaseUtils.checkTenantObject(tenant);
// Create filters
const filters: FilterParams = {};
// ID
@@ -257,11 +257,11 @@ export default class ConsumptionStorage {
// Project
DatabaseUtils.projectFields(aggregation, projectFields, ['_id']);
// Read DB
- const consumptionsMDB = await global.database.getCollection<Consumption>(tenantID, 'consumptions')
+ const consumptionsMDB = await global.database.getCollection<Consumption>(tenant.id, 'consumptions')
.aggregate(aggregation, { allowDiskUse: true })
.toArray();
// Debug
- await Logging.traceEnd(tenantID, MODULE_NAME, 'getSiteAreaConsumptions', uniqueTimerID, consumptionsMDB);
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'getSiteAreaConsumptions', uniqueTimerID, consumptionsMDB);
return consumptionsMDB;
}
@@ -362,12 +362,12 @@ export default class ConsumptionStorage {
};
}
- static async getTransactionConsumptions(tenantID: string, params: { transactionId: number },
+ static async getTransactionConsumptions(tenant: Tenant, params: { transactionId: number },
dbParams: DbParams = Constants.DB_PARAMS_MAX_LIMIT, projectFields?: string[]): Promise<DataResult<Consumption>> {
// Debug
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'getTransactionConsumptions');
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'getTransactionConsumptions');
// Check
- await DatabaseUtils.checkTenant(tenantID);
+ DatabaseUtils.checkTenantObject(tenant);
// Clone before updating the values
dbParams = Utils.cloneObject(dbParams);
// Check Limit
@@ -404,22 +404,22 @@ export default class ConsumptionStorage {
// Project
DatabaseUtils.projectFields(aggregation, projectFields);
// Read DB
- const consumptionsMDB = await global.database.getCollection<Consumption>(tenantID, 'consumptions')
+ const consumptionsMDB = await global.database.getCollection<Consumption>(tenant.id, 'consumptions')
.aggregate(aggregation, { allowDiskUse: true })
.toArray();
// Debug
- await Logging.traceEnd(tenantID, MODULE_NAME, 'getTransactionConsumptions', uniqueTimerID, consumptionsMDB);
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'getTransactionConsumptions', uniqueTimerID, consumptionsMDB);
return {
count: consumptionsMDB.length,
result: consumptionsMDB
};
}
- static async getLastTransactionConsumption(tenantID: string, params: { transactionId: number }): Promise<Consumption> {
+ static async getLastTransactionConsumption(tenant: Tenant, params: { transactionId: number }): Promise<Consumption> {
// Debug
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'getLastTransactionConsumption');
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'getLastTransactionConsumption');
// Check
- await DatabaseUtils.checkTenant(tenantID);
+ DatabaseUtils.checkTenantObject(tenant);
// Create Aggregation
const aggregation = [];
// Filters
@@ -443,22 +443,22 @@ export default class ConsumptionStorage {
});
let consumption: Consumption = null;
// Read DB
- const consumptionsMDB = await global.database.getCollection<Consumption>(tenantID, 'consumptions')
+ const consumptionsMDB = await global.database.getCollection<Consumption>(tenant.id, 'consumptions')
.aggregate(aggregation, { allowDiskUse: true })
.toArray();
if (consumptionsMDB && consumptionsMDB.length > 0) {
consumption = consumptionsMDB[0];
}
// Debug
- await Logging.traceEnd(tenantID, MODULE_NAME, 'getLastTransactionConsumption', uniqueTimerID, consumptionsMDB);
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'getLastTransactionConsumption', uniqueTimerID, consumptionsMDB);
return consumption;
}
- static async getOptimizedTransactionConsumptions(tenantID: string, params: { transactionId: number }, projectFields?: string[]): Promise<Consumption[]> {
+ static async getOptimizedTransactionConsumptions(tenant: Tenant, params: { transactionId: number }, projectFields?: string[]): Promise<Consumption[]> {
// Debug
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'getOptimizedTransactionConsumptions');
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'getOptimizedTransactionConsumptions');
// Check
- await DatabaseUtils.checkTenant(tenantID);
+ DatabaseUtils.checkTenantObject(tenant);
// Create Aggregation
const aggregation = [];
// Filters
@@ -490,7 +490,7 @@ export default class ConsumptionStorage {
// Project
DatabaseUtils.projectFields(aggregation, projectFields);
// Read DB
- const consumptionsMDB = await global.database.getCollection<any>(tenantID, 'consumptions')
+ const consumptionsMDB = await global.database.getCollection<any>(tenant.id, 'consumptions')
.aggregate(aggregation, { allowDiskUse: true })
.toArray();
// TODO: Handle this coding into MongoDB request
@@ -528,7 +528,7 @@ export default class ConsumptionStorage {
// Sort
consumptions.sort((cons1, cons2) => cons1.endedAt.getTime() - cons2.endedAt.getTime());
// Debug
- await Logging.traceEnd(tenantID, MODULE_NAME, 'getOptimizedTransactionConsumptions', uniqueTimerID, consumptions);
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'getOptimizedTransactionConsumptions', uniqueTimerID, consumptions);
return consumptions;
}

MODIFY
src/storage/mongodb/TransactionStorage.ts
src/storage/mongodb/TransactionStorage.ts
@@ -35,7 +35,7 @@ export default class TransactionStorage {
await global.database.getCollection<any>(tenant.id, 'metervalues')
.deleteMany({ 'transactionId': { $in: transactionsIDs } });
// Delete Consumptions
- await ConsumptionStorage.deleteConsumptions(tenant.id, transactionsIDs);
+ await ConsumptionStorage.deleteConsumptions(tenant, transactionsIDs);
// Debug
await Logging.traceEnd(tenant.id, MODULE_NAME, 'deleteTransaction', uniqueTimerID, { transactionsIDs });
return result.deletedCount;

Changing Tenant ID to Object Consumption Storage