65 lines
1.6 KiB
JavaScript
65 lines
1.6 KiB
JavaScript
const Model = require("flitter-orm/src/model/Model");
|
|
const { ObjectId } = require("mongodb");
|
|
const uuid = require('uuid/v4');
|
|
|
|
const ActiveScope = require('../scopes/Active.scope')
|
|
|
|
/*
|
|
* Page Model
|
|
* -------------------------------------------------------------
|
|
* Put some description here!
|
|
*/
|
|
class Page extends Model {
|
|
static get services() {
|
|
return [...super.services, "models"];
|
|
}
|
|
static get schema() {
|
|
// Return a flitter-orm schema here.
|
|
return {
|
|
UUID: {type: String, default: () => uuid()},
|
|
Name: String,
|
|
OrgUserId: ObjectId,
|
|
IsPublic: {type: Boolean, default: true},
|
|
IsVisibleInMenu: {type: Boolean, default: true},
|
|
ParentId: String,
|
|
NodeIds: [String],
|
|
CreatedAt: {type: Date, default: () => new Date},
|
|
UpdatedAt: {type: Date, default: () => new Date},
|
|
DeletedAt: Date,
|
|
Active: {type: Boolean, default: true},
|
|
CreatedUserId: {type: String},
|
|
UpdateUserId: {type: String},
|
|
ChildPageIds: [String],
|
|
};
|
|
}
|
|
|
|
static scopes = [new ActiveScope]
|
|
|
|
// Static and instance methods can go here
|
|
get user() {
|
|
const User = this.models.get("auth:User");
|
|
return this.belongs_to_one(User, "OrgUserId", "_id");
|
|
}
|
|
|
|
get nodes() {
|
|
const Node = this.models.get("api:Node");
|
|
return this.has_many(Node, "NodeIds", "UUID");
|
|
}
|
|
get childPages() {
|
|
const Page = this.models.get("api:Page")
|
|
return this.has_many(Page, "ChildPageIds", "UUID")
|
|
}
|
|
|
|
get parent() {
|
|
const Parent = this.models.get("api:Page")
|
|
return this.belongs_to_one(Parent, "ParentId", "UUID")
|
|
}
|
|
|
|
accessible_by(user, mode = 'view') {
|
|
return user.can(`page:${this.UUID}:${mode}`)
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = exports = Page;
|