2022-01-19 10:09:30 +08:00
|
|
|
import { Context } from '@nocobase/actions';
|
2022-03-21 15:23:59 +08:00
|
|
|
import { ActionParams } from '@nocobase/resourcer';
|
2022-03-16 18:45:20 +08:00
|
|
|
import lodash from 'lodash';
|
2022-03-20 20:04:07 +08:00
|
|
|
import UiSchemaRepository from '../repository';
|
2022-01-19 10:09:30 +08:00
|
|
|
|
|
|
|
const getRepositoryFromCtx = (ctx: Context) => {
|
2022-02-16 00:22:47 +08:00
|
|
|
return ctx.db.getCollection('uiSchemas').repository as UiSchemaRepository;
|
2022-01-19 10:09:30 +08:00
|
|
|
};
|
|
|
|
|
2022-03-21 14:51:23 +08:00
|
|
|
const callRepositoryMethod = (method, paramsKey: 'resourceIndex' | 'values', optionsBuilder?) => {
|
2022-03-16 18:45:20 +08:00
|
|
|
return async (ctx, next) => {
|
|
|
|
const params = lodash.get(ctx.action.params, paramsKey);
|
2022-03-21 14:51:23 +08:00
|
|
|
const options = optionsBuilder ? optionsBuilder(ctx.action.params) : {};
|
2022-01-19 10:09:30 +08:00
|
|
|
|
|
|
|
const repository = getRepositoryFromCtx(ctx);
|
2022-03-21 14:51:23 +08:00
|
|
|
const returnValue = await repository[method](params, options);
|
2022-01-19 10:09:30 +08:00
|
|
|
|
2022-03-16 18:45:20 +08:00
|
|
|
ctx.body = returnValue || {
|
2022-01-19 10:09:30 +08:00
|
|
|
result: 'ok',
|
|
|
|
};
|
|
|
|
|
|
|
|
await next();
|
2022-03-16 18:45:20 +08:00
|
|
|
};
|
|
|
|
};
|
2022-01-19 10:09:30 +08:00
|
|
|
|
2022-03-20 20:04:07 +08:00
|
|
|
function parseInsertAdjacentValues(values) {
|
|
|
|
if (lodash.has(values, 'schema')) {
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { schema: values, wrap: null };
|
|
|
|
}
|
|
|
|
|
2022-03-16 18:45:20 +08:00
|
|
|
export const uiSchemaActions = {
|
2022-03-21 14:51:23 +08:00
|
|
|
getJsonSchema: callRepositoryMethod('getJsonSchema', 'resourceIndex', (params: ActionParams) => {
|
|
|
|
return {
|
|
|
|
includeAsyncNode: params?.includeAsyncNode,
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
|
2022-03-16 18:45:20 +08:00
|
|
|
getProperties: callRepositoryMethod('getProperties', 'resourceIndex'),
|
|
|
|
insert: callRepositoryMethod('insert', 'values'),
|
2022-03-20 20:04:07 +08:00
|
|
|
insertNewSchema: callRepositoryMethod('insertNewSchema', 'values'),
|
2022-03-16 18:45:20 +08:00
|
|
|
remove: callRepositoryMethod('remove', 'resourceIndex'),
|
|
|
|
patch: callRepositoryMethod('patch', 'values'),
|
2022-08-20 18:04:14 +08:00
|
|
|
batchPatch: callRepositoryMethod('batchPatch', 'values'),
|
2022-03-16 18:45:20 +08:00
|
|
|
clearAncestor: callRepositoryMethod('clearAncestor', 'resourceIndex'),
|
2022-01-19 10:09:30 +08:00
|
|
|
|
|
|
|
async insertAdjacent(ctx: Context, next) {
|
2022-02-18 12:29:03 +08:00
|
|
|
const { resourceIndex, position, values, removeParentsIfNoChildren, breakRemoveOn } = ctx.action.params;
|
2022-01-19 10:09:30 +08:00
|
|
|
const repository = getRepositoryFromCtx(ctx);
|
|
|
|
|
2022-03-20 20:04:07 +08:00
|
|
|
const { schema, wrap } = parseInsertAdjacentValues(values);
|
|
|
|
|
|
|
|
ctx.body = await repository.insertAdjacent(position, resourceIndex, schema, {
|
2022-02-18 12:29:03 +08:00
|
|
|
removeParentsIfNoChildren,
|
|
|
|
breakRemoveOn,
|
2022-03-20 20:04:07 +08:00
|
|
|
wrap,
|
2022-02-18 12:29:03 +08:00
|
|
|
});
|
2022-01-19 10:09:30 +08:00
|
|
|
|
|
|
|
await next();
|
|
|
|
},
|
2022-03-16 18:45:20 +08:00
|
|
|
|
2022-01-19 10:09:30 +08:00
|
|
|
insertBeforeBegin: insertPositionActionBuilder('beforeBegin'),
|
|
|
|
insertAfterBegin: insertPositionActionBuilder('afterBegin'),
|
|
|
|
insertBeforeEnd: insertPositionActionBuilder('beforeEnd'),
|
|
|
|
insertAfterEnd: insertPositionActionBuilder('afterEnd'),
|
2022-03-21 15:23:59 +08:00
|
|
|
|
|
|
|
async saveAsTemplate(ctx: Context, next) {
|
|
|
|
const { filterByTk, values } = ctx.action.params;
|
|
|
|
const db = ctx.db;
|
|
|
|
const transaction = await db.sequelize.transaction();
|
|
|
|
try {
|
|
|
|
await db.getRepository('uiSchemaTemplates').create({
|
|
|
|
values: {
|
|
|
|
...values,
|
|
|
|
uid: filterByTk,
|
|
|
|
},
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
await db.getRepository<UiSchemaRepository>('uiSchemas').clearAncestor(filterByTk, { transaction });
|
|
|
|
ctx.body = {
|
|
|
|
result: 'ok',
|
|
|
|
};
|
|
|
|
await transaction.commit();
|
|
|
|
} catch (error) {
|
|
|
|
await transaction.rollback();
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
await next();
|
|
|
|
},
|
2022-01-19 10:09:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
function insertPositionActionBuilder(position: 'beforeBegin' | 'afterBegin' | 'beforeEnd' | 'afterEnd') {
|
|
|
|
return async function (ctx: Context, next) {
|
2022-02-18 12:29:03 +08:00
|
|
|
const { resourceIndex, values, removeParentsIfNoChildren, breakRemoveOn } = ctx.action.params;
|
2022-01-19 10:09:30 +08:00
|
|
|
const repository = getRepositoryFromCtx(ctx);
|
2022-03-20 20:04:07 +08:00
|
|
|
const { schema, wrap } = parseInsertAdjacentValues(values);
|
|
|
|
|
|
|
|
ctx.body = await repository.insertAdjacent(position, resourceIndex, schema, {
|
2022-02-18 12:29:03 +08:00
|
|
|
removeParentsIfNoChildren,
|
|
|
|
breakRemoveOn,
|
2022-03-20 20:04:07 +08:00
|
|
|
wrap,
|
2022-02-18 12:29:03 +08:00
|
|
|
});
|
2022-01-19 10:09:30 +08:00
|
|
|
await next();
|
|
|
|
};
|
|
|
|
}
|