Fix: Use correct API path for test email

This commit is contained in:
Tola Leng
2025-06-17 21:50:46 +08:00
parent 1daaf067d9
commit 3a6ab2258a
+21 -179
View File
@@ -1,198 +1,40 @@
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 { * Settings API handler
const { action, data } = body; */
console.log('Settings API called with action:', action, 'data:', data); const settingsApi = async (body: any, path?: string) => {
console.log('Settings API called with path:', path, 'body:', body);
const authToken = pb.authStore.token; // Handle test email endpoint specifically
const headers: Record<string, string> = { if (path === '/api/settings/test/email') {
'Content-Type': 'application/json', console.log('Handling test email request');
}; return await testEmail(body);
if (authToken) {
headers['Authorization'] = `Bearer ${authToken}`;
} }
const baseUrl = getCurrentEndpoint(); // Handle regular settings API with action-based routing
const action = body?.action;
console.log('Settings API called with action:', action, 'data:', body?.data);
switch (action) { switch (action) {
case 'getSettings': case 'getSettings':
try { return await getSettings();
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': case 'updateSettings':
try { return await updateSettings(body.data);
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': case 'testEmailConnection':
try { return await testEmailConnection(body.data);
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: default:
console.error('Unknown action:', action);
return { return {
status: 400, status: 400,
json: { success: false, message: 'Invalid action' }, json: { success: false, message: 'Unknown action' },
};
}
} catch (error) {
console.error('Unexpected error in settingsApi:', error);
return {
status: 500,
json: { success: false, message: 'Internal server error' },
}; };
} }
}; };