UNPKG

@adventurelabs/scout-core

Version:

Core utilities and helpers for Adventure Labs Scout applications

122 lines (121 loc) 4.72 kB
"use server"; import { newServerClient } from "../supabase/server"; import { IWebResponse } from "../types/requests"; import { generateSignedUrlsBatch } from "./storage"; export async function server_get_artifacts_by_herd(herd_id, limit = 50, offset = 0, client) { const supabase = client || (await newServerClient()); const { data, error } = await supabase.rpc("get_artifacts_for_herd", { herd_id_caller: herd_id, limit_caller: limit, offset_caller: offset, }); if (error) { return IWebResponse.error(error.message).to_compatible(); } if (!data) { return IWebResponse.error("No artifacts found for herd").to_compatible(); } // Generate signed URLs for artifacts const uniqueFilePaths = Array.from(new Set(data .map((artifact) => artifact.file_path) .filter((path) => !!path))); if (uniqueFilePaths.length === 0) { return IWebResponse.success(data).to_compatible(); } const signedUrls = await generateSignedUrlsBatch(uniqueFilePaths, undefined, client); const urlMap = new Map(); uniqueFilePaths.forEach((path, index) => { urlMap.set(path, signedUrls[index]); }); const artifactsWithUrls = data.map((artifact) => ({ ...artifact, media_url: artifact.file_path ? urlMap.get(artifact.file_path) || null : null, })); return IWebResponse.success(artifactsWithUrls).to_compatible(); } export async function server_get_artifacts_by_device_id(device_id, limit = 50, offset = 0, client) { const supabase = client || (await newServerClient()); const { data, error } = await supabase.rpc("get_artifacts_for_device", { device_id_caller: device_id, limit_caller: limit, offset_caller: offset, }); if (error) { return IWebResponse.error(error.message).to_compatible(); } if (!data) { return IWebResponse.error("No artifacts found for device").to_compatible(); } // Generate signed URLs for artifacts const uniqueFilePaths = Array.from(new Set(data .map((artifact) => artifact.file_path) .filter((path) => !!path))); if (uniqueFilePaths.length === 0) { return IWebResponse.success(data).to_compatible(); } const signedUrls = await generateSignedUrlsBatch(uniqueFilePaths, undefined, client); const urlMap = new Map(); uniqueFilePaths.forEach((path, index) => { urlMap.set(path, signedUrls[index]); }); const artifactsWithUrls = data.map((artifact) => ({ ...artifact, media_url: artifact.file_path ? urlMap.get(artifact.file_path) || null : null, })); return IWebResponse.success(artifactsWithUrls).to_compatible(); } export async function server_get_total_artifacts_by_herd(herd_id) { const supabase = await newServerClient(); const { data, error } = await supabase.rpc("get_total_artifacts_for_herd", { herd_id_caller: herd_id, }); if (error) { return IWebResponse.error(error.message).to_compatible(); } return IWebResponse.success(data || 0).to_compatible(); } export async function server_get_artifacts_by_device_ids_batch(device_ids, limit_per_device = 10, client) { const supabase = client || (await newServerClient()); if (device_ids.length === 0) { return {}; } const { data, error } = await supabase.rpc("get_artifacts_for_devices_batch", { device_ids: device_ids, limit_per_device: limit_per_device, }); if (error || !data) { console.warn("Error fetching artifacts batch:", error?.message); return {}; } // Generate signed URLs for artifacts const uniqueFilePaths = Array.from(new Set(data .map((artifact) => artifact.file_path) .filter((path) => !!path))); let artifactsWithUrls = data; if (uniqueFilePaths.length > 0) { const signedUrls = await generateSignedUrlsBatch(uniqueFilePaths, undefined, client); const urlMap = new Map(); uniqueFilePaths.forEach((path, index) => { urlMap.set(path, signedUrls[index]); }); artifactsWithUrls = data.map((artifact) => ({ ...artifact, media_url: artifact.file_path ? urlMap.get(artifact.file_path) || null : null, })); } // Group artifacts by device_id const artifactsByDevice = {}; artifactsWithUrls.forEach((artifact) => { if (!artifactsByDevice[artifact.device_id]) { artifactsByDevice[artifact.device_id] = []; } artifactsByDevice[artifact.device_id].push(artifact); }); return artifactsByDevice; }