mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 21:13:50 +00:00
4.6.4-alpha (#569)
This commit is contained in:
@@ -1,35 +1,15 @@
|
||||
import { TeamItemType } from '@fastgpt/global/support/user/team/type';
|
||||
import { connectionMongo, Types } from '../../../common/mongo';
|
||||
import { TeamItemType, TeamMemberWithTeamSchema } from '@fastgpt/global/support/user/team/type';
|
||||
import { Types } from '../../../common/mongo';
|
||||
import {
|
||||
TeamMemberRoleEnum,
|
||||
TeamMemberStatusEnum,
|
||||
TeamCollectionName,
|
||||
TeamMemberCollectionName,
|
||||
leaveStatus
|
||||
notLeaveStatus
|
||||
} from '@fastgpt/global/support/user/team/constant';
|
||||
import { MongoTeamMember } from './teamMemberSchema';
|
||||
import { MongoTeam } from './teamSchema';
|
||||
|
||||
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: match
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: TeamCollectionName,
|
||||
localField: 'teamId',
|
||||
foreignField: '_id',
|
||||
as: 'team'
|
||||
}
|
||||
},
|
||||
{
|
||||
$unwind: '$team'
|
||||
}
|
||||
]).toArray();
|
||||
const tmb = results[0];
|
||||
const tmb = (await MongoTeamMember.findOne(match).populate('teamId')) as TeamMemberWithTeamSchema;
|
||||
|
||||
if (!tmb) {
|
||||
return Promise.reject('member not exist');
|
||||
@@ -37,17 +17,17 @@ async function getTeam(match: Record<string, any>): Promise<TeamItemType> {
|
||||
|
||||
return {
|
||||
userId: String(tmb.userId),
|
||||
teamId: String(tmb.teamId),
|
||||
teamName: tmb.team.name,
|
||||
teamId: String(tmb.teamId._id),
|
||||
teamName: tmb.teamId.name,
|
||||
memberName: tmb.name,
|
||||
avatar: tmb.team.avatar,
|
||||
balance: tmb.team.balance,
|
||||
avatar: tmb.teamId.avatar,
|
||||
balance: tmb.teamId.balance,
|
||||
tmbId: String(tmb._id),
|
||||
role: tmb.role,
|
||||
status: tmb.status,
|
||||
defaultTeam: tmb.defaultTeam,
|
||||
canWrite: tmb.role !== TeamMemberRoleEnum.visitor,
|
||||
maxSize: tmb.team.maxSize
|
||||
maxSize: tmb.teamId.maxSize
|
||||
};
|
||||
}
|
||||
|
||||
@@ -57,7 +37,7 @@ export async function getTeamInfoByTmbId({ tmbId }: { tmbId: string }) {
|
||||
}
|
||||
return getTeam({
|
||||
_id: new Types.ObjectId(tmbId),
|
||||
status: leaveStatus
|
||||
status: notLeaveStatus
|
||||
});
|
||||
}
|
||||
|
||||
@@ -83,12 +63,8 @@ export async function createDefaultTeam({
|
||||
balance?: number;
|
||||
maxSize?: number;
|
||||
}) {
|
||||
const db = connectionMongo.connection.db;
|
||||
const Team = db.collection(TeamCollectionName);
|
||||
const TeamMember = db.collection(TeamMemberCollectionName);
|
||||
|
||||
// auth default team
|
||||
const tmb = await TeamMember.findOne({
|
||||
const tmb = await MongoTeamMember.findOne({
|
||||
userId: new Types.ObjectId(userId),
|
||||
defaultTeam: true
|
||||
});
|
||||
@@ -97,7 +73,7 @@ export async function createDefaultTeam({
|
||||
console.log('create default team', userId);
|
||||
|
||||
// create
|
||||
const { insertedId } = await Team.insertOne({
|
||||
const { _id: insertedId } = await MongoTeam.create({
|
||||
ownerId: userId,
|
||||
name: teamName,
|
||||
avatar,
|
||||
@@ -105,7 +81,7 @@ export async function createDefaultTeam({
|
||||
maxSize,
|
||||
createTime: new Date()
|
||||
});
|
||||
await TeamMember.insertOne({
|
||||
await MongoTeamMember.create({
|
||||
teamId: insertedId,
|
||||
userId,
|
||||
name: 'Owner',
|
||||
@@ -116,16 +92,11 @@ export async function createDefaultTeam({
|
||||
});
|
||||
} else {
|
||||
console.log('default team exist', userId);
|
||||
await Team.updateOne(
|
||||
{
|
||||
_id: new Types.ObjectId(tmb.teamId)
|
||||
},
|
||||
{
|
||||
$set: {
|
||||
...(balance !== undefined && { balance }),
|
||||
maxSize
|
||||
}
|
||||
await MongoTeam.findByIdAndUpdate(tmb.teamId, {
|
||||
$set: {
|
||||
...(balance !== undefined && { balance }),
|
||||
maxSize
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
51
packages/service/support/user/team/teamMemberSchema.ts
Normal file
51
packages/service/support/user/team/teamMemberSchema.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { connectionMongo, type Model } from '../../../common/mongo';
|
||||
const { Schema, model, models } = connectionMongo;
|
||||
import { TeamMemberSchema as TeamMemberType } from '@fastgpt/global/support/user/team/type.d';
|
||||
import { userCollectionName } from '../../user/schema';
|
||||
import {
|
||||
TeamMemberRoleMap,
|
||||
TeamMemberStatusMap,
|
||||
TeamMemberCollectionName,
|
||||
TeamCollectionName
|
||||
} from '@fastgpt/global/support/user/team/constant';
|
||||
|
||||
const TeamMemberSchema = new Schema({
|
||||
teamId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: TeamCollectionName,
|
||||
required: true
|
||||
},
|
||||
userId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: userCollectionName,
|
||||
required: true
|
||||
},
|
||||
name: {
|
||||
type: String,
|
||||
default: 'Member'
|
||||
},
|
||||
role: {
|
||||
type: String,
|
||||
enum: Object.keys(TeamMemberRoleMap)
|
||||
},
|
||||
status: {
|
||||
type: String,
|
||||
enum: Object.keys(TeamMemberStatusMap)
|
||||
},
|
||||
createTime: {
|
||||
type: Date,
|
||||
default: () => new Date()
|
||||
},
|
||||
defaultTeam: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
|
||||
export const MongoTeamMember: Model<TeamMemberType> =
|
||||
models[TeamMemberCollectionName] || model(TeamMemberCollectionName, TeamMemberSchema);
|
41
packages/service/support/user/team/teamSchema.ts
Normal file
41
packages/service/support/user/team/teamSchema.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { connectionMongo, type Model } from '../../../common/mongo';
|
||||
const { Schema, model, models } = connectionMongo;
|
||||
import { TeamSchema as TeamType } from '@fastgpt/global/support/user/team/type.d';
|
||||
import { userCollectionName } from '../../user/schema';
|
||||
import { TeamCollectionName } from '@fastgpt/global/support/user/team/constant';
|
||||
import { PRICE_SCALE } from '@fastgpt/global/support/wallet/bill/constants';
|
||||
|
||||
const TeamSchema = new Schema({
|
||||
name: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
ownerId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: userCollectionName
|
||||
},
|
||||
avatar: {
|
||||
type: String,
|
||||
default: '/icon/logo.svg'
|
||||
},
|
||||
createTime: {
|
||||
type: Date,
|
||||
default: () => Date.now()
|
||||
},
|
||||
balance: {
|
||||
type: Number,
|
||||
default: 2 * PRICE_SCALE
|
||||
},
|
||||
maxSize: {
|
||||
type: Number,
|
||||
default: 5
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
|
||||
export const MongoTeam: Model<TeamType> =
|
||||
models[TeamCollectionName] || model(TeamCollectionName, TeamSchema);
|
Reference in New Issue
Block a user