| 33 | } |
| 34 | |
| 35 | class MissionsService { |
| 36 | private supabase = createSupabaseClient(); |
| 37 | |
| 38 | async getMissions(): Promise<MissionWithWaypoints[]> { |
| 39 | const { data, error } = await this.supabase |
| 40 | .from('missions') |
| 41 | .select(` |
| 42 | *, |
| 43 | waypoints (*) |
| 44 | `) |
| 45 | .order('created_at', { ascending: false }); |
| 46 | |
| 47 | if (error) { |
| 48 | console.error('Error fetching missions:', error); |
| 49 | throw error; |
| 50 | } |
| 51 | |
| 52 | // Sort waypoints by order_index |
| 53 | const missions = data?.map(mission => ({ |
| 54 | ...mission, |
| 55 | waypoints: mission.waypoints.sort((a: Waypoint, b: Waypoint) => a.order_index - b.order_index) |
| 56 | })) || []; |
| 57 | |
| 58 | return missions; |
| 59 | } |
| 60 | |
| 61 | async getMission(id: string): Promise<MissionWithWaypoints | null> { |
| 62 | const { data, error } = await this.supabase |
| 63 | .from('missions') |
| 64 | .select(` |
| 65 | *, |
| 66 | waypoints (*) |
| 67 | `) |
| 68 | .eq('id', id) |
| 69 | .single(); |
| 70 | |
| 71 | if (error) { |
| 72 | console.error('Error fetching mission:', error); |
| 73 | throw error; |
| 74 | } |
| 75 | |
| 76 | if (data) { |
| 77 | // Sort waypoints by order_index |
| 78 | data.waypoints = data.waypoints.sort((a: Waypoint, b: Waypoint) => a.order_index - b.order_index); |
| 79 | } |
| 80 | |
| 81 | return data; |
| 82 | } |
| 83 | |
| 84 | async createMission(mission: Omit<MissionInsert, 'user_id'>): Promise<Mission> { |
| 85 | const { data: { user } } = await this.supabase.auth.getUser(); |
| 86 | |
| 87 | if (!user) { |
| 88 | throw new Error('User not authenticated'); |
| 89 | } |
| 90 | |
| 91 | const { data, error } = await this.supabase |
| 92 | .from('missions') |
nothing calls this directly
no test coverage detected