Fix: Use correct API path for test email
This commit is contained in:
@@ -1,199 +1,41 @@
|
||||
|
||||
import { pb, getCurrentEndpoint } from '@/lib/pocketbase';
|
||||
import { getSettings } from './actions/getSettings';
|
||||
import { updateSettings } from './actions/updateSettings';
|
||||
import { testEmailConnection } from './actions/testEmailConnection';
|
||||
import { testEmail } from './actions/testEmail';
|
||||
|
||||
const settingsApi = async (body: any) => {
|
||||
try {
|
||||
const { action, data } = body;
|
||||
console.log('Settings API called with action:', action, 'data:', data);
|
||||
/**
|
||||
* Settings API handler
|
||||
*/
|
||||
const settingsApi = async (body: any, path?: string) => {
|
||||
console.log('Settings API called with path:', path, 'body:', body);
|
||||
|
||||
// Handle test email endpoint specifically
|
||||
if (path === '/api/settings/test/email') {
|
||||
console.log('Handling test email request');
|
||||
return await testEmail(body);
|
||||
}
|
||||
|
||||
// Handle regular settings API with action-based routing
|
||||
const action = body?.action;
|
||||
console.log('Settings API called with action:', action, 'data:', body?.data);
|
||||
|
||||
const authToken = pb.authStore.token;
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
|
||||
if (authToken) {
|
||||
headers['Authorization'] = `Bearer ${authToken}`;
|
||||
}
|
||||
|
||||
const baseUrl = getCurrentEndpoint();
|
||||
|
||||
switch (action) {
|
||||
case 'getSettings':
|
||||
try {
|
||||
const response = await fetch(`${baseUrl}/api/settings`, {
|
||||
method: 'GET',
|
||||
headers,
|
||||
});
|
||||
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
||||
|
||||
const settings = await response.json();
|
||||
return {
|
||||
status: 200,
|
||||
json: { success: true, data: settings },
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error fetching settings:', error);
|
||||
return {
|
||||
status: 500,
|
||||
json: { success: false, message: 'Failed to fetch settings' },
|
||||
};
|
||||
}
|
||||
|
||||
case 'updateSettings':
|
||||
try {
|
||||
let response = await fetch(`${baseUrl}/api/settings`, {
|
||||
method: 'PATCH',
|
||||
headers,
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
|
||||
if (!response.ok && (response.status === 404 || response.status === 405)) {
|
||||
response = await fetch(`${baseUrl}/api/settings`, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
}
|
||||
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
||||
|
||||
const updatedSettings = await response.json();
|
||||
return {
|
||||
status: 200,
|
||||
json: { success: true, data: updatedSettings },
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error updating settings:', error);
|
||||
return {
|
||||
status: 500,
|
||||
json: { success: false, message: 'Failed to update settings' },
|
||||
};
|
||||
}
|
||||
|
||||
case 'testEmailConnection':
|
||||
try {
|
||||
const response = await fetch(`${baseUrl}/api/settings/test-email`, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
||||
|
||||
const result = await response.json();
|
||||
return {
|
||||
status: 200,
|
||||
json: {
|
||||
success: result.success || false,
|
||||
message:
|
||||
result.message || (result.success ? 'Connection successful' : 'Connection failed'),
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error testing email connection:', error);
|
||||
return {
|
||||
status: 500,
|
||||
json: { success: false, message: 'Failed to test email connection' },
|
||||
};
|
||||
}
|
||||
|
||||
case 'sendTestEmail':
|
||||
try {
|
||||
// Try different endpoints that might be available on the PocketBase server
|
||||
let response;
|
||||
|
||||
// First try: use admin API to send emails
|
||||
try {
|
||||
response = await fetch(`${baseUrl}/api/admins/auth-with-password`, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify({
|
||||
identity: data.email,
|
||||
password: "test" // This will fail but we just need to trigger email functionality
|
||||
}),
|
||||
});
|
||||
} catch (e) {
|
||||
// Expected to fail, this is just to test email functionality
|
||||
}
|
||||
|
||||
// Second try: use a verification request which should trigger email
|
||||
try {
|
||||
const collection = data.collection || '_superusers';
|
||||
response = await fetch(`${baseUrl}/api/collections/${collection}/request-verification`, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify({
|
||||
email: data.email
|
||||
}),
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
return {
|
||||
status: 200,
|
||||
json: {
|
||||
success: true,
|
||||
message: 'Test email sent successfully (verification request)',
|
||||
},
|
||||
};
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('Verification request failed, trying password reset...');
|
||||
}
|
||||
|
||||
// Third try: use password reset which should trigger email
|
||||
try {
|
||||
const collection = data.collection || '_superusers';
|
||||
response = await fetch(`${baseUrl}/api/collections/${collection}/request-password-reset`, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify({
|
||||
email: data.email
|
||||
}),
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
return {
|
||||
status: 200,
|
||||
json: {
|
||||
success: true,
|
||||
message: 'Test email sent successfully (password reset request)',
|
||||
},
|
||||
};
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('Password reset request failed');
|
||||
}
|
||||
|
||||
// If all specific endpoints fail, return a success message since we can't actually test
|
||||
// the email without a proper test endpoint
|
||||
return {
|
||||
status: 200,
|
||||
json: {
|
||||
success: true,
|
||||
message: 'SMTP configuration validated (actual email sending requires a user to exist)',
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error sending test email:', error);
|
||||
return {
|
||||
status: 500,
|
||||
json: { success: false, message: 'Failed to send test email' },
|
||||
};
|
||||
}
|
||||
|
||||
default:
|
||||
return {
|
||||
status: 400,
|
||||
json: { success: false, message: 'Invalid action' },
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Unexpected error in settingsApi:', error);
|
||||
return {
|
||||
status: 500,
|
||||
json: { success: false, message: 'Internal server error' },
|
||||
};
|
||||
switch (action) {
|
||||
case 'getSettings':
|
||||
return await getSettings();
|
||||
|
||||
case 'updateSettings':
|
||||
return await updateSettings(body.data);
|
||||
|
||||
case 'testEmailConnection':
|
||||
return await testEmailConnection(body.data);
|
||||
|
||||
default:
|
||||
console.error('Unknown action:', action);
|
||||
return {
|
||||
status: 400,
|
||||
json: { success: false, message: 'Unknown action' },
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user