Add model logic & api endpoint for reverting page versions
This commit is contained in:
@@ -98,11 +98,23 @@ class VersionedModel extends Model {
|
||||
|
||||
async as_version(version_num = undefined) {
|
||||
const data = await this.get_version_data(version_num)
|
||||
console.log('as version data', data, version_num)
|
||||
const inst = new this.constructor(data)
|
||||
inst.__initialize_version(this, data)
|
||||
return inst
|
||||
}
|
||||
|
||||
async revert_to_version(version_num, user_id = undefined) {
|
||||
const inst = await this.as_version(version_num)
|
||||
inst.version_archive = this.version_archive
|
||||
inst._id = this._id
|
||||
inst.__is_version_instantation = false
|
||||
inst.__version_base = undefined
|
||||
inst.__version_data = undefined
|
||||
inst.version_num = this.version_num
|
||||
await inst.new_version(`Revert to version ${version_num}`, user_id)
|
||||
await inst.save()
|
||||
return inst
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = exports = VersionedModel
|
||||
|
||||
@@ -85,6 +85,36 @@ class Node extends VersionedModel {
|
||||
|
||||
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;
|
||||
|
||||
@@ -308,6 +308,24 @@ class Page extends VersionedModel {
|
||||
data.node_version_nums = node_version_nums
|
||||
return data
|
||||
}
|
||||
|
||||
async revert_to_version(version_num, user_id = undefined) {
|
||||
const Node = this.models.get('api:Node')
|
||||
const reverted = await super.revert_to_version(version_num, user_id)
|
||||
const data = await this.get_version_data(version_num)
|
||||
|
||||
// Revert the nodes to the given versions
|
||||
for ( const node_data of data.node_version_nums ) {
|
||||
const node = await Node.findOne({ UUID: node_data.NodeId })
|
||||
if ( node ) {
|
||||
const reverted_node = await node.revert_to_version(node_data.version_num)
|
||||
await reverted_node.save()
|
||||
}
|
||||
}
|
||||
|
||||
await reverted.save()
|
||||
return reverted
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = exports = Page;
|
||||
|
||||
Reference in New Issue
Block a user