2021-04-25 20:00:42 +02:00
|
|
|
"use strict";
|
|
|
|
|
2021-05-17 22:09:49 +02:00
|
|
|
const utils = require('../../services/utils');
|
|
|
|
const sql = require('../../services/sql');
|
|
|
|
const entityChangesService = require('../../services/entity_changes');
|
|
|
|
const eventService = require("../../services/events");
|
|
|
|
const dateUtils = require("../../services/date_utils");
|
|
|
|
const cls = require("../../services/cls");
|
2021-04-25 21:19:18 +02:00
|
|
|
|
2021-04-30 22:13:13 +02:00
|
|
|
let becca = null;
|
2021-04-25 21:19:18 +02:00
|
|
|
|
2021-04-25 20:00:42 +02:00
|
|
|
class AbstractEntity {
|
|
|
|
beforeSaving() {
|
|
|
|
this.generateIdIfNecessary();
|
|
|
|
}
|
|
|
|
|
|
|
|
generateIdIfNecessary() {
|
|
|
|
if (!this[this.constructor.primaryKeyName]) {
|
|
|
|
this[this.constructor.primaryKeyName] = utils.newEntityId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-08 21:10:58 +02:00
|
|
|
generateHash(isDeleted = false) {
|
2021-04-25 20:00:42 +02:00
|
|
|
let contentToHash = "";
|
|
|
|
|
|
|
|
for (const propertyName of this.constructor.hashedProperties) {
|
|
|
|
contentToHash += "|" + this[propertyName];
|
|
|
|
}
|
|
|
|
|
2021-05-08 21:10:58 +02:00
|
|
|
if (isDeleted) {
|
|
|
|
contentToHash += "|deleted";
|
|
|
|
}
|
|
|
|
|
2021-04-25 20:00:42 +02:00
|
|
|
return utils.hash(contentToHash).substr(0, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
getUtcDateChanged() {
|
2021-06-26 17:32:40 +02:00
|
|
|
return this.utcDateModified || this.utcDateCreated;
|
2021-04-25 20:00:42 +02:00
|
|
|
}
|
|
|
|
|
2021-04-30 22:13:13 +02:00
|
|
|
get becca() {
|
|
|
|
if (!becca) {
|
2021-05-17 22:09:49 +02:00
|
|
|
becca = require('../becca.js');
|
2021-04-30 22:13:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return becca;
|
|
|
|
}
|
|
|
|
|
2021-05-08 21:10:58 +02:00
|
|
|
addEntityChange(isDeleted = false) {
|
|
|
|
entityChangesService.addEntityChange({
|
|
|
|
entityName: this.constructor.entityName,
|
|
|
|
entityId: this[this.constructor.primaryKeyName],
|
|
|
|
hash: this.generateHash(isDeleted),
|
|
|
|
isErased: false,
|
|
|
|
utcDateChanged: this.getUtcDateChanged(),
|
|
|
|
isSynced: this.constructor.entityName !== 'options' || !!this.isSynced
|
|
|
|
});
|
2021-04-25 20:00:42 +02:00
|
|
|
}
|
|
|
|
|
2021-09-08 22:35:32 +02:00
|
|
|
getPojoToSave() {
|
|
|
|
return this.getPojo();
|
|
|
|
}
|
|
|
|
|
2021-04-25 20:00:42 +02:00
|
|
|
save() {
|
2021-05-08 21:10:58 +02:00
|
|
|
const entityName = this.constructor.entityName;
|
|
|
|
const primaryKeyName = this.constructor.primaryKeyName;
|
|
|
|
|
|
|
|
const isNewEntity = !this[primaryKeyName];
|
|
|
|
|
|
|
|
if (this.beforeSaving) {
|
|
|
|
this.beforeSaving();
|
|
|
|
}
|
|
|
|
|
2021-09-08 22:35:32 +02:00
|
|
|
const pojo = this.getPojoToSave();
|
2021-05-08 21:10:58 +02:00
|
|
|
|
|
|
|
sql.transactional(() => {
|
|
|
|
sql.upsert(entityName, primaryKeyName, pojo);
|
|
|
|
|
|
|
|
if (entityName === 'recent_notes') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.addEntityChange(false);
|
|
|
|
|
|
|
|
if (!cls.isEntityEventsDisabled()) {
|
|
|
|
const eventPayload = {
|
|
|
|
entityName,
|
|
|
|
entity: this
|
|
|
|
};
|
|
|
|
|
|
|
|
if (isNewEntity) {
|
|
|
|
eventService.emit(eventService.ENTITY_CREATED, eventPayload);
|
|
|
|
}
|
|
|
|
|
|
|
|
eventService.emit(eventService.ENTITY_CHANGED, eventPayload);
|
|
|
|
}
|
|
|
|
});
|
2021-04-25 20:00:42 +02:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2021-05-08 21:10:58 +02:00
|
|
|
|
|
|
|
markAsDeleted(deleteId = null) {
|
2021-05-08 23:31:20 +02:00
|
|
|
const entityId = this[this.constructor.primaryKeyName];
|
|
|
|
const entityName = this.constructor.entityName;
|
2021-05-08 21:10:58 +02:00
|
|
|
|
2021-05-09 11:12:53 +02:00
|
|
|
sql.execute(`UPDATE ${entityName} SET isDeleted = 1, deleteId = ?, utcDateModified = ?
|
|
|
|
WHERE ${this.constructor.primaryKeyName} = ?`,
|
|
|
|
[deleteId, dateUtils.utcNowDateTime(), entityId]);
|
|
|
|
|
|
|
|
if (this.dateModified) {
|
|
|
|
sql.execute(`UPDATE ${entityName} SET dateModified = ? WHERE ${this.constructor.primaryKeyName} = ?`,
|
|
|
|
[dateUtils.localNowDateTime(), entityId]);
|
|
|
|
}
|
2021-05-08 21:10:58 +02:00
|
|
|
|
2021-05-08 23:31:20 +02:00
|
|
|
this.addEntityChange(true);
|
2021-05-08 21:10:58 +02:00
|
|
|
|
2021-07-22 20:19:44 +02:00
|
|
|
eventService.emit(eventService.ENTITY_DELETED, { entityName, entityId, entity: this });
|
2021-05-08 21:10:58 +02:00
|
|
|
}
|
2021-04-25 20:00:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = AbstractEntity;
|