import { User } from "indite-js/prisma"; import { Block, PublicBot, Bot, BotLinkBlock } from "indite-js/schemas"; import { isDefined } from "indite-js/lib"; import { fetchLinkedBots } from "./fetchLinkedBots"; import { LogicBlockType } from "indite-js/schemas/features/blocks/logic/constants"; type Props = { bots: Pick[]; userId: string | undefined; isPreview?: boolean; }; export const fetchLinkedChildBots = ({ bots, userId, isPreview }: Props) => async ( capturedLinkedBots: (Bot | PublicBot)[] ): Promise<(Bot | PublicBot)[]> => { const linkedBotIds = bots .flatMap((bot) => ( bot.groups .flatMap((group) => group.blocks) .filter( (block) => block.type === LogicBlockType.BOT_LINK && isDefined(block.options?.botId) && !capturedLinkedBots.some( (bot) => ("botId" in bot ? bot.botId : bot.id) === block.options?.botId ) ) as BotLinkBlock[] ).map((b) => b.options?.botId) ) .filter(isDefined); if (linkedBotIds.length === 0) return capturedLinkedBots; const linkedBots = (await fetchLinkedBots({ userId, botIds: linkedBotIds, isPreview, })) as (Bot | PublicBot)[]; return fetchLinkedChildBots({ bots: linkedBots, userId, isPreview, })([...capturedLinkedBots, ...linkedBots]); };