fix: allow testing Asaas connection with key currently entered in settings form before saving
Build and Deploy (Gitea) / build-and-deploy (push) Successful in 46s Details

This commit is contained in:
Sidney Gomes 2026-07-20 21:34:55 -03:00
parent ac24b3a2e6
commit 42f33a049f
3 changed files with 23 additions and 10 deletions

View File

@ -639,9 +639,10 @@ app.post('/api/admin/settings', authenticateToken, requireAdmin, (req: Authentic
res.json({ success: true, settings: db.settings }); res.json({ success: true, settings: db.settings });
}); });
app.get('/api/admin/test-asaas', authenticateToken, requireAdmin, async (req: AuthenticatedRequest, res: Response) => { app.post('/api/admin/test-asaas', authenticateToken, requireAdmin, async (req: AuthenticatedRequest, res: Response) => {
try { try {
const result = await AsaasService.testConnection(); const { asaasApiKey, asaasEnvironment } = req.body || {};
const result = await AsaasService.testConnection(asaasApiKey, asaasEnvironment);
res.json(result); res.json(result);
} catch (err: any) { } catch (err: any) {
res.status(500).json({ success: false, message: err.message || 'Erro ao testar conexão Asaas' }); res.status(500).json({ success: false, message: err.message || 'Erro ao testar conexão Asaas' });

View File

@ -128,7 +128,15 @@ export default function AdminDashboard({ token }: AdminDashboardProps) {
setAsaasTestResult({ loading: true, success: null, message: 'Testando conexão com Asaas...' }); setAsaasTestResult({ loading: true, success: null, message: 'Testando conexão com Asaas...' });
try { try {
const res = await fetch('/api/admin/test-asaas', { const res = await fetch('/api/admin/test-asaas', {
headers: { 'Authorization': `Bearer ${token}` } method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({
asaasApiKey: settings?.asaasApiKey,
asaasEnvironment: settings?.asaasEnvironment
})
}); });
const data = await res.json(); const data = await res.json();
setAsaasTestResult({ setAsaasTestResult({

View File

@ -24,22 +24,26 @@ export class AsaasService {
/** /**
* Test the connection to Asaas API * Test the connection to Asaas API
*/ */
public static async testConnection(): Promise<{ success: boolean; message: string; environment: string }> { public static async testConnection(customApiKey?: string, customEnv?: string): Promise<{ success: boolean; message: string; environment: string }> {
const apiKey = this.getApiKey(); const apiKey = customApiKey || this.getApiKey();
const env = loadDb().settings?.asaasEnvironment || 'sandbox'; const env = customEnv || loadDb().settings?.asaasEnvironment || 'sandbox';
const apiUrl = env === 'production' ? 'https://api.asaas.com/v3' : 'https://sandbox.asaas.com/v3';
if (!apiKey) { if (!apiKey || apiKey.trim() === '') {
return { return {
success: false, success: false,
message: 'Chave de API (API Key) não foi configurada.', message: 'A chave de API (API Key) não foi definida ou está em branco.',
environment: env environment: env
}; };
} }
try { try {
const response = await fetch(`${this.getApiUrl()}/customers?limit=1`, { const response = await fetch(`${apiUrl}/customers?limit=1`, {
method: 'GET', method: 'GET',
headers: this.getHeaders() headers: {
'Content-Type': 'application/json',
'access_token': apiKey.trim()
}
}); });
if (response.ok) { if (response.ok) {