109 lines
2.9 KiB
JavaScript
109 lines
2.9 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.Entity = void 0;
|
|
const bson_objectid_1 = __importDefault(require("bson-objectid"));
|
|
const date_helper_1 = require("./helpers/date.helper");
|
|
function equals(a, b) {
|
|
if (a === null || b === null) {
|
|
return a === b;
|
|
}
|
|
if (a === undefined || b === undefined) {
|
|
return a === b;
|
|
}
|
|
if (typeof a === 'object' && Reflect.has(a, 'equals')) {
|
|
const equalsFn = Reflect.get(a, 'equals');
|
|
if (typeof equalsFn === 'function') {
|
|
return equalsFn.call(a, b);
|
|
}
|
|
}
|
|
return a === b;
|
|
}
|
|
class Entity {
|
|
attr;
|
|
constructor(attr, actor) {
|
|
this.attr = attr;
|
|
this.attr = attr;
|
|
if (!this.attr.id) {
|
|
this.attr.id = new bson_objectid_1.default();
|
|
}
|
|
if (!this.attr.createdAt) {
|
|
this.attr.createdAt = (0, date_helper_1.now)();
|
|
}
|
|
if (actor) {
|
|
this.actor = actor;
|
|
}
|
|
if (!this.attr.createdBy) {
|
|
if (this.actor) {
|
|
this.attr.createdBy = this.actor;
|
|
}
|
|
else {
|
|
// TODO: This should maybe be system user?
|
|
this.attr.createdBy = {
|
|
id: bson_objectid_1.default.createFromHexString('d34d01d0d34d01d0d34d01d0'),
|
|
type: 'user',
|
|
role: 'Owner'
|
|
};
|
|
}
|
|
}
|
|
this.attr.deleted = false;
|
|
}
|
|
events = [];
|
|
addEvent(event) {
|
|
this.events.push(event);
|
|
}
|
|
static getEventsToDispatch(entity, fn) {
|
|
const events = entity.events;
|
|
entity.events = [];
|
|
fn(events);
|
|
}
|
|
actor;
|
|
setActor(actor) {
|
|
if (this.actor !== null) {
|
|
throw new Error(`Entity already owned by ${actor.id}`);
|
|
}
|
|
this.actor = actor;
|
|
}
|
|
get id() {
|
|
return this.attr.id;
|
|
}
|
|
get createdAt() {
|
|
return this.attr.createdAt;
|
|
}
|
|
get createdBy() {
|
|
return this.attr.createdBy;
|
|
}
|
|
get updatedAt() {
|
|
return this.attr.updatedAt;
|
|
}
|
|
get updatedBy() {
|
|
return this.attr.updatedBy;
|
|
}
|
|
get deleted() {
|
|
return this.attr.deleted;
|
|
}
|
|
delete() {
|
|
this.attr.deleted = true;
|
|
}
|
|
set(key, value, actor) {
|
|
if (equals(this.attr[key], value)) {
|
|
return;
|
|
}
|
|
this.attr[key] = value;
|
|
if (actor) {
|
|
this.attr.updatedAt = (0, date_helper_1.now)();
|
|
this.attr.updatedBy = actor;
|
|
}
|
|
else if (this.actor) {
|
|
this.attr.updatedAt = (0, date_helper_1.now)();
|
|
this.attr.updatedBy = this.actor;
|
|
}
|
|
else {
|
|
// Maybe log a warning or smth?
|
|
}
|
|
}
|
|
}
|
|
exports.Entity = Entity;
|
|
//# sourceMappingURL=entity.base.js.map
|