Files
moltbot/src/tasks/flow-owner-access.ts
Mariano 2fa4c7cc61 TaskFlow: restore managed substrate (#58930)
Merged via squash.

Prepared head SHA: c99093838f
Co-authored-by: mbelinky <132747814+mbelinky@users.noreply.github.com>
Co-authored-by: mbelinky <132747814+mbelinky@users.noreply.github.com>
Reviewed-by: @mbelinky
2026-04-02 12:17:56 +02:00

49 lines
1.7 KiB
TypeScript

import { findLatestFlowForOwnerKey, getFlowById, listFlowsForOwnerKey } from "./flow-registry.js";
import type { FlowRecord } from "./flow-registry.types.js";
function normalizeOwnerKey(ownerKey?: string): string | undefined {
const trimmed = ownerKey?.trim();
return trimmed ? trimmed : undefined;
}
function canOwnerAccessFlow(flow: FlowRecord, callerOwnerKey: string): boolean {
return normalizeOwnerKey(flow.ownerKey) === normalizeOwnerKey(callerOwnerKey);
}
export function getFlowByIdForOwner(params: {
flowId: string;
callerOwnerKey: string;
}): FlowRecord | undefined {
const flow = getFlowById(params.flowId);
return flow && canOwnerAccessFlow(flow, params.callerOwnerKey) ? flow : undefined;
}
export function listFlowsForOwner(params: { callerOwnerKey: string }): FlowRecord[] {
const ownerKey = normalizeOwnerKey(params.callerOwnerKey);
return ownerKey ? listFlowsForOwnerKey(ownerKey) : [];
}
export function findLatestFlowForOwner(params: { callerOwnerKey: string }): FlowRecord | undefined {
const ownerKey = normalizeOwnerKey(params.callerOwnerKey);
return ownerKey ? findLatestFlowForOwnerKey(ownerKey) : undefined;
}
export function resolveFlowForLookupTokenForOwner(params: {
token: string;
callerOwnerKey: string;
}): FlowRecord | undefined {
const direct = getFlowByIdForOwner({
flowId: params.token,
callerOwnerKey: params.callerOwnerKey,
});
if (direct) {
return direct;
}
const normalizedToken = normalizeOwnerKey(params.token);
const normalizedCallerOwnerKey = normalizeOwnerKey(params.callerOwnerKey);
if (!normalizedToken || normalizedToken !== normalizedCallerOwnerKey) {
return undefined;
}
return findLatestFlowForOwner({ callerOwnerKey: normalizedCallerOwnerKey });
}