/
ncit
/
c4panelmodel
Обзор
Документация
Войти
/
ncit
/
c4panelmodel
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
src/lib/server/services/flow.ts
108 строк
4 KB
ncit
feat: rewrite hooks.server.ts to use Supabase Auth
07 июн 2026, 19:32
07 июн 2026, 19:32
b6162ab
Код
Авторство
О чём код?
import { db } from '$lib/server/db'; // ====== Flows: ordered walkthroughs of a diagram (IcePanel-style) ====== export async function listFlows(projectId: string) { return db.flow.findMany({ where: { projectId }, include: { diagram: { select: { id: true, name: true } }, _count: { select: { steps: true } } }, orderBy: [{ order: 'asc' }, { createdAt: 'asc' }] }); } export async function createFlow(projectId: string, name: string, diagramId?: string | null) { const count = await db.flow.count({ where: { projectId } }); return db.flow.create({ data: { projectId, name, diagramId: diagramId ?? null, order: count } }); } export async function deleteFlow(projectId: string, flowId: string) { await db.flow.deleteMany({ where: { id: flowId, projectId } }); } export async function getFlowForEditor(projectId: string, flowId: string) { const flow = await db.flow.findFirst({ where: { id: flowId, projectId }, include: { diagram: { select: { id: true, name: true } }, steps: { orderBy: { order: 'asc' }, include: { sourceObject: { select: { id: true, name: true } }, targetObject: { select: { id: true, name: true } } } } } }); if (!flow) return null; // Objects + connections available to reference from steps. Scope to the flow's // diagram if it has one, otherwise the whole project model. const objects = await db.modelObject.findMany({ where: { projectId }, select: { id: true, name: true, type: true }, orderBy: { name: 'asc' } }); const connections = await db.connection.findMany({ where: { projectId }, select: { id: true, sourceId: true, targetId: true, label: true, technology: true } }); return { flow, objects, connections }; } interface StepInput { flowId: string; type: string; label?: string | null; description?: string | null; sourceObjectId?: string | null; targetObjectId?: string | null; connectionId?: string | null; } export async function addStep(projectId: string, input: StepInput) { // Guard the flow belongs to this project. const flow = await db.flow.findFirst({ where: { id: input.flowId, projectId }, select: { id: true } }); if (!flow) throw new Error('Flow not found'); const count = await db.flowStep.count({ where: { flowId: input.flowId } }); return db.flowStep.create({ data: { flowId: input.flowId, type: input.type as never, order: count, label: input.label ?? null, description: input.description ?? null, sourceObjectId: input.sourceObjectId ?? null, targetObjectId: input.targetObjectId ?? null, connectionId: input.connectionId ?? null } }); } export async function deleteStep(projectId: string, flowId: string, stepId: string) { const flow = await db.flow.findFirst({ where: { id: flowId, projectId }, select: { id: true } }); if (!flow) return; await db.flowStep.deleteMany({ where: { id: stepId, flowId } }); // Re-pack order so playback stays sequential. const remaining = await db.flowStep.findMany({ where: { flowId }, orderBy: { order: 'asc' }, select: { id: true } }); await Promise.all(remaining.map((s, i) => db.flowStep.update({ where: { id: s.id }, data: { order: i } }))); } export async function moveStep(projectId: string, flowId: string, stepId: string, dir: 'up' | 'down') { const flow = await db.flow.findFirst({ where: { id: flowId, projectId }, select: { id: true } }); if (!flow) return; const steps = await db.flowStep.findMany({ where: { flowId }, orderBy: { order: 'asc' } }); const idx = steps.findIndex((s) => s.id === stepId); if (idx === -1) return; const swap = dir === 'up' ? idx - 1 : idx + 1; if (swap < 0 || swap >= steps.length) return; await db.$transaction([ db.flowStep.update({ where: { id: steps[idx].id }, data: { order: steps[swap].order } }), db.flowStep.update({ where: { id: steps[swap].id }, data: { order: steps[idx].order } }) ]); }