const VersionedModel = require('../VersionedModel') const uuid = require('uuid/v4') /* * Node Model * ------------------------------------------------------------- * Put some description here! */ class Node extends VersionedModel { static get services() { return [...super.services, 'models'] } static get schema() { // Return a flitter-orm schema here. return { ...super.schema, UUID: { type: String, default: () => uuid() }, Type: String, Value: { Mode: {type: String, default: 'norm'}, Value: String, }, PageId: String, CreatedAt: { type: Date, default: () => new Date }, UpdatedAt: { type: Date, default: () => new Date }, CreatedUserId: String, UpdateUserId: String }; } // Static and instance methods can go here get page() { const Page = this.models.get('api:Page') return this.belongs_to_one(Page, "PageId", "UUID") } to_html() { switch(this.Type){ case 'paragraph': return '
' + this.Value.Value + '
' case 'header1': return '' + this.Value.Value + '
'
case 'click_link':
return '' + this.Value.Value + ''
}
}
update_from_raw(data) {
if ( data.Type ) this.Type = data.Type
if ( data.Value ) this.Value = data.Value
}
async cast_to_version_data() {
const data = await super.cast_to_version_data()
// If supported, fetch the current version of the node item
if ( this.Type === 'code_ref' && this.Value?.Value ) {
const Codium = this.models.get('api:Codium')
const code = await Codium.findOne({ UUID: this.Value?.Value, Active: true })
if ( code ) {
data.associated_type_version_num = code.version_num
}
} else if ( this.Type === 'file_ref' && this.Value?.Value ) {
const FileGroup = this.models.get('api:FileGroup')
const group = await FileGroup.findOne({ UUID: this.Value?.Value })
if ( group ) {
data.associated_type_version_num = group.version_num
}
} else if ( this.Type === 'database_ref' && this.Value?.Value ) {
const Database = this.models.get('api:db:Database')
const db = await Database.findOne({ Active: true, UUID: this.Value?.Value })
if ( db ) {
data.associated_type_version_num = db.version_num
}
}
return data
}
async revert_to_version(version_num, user_id = undefined) {
const reverted = await super.revert_to_version(version_num, user_id)
const data = await this.get_version_data(version_num)
if ( !data.associated_type_version_num ) return reverted
// If supported, revert the version of the node item
if ( this.Type === 'code_ref' && this.Value?.Value ) {
const Codium = this.models.get('api:Codium')
const code = await Codium.findOne({ UUID: this.Value?.Value, Active: true })
if ( code ) {
await code.revert_to_version(data.associated_type_version_num)
}
} else if ( this.Type === 'file_ref' && this.Value?.Value ) {
const FileGroup = this.models.get('api:FileGroup')
const group = await FileGroup.findOne({ UUID: this.Value?.Value })
if ( group ) {
await group.revert_to_version(data.associated_type_version_num)
}
} else if ( this.Type === 'database_ref' && this.Value?.Value ) {
const Database = this.models.get('api:db:Database')
const db = await Database.findOne({ Active: true, UUID: this.Value?.Value })
if ( db ) {
await db.revert_to_version(data.associated_type_version_num)
}
}
return reverted
}
}
module.exports = exports = Node;