Files
SIB/frontend/src/stores/favorites.ts
Hanzo_dev 84055a25de refactor: migrate fully to Supabase, remove Firebase/Render/Python backend
- DELETED: entire backend/ (Python/FastAPI — replaced by Supabase)
- DELETED: old/ directory (obsolete code)
- DELETED: render.yaml, inject_api.py, check_tags.py, PENDING_FOR_TOMORROW.md
- DELETED: frontend/src/firebaseConfig.ts (Firebase Auth replaced by Supabase Auth)
- DELETED: frontend/src/services/apiClient.ts (HTTP client for dead backend)

- MIGRATED services to Supabase native:
  schedulesService, favoritesService, usersService,
  telemetryService (stub), reportsService, analyticsService (stub)

- MIGRATED stores/favorites.ts to Supabase direct queries
- MIGRATED views: SplashScreen, AdminTaxis, AdminDrivers, StrategicAnalytics
- MIGRATED utils/imageUrl.ts to Supabase Storage URLs

- FIXED router/index.ts: guard now uses supabase.auth.getSession()
  instead of old localStorage auth_token (fixes logout + map loading)
- FIXED AuthView.vue: removed aggressive watch({ immediate: true })
  that caused wrong redirects on map route
- FIXED SplashScreen.vue: navigate() now reads Supabase session + role
- FIXED RLS: added INSERT policy on public.users for trigger
- CONFIRMED: admin@sibu.com assigned ADMIN role in Supabase
2026-02-25 21:49:26 -05:00

120 lines
4.0 KiB
TypeScript

import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { supabase } from '@/supabase'
export interface Favorite {
id: string
user_id: string
item_type: 'coupon' | 'business' | 'taxi' | 'route' | 'stop'
item_id: string
item_name?: string
item_image?: string
created_at: string
}
export const useFavoritesStore = defineStore('favorites', () => {
const favorites = ref<Favorite[]>([])
const isLoading = ref(false)
const coupons = computed(() => favorites.value.filter(f => f.item_type === 'coupon'))
const businesses = computed(() => favorites.value.filter(f => f.item_type === 'business'))
const taxis = computed(() => favorites.value.filter(f => f.item_type === 'taxi'))
const routes = computed(() => favorites.value.filter(f => f.item_type === 'route'))
const stops = computed(() => favorites.value.filter(f => f.item_type === 'stop'))
async function loadFavorites() {
isLoading.value = true
try {
const { data: userData } = await supabase.auth.getUser()
if (!userData?.user) { favorites.value = []; return }
const { data, error } = await supabase
.from('favorites')
.select('*')
.eq('user_id', userData.user.id)
if (error) throw error
favorites.value = data as Favorite[]
} catch (error) {
console.error('Error loading favorites:', error)
} finally {
isLoading.value = false
}
}
async function addFavorite(
itemType: 'coupon' | 'business' | 'taxi' | 'route' | 'stop',
itemId: string,
itemName?: string,
itemImage?: string
) {
try {
const { data: userData } = await supabase.auth.getUser()
if (!userData?.user) throw new Error('Not authenticated')
const { data, error } = await supabase.from('favorites').insert([{
user_id: userData.user.id,
item_type: itemType,
item_id: itemId,
item_name: itemName,
item_image: itemImage
}]).select().single()
if (error) throw error
favorites.value.unshift(data as Favorite)
return true
} catch (error) {
console.error('Error adding favorite:', error)
throw error
}
}
async function removeFavorite(itemType: string, itemId: string) {
try {
const { data: userData } = await supabase.auth.getUser()
if (!userData?.user) throw new Error('Not authenticated')
const { error } = await supabase.from('favorites')
.delete()
.eq('user_id', userData.user.id)
.eq('item_type', itemType)
.eq('item_id', itemId)
if (error) throw error
favorites.value = favorites.value.filter(
f => !(f.item_type === itemType && f.item_id === itemId)
)
return true
} catch (error) {
console.error('Error removing favorite:', error)
throw error
}
}
async function toggleFavorite(
itemType: 'coupon' | 'business' | 'taxi' | 'route' | 'stop',
itemId: string,
itemName?: string,
itemImage?: string
) {
const existing = favorites.value.find(
f => f.item_type === itemType && f.item_id === itemId
)
if (existing) {
await removeFavorite(itemType, itemId)
return false
} else {
await addFavorite(itemType, itemId, itemName, itemImage)
return true
}
}
function isFavorite(itemType: string, itemId: string): boolean {
return favorites.value.some(
f => f.item_type === itemType && f.item_id === itemId
)
}
return {
favorites, isLoading, coupons, businesses, taxis, routes, stops,
loadFavorites, addFavorite, removeFavorite, toggleFavorite, isFavorite
}
})