add lastChangedOn attribute to car catalogs
MODIFY
src/integration/car/CarIntegration.ts
src/integration/car/CarIntegration.ts
@@ -22,6 +22,7 @@ export default abstract class CarIntegration {
if (!internalCar) {
// New Car: Create it
externalCar.hash = Cypher.hash(JSON.stringify(externalCar));
+ externalCar.lastChangedOn = new Date();
externalCar.createdOn = new Date();
// Get image
externalCar.image = await this.getCarCatalogThumb(externalCar);

MODIFY
src/migration/MigrationHandler.ts
src/migration/MigrationHandler.ts
@@ -6,6 +6,7 @@ import AddDescriptionToTagsTask from './tasks/AddDescriptionToTagsTask';
import AddInactivityStatusInTransactionsTask from './tasks/AddInactivityStatusInTransactionsTask';
import AddIssuerFieldTask from './tasks/AddIssuerFieldTask';
import AddLastChangePropertiesToBadgeTask from './tasks/AddLastChangePropertiesToBadgeTask';
+import AddLastChangedOnToCarCatalogTask from './tasks/AddLastChangedOnToCarCatalogTask';
import AddNotificationsFlagsToUsersTask from './tasks/AddNotificationsFlagsToUsersTask';
import AddSensitiveDataInSettingsTask from './tasks/AddSensitiveDataInSettingsTask';
import AddSiteAreaLimitToConsumptionsTask from './tasks/AddSiteAreaLimitToConsumptionsTask';
@@ -105,6 +106,7 @@ export default class MigrationHandler {
currentMigrationTasks.push(new FixedConsumptionRoundedPriceTask());
currentMigrationTasks.push(new MigrateCryptoSettingsFromConfigToDBTask());
currentMigrationTasks.push(new ImportLocalCarCatalogTask());
+ currentMigrationTasks.push(new AddLastChangedOnToCarCatalogTask());
// Get the already done migrations from the DB
const migrationTasksDone = await MigrationStorage.getMigrations();
// Check

ADD
src/migration/tasks/AddLastChangedOnToCarCatalogTask.ts
+lastChangedOnimport Constants from '../../utils/Constants';
+import Logging from '../../utils/Logging';
+import MigrationTask from '../MigrationTask';
+import { ServerAction } from '../../types/Server';
+import global from '../../types/GlobalType';
+
+const MODULE_NAME = 'AddLastChangedOnToCarCatalogTask';
+
+export default class AddLastChangedOnToCarCatalogTask extends MigrationTask {
+ async migrate(): Promise<void> {
+ try {
+ const result = await global.database.getCollection<any>(Constants.DEFAULT_TENANT, 'carcatalogs').updateMany({
+ 'lastChangedOn': { $exists: false }
+ },
+ [
+ { '$set': { 'lastChangedOn': '$createdOn' } }
+ ]);
+ if (result.modifiedCount > 0) {
+ Logging.logDebug({
+ tenantID: Constants.DEFAULT_TENANT,
+ action: ServerAction.MIGRATION,
+ module: MODULE_NAME, method: 'migrate',
+ message: `${result.modifiedCount} Car(s) catalog(s) have been updated in the default tenant`
+ });
+ }
+ } catch (error) {
+ Logging.logError({
+ tenantID: Constants.DEFAULT_TENANT,
+ module: MODULE_NAME, method: 'migrate',
+ action: ServerAction.MIGRATION,
+ message: `Error while updating the Cars catalog: ${error.message}`,
+ detailedMessages: { error: error.message, stack: error.stack }
+ });
+ }
+ }
+
+ getVersion(): string {
+ return '1.2';
+ }
+
+ isAsynchronous(): boolean {
+ return true;
+ }
+
+ getName(): string {
+ return 'AddLastChangedOnToCarCatalogTask';
+ }
+}

MODIFY
src/storage/mongodb/CarStorage.ts
src/storage/mongodb/CarStorage.ts
@@ -111,7 +111,9 @@ export default class CarStorage {
if: { $gt: ['$image', null] }, then: {
$concat: [
`${Utils.buildRestServerURL()}/client/util/CarCatalogImage?ID=`,
- { $toString: '$_id' }
+ { $toString: '$_id' },
+ '&LastChangedOn=',
+ { $toString: '$lastChangedOn' }
]
}, else: null
}
@@ -713,7 +715,9 @@ export default class CarStorage {
if: { $gt: ['$carCatalog.image', null] }, then: {
$concat: [
`${Utils.buildRestServerURL()}/client/util/CarCatalogImage?ID=`,
- '$carCatalog.id'
+ '$carCatalog.id',
+ '&LastChangedOn=',
+ { $toString: '$carCatalog.lastChangedOn' }
]
}, else: null
}

add lastChangedOn attribute to car catalogs