import { v4 } from 'uuid'; import { Session } from './session/session'; import { get } from 'lodash'; export declare type ExtraProperties = { [P in keyof T]?: any; } /** * Entidade do sistema com metadados. */ export class Entity { public _id: string; public _created_at: string; public _updated_at: string; public _id_cliente: string; public _id_empresa: string; public _sync: boolean; public _sync_local: boolean; public _last_sync: string; public _first_sync: string; public _deleted: boolean; public extra_properties?: {[property: string]: ExtraProperties}; public additional_properties?: {[property: string]: ExtraProperties}; constructor(json?: any, generateId?: boolean, overwriteFromExtraProperties?: boolean) { json = json || {}; const gerarId = json.generateId !== false && generateId !== false; delete json.generateId; this._id = json._id || ( gerarId ? v4() : undefined ); this._created_at = json._created_at || new Date().toISOString(); this._updated_at = json._updated_at || new Date().toISOString(); this._id_cliente = json._id_cliente; this._id_empresa = json._id_empresa; this._last_sync = json._last_sync; this._first_sync = json._first_sync; this._sync = json._sync || false; this._sync_local = true; this._deleted = json._deleted; this.extra_properties = json.extra_properties; if (this.extra_properties && overwriteFromExtraProperties) Object.keys(this.extra_properties) .map(key => this[key] = json.extra_properties[key]); this.additional_properties = json.additional_properties if (this.additional_properties) Object.keys(json.additional_properties) .map(key => this[key] = json.additional_properties[key]); } public getId() { return this._id; } public update() { this._updated_at = new Date().toISOString(); this._sync = false; this._sync_local = false; return this; } public sign(session: Session) { this._id_cliente = get(session, 'apisession.cliente._id'); this._id_empresa = get(session, 'apisession.empresa._id'); return this; } toJson(): any { return Object.assign({}, this, { _sync_local: undefined }); } }