nocobase/packages/plugin-pages/src/server.ts

207 lines
6.5 KiB
TypeScript
Raw Normal View History

import path from 'path';
import Database from '@nocobase/database';
import Resourcer from '@nocobase/resourcer';
2020-11-11 12:57:18 +00:00
import getCollection from './actions/getCollection';
import getView from './actions/getView';
import getRoutes from './actions/getRoutes';
import getPageInfo from './actions/getPageInfo';
import * as rolesPagesActions from './actions/roles.pages';
2021-01-30 11:06:01 +00:00
import getCollections from './actions/getCollections';
import menusList from './actions/menus:list';
import getTree from './actions/getTree';
import getInfo from './actions/getInfo';
import viewGetInfo from './actions/views_v2:getInfo';
import { RANDOMSTRING } from './fields/randomString';
import { registerFields, registerModels } from '@nocobase/database';
import { BaseModel } from './models/BaseModel'
import * as rolesMenusActions from './actions/roles.menus';
2021-03-24 02:21:38 +00:00
import _ from 'lodash';
export default async function (options = {}) {
const database: Database = this.database;
const resourcer: Resourcer = this.resourcer;
registerFields({
RANDOMSTRING,
});
registerModels({
BaseModelV2: BaseModel,
});
2020-11-11 12:57:18 +00:00
database.import({
directory: path.resolve(__dirname, 'collections'),
});
resourcer.use(async (ctx, next) => {
const { actionName, resourceName, resourceKey } = ctx.action.params;
if (resourceName === 'system_settings' && actionName === 'get') {
const SystemSetting = database.getModel('system_settings');
let model = await SystemSetting.findOne();
if (!model) {
model = await SystemSetting.create();
}
ctx.action.mergeParams({
resourceKey: model.id,
});
}
await next();
});
resourcer.use(async (ctx, next) => {
const { actionName, resourceName, values } = ctx.action.params;
if (resourceName === 'menus' && ['create', 'update'].includes(actionName)) {
2021-03-24 02:21:38 +00:00
console.log({values});
if (values.parent) {
delete values.parent.children;
ctx.action.mergeParams({
values: {...values},
}, {
payload: 'replace',
});
}
}
await next();
});
resourcer.registerActionHandler('getCollection', getCollection);
resourcer.registerActionHandler('getView', getView);
resourcer.registerActionHandler('getPageInfo', getPageInfo);
2021-01-30 11:06:01 +00:00
resourcer.registerActionHandler('getCollections', getCollections);
resourcer.registerActionHandler('pages:getRoutes', getRoutes);
resourcer.registerActionHandler('menus:getTree', getTree);
resourcer.registerActionHandler('menus:getInfo', getInfo);
resourcer.registerActionHandler('views_v2:getInfo', viewGetInfo);
resourcer.registerActionHandler('menus:list', menusList);
Object.keys(rolesPagesActions).forEach(actionName => {
resourcer.registerActionHandler(`roles.pages:${actionName}`, rolesPagesActions[actionName]);
});
Object.keys(rolesMenusActions).forEach(actionName => {
resourcer.registerActionHandler(`roles.menus:${actionName}`, rolesMenusActions[actionName]);
});
2021-03-24 02:21:38 +00:00
const createDetailsViews = async (model, options) => {
const data = model.get();
const View = database.getModel('views_v2');
2021-03-25 08:11:45 +00:00
const types = ['table', 'calendar', 'kanban'];
for (const type of types) {
const items = _.get(data, `x-${type}-props.details`) || [];
if (items.length) {
const details = [];
for (const item of items) {
if (item.view) {
if (!item.view.id) {
const view = await View.create(item.view);
await view.updateAssociations(item.view);
item.view.id = view.id;
} else {
const view = await View.findByPk(item.view.id);
if (view) {
await view.update(item.view);
await view.updateAssociations(item.view);
}
}
const view = await View.findOne(View.parseApiJson({
filter: {
id: item.view.id,
},
fields: {
appends: ['collection', 'targetField', 'targetView'],
},
}));
if (view) {
console.log({view});
item.view = view.toJSON();
}
2021-03-24 02:21:38 +00:00
}
2021-03-25 08:11:45 +00:00
details.push(item);
2021-03-24 02:21:38 +00:00
}
2021-03-25 08:11:45 +00:00
model.set(`options.x-${type}-props.details`, details);
2021-03-24 02:21:38 +00:00
}
2020-12-07 09:25:36 +00:00
}
2021-03-24 02:21:38 +00:00
};
2020-12-07 09:25:36 +00:00
2021-03-24 02:21:38 +00:00
database.getModel('views_v2').addHook('beforeCreate', createDetailsViews);
database.getModel('views_v2').addHook('beforeUpdate', createDetailsViews);
2021-03-25 08:11:45 +00:00
database.getModel('views_v2').addHook('beforeSave', async (model, options) => {
const data = model.get();
if (data.type !== 'kanban') {
return;
}
let groupField = _.get(data, `x-kanban-props.groupField`);
if (!groupField) {
return;
}
if (typeof groupField === 'object' && groupField.name) {
groupField = groupField.name;
}
const Field = database.getModel('fields');
let field = await Field.findOne({
where: {
name: `${groupField}_sort`,
collection_name: data.collection_name,
},
});
if (field) {
return;
}
await Field.create({
interface: 'sort',
type: 'sort',
name: `${groupField}_sort`,
// TODO: 不支持相关数据
collection_name: data.collection_name,
scope: [groupField],
title: '看板分组排序',
developerMode: true,
component: {
type: 'sort',
},
});
});
2021-03-24 02:21:38 +00:00
database.getModel('menus').addHook('beforeSave', async (model, options) => {
const { transaction } = options;
// console.log('beforeSave', model.get('views'));
const items = model.get('views');
if (!Array.isArray(items)) {
return;
}
2021-03-24 02:21:38 +00:00
const View = database.getModel('views_v2');
const views = [];
for (const item of items) {
if (item.view) {
if (!item.view.id) {
const view = await View.create(item.view);
await view.updateAssociations(item.view);
item.view.id = view.id;
} else {
const view = await View.findByPk(item.view.id);
await view.update(item.view);
await view.updateAssociations(item.view);
}
const view = await View.findOne(View.parseApiJson({
filter: {
id: item.view.id,
},
fields: {
appends: ['collection', 'targetField', 'targetView'],
},
}));
if (view) {
console.log({view});
item.view = view.toJSON();
}
}
views.push(item);
}
2021-03-24 02:21:38 +00:00
model.set('views', views);
// @ts-ignore
model.changed('views', true);
});
}