/** * Data transformation functions for Reports API * Handles bidirectional mapping between frontend (Supabase) and backend (Django) formats */ import type { Report, LegacyReport, SubmitReportData, LegacySubmitReportData, } from './types'; /** * Map legacy frontend submission data to Django format * Converts Supabase field names to Django field names * * @param data - Report submission data with Supabase field names * @returns Report submission data with Django field names */ export function mapSubmitReportToBackend( data: LegacySubmitReportData ): SubmitReportData { return { entity_type: data.reported_entity_type, entity_id: data.reported_entity_id, report_type: data.report_type, description: data.reason || undefined, }; } /** * Map Django report response to legacy frontend format * Converts Django field names to Supabase field names for backward compatibility * Adds synthetic profile structures from email data * * @param report - Report data from Django API * @returns Report data in legacy Supabase format */ export function mapReportToLegacy(report: Report): LegacyReport { return { id: report.id, reported_entity_type: report.entity_type, reported_entity_id: report.entity_id, report_type: report.report_type, reason: report.description, status: report.status, reporter_id: report.reported_by_id, reviewed_by: report.reviewed_by_id, reviewed_at: report.reviewed_at, resolution_notes: report.resolution_notes, created_at: report.created_at, updated_at: report.updated_at, // Create synthetic profile objects from email data reporter_profile: report.reported_by_email ? { username: report.reported_by_email.split('@')[0], display_name: null, } : null, reviewer_profile: report.reviewed_by_email ? { username: report.reviewed_by_email.split('@')[0], display_name: null, } : null, }; } /** * Map array of Django reports to legacy format * Convenience function for bulk transformations * * @param reports - Array of reports from Django API * @returns Array of reports in legacy Supabase format */ export function mapReportsToLegacy(reports: Report[]): LegacyReport[] { return reports.map(mapReportToLegacy); } /** * Extract username from email address * Fallback for creating synthetic usernames * * @param email - Email address * @returns Username (part before @) or null */ export function extractUsernameFromEmail(email: string | null): string | null { if (!email) return null; return email.split('@')[0]; }