Save consumption properly
MODIFY
src/scheduler/tasks/AssetGetConsumptionTask.ts
src/scheduler/tasks/AssetGetConsumptionTask.ts
@@ -54,8 +54,8 @@ export default class AssetGetConsumptionTask extends SchedulerTask {
await AssetStorage.saveAsset(tenant.id, asset);
// Create Consumption
const consumption: Consumption = {
- id: '1234',
startedAt: asset.lastConsumption.timestamp,
+ endedAt: new Date(),
assetID: asset.id,
cumulatedConsumptionWh: asset.currentConsumptionWh,
cumulatedConsumptionAmps: Math.floor(asset.currentConsumptionWh / 230),

MODIFY
src/storage/mongodb/ConsumptionStorage.ts
src/storage/mongodb/ConsumptionStorage.ts
@@ -15,9 +15,14 @@ export default class ConsumptionStorage {
await Utils.checkTenant(tenantID);
// Set the ID
if (!consumptionToSave.id ) {
- // Set the ID
const timestamp = Utils.convertToDate(consumptionToSave.endedAt);
+ if (consumptionToSave.transactionId) {
consumptionToSave.id = Cypher.hash(`${consumptionToSave.transactionId}~${timestamp.toISOString()}`);
+ } else if (consumptionToSave.assetID) {
+ consumptionToSave.id = Cypher.hash(`${consumptionToSave.assetID}~${timestamp.toISOString()}`);
+ } else {
+ throw new Error('Consumption cannot be saved: no Transaction ID and Asset ID provided');
+ }
}
// Transfer
const consumptionMDB: any = {

MODIFY
src/types/Consumption.ts
src/types/Consumption.ts
@@ -44,7 +44,7 @@ export interface AbstractConsumption {
}
export default interface Consumption extends AbstractConsumption {
- id: string;
+ id?: string;
startedAt: Date;
endedAt?: Date;
transactionId?: number;

Save consumption properly