mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-14 23:01:09 +02:00
fix(manacore-web): handle contacts API response format
The Contacts API returns { contacts: Contact[], total: number }
but the service was treating it as a direct Contact[] array.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
bf5df284ff
commit
212f4992c1
1 changed files with 25 additions and 10 deletions
|
|
@ -67,6 +67,14 @@ export interface ContactActivity {
|
|||
createdAt: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* API response format from Contacts backend
|
||||
*/
|
||||
interface ContactsApiResponse {
|
||||
contacts: Contact[];
|
||||
total: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Contacts service for dashboard widgets
|
||||
*/
|
||||
|
|
@ -75,22 +83,29 @@ export const contactsService = {
|
|||
* Get favorite contacts
|
||||
*/
|
||||
async getFavoriteContacts(limit: number = 5): Promise<ApiResult<Contact[]>> {
|
||||
const result = await getClient().get<Contact[]>(`/contacts?isFavorite=true&limit=${limit}`);
|
||||
return result;
|
||||
const result = await getClient().get<ContactsApiResponse>(
|
||||
`/contacts?isFavorite=true&limit=${limit}`
|
||||
);
|
||||
|
||||
if (result.error || !result.data) {
|
||||
return { data: null, error: result.error };
|
||||
}
|
||||
|
||||
return { data: result.data.contacts || [], error: null };
|
||||
},
|
||||
|
||||
/**
|
||||
* Get recent contacts (by updatedAt)
|
||||
*/
|
||||
async getRecentContacts(limit: number = 5): Promise<ApiResult<Contact[]>> {
|
||||
const result = await getClient().get<Contact[]>(`/contacts?limit=${limit}`);
|
||||
const result = await getClient().get<ContactsApiResponse>(`/contacts?limit=${limit}`);
|
||||
|
||||
if (result.error || !result.data) {
|
||||
return result;
|
||||
return { data: null, error: result.error };
|
||||
}
|
||||
|
||||
// Sort by updatedAt and filter archived
|
||||
const sorted = result.data
|
||||
const sorted = (result.data.contacts || [])
|
||||
.filter((c) => !c.isArchived)
|
||||
.sort((a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime())
|
||||
.slice(0, limit);
|
||||
|
|
@ -102,16 +117,16 @@ export const contactsService = {
|
|||
* Get contacts with upcoming birthdays
|
||||
*/
|
||||
async getUpcomingBirthdays(days: number = 30): Promise<ApiResult<Contact[]>> {
|
||||
const result = await getClient().get<Contact[]>('/contacts');
|
||||
const result = await getClient().get<ContactsApiResponse>('/contacts');
|
||||
|
||||
if (result.error || !result.data) {
|
||||
return result;
|
||||
return { data: null, error: result.error };
|
||||
}
|
||||
|
||||
const today = new Date();
|
||||
const futureDate = new Date(Date.now() + days * 24 * 60 * 60 * 1000);
|
||||
|
||||
const withBirthdays = result.data.filter((c) => {
|
||||
const withBirthdays = (result.data.contacts || []).filter((c) => {
|
||||
if (!c.birthday || c.isArchived) return false;
|
||||
|
||||
const birthday = new Date(c.birthday);
|
||||
|
|
@ -133,13 +148,13 @@ export const contactsService = {
|
|||
* Get contact count
|
||||
*/
|
||||
async getContactCount(): Promise<ApiResult<{ total: number; favorites: number }>> {
|
||||
const result = await getClient().get<Contact[]>('/contacts');
|
||||
const result = await getClient().get<ContactsApiResponse>('/contacts');
|
||||
|
||||
if (result.error || !result.data) {
|
||||
return { data: null, error: result.error };
|
||||
}
|
||||
|
||||
const active = result.data.filter((c) => !c.isArchived);
|
||||
const active = (result.data.contacts || []).filter((c) => !c.isArchived);
|
||||
const favorites = active.filter((c) => c.isFavorite);
|
||||
|
||||
return { data: { total: active.length, favorites: favorites.length }, error: null };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue