Save only user necessary data
MODIFY
src/server/rest/v1/service/ChargingStationService.ts
src/server/rest/v1/service/ChargingStationService.ts
@@ -1022,10 +1022,10 @@ export default class ChargingStationService {
// Ok: Execute it
result = await this.handleChargingStationCommand(
req.user.tenantID, req.user, chargingStation, action, command, filteredRequest.args);
- if (user && result && result.status === OCPPRemoteStartStopStatus.ACCEPTED) {
- if (filteredRequest.carID !== user.lastSelectedCarID) {
- user.lastSelectedCarID = filteredRequest.carID;
- await UserStorage.saveUser(req.user.tenantID, user);
+ // Save Car ID
+ if (result?.status === OCPPRemoteStartStopStatus.ACCEPTED) {
+ if (filteredRequest.carID && filteredRequest.carID !== user.lastSelectedCarID) {
+ await UserStorage.saveUserLastSelectedCarID(req.user.tenantID, user.id, filteredRequest.carID);
}
}
} else if (command === Command.GET_COMPOSITE_SCHEDULE) {

MODIFY
src/storage/mongodb/UserStorage.ts
src/storage/mongodb/UserStorage.ts
@@ -240,7 +240,6 @@ export default class UserStorage {
sendBillingNewInvoice: userToSave.notifications ? Utils.convertToBoolean(userToSave.notifications.sendBillingNewInvoice) : false,
},
deleted: Utils.objectHasProperty(userToSave, 'deleted') ? userToSave.deleted : false,
- lastSelectedCarID: userToSave.lastSelectedCarID
};
if (userToSave.address) {
userMDB.address = {
@@ -301,6 +300,19 @@ export default class UserStorage {
Logging.traceEnd(tenantID, MODULE_NAME, 'saveUserStatus', uniqueTimerID);
}
+ public static async saveUserLastSelectedCarID(tenantID: string, userID: string, lastSelectedCarID: string): Promise<void> {
+ // Debug
+ const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'saveUserStatus');
+ // Check Tenant
+ await DatabaseUtils.checkTenant(tenantID);
+ // Modify and return the modified document
+ await global.database.getCollection<any>(tenantID, 'users').findOneAndUpdate(
+ { '_id': Utils.convertToObjectID(userID) },
+ { $set: { lastSelectedCarID } });
+ // Debug
+ Logging.traceEnd(tenantID, MODULE_NAME, 'saveUserStatus', uniqueTimerID);
+ }
+
public static async saveUserMobileToken(tenantID: string, userID: string,
params: { mobileToken: string; mobileOs: string; mobileLastChangedOn: Date }): Promise<void> {
// Debug

Save only user necessary data