33 lines
1.2 KiB
TypeScript
33 lines
1.2 KiB
TypeScript
import type { Contact, ContactWrite } from './dto/contact'
|
|
import type { HydraCollection } from '~/utils/api'
|
|
import { extractHydraMembers } from '~/utils/api'
|
|
|
|
type Owner = { client?: string, prospect?: string }
|
|
|
|
export function useContactService() {
|
|
const api = useApi()
|
|
|
|
async function getByOwner(owner: Owner): Promise<Contact[]> {
|
|
const data = await api.get<HydraCollection<Contact>>('/contacts', owner as Record<string, unknown>)
|
|
return extractHydraMembers(data)
|
|
}
|
|
|
|
async function create(payload: ContactWrite): Promise<Contact> {
|
|
return api.post<Contact>('/contacts', payload as Record<string, unknown>, {
|
|
toastSuccessKey: 'directory.contacts.saved',
|
|
})
|
|
}
|
|
|
|
async function update(id: number, payload: Partial<ContactWrite>): Promise<Contact> {
|
|
return api.patch<Contact>(`/contacts/${id}`, payload as Record<string, unknown>, {
|
|
toastSuccessKey: 'directory.contacts.saved',
|
|
})
|
|
}
|
|
|
|
async function remove(id: number): Promise<void> {
|
|
await api.delete(`/contacts/${id}`, {}, { toastSuccessKey: 'directory.contacts.deleted' })
|
|
}
|
|
|
|
return { getByOwner, create, update, remove }
|
|
}
|