Harden Tahesab HTTP client against keep-alive hangs and retry health on alt ports.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-10-03 17:02:14 +03:30
co-authored by Cursor
parent 17628722d3
commit beae16437b
3 changed files with 164 additions and 52 deletions
@@ -252,20 +252,51 @@ export class TahesabConnectionsService {
const { row, api } = await this.getApi(organizationId, id);
try {
const result = await api.checkHealth();
const patch: {
lastHealthAt: Date;
lastHealthOk: boolean;
lastError: string | null;
baseUrl?: string;
allowInsecureTls?: boolean;
} = {
lastHealthAt: new Date(),
lastHealthOk: true,
lastError: null,
};
// اگر health فقط با آدرس/پروتکل جایگزین جواب داد، همان را ذخیره کن
if (
result.usedBaseUrl &&
result.usedBaseUrl.replace(/\/+$/, '') !== row.baseUrl.replace(/\/+$/, '')
) {
patch.baseUrl = result.usedBaseUrl.replace(/\/+$/, '');
}
// اگر فقط با TLS بدون verify جواب داد، فلگ را روشن کن تا بقیهٔ متدها هم کار کنند
if (!row.allowInsecureTls && result.usedInsecureTls) {
patch.allowInsecureTls = true;
}
const updated = await this.prisma.accountingConnection.update({
where: { id: row.id },
data: {
lastHealthAt: new Date(),
lastHealthOk: true,
lastError: null,
},
data: patch,
});
return { ok: true, connection: this.toDto(updated), health: result };
return {
ok: true,
connection: this.toDto(updated),
health: result,
adjusted:
Boolean(patch.baseUrl) || Boolean(patch.allowInsecureTls)
? {
baseUrl: patch.baseUrl,
allowInsecureTls: patch.allowInsecureTls,
}
: undefined,
};
} catch (err) {
const message = (err as Error).message ?? 'خطای ناشناخته';
const hint =
/مهلت اتصال|timeout/i.test(message) && !row.allowInsecureTls && /^https:/i.test(row.baseUrl)
? ' اگر گواهی ته‌حساب خودامضاست، «پذیرش گواهی SSL خودامضا» را فعال کنید.'
/مهلت اتصال|timeout/i.test(message)
? ' از سرور CRM به IP/پورت ته‌حساب دسترسی شبکه لازم است (فایروال). برای https خودامضا گزینه SSL را فعال کنید. پورت پیش‌فرض: https 8081 یا http 9550.'
: '';
const full = `${message}${hint}`.slice(0, 500);
const updated = await this.prisma.accountingConnection.update({