2020-11-11 07:23:39 +00:00
|
|
|
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';
|
2020-11-13 14:01:14 +00:00
|
|
|
import getRoutes from './actions/getRoutes';
|
|
|
|
import getPageInfo from './actions/getPageInfo';
|
2021-01-20 00:33:47 +00:00
|
|
|
import * as rolesPagesActions from './actions/roles.pages';
|
2021-01-30 11:06:01 +00:00
|
|
|
import getCollections from './actions/getCollections';
|
2021-03-16 06:31:54 +00:00
|
|
|
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';
|
2020-11-11 07:23:39 +00:00
|
|
|
|
|
|
|
export default async function (options = {}) {
|
|
|
|
const database: Database = this.database;
|
|
|
|
const resourcer: Resourcer = this.resourcer;
|
|
|
|
|
2021-03-16 06:31:54 +00:00
|
|
|
registerFields({
|
|
|
|
RANDOMSTRING,
|
|
|
|
});
|
|
|
|
|
|
|
|
registerModels({
|
|
|
|
BaseModelV2: BaseModel,
|
|
|
|
});
|
|
|
|
|
2020-11-11 12:57:18 +00:00
|
|
|
database.import({
|
2020-11-11 07:23:39 +00:00
|
|
|
directory: path.resolve(__dirname, 'collections'),
|
|
|
|
});
|
|
|
|
|
2021-03-16 06:31:54 +00:00
|
|
|
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)) {
|
|
|
|
if (values.parent) {
|
|
|
|
delete values.parent.children;
|
|
|
|
ctx.action.mergeParams({
|
|
|
|
values: {...values},
|
|
|
|
}, {
|
|
|
|
payload: 'replace',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await next();
|
|
|
|
});
|
|
|
|
|
2021-03-28 00:56:17 +00:00
|
|
|
resourcer.use(async (ctx, next) => {
|
|
|
|
await next();
|
|
|
|
const { actionName, resourceName } = ctx.action.params;
|
|
|
|
if (resourceName === 'menus' && actionName === 'get') {
|
|
|
|
const menu = ctx.body;
|
|
|
|
const items = menu.get('views') || [];
|
|
|
|
const View = database.getModel('views_v2');
|
|
|
|
for (const item of items) {
|
|
|
|
if (!(item.view && item.view.id)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const view = await View.findOne(View.parseApiJson({
|
|
|
|
filter: {
|
|
|
|
id: item.view.id,
|
|
|
|
},
|
|
|
|
fields: {
|
|
|
|
appends: ['collection', 'targetField', 'targetView'],
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
if (!view) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const details = view.get(`options.x-${view.type}-props.details`);
|
|
|
|
if (!Array.isArray(details)) {
|
|
|
|
item.view = view;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (const detail of details) {
|
|
|
|
if (!(detail.view && detail.view.id)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const detailView = await View.findOne(View.parseApiJson({
|
|
|
|
filter: {
|
|
|
|
id: detail.view.id,
|
|
|
|
},
|
|
|
|
fields: {
|
|
|
|
appends: ['collection', 'targetField', 'targetView'],
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
if (!detailView) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
detail.view = detailView;
|
|
|
|
}
|
|
|
|
view.set(`options.x-${view.type}-props.details`, details);
|
|
|
|
item.view = view;
|
|
|
|
}
|
|
|
|
menu.set('views', items);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-12-02 05:48:19 +00:00
|
|
|
resourcer.registerActionHandler('getCollection', getCollection);
|
|
|
|
resourcer.registerActionHandler('getView', getView);
|
|
|
|
resourcer.registerActionHandler('getPageInfo', getPageInfo);
|
2021-01-30 11:06:01 +00:00
|
|
|
resourcer.registerActionHandler('getCollections', getCollections);
|
2020-12-02 05:48:19 +00:00
|
|
|
resourcer.registerActionHandler('pages:getRoutes', getRoutes);
|
2021-03-16 06:31:54 +00:00
|
|
|
resourcer.registerActionHandler('menus:getTree', getTree);
|
|
|
|
resourcer.registerActionHandler('menus:getInfo', getInfo);
|
|
|
|
resourcer.registerActionHandler('views_v2:getInfo', viewGetInfo);
|
|
|
|
|
|
|
|
resourcer.registerActionHandler('menus:list', menusList);
|
2021-01-20 00:33:47 +00:00
|
|
|
|
|
|
|
Object.keys(rolesPagesActions).forEach(actionName => {
|
|
|
|
resourcer.registerActionHandler(`roles.pages:${actionName}`, rolesPagesActions[actionName]);
|
|
|
|
});
|
2021-03-16 06:31:54 +00:00
|
|
|
|
|
|
|
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);
|
2020-12-15 14:44:20 +00:00
|
|
|
|
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;
|
2020-12-15 14:44:20 +00:00
|
|
|
}
|
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);
|
2020-12-15 14:44:20 +00:00
|
|
|
}
|
2021-03-24 02:21:38 +00:00
|
|
|
model.set('views', views);
|
|
|
|
// @ts-ignore
|
|
|
|
model.changed('views', true);
|
2020-12-15 14:44:20 +00:00
|
|
|
});
|
2020-11-11 07:23:39 +00:00
|
|
|
}
|