mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 05:12:39 +00:00
v4.6-3 (#471)
This commit is contained in:
@@ -4,57 +4,42 @@ import {
|
||||
TeamMemberRoleEnum,
|
||||
TeamMemberStatusEnum,
|
||||
TeamCollectionName,
|
||||
TeamMemberCollectionName
|
||||
TeamMemberCollectionName,
|
||||
leaveStatus
|
||||
} from '@fastgpt/global/support/user/team/constant';
|
||||
|
||||
export async function getTeamInfoByTmbId({
|
||||
tmbId,
|
||||
userId
|
||||
}: {
|
||||
tmbId?: string;
|
||||
userId?: string;
|
||||
}): Promise<TeamItemType> {
|
||||
if (!tmbId && !userId) {
|
||||
return Promise.reject('tmbId or userId is required');
|
||||
}
|
||||
|
||||
async function getTeam(match: Record<string, any>): Promise<TeamItemType> {
|
||||
const db = connectionMongo?.connection?.db;
|
||||
|
||||
const TeamMember = db.collection(TeamMemberCollectionName);
|
||||
|
||||
const results = await TeamMember.aggregate([
|
||||
{
|
||||
$match: tmbId
|
||||
? {
|
||||
_id: new Types.ObjectId(tmbId)
|
||||
}
|
||||
: {
|
||||
userId: new Types.ObjectId(userId),
|
||||
defaultTeam: true
|
||||
}
|
||||
$match: match
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: TeamCollectionName, // 关联的集合名
|
||||
localField: 'teamId', // TeamMember 集合中用于关联的字段
|
||||
foreignField: '_id', // Team 集合中用于关联的字段
|
||||
as: 'team' // 查询结果中的字段名,存放关联查询的结果
|
||||
from: TeamCollectionName,
|
||||
localField: 'teamId',
|
||||
foreignField: '_id',
|
||||
as: 'team'
|
||||
}
|
||||
},
|
||||
{
|
||||
$unwind: '$team' // 将查询结果中的 team 字段展开,变成一个对象
|
||||
$unwind: '$team'
|
||||
}
|
||||
]).toArray();
|
||||
const tmb = results[0];
|
||||
|
||||
if (!tmb) {
|
||||
return Promise.reject('team not exist');
|
||||
return Promise.reject('member not exist');
|
||||
}
|
||||
|
||||
return {
|
||||
userId: String(tmb.userId),
|
||||
teamId: String(tmb.teamId),
|
||||
teamName: tmb.team.name,
|
||||
memberName: tmb.name,
|
||||
avatar: tmb.team.avatar,
|
||||
balance: tmb.team.balance,
|
||||
tmbId: String(tmb._id),
|
||||
@@ -65,11 +50,31 @@ export async function getTeamInfoByTmbId({
|
||||
maxSize: tmb.team.maxSize
|
||||
};
|
||||
}
|
||||
|
||||
export async function getTeamInfoByTmbId({ tmbId }: { tmbId: string }) {
|
||||
if (!tmbId) {
|
||||
return Promise.reject('tmbId or userId is required');
|
||||
}
|
||||
return getTeam({
|
||||
_id: new Types.ObjectId(tmbId),
|
||||
status: leaveStatus
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUserDefaultTeam({ userId }: { userId: string }) {
|
||||
if (!userId) {
|
||||
return Promise.reject('tmbId or userId is required');
|
||||
}
|
||||
return getTeam({
|
||||
userId: new Types.ObjectId(userId),
|
||||
defaultTeam: true
|
||||
});
|
||||
}
|
||||
export async function createDefaultTeam({
|
||||
userId,
|
||||
teamName = 'My Team',
|
||||
avatar = '/icon/logo.svg',
|
||||
balance = 0,
|
||||
balance,
|
||||
maxSize = 5
|
||||
}: {
|
||||
userId: string;
|
||||
@@ -103,6 +108,7 @@ export async function createDefaultTeam({
|
||||
await TeamMember.insertOne({
|
||||
teamId: insertedId,
|
||||
userId,
|
||||
name: 'Owner',
|
||||
role: TeamMemberRoleEnum.owner,
|
||||
status: TeamMemberStatusEnum.active,
|
||||
createTime: new Date(),
|
||||
@@ -116,7 +122,7 @@ export async function createDefaultTeam({
|
||||
},
|
||||
{
|
||||
$set: {
|
||||
balance,
|
||||
...(balance !== undefined && { balance }),
|
||||
maxSize
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user