Edit BadGatewayException

This commit is contained in:
2026-09-29 15:27:14 +03:30
parent 2309e2c5ab
commit 032eb70524
3 changed files with 146 additions and 49 deletions
@@ -180,13 +180,39 @@ export class ConversationsService {
await this.requireUse(userId, organizationId, role);
const creds = await this.getCredentials(organizationId);
const chatUserId = buildChatUserId(organizationId, userId);
const external = await this.chat.listUserRooms(creds.host, creds.apiKey, chatUserId);
const tracked = await this.prisma.conversationRoom.findMany({
where: { organizationId },
});
const trackedByExternal = new Map(tracked.map((t) => [t.externalRoomId, t]));
return external.map((row) => this.toRoomDto(row, trackedByExternal, organizationId, userId));
try {
const external = await this.chat.listUserRooms(creds.host, creds.apiKey, chatUserId);
return external.map((row) =>
this.toRoomDto(row, trackedByExternal, organizationId, userId),
);
} catch (e) {
// اگر سرویس چت در دسترس نبود، حداقل roomهای ثبت‌شده در CRM را نشان بده
if (tracked.length) {
return tracked.map((t) => ({
id: t.id,
externalRoomId: t.externalRoomId,
kind: t.kind === 'group' ? 'group' : 'dm',
name: t.name || (t.kind === 'group' ? 'گروه' : 'گفتگو'),
unreadCount: 0,
lastMessagePreview: null,
lastMessageAt: null,
peerUserId:
t.kind === 'dm'
? t.dmUserMinId === userId
? t.dmUserMaxId
: t.dmUserMaxId === userId
? t.dmUserMinId
: null
: null,
}));
}
throw e;
}
}
async createDm(
@@ -366,20 +392,26 @@ export class ConversationsService {
});
if (!row) {
throw new BadRequestException(
'تنظیمات گفتگو برای این محیط انجام نشده است. از بخش تنظیمات پیکربندی کنید.',
'تنظیمات گفتگو برای این محیط انجام نشده است. از سوپرادمین → محیط‌ها → تنظیمات گفتگو پیکربندی کنید.',
);
}
try {
const apiKey = this.crypto.decrypt({
ciphertext: row.apiKeyCipher,
iv: row.apiKeyIv,
tag: row.apiKeyTag,
});
return {
host: this.chat.normalizeHost(row.chatHost),
apiKey,
uploadUrl: row.uploadUrl,
downloadUrl: row.downloadUrl,
};
} catch {
throw new BadRequestException(
'رمزگشایی API Key گفتگو ناموفق بود. INTEGRATIONS_ENCRYPTION_KEY سرور را بررسی کنید یا تنظیمات را دوباره ذخیره کنید.',
);
}
const apiKey = this.crypto.decrypt({
ciphertext: row.apiKeyCipher,
iv: row.apiKeyIv,
tag: row.apiKeyTag,
});
return {
host: this.chat.normalizeHost(row.chatHost),
apiKey,
uploadUrl: row.uploadUrl,
downloadUrl: row.downloadUrl,
};
}
private toRoomDto(