mirror of
https://github.com/labring/FastGPT.git
synced 2025-10-14 23:22:22 +00:00
@@ -5,6 +5,20 @@ import { MongoApp } from './schema';
|
||||
import type { StoreNodeItemType } from '@fastgpt/global/core/workflow/type/node';
|
||||
import { encryptSecretValue, storeSecretValue } from '../../common/secret/utils';
|
||||
import { SystemToolInputTypeEnum } from '@fastgpt/global/core/app/systemTool/constants';
|
||||
import { type ClientSession } from '../../common/mongo';
|
||||
import { MongoEvaluation } from './evaluation/evalSchema';
|
||||
import { removeEvaluationJob } from './evaluation/mq';
|
||||
import { deleteChatFiles } from '../chat/controller';
|
||||
import { MongoChatItem } from '../chat/chatItemSchema';
|
||||
import { MongoChat } from '../chat/chatSchema';
|
||||
import { MongoOutLink } from '../../support/outLink/schema';
|
||||
import { MongoOpenApi } from '../../support/openapi/schema';
|
||||
import { MongoAppVersion } from './version/schema';
|
||||
import { MongoChatInputGuide } from '../chat/inputGuide/schema';
|
||||
import { MongoResourcePermission } from '../../support/permission/schema';
|
||||
import { PerResourceTypeEnum } from '@fastgpt/global/support/permission/constant';
|
||||
import { removeImageByPath } from '../../common/file/image/controller';
|
||||
import { mongoSessionRun } from '../../common/mongo/sessionRun';
|
||||
|
||||
export const beforeUpdateAppFormat = ({ nodes }: { nodes?: StoreNodeItemType[] }) => {
|
||||
if (!nodes) return;
|
||||
@@ -110,3 +124,88 @@ export const getAppBasicInfoByIds = async ({ teamId, ids }: { teamId: string; id
|
||||
avatar: item.avatar
|
||||
}));
|
||||
};
|
||||
|
||||
export const onDelOneApp = async ({
|
||||
teamId,
|
||||
appId,
|
||||
session
|
||||
}: {
|
||||
teamId: string;
|
||||
appId: string;
|
||||
session?: ClientSession;
|
||||
}) => {
|
||||
const apps = await findAppAndAllChildren({
|
||||
teamId,
|
||||
appId,
|
||||
fields: '_id avatar'
|
||||
});
|
||||
|
||||
// Remove eval job
|
||||
const evalJobs = await MongoEvaluation.find(
|
||||
{
|
||||
appId: { $in: apps.map((app) => app._id) }
|
||||
},
|
||||
'_id'
|
||||
).lean();
|
||||
await Promise.all(evalJobs.map((evalJob) => removeEvaluationJob(evalJob._id)));
|
||||
|
||||
const del = async (session: ClientSession) => {
|
||||
for await (const app of apps) {
|
||||
const appId = app._id;
|
||||
// Chats
|
||||
await deleteChatFiles({ appId });
|
||||
await MongoChatItem.deleteMany(
|
||||
{
|
||||
appId
|
||||
},
|
||||
{ session }
|
||||
);
|
||||
await MongoChat.deleteMany(
|
||||
{
|
||||
appId
|
||||
},
|
||||
{ session }
|
||||
);
|
||||
|
||||
// 删除分享链接
|
||||
await MongoOutLink.deleteMany({
|
||||
appId
|
||||
}).session(session);
|
||||
// Openapi
|
||||
await MongoOpenApi.deleteMany({
|
||||
appId
|
||||
}).session(session);
|
||||
|
||||
// delete version
|
||||
await MongoAppVersion.deleteMany({
|
||||
appId
|
||||
}).session(session);
|
||||
|
||||
await MongoChatInputGuide.deleteMany({
|
||||
appId
|
||||
}).session(session);
|
||||
|
||||
await MongoResourcePermission.deleteMany({
|
||||
resourceType: PerResourceTypeEnum.app,
|
||||
teamId,
|
||||
resourceId: appId
|
||||
}).session(session);
|
||||
|
||||
// delete app
|
||||
await MongoApp.deleteOne(
|
||||
{
|
||||
_id: appId
|
||||
},
|
||||
{ session }
|
||||
);
|
||||
|
||||
await removeImageByPath(app.avatar, session);
|
||||
}
|
||||
};
|
||||
|
||||
if (session) {
|
||||
return del(session);
|
||||
}
|
||||
|
||||
return mongoSessionRun(del);
|
||||
};
|
||||
|
@@ -10,6 +10,10 @@ import { MongoDatasetDataText } from './data/dataTextSchema';
|
||||
import { DatasetErrEnum } from '@fastgpt/global/common/error/code/dataset';
|
||||
import { retryFn } from '@fastgpt/global/common/system/utils';
|
||||
import { clearDatasetImages } from './image/utils';
|
||||
import { MongoDatasetCollectionTags } from './tag/schema';
|
||||
import { removeDatasetSyncJobScheduler } from './datasetSync';
|
||||
import { mongoSessionRun } from '../../common/mongo/sessionRun';
|
||||
import { removeImageByPath } from '../../common/file/image/controller';
|
||||
|
||||
/* ============= dataset ========== */
|
||||
/* find all datasetId by top datasetId */
|
||||
@@ -118,3 +122,44 @@ export async function delDatasetRelevantData({
|
||||
datasetId: { $in: datasetIds }
|
||||
}).session(session);
|
||||
}
|
||||
|
||||
export const deleteDatasets = async ({
|
||||
teamId,
|
||||
datasets
|
||||
}: {
|
||||
teamId: string;
|
||||
datasets: DatasetSchemaType[];
|
||||
}) => {
|
||||
const datasetIds = datasets.map((d) => d._id);
|
||||
|
||||
// delete collection.tags
|
||||
await MongoDatasetCollectionTags.deleteMany({
|
||||
teamId,
|
||||
datasetId: { $in: datasetIds }
|
||||
});
|
||||
|
||||
// Remove cron job
|
||||
await Promise.all(
|
||||
datasets.map((dataset) => {
|
||||
return removeDatasetSyncJobScheduler(dataset._id);
|
||||
})
|
||||
);
|
||||
|
||||
// delete all dataset.data and pg data
|
||||
await mongoSessionRun(async (session) => {
|
||||
// delete dataset data
|
||||
await delDatasetRelevantData({ datasets, session });
|
||||
|
||||
// delete dataset
|
||||
await MongoDataset.deleteMany(
|
||||
{
|
||||
_id: { $in: datasetIds }
|
||||
},
|
||||
{ session }
|
||||
);
|
||||
|
||||
for await (const dataset of datasets) {
|
||||
await removeImageByPath(dataset.avatar, session);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
@@ -14,7 +14,8 @@ export const ResourcePermissionCollectionName = 'resource_permissions';
|
||||
export const ResourcePermissionSchema = new Schema({
|
||||
teamId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: TeamCollectionName
|
||||
ref: TeamCollectionName,
|
||||
required: true
|
||||
},
|
||||
tmbId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
|
@@ -86,7 +86,7 @@ const getSession = async (key: string): Promise<SessionType> => {
|
||||
export const delUserAllSession = async (userId: string, whiteList?: (string | undefined)[]) => {
|
||||
const formatWhiteList = whiteList?.map((item) => item && getSessionKey(item));
|
||||
const redis = getGlobalRedisConnection();
|
||||
const keys = (await getAllKeysByPrefix(`${redisPrefix}${userId}`)).filter(
|
||||
const keys = (await getAllKeysByPrefix(`${redisPrefix}${String(userId)}`)).filter(
|
||||
(item) => !formatWhiteList?.includes(item)
|
||||
);
|
||||
|
||||
|
@@ -17,6 +17,7 @@ const CouponSchema = new Schema({
|
||||
enum: Object.values(CouponTypeEnum)
|
||||
},
|
||||
price: Number,
|
||||
description: String,
|
||||
subscriptions: {
|
||||
type: [Object],
|
||||
required: true
|
||||
|
Reference in New Issue
Block a user