tachybase_todo/packages/plugin-ui-schema/src/actions/index.ts

138 lines
3.6 KiB
TypeScript
Raw Normal View History

2021-07-17 22:35:50 +08:00
import { Model, ModelCtor } from '@nocobase/database';
import { actions, middlewares } from '@nocobase/actions';
import { sort } from '@nocobase/actions/src/actions/common';
import { cloneDeep, omit } from 'lodash';
export const create = async (ctx: actions.Context, next: actions.Next) => {
const values = cloneDeep(ctx.action.params.values);
ctx.action.mergeParams(
{
2021-07-19 23:57:54 +08:00
values: cloneDeep(omit(values, [
2021-07-17 22:35:50 +08:00
'__insertAfter__',
'__insertBefore__',
'__prepend__',
'_isJSONSchemaObject',
2021-07-19 23:57:54 +08:00
])),
2021-07-17 22:35:50 +08:00
},
{
payload: 'replace',
},
);
2021-07-18 17:16:57 +08:00
await actions.common.create(ctx, async () => { });
const sticky = values['__prepend__'];
2021-07-17 22:35:50 +08:00
const targetKey = values['__insertAfter__'] || values['__insertBefore__'];
2021-07-18 17:16:57 +08:00
if (sticky || targetKey) {
2021-07-19 23:57:54 +08:00
const body = ctx.body;
ctx.action.mergeParams(
{
associatedKey: values.parentKey,
resourceKey: body.key,
values: sticky ? {
field: 'sort',
sticky: true,
target: {},
} : {
field: 'sort',
insertAfter: !!values['__insertAfter__'],
target: {
key: targetKey,
},
2021-07-17 22:35:50 +08:00
},
},
2021-07-19 23:57:54 +08:00
{
payload: 'replace',
},
);
2021-07-23 12:34:15 +08:00
// console.log(ctx.action.params.values);
2021-07-19 23:57:54 +08:00
await middlewares.associated(ctx, async () => { });
await sort(ctx, async () => { });
2021-07-23 12:34:15 +08:00
// console.log(ctx.body.toJSON());
2021-07-19 23:57:54 +08:00
}
await next();
};
export const update = async (ctx: actions.Context, next: actions.Next) => {
const values = cloneDeep(ctx.action.params.values);
ctx.action.mergeParams(
{
values: cloneDeep(omit(values, [
'__insertAfter__',
'__insertBefore__',
'__prepend__',
'_isJSONSchemaObject',
])),
},
{
payload: 'replace',
},
);
await actions.common.update(ctx, async () => { });
const sticky = values['__prepend__'];
const targetKey = values['__insertAfter__'] || values['__insertBefore__'];
if (sticky || targetKey) {
const body = ctx.body;
2021-07-17 22:35:50 +08:00
ctx.action.mergeParams(
{
associatedKey: values.parentKey,
2021-07-19 23:57:54 +08:00
resourceKey: body.key,
2021-07-18 17:16:57 +08:00
values: sticky ? {
field: 'sort',
sticky: true,
target: {},
} : {
2021-07-17 22:35:50 +08:00
field: 'sort',
insertAfter: !!values['__insertAfter__'],
target: {
key: targetKey,
},
},
},
{
payload: 'replace',
},
);
2021-07-23 12:34:15 +08:00
// console.log(ctx.action.params.values);
2021-07-18 17:16:57 +08:00
await middlewares.associated(ctx, async () => { });
await sort(ctx, async () => { });
2021-07-23 12:34:15 +08:00
// console.log(ctx.body.toJSON());
2021-07-17 22:35:50 +08:00
}
await next();
};
export const getTree = async (ctx: actions.Context, next: actions.Next) => {
const { resourceKey, filter } = ctx.action.params;
const UISchema = ctx.db.getModel('ui_schemas');
if (resourceKey) {
const schema = await UISchema.findByPk(resourceKey);
const property = schema.toProperty();
const properties = await schema.getProperties();
if (Object.keys(properties).length) {
property.properties = properties;
}
ctx.body = property;
} else {
const schemas = await UISchema.findAll(
UISchema.parseApiJson({
filter,
sort: ['sort'],
}),
);
2021-07-23 12:34:15 +08:00
// console.log({ schemas });
2021-07-17 22:35:50 +08:00
let properties = {};
for (const schema of schemas) {
const property = schema.toProperty();
const childProperties = await schema.getProperties();
if (Object.keys(childProperties).length) {
property.properties = childProperties;
}
properties[property.name] = property;
}
ctx.body = {
type: 'object',
properties,
};
}
await next();
};