import { http, getAccessToken } from './client'; import type { Company, CompanyMember, Profile } from '../types'; interface Notification { id: string; type: string; title: string; content: string; is_read: boolean; created_at: string; } interface CompanyRelationship { id: string; company: Company; role: string; } export const commonApi = { getCompany: (id: string) => http.get(`/common/companies/${id}`).then(r => r.data), listRelationships: () => http.get('/common/companies').then(r => r.data), listMembers: () => http.get<(CompanyMember & { profile: Profile })[]>('/common/members').then(r => r.data), createMember: (data: { username: string; password: string; display_name: string; phone?: string }) => http.post('/common/members', data).then(r => r.data), deleteMember: (id: string) => http.delete(`/common/members/${id}`), listNotifications: () => http.get('/common/notifications').then(r => r.data), markNotificationRead: (id: string) => http.patch(`/common/notifications/${id}`, { is_read: true }), createShareLink: (data: { resource_type: string; resource_id: string }) => http.post<{ code: string }>('/common/share-links', data).then(r => r.data), getShareLink: (code: string) => http.get(`/common/share-links/${code}`).then(r => r.data), submitFeedback: (data: { content: string }) => http.post('/common/feedback', data), uploadFile: async (file: File, bucket: string, pathPrefix: string): Promise => { const formData = new FormData(); formData.append('file', file); formData.append('bucket', bucket); formData.append('path_prefix', pathPrefix); if (!getAccessToken()) { const { authApi } = await import('./auth'); try { await authApi.refresh(); } catch { /* will fail on upload */ } } const doUpload = async () => { const token = getAccessToken(); const headers: Record = {}; if (token) headers['Authorization'] = `Bearer ${token}`; return fetch('/api/v1/common/upload', { method: 'POST', headers, body: formData, credentials: 'include', }); }; let res = await doUpload(); if (res.status === 401) { const { authApi } = await import('./auth'); try { await authApi.refresh(); } catch { throw new Error('auth expired'); } res = await doUpload(); } if (!res.ok) { throw new Error('upload failed'); } const json = await res.json(); if (json.code !== 0) { throw new Error(json.message || 'upload failed'); } return json.data?.url || ''; }, };