|
| 1 | +import { NextAPI } from '@/service/middleware/entry'; |
| 2 | +import { getDatasetCollections } from '@/web/core/dataset/api'; |
| 3 | +import { DatasetTypeEnum } from '@fastgpt/global/core/dataset/constants'; |
| 4 | +import { mongoSessionRun } from '@fastgpt/service/common/mongo/sessionRun'; |
| 5 | +import { MongoDatasetCollection } from '@fastgpt/service/core/dataset/collection/schema'; |
| 6 | +import { MongoDataset } from '@fastgpt/service/core/dataset/schema'; |
| 7 | +import { upsertWebsiteSyncJobScheduler } from '@fastgpt/service/core/dataset/websiteSync'; |
| 8 | +import { authCert } from '@fastgpt/service/support/permission/auth/common'; |
| 9 | +import { NextApiRequest, NextApiResponse } from 'next'; |
| 10 | + |
| 11 | +async function handler(req: NextApiRequest, _res: NextApiResponse) { |
| 12 | + await authCert({ req, authRoot: true }); |
| 13 | + |
| 14 | + await mongoSessionRun(async (session) => { |
| 15 | + // find out all website dataset |
| 16 | + const datasets = await MongoDataset.find({ type: DatasetTypeEnum.websiteDataset }, undefined, { |
| 17 | + session |
| 18 | + }); |
| 19 | + |
| 20 | + await Promise.all( |
| 21 | + datasets.flatMap((dataset) => { |
| 22 | + if (dataset.autoSync) |
| 23 | + return upsertWebsiteSyncJobScheduler({ datasetId: String(dataset._id) }); |
| 24 | + else return []; |
| 25 | + }) |
| 26 | + ); |
| 27 | + |
| 28 | + // clear all collection next sync time |
| 29 | + for (const dataset of datasets) { |
| 30 | + await MongoDatasetCollection.updateMany( |
| 31 | + { |
| 32 | + teamId: dataset.teamId, |
| 33 | + datasetId: dataset._id |
| 34 | + }, |
| 35 | + { |
| 36 | + $unset: { |
| 37 | + nextSyncTime: 1 |
| 38 | + } |
| 39 | + }, |
| 40 | + { session } |
| 41 | + ); |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + return { success: true }; |
| 46 | +} |
| 47 | + |
| 48 | +export default NextAPI(handler); |
0 commit comments