2025-11-01 03:17:59 +00:00
|
|
|
import { fetchApi } from '@/api/client'
|
|
|
|
|
|
|
|
|
|
export type Household = { id: number; name: string; slug: string }
|
|
|
|
|
|
|
|
|
|
export async function acceptInvitation(token: string): Promise<Household> {
|
|
|
|
|
const resp = await fetchApi('/api/v1/invitations/accept', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({ token }),
|
|
|
|
|
})
|
|
|
|
|
if (!resp.ok) throw new Error(`${resp.status} ${resp.statusText || 'HTTP error'}`)
|
|
|
|
|
const data = await resp.json().catch(() => null)
|
|
|
|
|
const h = data?.household
|
|
|
|
|
if (!h || typeof h.id !== 'number' || typeof h.slug !== 'string' || typeof h.name !== 'string') {
|
|
|
|
|
throw new Error('Invalid invitation accept response')
|
|
|
|
|
}
|
|
|
|
|
return { id: h.id, name: h.name, slug: h.slug }
|
|
|
|
|
}
|
2025-11-01 03:21:48 +00:00
|
|
|
|
|
|
|
|
export async function sendInvitation(email: string): Promise<void> {
|
|
|
|
|
const resp = await fetchApi('/api/v1/invitations', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({ email }),
|
|
|
|
|
})
|
|
|
|
|
if (!resp.ok && resp.status !== 204) throw new Error(`${resp.status} ${resp.statusText || 'HTTP error'}`)
|
|
|
|
|
}
|