This commit is contained in:
Archer
2023-11-15 11:36:25 +08:00
committed by GitHub
parent 592e1a93a2
commit bfd8be5df0
181 changed files with 2499 additions and 1552 deletions

View File

@@ -23,6 +23,7 @@ const PromotionRecordSchema = new Schema({
enum: ['pay', 'register']
},
amount: {
// 1 * PRICE_SCALE
type: Number,
required: true
}

View File

@@ -30,7 +30,7 @@ export const pushResult2Remote = async ({
shareId?: string;
responseData?: any[];
}) => {
if (!shareId || !authToken) return;
if (!shareId || !authToken || !global.systemEnv.pluginBaseUrl) return;
try {
const outLink = await MongoOutLink.findOne({
shareId

View File

@@ -1,5 +1,7 @@
import { AuthUserTypeEnum } from '@fastgpt/global/support/permission/constant';
import { parseHeaderCert } from '../controller';
import { AuthModeType } from '../type';
import { authOutLinkValid } from './outLink';
export const authCert = async (props: AuthModeType) => {
const result = await parseHeaderCert(props);
@@ -10,3 +12,22 @@ export const authCert = async (props: AuthModeType) => {
canWrite: true
};
};
export async function authCertAndShareId({
shareId,
...props
}: AuthModeType & { shareId?: string }) {
if (!shareId) {
return authCert(props);
}
const { app } = await authOutLinkValid({ shareId });
return {
teamId: String(app.teamId),
tmbId: String(app.tmbId),
authType: AuthUserTypeEnum.outLink,
apikey: '',
isOwner: false,
canWrite: false
};
}

View File

@@ -27,11 +27,11 @@ export async function authDataset({
}
> {
const result = await parseHeaderCert(props);
const { userId, teamId, tmbId } = result;
const { teamId, tmbId } = result;
const { role } = await getTeamInfoByTmbId({ tmbId });
const { dataset, isOwner, canWrite } = await (async () => {
const dataset = (await MongoDataset.findOne({ _id: datasetId, teamId }))?.toJSON();
const dataset = (await MongoDataset.findOne({ _id: datasetId, teamId }))?.toObject();
if (!dataset) {
return Promise.reject(DatasetErrEnum.unAuthDataset);

View File

@@ -6,7 +6,6 @@ import { getTeamInfoByTmbId } from '../../user/team/controller';
import { MongoOpenApi } from '../../openapi/schema';
import { OpenApiErrEnum } from '@fastgpt/global/common/error/code/openapi';
import { TeamMemberRoleEnum } from '@fastgpt/global/support/user/team/constant';
import { PermissionTypeEnum } from '@fastgpt/global/support/permission/constant';
export async function authOpenApiKeyCrud({
id,

View File

@@ -37,13 +37,11 @@ export async function authUserRole(props: AuthModeType): Promise<
teamOwner: boolean;
}
> {
const { userId, teamId, tmbId } = await parseHeaderCert(props);
const { role: userRole, canWrite } = await getTeamInfoByTmbId({ tmbId });
const result = await parseHeaderCert(props);
const { role: userRole, canWrite } = await getTeamInfoByTmbId({ tmbId: result.tmbId });
return {
userId,
teamId,
tmbId,
...result,
isOwner: true,
role: userRole,
teamOwner: userRole === TeamMemberRoleEnum.owner,

View File

@@ -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
}
}

View File

@@ -36,6 +36,7 @@ const BillSchema = new Schema({
default: () => new Date()
},
total: {
// 1 * PRICE_SCALE
type: Number,
required: true
},