UNPKG

@adventurelabs/scout-core

Version:

Core utilities and helpers for Adventure Labs Scout applications

159 lines (158 loc) 5.65 kB
import { IWebResponse } from "../types/requests"; export async function get_pins_for_herd(client, herd_id) { // Call get_pins_for_herd with rpc - returns pins_pretty_location with extracted coordinates const { data, error } = await client.rpc("get_pins_for_herd", { herd_id_caller: herd_id, }); if (error) { return IWebResponse.error(error.message).to_compatible(); } if (!data) { return IWebResponse.error("No pins found for herd").to_compatible(); } return IWebResponse.success(data).to_compatible(); } export async function get_pin_by_id(client, pin_id) { const { data, error } = await client .from("pins") .select("*") .eq("id", pin_id) .single(); if (error) { return IWebResponse.error(error.message).to_compatible(); } if (!data) { return IWebResponse.error("Pin not found").to_compatible(); } // Raw table data - no coordinate extraction (use get_pins_for_herd for coordinates) return IWebResponse.success(data).to_compatible(); } export async function create_pin(client, newPin) { const { data, error } = await client .from("pins") .insert([newPin]) .select("*") .single(); if (error) { return IWebResponse.error(error.message).to_compatible(); } if (!data) { return IWebResponse.error("Failed to create pin").to_compatible(); } // Raw table data - coordinates extracted by RPC function or realtime broadcasts return IWebResponse.success(data).to_compatible(); } export async function create_pin_with_coordinates(client, latitude, longitude, pinData) { // Create pin with PostGIS Point from lat/lng coordinates const { data, error } = await client .from("pins") .insert([ { ...pinData, // Use PostGIS ST_MakePoint to create geography from coordinates // Note: PostGIS Point format is (longitude, latitude) location: `SRID=4326;POINT(${longitude} ${latitude})`, }, ]) .select("*") .single(); if (error) { return IWebResponse.error(error.message).to_compatible(); } if (!data) { return IWebResponse.error("Failed to create pin").to_compatible(); } return IWebResponse.success(data).to_compatible(); } export async function update_pin(client, pin_id, updatedPin) { // Remove fields that shouldn't be updated const updateData = { ...updatedPin }; delete updateData.id; delete updateData.created_at; delete updateData.created_by; // RLS handles permissions const { data, error } = await client .from("pins") .update(updateData) .eq("id", pin_id) .select("*") .single(); if (error) { return IWebResponse.error(error.message).to_compatible(); } if (!data) { return IWebResponse.error("Pin not found or update failed").to_compatible(); } // Raw table data - coordinates extracted by RPC function or realtime broadcasts return IWebResponse.success(data).to_compatible(); } export async function update_pin_location(client, pin_id, latitude, longitude, updatedPin) { const updateData = { ...updatedPin, // Use PostGIS format to update location location: `SRID=4326;POINT(${longitude} ${latitude})`, }; // Remove fields that shouldn't be updated delete updateData.id; delete updateData.created_at; delete updateData.created_by; const { data, error } = await client .from("pins") .update(updateData) .eq("id", pin_id) .select("*") .single(); if (error) { return IWebResponse.error(error.message).to_compatible(); } if (!data) { return IWebResponse.error("Pin not found or update failed").to_compatible(); } return IWebResponse.success(data).to_compatible(); } export async function delete_pin(client, pin_id) { const { data, error } = await client .from("pins") .delete() .eq("id", pin_id) .select("*") .single(); if (error) { return IWebResponse.error(error.message).to_compatible(); } if (!data) { return IWebResponse.error("Pin not found or deletion failed").to_compatible(); } // Raw table data - coordinates extracted by realtime broadcasts return IWebResponse.success(data).to_compatible(); } export async function get_pins_by_created_by(client, user_id) { const { data, error } = await client .from("pins") .select("*") .eq("created_by", user_id) .order("created_at", { ascending: false }); if (error) { return IWebResponse.error(error.message).to_compatible(); } if (!data) { return IWebResponse.error("No pins found for user").to_compatible(); } // Raw table data without extracted coordinates - use get_pins_for_herd for coordinates return IWebResponse.success(data).to_compatible(); } export async function get_pins_by_color(client, herd_id, color) { const { data, error } = await client .from("pins") .select("*") .eq("herd_id", herd_id) .eq("color", color) .order("created_at", { ascending: false }); if (error) { return IWebResponse.error(error.message).to_compatible(); } if (!data) { return IWebResponse.error(`No pins found with color: ${color}`).to_compatible(); } // Raw table data without extracted coordinates - use get_pins_for_herd for coordinates return IWebResponse.success(data).to_compatible(); }