import { MongoDatasetCollection } from './schema'; import { ParentTreePathItemType } from '@fastgpt/global/common/parentFolder/type'; /** * get all collection by top collectionId */ export async function findCollectionAndChild(id: string, fields = '_id parentId name metadata') { async function find(id: string) { // find children const children = await MongoDatasetCollection.find({ parentId: id }, fields); let collections = children; for (const child of children) { const grandChildrenIds = await find(child._id); collections = collections.concat(grandChildrenIds); } return collections; } const [collection, childCollections] = await Promise.all([ MongoDatasetCollection.findById(id, fields), find(id) ]); if (!collection) { return Promise.reject('Collection not found'); } return [collection, ...childCollections]; } export async function getDatasetCollectionPaths({ parentId = '', userId }: { parentId?: string; userId: string; }): Promise { async function find(parentId?: string): Promise { if (!parentId) { return []; } const parent = await MongoDatasetCollection.findOne({ _id: parentId, userId }, 'name parentId'); if (!parent) return []; const paths = await find(parent.parentId); paths.push({ parentId, parentName: parent.name }); return paths; } return await find(parentId); } export function getCollectionUpdateTime({ name, time }: { time?: Date; name: string }) { if (time) return time; if (name.startsWith('手动') || ['manual', 'mark'].includes(name)) return new Date('2999/9/9'); return new Date(); }