2020-11-11 20:57:18 +08:00
|
|
|
import { Model, ModelCtor } from '@nocobase/database';
|
|
|
|
import { ResourceOptions } from '@nocobase/resourcer';
|
|
|
|
import { get } from 'lodash';
|
|
|
|
|
|
|
|
export default async (ctx, next) => {
|
|
|
|
const { resourceName, resourceKey } = ctx.action.params;
|
2020-11-13 22:01:14 +08:00
|
|
|
const [Collection, Field, Tab, View] = ctx.db.getModels(['collections', 'fields', 'tabs', 'views']) as ModelCtor<Model>[];
|
2020-11-11 20:57:18 +08:00
|
|
|
const collection = await Collection.findOne(Collection.parseApiJson({
|
|
|
|
filter: {
|
|
|
|
name: resourceName,
|
|
|
|
},
|
|
|
|
// fields: {
|
|
|
|
// // appends: ['tabs'],
|
|
|
|
// },
|
|
|
|
}));
|
|
|
|
const views = await collection.getViews({
|
|
|
|
where: {
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
collection.setDataValue('defaultViewId', get(views, [0, 'id']));
|
|
|
|
collection.setDataValue('defaultViewName', get(views, [0, 'name']));
|
2020-11-19 21:12:15 +08:00
|
|
|
const tabs = await collection.getTabs({
|
|
|
|
order: [['sort', 'asc']],
|
|
|
|
}) as Model[];
|
2020-11-13 22:01:14 +08:00
|
|
|
const tabItems = [];
|
|
|
|
for (const tab of tabs) {
|
|
|
|
const itemTab = {
|
2020-11-11 20:57:18 +08:00
|
|
|
...tab.toJSON(),
|
|
|
|
...tab.options,
|
2020-11-13 22:01:14 +08:00
|
|
|
};
|
|
|
|
if (itemTab.type == 'association') {
|
|
|
|
const field = await Field.findOne({
|
|
|
|
where: {
|
|
|
|
collection_name: itemTab.collection_name,
|
|
|
|
name: itemTab.association,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
itemTab.field = field ? {
|
|
|
|
...field.toJSON(),
|
|
|
|
...field.options,
|
|
|
|
} : {};
|
|
|
|
itemTab.viewCollectionName = itemTab.association;
|
|
|
|
} else {
|
|
|
|
itemTab.viewCollectionName = itemTab.collection_name;
|
|
|
|
}
|
|
|
|
tabItems.push(itemTab);
|
|
|
|
}
|
|
|
|
ctx.body = {
|
|
|
|
...collection.toJSON(),
|
|
|
|
tabs: tabItems,
|
2020-11-11 20:57:18 +08:00
|
|
|
};
|
|
|
|
await next();
|
|
|
|
}
|