/** * Profile API Module * * HTTP client methods for user profile operations. * Returns UserProfile domain VO from /auth/me. * * @layer Infrastructure - API Client */ import { apiClient } from '../../ApiClient'; import { ME, CHANGE_PASSWORD, RESEND_VERIFICATION, PROFILE_UPDATE } from '@archer/api-interface/endpoints/customer-api'; import type { UserResponse } from '@archer/api-interface/schemas/customer-api/response/auth/user.response'; import { UserProfile } from '@/domain/entities/UserProfile'; import type { ProfileFormData } from '@/domain/entities/UserProfile'; import { authenticatedUserStore } from '@/infrastructure/storage/AuthenticatedUserStore'; export type { ProfileFormData }; const syncStoreFromMeResponse = (res: UserResponse): void => { if (!res.account) return; // /auth/me returns the same account snapshot generator as /auth/login. Push // the refreshed slice into the store so banner/badge counts stay current. authenticatedUserStore.update({ account: { preferredCurrency: res.account.preferredCurrency, isProfileComplete: res.account.isProfileComplete, missingProfileFields: res.account.missingProfileFields, emailVerified: res.account.emailVerified, legal: res.account.legal, }, }); }; export const profileApi = { async getProfile(): Promise { const res = await apiClient.get(ME.path); syncStoreFromMeResponse(res); return UserProfile.fromApi({ id: res.id, email: res.email, firstName: res.profile?.firstName, lastName: res.profile?.lastName, phone: res.profile?.phone, locale: res.profile?.locale, timezone: res.profile?.timezone, emailNotVerified: res.emailNotVerified, companyType: res.currentCompanyType, isProfileComplete: res.isProfileComplete, missingProfileFields: res.missingProfileFields, }); }, async updateProfile(data: ProfileFormData): Promise { const res = await apiClient.put(PROFILE_UPDATE.path, data); return UserProfile.fromApi({ id: res.id, email: res.email, firstName: res.profile?.firstName, lastName: res.profile?.lastName, phone: res.profile?.phone, locale: res.profile?.locale, timezone: res.profile?.timezone, emailNotVerified: res.emailNotVerified, companyType: res.currentCompanyType, isProfileComplete: res.isProfileComplete, missingProfileFields: res.missingProfileFields, }); }, async changePassword(data: { currentPassword: string; newPassword: string }): Promise<{ message: string }> { return apiClient.post<{ message: string }>(CHANGE_PASSWORD.path, data); }, async resendVerification(): Promise<{ message: string }> { return apiClient.post<{ message: string }>(RESEND_VERIFICATION.path); }, };