Skip to content

Commit

Permalink
fix: mongo connection
Browse files Browse the repository at this point in the history
  • Loading branch information
c121914yu committed Nov 7, 2023
1 parent c542c47 commit 0e4e493
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 18 deletions.
1 change: 1 addition & 0 deletions packages/global/support/user/team/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type TeamItemType = {
role: `${TeamMemberRoleEnum}`;
status: `${TeamMemberStatusEnum}`;
canWrite: boolean;
maxSize: number;
};

export type TeamMemberItemType = {
Expand Down
11 changes: 3 additions & 8 deletions packages/service/common/mongo/init.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import mongoose from './index';
import 'winston-mongodb';
import { createLogger, format, transports } from 'winston';
import 'winston-mongodb';

/**
* connect MongoDB and init data
Expand All @@ -19,9 +19,6 @@ export async function connectMongo({
beforeHook && (await beforeHook());
global.mongodb = mongoose;

// logger
initLogger();

console.log('mongo start connect');
try {
mongoose.set('strictQuery', true);
Expand All @@ -35,9 +32,11 @@ export async function connectMongo({
});

console.log('mongo connected');
initLogger();

afterHook && (await afterHook());
} catch (error) {
global.mongodb.disconnect();
console.log('error->', 'mongo connect error', error);
global.mongodb = undefined;
}
Expand Down Expand Up @@ -73,8 +72,4 @@ function initLogger() {
})
]
});
global.logger.on('error', (err) => {
console.log(err);
global.mongodb = undefined;
});
}
9 changes: 6 additions & 3 deletions packages/service/support/user/team/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,22 @@ export async function getTeamInfoByTmbId({
role: tmb.role,
status: tmb.status,
defaultTeam: tmb.defaultTeam,
canWrite: tmb.role !== TeamMemberRoleEnum.visitor
canWrite: tmb.role !== TeamMemberRoleEnum.visitor,
maxSize: tmb.team.maxSize
};
}
export async function createDefaultTeam({
userId,
teamName = 'My Team',
avatar = '/icon/logo.svg',
balance = 0
balance = 0,
maxSize = 5
}: {
userId: string;
teamName?: string;
avatar?: string;
balance?: number;
maxSize?: number;
}) {
const db = connectionMongo.connection.db;
const Team = db.collection(TeamCollectionName);
Expand All @@ -94,7 +97,7 @@ export async function createDefaultTeam({
name: teamName,
avatar,
balance,
maxSize: 1,
maxSize,
createTime: new Date()
});
await TeamMember.insertOne({
Expand Down
2 changes: 1 addition & 1 deletion projects/app/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const nextConfig = {
},
transpilePackages: ['@fastgpt/*'],
experimental: {
serverComponentsExternalPackages: ['mongoose', 'pg'],
serverComponentsExternalPackages: ['mongoose', 'winston', 'winston-mongodb', 'pg'],
outputFileTracingRoot: path.join(__dirname, '../../')
}
};
Expand Down
1 change: 1 addition & 0 deletions projects/app/public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@
"Leave Team Failed": "Leave Team Failed",
"Manage": "Team Manage",
"Member": "Member",
"Over Max Member Tip": "Team max {{max}} people",
"Personal Team": "Personal",
"Processing invitations": "Processing invitations",
"Processing invitations Tips": "You have {{amount}} of team invitations that need to be processed",
Expand Down
1 change: 1 addition & 0 deletions projects/app/public/locales/zh/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@
"Leave Team Failed": "离开团队异常",
"Manage": "团队管理",
"Member": "成员",
"Over Max Member Tip": "团队最多{{max}}人",
"Personal Team": "个人团队",
"Processing invitations": "处理邀请",
"Processing invitations Tips": "你有{{amount}}个需要处理的团队邀请",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { useLoading } from '@/web/common/hooks/useLoading';
import { FormDataType, defaultForm } from './EditModal';
import MyMenu from '@/components/MyMenu';
import { useConfirm } from '@/web/common/hooks/useConfirm';
import { useToast } from '@/web/common/hooks/useToast';

const EditModal = dynamic(() => import('./EditModal'));
const InviteModal = dynamic(() => import('./InviteModal'));
Expand All @@ -50,6 +51,7 @@ const TeamManageModal = ({ onClose }: { onClose: () => void }) => {
const theme = useTheme();
const { t } = useTranslation();
const { Loading } = useLoading();
const { toast } = useToast();

const { ConfirmModal: ConfirmRemoveMemberModal, openConfirm: openRemoveMember } = useConfirm();
const { ConfirmModal: ConfirmLeaveTeamModal, openConfirm: openLeaveConfirm } = useConfirm({
Expand Down Expand Up @@ -257,7 +259,16 @@ const TeamManageModal = ({ onClose }: { onClose: () => void }) => {
borderRadius={'md'}
ml={3}
leftIcon={<MyIcon name={'common/inviteLight'} w={'14px'} color={'myBlue.600'} />}
onClick={onOpenInvite}
onClick={() => {
if (userInfo.team.maxSize > members.length) {
toast({
status: 'warning',
title: t('user.team.Over Max Member Tip', { max: userInfo.team.maxSize })
});
} else {
onOpenInvite();
}
}}
>
{t('user.team.Invite Member')}
</Button>
Expand Down
9 changes: 5 additions & 4 deletions projects/app/src/pages/api/admin/initv46.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ import { POST } from '@fastgpt/service/common/api/plusRequest';

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
const { limit = 50 } = req.body as { limit: number };
const { limit = 50, maxSize = 3 } = req.body as { limit: number; maxSize: number };
await connectToDatabase();

await initDefaultTeam(limit);
await initDefaultTeam(limit, maxSize);
await initMongoTeamId(limit);
await initDatasetAndApp();
await initCollectionFileTeam(limit);
Expand All @@ -59,7 +59,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
}

async function initDefaultTeam(limit: number) {
async function initDefaultTeam(limit: number, maxSize: number) {
/* init user default Team */
const users = await MongoUser.find({}, '_id balance');
console.log('init user default team', users.length);
Expand All @@ -79,7 +79,8 @@ async function initDefaultTeam(limit: number) {
try {
await createDefaultTeam({
userId: user._id,
balance: user.balance
balance: user.balance,
maxSize
});
} catch (error) {
console.log(error);
Expand Down
2 changes: 1 addition & 1 deletion projects/app/src/service/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async function initRootUser() {
rootId = _id;
}
// init root team
await createDefaultTeam({ userId: rootId });
await createDefaultTeam({ userId: rootId, maxSize: 1 });

console.log(`root user init:`, {
username: 'root',
Expand Down

0 comments on commit 0e4e493

Please sign in to comment.