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 });
});
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 {
const result = await AsaasService.testConnection();
const { asaasApiKey, asaasEnvironment } = req.body || {};
const result = await AsaasService.testConnection(asaasApiKey, asaasEnvironment);
res.json(result);
} catch (err: any) {
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...' });
try {
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();
setAsaasTestResult({

View File

@ -24,22 +24,26 @@ export class AsaasService {
/**
* Test the connection to Asaas API
*/
public static async testConnection(): Promise<{ success: boolean; message: string; environment: string }> {
const apiKey = this.getApiKey();
const env = loadDb().settings?.asaasEnvironment || 'sandbox';
public static async testConnection(customApiKey?: string, customEnv?: string): Promise<{ success: boolean; message: string; environment: string }> {
const apiKey = customApiKey || this.getApiKey();
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 {
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
};
}
try {
const response = await fetch(`${this.getApiUrl()}/customers?limit=1`, {
const response = await fetch(`${apiUrl}/customers?limit=1`, {
method: 'GET',
headers: this.getHeaders()
headers: {
'Content-Type': 'application/json',
'access_token': apiKey.trim()
}
});
if (response.ok) {