Skip to content

Commit

Permalink
refactor: org pathId (#3516)
Browse files Browse the repository at this point in the history
  • Loading branch information
I-Info authored Jan 3, 2025
1 parent 1ea1438 commit f163362
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 14 deletions.
4 changes: 4 additions & 0 deletions packages/global/support/user/team/org/constant.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { OrgSchemaType } from './type';

export const OrgCollectionName = 'team_orgs';
export const OrgMemberCollectionName = 'team_org_members';

export const getChildrenPath = (org: OrgSchemaType) => `${org.path}/${org.pathId}`;

// export enum OrgMemberRole {
// owner = 'owner',
// admin = 'admin',
Expand Down
1 change: 1 addition & 0 deletions packages/global/support/user/team/org/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ResourcePermissionType } from '../type';
type OrgSchemaType = {
_id: string;
teamId: string;
pathId: string;
path: string;
name: string;
avatar?: string;
Expand Down
3 changes: 2 additions & 1 deletion packages/service/support/permission/org/controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { OrgSchemaType } from '@fastgpt/global/support/user/team/org/type';
import type { ClientSession } from 'mongoose';
import { MongoOrgModel } from './orgSchema';
import { MongoOrgMemberModel } from './orgMemberSchema';
import { getChildrenPath } from '@fastgpt/global/support/user/team/org/constant';

export const getOrgsByTmbId = async ({ teamId, tmbId }: { teamId: string; tmbId: string }) =>
MongoOrgMemberModel.find({ teamId, tmbId }, 'orgId').lean();
Expand Down Expand Up @@ -42,7 +43,7 @@ export const getChildrenByOrg = async ({
teamId: string;
session?: ClientSession;
}) => {
return MongoOrgModel.find({ teamId, path: { $regex: `^${org.path}/${org._id}` } }, undefined, {
return MongoOrgModel.find({ teamId, path: { $regex: `^${getChildrenPath(org)}` } }, undefined, {
session
}).lean();
};
Expand Down
17 changes: 16 additions & 1 deletion packages/service/support/permission/org/orgSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { TeamCollectionName } from '@fastgpt/global/support/user/team/constant';
import { OrgCollectionName } from '@fastgpt/global/support/user/team/org/constant';
import type { OrgSchemaType } from '@fastgpt/global/support/user/team/org/type';
import { connectionMongo, getMongoModel } from '../../../common/mongo';
import { ResourcePermissionCollectionName } from '../schema';
import { OrgMemberCollectionName } from './orgMemberSchema';
import { getNanoid } from '@fastgpt/global/common/string/tools';
const { Schema } = connectionMongo;

function requiredStringPath(this: OrgSchemaType) {
Expand All @@ -17,6 +17,12 @@ export const OrgSchema = new Schema(
ref: TeamCollectionName,
required: true
},
pathId: {
// path id, only used for path
type: String,
required: true,
default: () => getNanoid()
},
path: {
type: String,
required: requiredStringPath // allow empty string, but not null
Expand Down Expand Up @@ -57,6 +63,15 @@ try {
teamId: 1,
path: 1
});
OrgSchema.index(
{
teamId: 1,
pathId: 1
},
{
unique: true
}
);
} catch (error) {
console.log(error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Avatar from '@fastgpt/web/components/common/Avatar';
import { useToggle } from 'ahooks';
import { useMemo } from 'react';
import IconButton from './IconButton';
import { getChildrenPath } from '@fastgpt/global/support/user/team/org/constant';

function OrgTreeNode({
org,
Expand All @@ -19,7 +20,7 @@ function OrgTreeNode({
index?: number;
}) {
const children = useMemo(
() => list.filter((item) => item.path === `${org.path}/${org._id}`),
() => list.filter((item) => item.path === getChildrenPath(org)),
[org, list]
);
const [isExpanded, toggleIsExpanded] = useToggle(index === 0);
Expand Down
19 changes: 8 additions & 11 deletions projects/app/src/pages/account/team/components/OrgManage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ import { TeamContext } from '../context';
import { getOrgList } from '@/web/support/user/team/org/api';

import IconButton from './IconButton';
import type { defaultOrgForm, OrgFormType } from './OrgInfoModal';
import { defaultOrgForm, type OrgFormType } from './OrgInfoModal';

import dynamic from 'next/dynamic';
import MyBox from '@fastgpt/web/components/common/MyBox';
import Path from '@/components/common/folder/Path';
import { ParentTreePathItemType } from '@fastgpt/global/common/parentFolder/type';
import { getChildrenPath } from '@fastgpt/global/support/user/team/org/constant';

const OrgInfoModal = dynamic(() => import('./OrgInfoModal'));
const OrgMemberManageModal = dynamic(() => import('./OrgMemberManageModal'));
Expand Down Expand Up @@ -88,7 +89,7 @@ function OrgTable() {
const currentOrgs = useMemo(() => {
if (orgs.length === 0) return [];
if (parentPath === '') {
setParentPath(`/${orgs[0]._id}`);
setParentPath(`/${orgs[0].pathId}`);
return [];
}
return orgs
Expand All @@ -97,8 +98,7 @@ function OrgTable() {
return {
...item,
count:
item.members.length +
orgs.filter((org) => org.path === `${item.path}/${item._id}`).length
item.members.length + orgs.filter((org) => org.path === getChildrenPath(item)).length
};
});
}, [orgs, parentPath]);
Expand All @@ -107,18 +107,18 @@ function OrgTable() {
const currentOrgId = splitPath[splitPath.length - 1];
if (!currentOrgId) return;

return orgs.find((org) => org._id === currentOrgId);
return orgs.find((org) => org.pathId === currentOrgId);
}, [orgs, parentPath]);
const paths = useMemo(() => {
const splitPath = parentPath.split('/').filter(Boolean);
return splitPath
.map((id) => {
const org = orgs.find((org) => org._id === id)!;
const org = orgs.find((org) => org.pathId === id)!;

if (org.path === '') return;

return {
parentId: `${org.path}/${org._id}`,
parentId: getChildrenPath(org),
parentName: org.name
};
})
Expand Down Expand Up @@ -175,10 +175,7 @@ function OrgTable() {
{currentOrgs.map((org) => (
<Tr key={org._id} overflow={'unset'}>
<Td>
<HStack
cursor={'pointer'}
onClick={() => setParentPath(`${org.path}/${org._id}`)}
>
<HStack cursor={'pointer'} onClick={() => setParentPath(getChildrenPath(org))}>
<MemberTag name={org.name} avatar={org.avatar} />
<Tag size="sm">{org.count}</Tag>
<MyIcon
Expand Down

0 comments on commit f163362

Please sign in to comment.