Add VersionedModel base and version page changes
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2020-11-02 11:59:57 -06:00
parent dffb3d9efa
commit 3aab56b5ac
4 changed files with 82 additions and 27 deletions

View File

@@ -0,0 +1,46 @@
const Model = require('flitter-orm/src/model/Model');
const uuid = require('uuid/v4');
class VersionedModel extends Model {
static get schema() {
return {
version_archive: [Object],
version_num: { type: Number, default: 1 },
version_create_date: { type: Date, default: () => new Date },
}
}
async version_save(message = undefined) {
await this.new_version(message)
return this.save()
}
async new_version(message = undefined) {
const version_data = await this.cast_to_version_data()
version_data.version_UUID = uuid()
version_data.version_message = message
if ( !this.version_num ) this.version_num = 1
if ( !this.version_archive ) this.version_archive = []
this.version_num += 1
this.version_archive.push(version_data)
this.version_create_date = new Date()
}
async cast_to_version_data() {
const schema = this.constructor.__schema
const shallow_object = {}
for ( const prop in schema.schema ) {
if ( prop in this ) {
shallow_object[prop] = this[prop]
}
}
const data = await this.__scope_limit_save(schema.cast_to_schema(shallow_object))
delete data.version_archive
return data
}
}
module.exports = exports = VersionedModel

View File

@@ -1,6 +1,6 @@
const Model = require("flitter-orm/src/model/Model");
const { ObjectId } = require("mongodb");
const uuid = require('uuid/v4');
const VersionedModel = require('../VersionedModel')
const { ObjectId } = require('mongodb')
const uuid = require('uuid/v4')
const ActiveScope = require('../scopes/Active.scope')
@@ -9,13 +9,14 @@ const ActiveScope = require('../scopes/Active.scope')
* -------------------------------------------------------------
* Put some description here!
*/
class Page extends Model {
class Page extends VersionedModel {
static get services() {
return [...super.services, "models"];
return [...super.services, 'models'];
}
static get schema() {
// Return a flitter-orm schema here.
return {
...super.schema,
UUID: {type: String, default: () => uuid()},
Name: String,
OrgUserId: ObjectId,
@@ -201,7 +202,8 @@ class Page extends Model {
// Add the user to the appropriate access list
this[`shared_users_${level}`].push(user._id)
await this.save()
// TODO replace user.uid with name of user when we support that
await this.version_save(`Shared with ${user.uid} (${level} access)`)
await user.save()
}
@@ -217,7 +219,7 @@ class Page extends Model {
this.shared_users_update = this.shared_users_update.filter(x => String(x) !== user.id)
this.shared_users_manage = this.shared_users_manage.filter(x => String(x) !== user.id)
await this.save()
await this.version_save(`Unshared with ${user.uid}`)
await user.save()
}