/** * Centralized ProfileService for handling user profile operations * Used across: profile.js */ class ProfileService { constructor(apiBaseUrl) { this.apiBaseUrl = apiBaseUrl; } /** * Validate profile data before saving * @param {Object} profileData - The profile data to validate * @throws {Error} If validation fails */ validateProfile(profileData) { // Validate that email is provided const hasValidEmail = profileData.contact_email && profileData.contact_email.trim(); if (!hasValidEmail) { throw new Error('Email address is required'); } // Validate email format if provided if (hasValidEmail && !ValidationUtils.isValidEmail(profileData.contact_email.trim())) { throw new Error('Please enter a valid email address'); } // Validate phone format if provided if (hasValidPhone && !ValidationUtils.isValidPhone(profileData.contact_phone.trim())) { throw new Error('Please enter a valid phone number (e.g., +1-555-123-4567, (555) 123-4567, or 555.123.4567)'); } } /** * Update user profile * @param {Object} profileData - The profile data to update * @returns {Promise} The updated profile data * @throws {Error} If the update fails */ async updateProfile(profileData) { // Validate data first this.validateProfile(profileData); const { data } = await window.apiClient.put('/auth/profile', profileData); return data; } /** * Get user profile * @returns {Promise} The user profile data * @throws {Error} If the fetch fails */ async getProfile() { const { data } = await window.apiClient.get('/auth/profile'); return data; } /** * Get authentication status and user data * @returns {Promise} Auth status and user data */ async getAuthStatus() { const { data } = await window.apiClient.get('/auth/status'); return data; } /** * Helper method to check if user has required contact information * @param {Object} user - User object to check * @returns {boolean} True if user has email */ hasContactInfo(user) { if (!user) {return false;} const hasEmail = user.contact_email && user.contact_email.trim(); return !!hasEmail; } /** * Create form data object for profile updates * @param {Object} formElements - Object containing form element values * @returns {Object} Formatted profile data */ createProfileData(formElements) { return { first_name: formElements.first_name?.trim() || null, last_name: formElements.last_name?.trim() || null, contact_email: formElements.contact_email?.trim() || null, contact_phone: formElements.contact_phone?.trim() || null }; } } // Make ProfileService available globally window.ProfileService = ProfileService;