Put user start transaction info in a dedicated structure
MODIFY
src/server/ocpp/services/OCPPService.ts
src/server/ocpp/services/OCPPService.ts
@@ -1625,10 +1625,11 @@ export default class OCPPService {
case TransactionAction.START:
// Handle car in transaction start
if (Utils.isTenantComponentActive(tenant, TenantComponents.CAR) && user) {
- // Check default car
- if (user.lastSelectedCarID) {
- transaction.carID = user.lastSelectedCarID;
- } else if (!user.lastSelectedCar) {
+ // Check Car from User selection (valid for 5 mins)
+ if (user.startTransactionData?.lastSelectedCar &&
+ user.startTransactionData.lastChangedOn.getTime() + 5 * 60 * 1000 > Date.now()) {
+ transaction.carID = user.startTransactionData.lastSelectedCarID;
+ } else {
// Get default car if any
const defaultCar = await CarStorage.getDefaultUserCar(tenant, user.id, {},
['id', 'carCatalogID', 'vin', 'carConnectorData.carConnectorID', 'carConnectorData.carConnectorMeterID']);

MODIFY
src/server/ocpp/utils/OCPPUtils.ts
src/server/ocpp/utils/OCPPUtils.ts
@@ -1555,12 +1555,17 @@ export default class OCPPUtils {
message: 'Charging Station has been forced as inactive!'
});
}
- // Save Charging Station lastSeen date
+ // Reassign to the Charging station
+ chargingStation.lastSeen = new Date();
+ chargingStation.tokenID = tokenID;
+ chargingStation.cloudHostIP = Utils.getHostIP();
+ chargingStation.cloudHostName = Utils.getHostName();
+ // Save Charging Station runtime data
await ChargingStationStorage.saveChargingStationRuntimeData(tenant, chargingStation.id, {
- lastSeen: new Date(),
- tokenID: tokenID,
- cloudHostIP: Utils.getHostIP(),
- cloudHostName: Utils.getHostName(),
+ lastSeen: chargingStation.lastSeen,
+ tokenID: chargingStation.tokenID,
+ cloudHostIP: chargingStation.cloudHostIP,
+ cloudHostName: chargingStation.cloudHostName,
});
}
return { tenant, chargingStation, token };

MODIFY
src/server/rest/v1/service/ChargingStationService.ts
src/server/rest/v1/service/ChargingStationService.ts
@@ -1784,7 +1784,7 @@ export default class ChargingStationService {
Authorizations.isChargingStationValidInOrganization(action, req.tenant, chargingStation);
// Save Car selection
if (Utils.isComponentActiveFromToken(req.user, TenantComponents.CAR)) {
- if (filteredRequest.carID && filteredRequest.carID !== user.lastSelectedCarID) {
+ if (filteredRequest.carID && filteredRequest.carID !== user.startTransactionData?.lastSelectedCarID) {
await UserStorage.saveLastSelectedCarID(req.tenant, user.id, filteredRequest.carID, true);
} else {
await UserStorage.saveLastSelectedCarID(req.tenant, user.id, null, true);

MODIFY
src/storage/mongodb/ChargingStationStorage.ts
src/storage/mongodb/ChargingStationStorage.ts
@@ -608,7 +608,6 @@ export default class ChargingStationStorage {
params: { lastSeen: Date; currentIPAddress?: string | string[]; tokenID?: string; cloudHostIP?: string; cloudHostName?: string; }): Promise<void> {
const startTime = Logging.traceDatabaseRequestStart();
DatabaseUtils.checkTenantObject(tenant);
- // Set data
// Modify document
await global.database.getCollection<any>(tenant.id, 'chargingstations').findOneAndUpdate(
{ '_id': id },
@@ -691,7 +690,7 @@ export default class ChargingStationStorage {
return value;
}
- static async saveOcppParameters(tenant: Tenant, parameters: ChargingStationOcppParameters): Promise<void> {
+ public static async saveOcppParameters(tenant: Tenant, parameters: ChargingStationOcppParameters): Promise<void> {
const startTime = Logging.traceDatabaseRequestStart();
DatabaseUtils.checkTenantObject(tenant);
// Modify

MODIFY
src/storage/mongodb/UserStorage.ts
src/storage/mongodb/UserStorage.ts
@@ -366,7 +366,13 @@ export default class UserStorage {
// Modify and return the modified document
await global.database.getCollection<any>(tenant.id, 'users').findOneAndUpdate(
{ '_id': DatabaseUtils.convertToObjectID(userID) },
- { $set: { lastSelectedCarID, lastSelectedCar } });
+ { $set: {
+ startTransactionData: {
+ lastChangedOn: new Date(),
+ lastSelectedCarID,
+ lastSelectedCar
+ }
+ } });
await Logging.traceDatabaseRequestEnd(tenant, MODULE_NAME, 'saveUserLastSelectedCarID', startTime, { lastSelectedCarID });
}

MODIFY
src/types/User.ts
src/types/User.ts
@@ -36,8 +36,7 @@ export default interface User extends CreatedUpdatedProps, AuthorizationActions
mobileOs: string;
mobileToken: string;
mobileLastChangedOn: Date;
- lastSelectedCarID?: string;
- lastSelectedCar?: boolean;
+ startTransactionData?: StartTransactionUserData;
authorizationID?: string;
importedData?: {
autoActivateUserAtImport: boolean;
@@ -46,6 +45,12 @@ export default interface User extends CreatedUpdatedProps, AuthorizationActions
freeAccess?: boolean;
}
+export interface StartTransactionUserData {
+ lastChangedOn: Date;
+ lastSelectedCarID: string;
+ lastSelectedCar: boolean;
+}
+
export interface UserSite {
user: User;
userID?: string;

Put user start transaction info in a dedicated structure