mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 21:13:50 +00:00
4.6.7 first pr (#726)
This commit is contained in:
@@ -1,18 +1,22 @@
|
||||
import { MongoOpenApi } from './schema';
|
||||
|
||||
export async function updateApiKeyUsedTime(id: string) {
|
||||
await MongoOpenApi.findByIdAndUpdate(id, {
|
||||
export function updateApiKeyUsedTime(id: string) {
|
||||
MongoOpenApi.findByIdAndUpdate(id, {
|
||||
lastUsedTime: new Date()
|
||||
}).catch((err) => {
|
||||
console.log('update apiKey used time error', err);
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateApiKeyUsage({ apikey, usage }: { apikey: string; usage: number }) {
|
||||
await MongoOpenApi.findOneAndUpdate(
|
||||
export function updateApiKeyUsage({ apikey, usage }: { apikey: string; usage: number }) {
|
||||
MongoOpenApi.findOneAndUpdate(
|
||||
{ apiKey: apikey },
|
||||
{
|
||||
$inc: {
|
||||
usage
|
||||
}
|
||||
}
|
||||
);
|
||||
).catch((err) => {
|
||||
console.log('update apiKey usage error', err);
|
||||
});
|
||||
}
|
||||
|
@@ -9,17 +9,15 @@ export const updateOutLinkUsage = async ({
|
||||
shareId: string;
|
||||
total: number;
|
||||
}) => {
|
||||
try {
|
||||
await MongoOutLink.findOneAndUpdate(
|
||||
{ shareId },
|
||||
{
|
||||
$inc: { total },
|
||||
lastTime: new Date()
|
||||
}
|
||||
);
|
||||
} catch (err) {
|
||||
MongoOutLink.findOneAndUpdate(
|
||||
{ shareId },
|
||||
{
|
||||
$inc: { total },
|
||||
lastTime: new Date()
|
||||
}
|
||||
).catch((err) => {
|
||||
console.log('update shareChat error', err);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const pushResult2Remote = async ({
|
||||
|
20
packages/service/support/permission/limit/dataset.ts
Normal file
20
packages/service/support/permission/limit/dataset.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { getVectorCountByTeamId } from '../../../common/vectorStore/controller';
|
||||
import { getTeamDatasetValidSub } from '../../wallet/sub/utils';
|
||||
|
||||
export const checkDatasetLimit = async ({
|
||||
teamId,
|
||||
freeSize = Infinity,
|
||||
insertLen = 0
|
||||
}: {
|
||||
teamId: string;
|
||||
freeSize?: number;
|
||||
insertLen?: number;
|
||||
}) => {
|
||||
const { maxSize } = await getTeamDatasetValidSub({ teamId, freeSize });
|
||||
const usedSize = await getVectorCountByTeamId(teamId);
|
||||
|
||||
if (usedSize + insertLen >= maxSize) {
|
||||
return Promise.reject(`数据库容量已满,无法继续添加。可以在账号页面进行扩容。`);
|
||||
}
|
||||
return;
|
||||
};
|
@@ -30,9 +30,6 @@ const TeamSchema = new Schema({
|
||||
type: Number,
|
||||
default: 5
|
||||
},
|
||||
lastDatasetBillTime: {
|
||||
type: Date
|
||||
},
|
||||
limit: {
|
||||
lastExportDatasetTime: {
|
||||
type: Date
|
||||
|
@@ -54,6 +54,7 @@ const BillSchema = new Schema({
|
||||
try {
|
||||
BillSchema.index({ teamId: 1 });
|
||||
BillSchema.index({ tmbId: 1 });
|
||||
BillSchema.index({ tmbId: 1, time: 1 });
|
||||
BillSchema.index({ time: 1 }, { expireAfterSeconds: 90 * 24 * 60 * 60 });
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
|
55
packages/service/support/wallet/sub/schema.ts
Normal file
55
packages/service/support/wallet/sub/schema.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { connectionMongo, type Model } from '../../../common/mongo';
|
||||
const { Schema, model, models } = connectionMongo;
|
||||
import { TeamCollectionName } from '@fastgpt/global/support/user/team/constant';
|
||||
import { subModeMap, subStatusMap, subTypeMap } from '@fastgpt/global/support/wallet/sub/constants';
|
||||
import type { TeamSubSchema } from '@fastgpt/global/support/wallet/sub/type';
|
||||
|
||||
export const subCollectionName = 'team.subscription';
|
||||
|
||||
const SubSchema = new Schema({
|
||||
teamId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: TeamCollectionName,
|
||||
required: true
|
||||
},
|
||||
type: {
|
||||
type: String,
|
||||
enum: Object.keys(subTypeMap),
|
||||
required: true
|
||||
},
|
||||
mode: {
|
||||
type: String,
|
||||
enum: Object.keys(subModeMap),
|
||||
required: true
|
||||
},
|
||||
status: {
|
||||
type: String,
|
||||
enum: Object.keys(subStatusMap),
|
||||
required: true
|
||||
},
|
||||
renew: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
},
|
||||
startTime: {
|
||||
type: Date
|
||||
},
|
||||
expiredTime: {
|
||||
type: Date
|
||||
},
|
||||
datasetStoreAmount: {
|
||||
type: Number
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
SubSchema.index({ teamId: 1 });
|
||||
SubSchema.index({ status: 1 });
|
||||
SubSchema.index({ type: 1 });
|
||||
SubSchema.index({ expiredTime: -1 });
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
|
||||
export const MongoTeamSub: Model<TeamSubSchema> =
|
||||
models[subCollectionName] || model(subCollectionName, SubSchema);
|
31
packages/service/support/wallet/sub/utils.ts
Normal file
31
packages/service/support/wallet/sub/utils.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { SubStatusEnum } from '@fastgpt/global/support/wallet/sub/constants';
|
||||
import { MongoTeamSub } from './schema';
|
||||
|
||||
/* get team dataset size */
|
||||
export const getTeamDatasetValidSub = async ({
|
||||
teamId,
|
||||
freeSize = Infinity
|
||||
}: {
|
||||
teamId: string;
|
||||
freeSize?: number;
|
||||
}) => {
|
||||
const sub = await MongoTeamSub.findOne({
|
||||
teamId,
|
||||
status: SubStatusEnum.active
|
||||
})
|
||||
.sort({
|
||||
expiredTime: -1
|
||||
})
|
||||
.lean();
|
||||
|
||||
const maxSize = (() => {
|
||||
if (!sub || !sub.datasetStoreAmount) return freeSize;
|
||||
|
||||
return sub.datasetStoreAmount + freeSize;
|
||||
})();
|
||||
|
||||
return {
|
||||
maxSize,
|
||||
sub
|
||||
};
|
||||
};
|
Reference in New Issue
Block a user