All files / src/api log-api.ts

94.2% Statements 65/69
66.66% Branches 6/9
100% Functions 2/2
94.2% Lines 65/69

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 701x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x     5x 5x     5x 5x 5x 5x 5x 5x 5x 1x 1x 4x 4x 4x 4x 5x 1x 1x 1x 1x 1x 5x  
import { LOG_ENDPOINT } from "./types/endpoints";
import { LogData } from "./types/event";
import { convertToSnakeCase } from "../utils";
 
 
/**
 * Logs a user interaction event related to an AI-generated suggestion.
 *
 * Converts the event data to snake_case and sends it to the backend logging service.
 *
 * @param logData - The data describing the user event to be logged.
 */
export function trackEvent(logData: LogData) {
  const logDataForBackend = convertToSnakeCase(logData);
 
  console.log("Logging data for event:", logDataForBackend.event);
 
  fetch(LOG_ENDPOINT, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(logDataForBackend),
  }).catch((err) => console.error("Failed to log data:", err));
}
 
/**
 * Fetches all logged user interaction events for a specific user.
 *
 * Supports optional filtering by user section ID and/or user class ID.
 *
 * @param userId - The unique ID of the user whose logs should be fetched.
 * @param userSectionId - (Optional) The section ID to filter logs by user section.
 * @param userClassId - (Optional) The class ID to filter logs by user class.
 * @returns A promise resolving to an object containing either the logs array or an error message.
 */
export async function getLogsByUser(
  userId: string,
  userSectionId?: string,
  userClassId?: string
): Promise<{ logs?: LogData[]; error?: string }> {
  try {
    const url = new URL(`${LOG_ENDPOINT}/${userId}`);
 
    if (userSectionId) {
      url.searchParams.append("user_section_id", userSectionId);
    }
 
    if (userClassId) {
      url.searchParams.append("user_class_id", userClassId);
    }
 
    const response = await fetch(url.toString(), {
      method: "GET",
      headers: { "Content-Type": "application/json" },
    });
 
    if (!response.ok) {
      throw new Error(`Failed to fetch logs: ${response.statusText}`);
    }
 
    const data = await response.json();
 
    return { logs: data.data };
  } catch (error) {
    console.error("Error fetching logs:", error);
    return {
      error: error instanceof Error ? error.message : "Unknown error occurred",
    };
  }
}