2020-02-08 09:54:48 +00:00
|
|
|
const Model = require("flitter-orm/src/model/Model");
|
|
|
|
const { ObjectId } = require("mongodb");
|
2020-02-08 12:57:38 +00:00
|
|
|
const uuid = require('uuid/v4');
|
2020-02-08 09:54:48 +00:00
|
|
|
|
2020-02-08 21:27:53 +00:00
|
|
|
const ActiveScope = require('../scopes/Active.scope')
|
|
|
|
|
2020-02-08 09:54:48 +00:00
|
|
|
/*
|
|
|
|
* 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 {
|
2020-02-08 12:57:38 +00:00
|
|
|
UUID: {type: String, default: () => uuid()},
|
2020-02-08 09:54:48 +00:00
|
|
|
Name: String,
|
2020-02-08 14:07:26 +00:00
|
|
|
OrgUserId: ObjectId,
|
2020-02-08 12:57:38 +00:00
|
|
|
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},
|
2020-02-08 21:27:53 +00:00
|
|
|
DeletedAt: Date,
|
|
|
|
Active: {type: Boolean, default: true},
|
2020-02-08 12:57:38 +00:00
|
|
|
CreatedUserId: {type: String},
|
|
|
|
UpdateUserId: {type: String},
|
|
|
|
ChildPageIds: [String],
|
2020-02-08 09:54:48 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-08 21:27:53 +00:00
|
|
|
static scopes = [new ActiveScope]
|
|
|
|
|
2020-02-08 09:54:48 +00:00
|
|
|
// 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");
|
2020-02-08 14:07:26 +00:00
|
|
|
return this.has_many(Node, "NodeIds", "UUID");
|
2020-02-08 09:54:48 +00:00
|
|
|
}
|
|
|
|
get childPages() {
|
|
|
|
const Page = this.models.get("api:Page")
|
2020-02-08 14:07:26 +00:00
|
|
|
return this.has_many(Page, "ChildPageIds", "UUID")
|
2020-02-08 09:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get parent() {
|
|
|
|
const Parent = this.models.get("api:Page")
|
2020-02-08 14:07:26 +00:00
|
|
|
return this.belongs_to_one(Parent, "ParentId", "UUID")
|
2020-02-08 09:54:48 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 12:57:38 +00:00
|
|
|
accessible_by(user, mode = 'view') {
|
|
|
|
return user.can(`page:${this.UUID}:${mode}`)
|
|
|
|
}
|
|
|
|
|
2020-02-08 09:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = Page;
|