🚀 Feat(Core): Migración oficial y total B a Supabase (Auth, DB, APIs) + Eliminación de Backend Python y Fixes Firebase + Soporte Vite Vercel
This commit is contained in:
@ -1,42 +1,82 @@
|
||||
/** Service for business-related API calls */
|
||||
import { apiClient } from './apiClient'
|
||||
import { supabase } from '@/supabase'
|
||||
import type { Business } from '@/types'
|
||||
|
||||
export const businessService = {
|
||||
/** Helper to upload file to supabase storage */
|
||||
async uploadImage(file: File): Promise<string> {
|
||||
const fileExt = file.name.split('.').pop()
|
||||
const fileName = `${Math.random()}-${Date.now()}.${fileExt}`
|
||||
const filePath = `businesses/${fileName}`
|
||||
|
||||
const { error } = await supabase.storage.from('uploads').upload(filePath, file)
|
||||
if (error) throw new Error(error.message)
|
||||
|
||||
const { data } = supabase.storage.from('uploads').getPublicUrl(filePath)
|
||||
return data.publicUrl
|
||||
},
|
||||
|
||||
/** Get all businesses */
|
||||
async getAllBusinesses(): Promise<Business[]> {
|
||||
const response = await apiClient.get<Business[]>('/api/businesses')
|
||||
return response.data
|
||||
const { data, error } = await supabase.from('businesses').select('*')
|
||||
if (error) throw new Error(error.message)
|
||||
return data as Business[]
|
||||
},
|
||||
|
||||
/** Get a single business by ID */
|
||||
async getBusiness(id: string): Promise<Business> {
|
||||
const response = await apiClient.get<Business>(`/api/businesses/${id}`)
|
||||
return response.data
|
||||
const { data, error } = await supabase.from('businesses').select('*').eq('id', id).single()
|
||||
if (error) throw new Error(error.message)
|
||||
return data as Business
|
||||
},
|
||||
|
||||
/** Create a new business */
|
||||
async createBusiness(businessData: FormData): Promise<Business> {
|
||||
const response = await apiClient.post<Business>('/api/businesses', businessData, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data'
|
||||
const payload: any = {}
|
||||
let fileUpload: File | null = null
|
||||
|
||||
businessData.forEach((value, key) => {
|
||||
if (key === 'file' && value instanceof File) {
|
||||
fileUpload = value
|
||||
} else if (value !== 'null' && value !== '') {
|
||||
payload[key] = value
|
||||
}
|
||||
})
|
||||
return response.data
|
||||
|
||||
if (fileUpload) {
|
||||
payload.image_url = await this.uploadImage(fileUpload)
|
||||
}
|
||||
|
||||
const { data, error } = await supabase.from('businesses').insert([payload]).select().single()
|
||||
if (error) throw new Error(error.message)
|
||||
return data as Business
|
||||
},
|
||||
|
||||
/** Update an existing business */
|
||||
async updateBusiness(id: string, businessData: FormData): Promise<Business> {
|
||||
const response = await apiClient.patch<Business>(`/api/businesses/${id}`, businessData, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data'
|
||||
const payload: any = {}
|
||||
let fileUpload: File | null = null
|
||||
|
||||
businessData.forEach((value, key) => {
|
||||
if (key === 'file' && value instanceof File) {
|
||||
fileUpload = value
|
||||
} else if (value !== 'null' && value !== '') {
|
||||
payload[key] = value
|
||||
}
|
||||
})
|
||||
return response.data
|
||||
|
||||
if (fileUpload) {
|
||||
payload.image_url = await this.uploadImage(fileUpload)
|
||||
}
|
||||
|
||||
const { data, error } = await supabase.from('businesses').update(payload).eq('id', id).select().single()
|
||||
if (error) throw new Error(error.message)
|
||||
return data as Business
|
||||
},
|
||||
|
||||
/** Delete a business */
|
||||
async deleteBusiness(id: string): Promise<void> {
|
||||
await apiClient.delete(`/api/businesses/${id}`)
|
||||
const { error } = await supabase.from('businesses').delete().eq('id', id)
|
||||
if (error) throw new Error(error.message)
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user