2022-01-24 14:10:35 +08:00
|
|
|
import { Database } from '@nocobase/database';
|
|
|
|
|
|
|
|
type UsingConfigType = 'strategy' | 'resourceAction';
|
|
|
|
|
2022-05-21 15:36:46 +08:00
|
|
|
function totalPage(total, pageSize): number {
|
|
|
|
return Math.ceil(total / pageSize);
|
|
|
|
}
|
|
|
|
|
2022-01-24 14:10:35 +08:00
|
|
|
const roleCollectionsResource = {
|
|
|
|
name: 'roles.collections',
|
|
|
|
actions: {
|
|
|
|
async list(ctx, next) {
|
|
|
|
const role = ctx.action.params.associatedIndex;
|
2022-05-21 15:36:46 +08:00
|
|
|
const { page = 1, pageSize = 20 } = ctx.action.params;
|
2022-01-24 14:10:35 +08:00
|
|
|
|
|
|
|
const db: Database = ctx.db;
|
|
|
|
const collectionRepository = db.getRepository('collections');
|
2023-01-09 07:35:48 +08:00
|
|
|
const fieldRepository = db.getRepository('fields');
|
2022-02-20 01:23:04 +08:00
|
|
|
|
|
|
|
// all collections
|
2022-05-21 15:36:46 +08:00
|
|
|
const [collections, count] = await collectionRepository.findAndCount({
|
2022-03-02 18:35:49 +08:00
|
|
|
filter: ctx.action.params.filter,
|
2022-05-21 15:36:46 +08:00
|
|
|
sort: 'sort',
|
2022-03-02 18:35:49 +08:00
|
|
|
});
|
2022-01-24 14:10:35 +08:00
|
|
|
|
2022-02-20 01:23:04 +08:00
|
|
|
// role collections
|
2022-01-24 14:10:35 +08:00
|
|
|
const roleResources = await db.getRepository('rolesResources').find({
|
|
|
|
filter: {
|
|
|
|
roleName: role,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-02-20 01:23:04 +08:00
|
|
|
// role collections
|
2022-01-24 14:10:35 +08:00
|
|
|
const roleResourcesNames = roleResources.map((roleResource) => roleResource.get('name'));
|
2022-02-20 01:23:04 +08:00
|
|
|
const roleResourceActionResourceNames = roleResources
|
|
|
|
.filter((roleResources) => roleResources.get('usingActionsConfig'))
|
|
|
|
.map((roleResources) => roleResources.get('name'));
|
2022-01-24 14:10:35 +08:00
|
|
|
|
2023-01-09 07:35:48 +08:00
|
|
|
const items = collections.map((collection, i) => {
|
|
|
|
const exists = roleResourcesNames.includes(collection.get('name'));
|
|
|
|
|
|
|
|
const usingConfig: UsingConfigType = roleResourceActionResourceNames.includes(collection.get('name'))
|
|
|
|
? 'resourceAction'
|
|
|
|
: 'strategy';
|
2022-02-20 01:23:04 +08:00
|
|
|
|
2023-01-09 07:35:48 +08:00
|
|
|
const c = db.getCollection(collection.get('name'));
|
2022-02-15 22:32:02 +08:00
|
|
|
|
2023-01-09 07:35:48 +08:00
|
|
|
// const children = [...c.fields.values()]
|
|
|
|
// .filter(
|
|
|
|
// (f) => f.options.interface && ['hasOne', 'hasMany', 'belongsTo', 'belongsToMany'].includes(f.options.type),
|
|
|
|
// )
|
|
|
|
// .map((f, j) => {
|
|
|
|
// const name = `${collection.get('name')}.${f.options.name}`;
|
|
|
|
// const usingConfig: UsingConfigType = roleResourceActionResourceNames.includes(name)
|
|
|
|
// ? 'resourceAction'
|
|
|
|
// : 'strategy';
|
|
|
|
// const exists = roleResourcesNames.includes(name);
|
|
|
|
// return {
|
|
|
|
// type: 'association',
|
|
|
|
// __index: `${i}.children.${j}`,
|
|
|
|
// name,
|
|
|
|
// collectionName: f.options.target,
|
|
|
|
// title: f.options?.uiSchema?.title,
|
|
|
|
// roleName: role,
|
|
|
|
// usingConfig,
|
|
|
|
// exists,
|
|
|
|
// };
|
|
|
|
// });
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: 'collection',
|
|
|
|
name: collection.get('name') as string,
|
|
|
|
collectionName: collection.get('name'),
|
|
|
|
title: collection.get('title') as string,
|
|
|
|
roleName: role,
|
|
|
|
usingConfig,
|
|
|
|
exists,
|
|
|
|
// children: children.length > 0 ? children : null,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
count,
|
|
|
|
rows: items,
|
2022-05-21 15:36:46 +08:00
|
|
|
page: Number(page),
|
|
|
|
pageSize: Number(pageSize),
|
|
|
|
totalPage: totalPage(count, pageSize),
|
|
|
|
};
|
2022-02-11 23:59:03 +08:00
|
|
|
|
|
|
|
await next();
|
2022-01-24 14:10:35 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export { roleCollectionsResource };
|