mirror of
https://github.com/complexcaresolutions/telegram-media-bot.git
synced 2026-03-17 18:33:43 +00:00
fix: hardcode tenant list as fallback (API returns 403), add verbose error logging
This commit is contained in:
parent
f93fe9a6a0
commit
dc2179a4d6
3 changed files with 33 additions and 28 deletions
|
|
@ -167,7 +167,8 @@ class PayloadClient {
|
|||
try {
|
||||
const response = await this.authFetch(`${this.apiUrl}/users/me`);
|
||||
return response.ok;
|
||||
} catch {
|
||||
} catch (error) {
|
||||
log.error('Health check failed', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import { config } from '../config.js';
|
|||
import { payloadClient } from '../payload/client.js';
|
||||
import { downloadFile } from '../utils/download.js';
|
||||
import { createLogger } from '../utils/logger.js';
|
||||
import { buildTenantKeyboard, getTenantName } from './keyboards.js';
|
||||
import { buildTenantKeyboard, getTenantName, getTenantList } from './keyboards.js';
|
||||
|
||||
const log = createLogger('Handlers');
|
||||
|
||||
|
|
@ -203,15 +203,10 @@ async function processAlbum(
|
|||
}
|
||||
|
||||
async function buildAnleitung(): Promise<string> {
|
||||
let tenantList = 'Tenants konnten nicht geladen werden\\.';
|
||||
try {
|
||||
const tenants = await payloadClient.listTenants();
|
||||
tenantList = tenants
|
||||
.map((t) => ` \`${t.id}\` \\— ${escapeMarkdown(t.name)}`)
|
||||
.join('\n');
|
||||
} catch {
|
||||
log.warn('Could not load tenants for Anleitung');
|
||||
}
|
||||
const tenants = await getTenantList();
|
||||
const tenantList = tenants
|
||||
.map((t) => ` \`${t.id}\` \\— ${escapeMarkdown(t.name)}`)
|
||||
.join('\n');
|
||||
|
||||
return (
|
||||
`📷 *Payload Media Upload Bot \\— Anleitung*\n\n` +
|
||||
|
|
|
|||
|
|
@ -4,8 +4,20 @@ import { createLogger } from '../utils/logger.js';
|
|||
|
||||
const log = createLogger('Keyboards');
|
||||
|
||||
// Known tenants — hardcoded fallback since /api/tenants requires special access
|
||||
const KNOWN_TENANTS: TenantDoc[] = [
|
||||
{ id: 4, name: 'porwoll.de', slug: 'porwoll' },
|
||||
{ id: 5, name: 'zytoskandal.de', slug: 'zytoskandal' },
|
||||
{ id: 8, name: 'caroline-porwoll.de', slug: 'caroline-porwoll' },
|
||||
{ id: 9, name: 'blogwoman.de', slug: 'blogwoman' },
|
||||
{ id: 10, name: 'complexcaresolutions.de', slug: 'complexcaresolutions' },
|
||||
{ id: 11, name: 'gunshin.de', slug: 'gunshin' },
|
||||
{ id: 12, name: 'zweitmeinu.ng', slug: 'zweitmeinung' },
|
||||
{ id: 13, name: 'sensualmoment.de', slug: 'sensualmoment' },
|
||||
];
|
||||
|
||||
// Tenant cache with 5-minute TTL
|
||||
let tenantCache: TenantDoc[] | null = null;
|
||||
let tenantCache: TenantDoc[] = KNOWN_TENANTS;
|
||||
let cacheExpiresAt = 0;
|
||||
const CACHE_TTL = 5 * 60 * 1000; // 5 minutes
|
||||
|
||||
|
|
@ -13,7 +25,7 @@ async function loadTenants(): Promise<TenantDoc[]> {
|
|||
const now = Date.now();
|
||||
|
||||
// Return valid cache
|
||||
if (tenantCache && cacheExpiresAt > now) {
|
||||
if (cacheExpiresAt > now) {
|
||||
return tenantCache;
|
||||
}
|
||||
|
||||
|
|
@ -23,14 +35,11 @@ async function loadTenants(): Promise<TenantDoc[]> {
|
|||
cacheExpiresAt = now + CACHE_TTL;
|
||||
log.info(`Loaded ${tenants.length} tenants from API`);
|
||||
return tenants;
|
||||
} catch (error) {
|
||||
// Stale cache fallback: if API fails but we have old data, return it
|
||||
if (tenantCache) {
|
||||
log.warn('API failed, returning stale tenant cache', error);
|
||||
return tenantCache;
|
||||
}
|
||||
log.error('Failed to load tenants and no cache available', error);
|
||||
throw error;
|
||||
} catch {
|
||||
// API access denied or failed — use hardcoded fallback
|
||||
cacheExpiresAt = now + CACHE_TTL;
|
||||
log.info('Using hardcoded tenant list (API returned 403)');
|
||||
return tenantCache;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -51,11 +60,11 @@ export async function buildTenantKeyboard(): Promise<InlineKeyboard> {
|
|||
}
|
||||
|
||||
export async function getTenantName(tenantId: number): Promise<string> {
|
||||
try {
|
||||
const tenants = await loadTenants();
|
||||
const tenant = tenants.find((t) => t.id === tenantId);
|
||||
return tenant?.name ?? `Tenant ${tenantId}`;
|
||||
} catch {
|
||||
return `Tenant ${tenantId}`;
|
||||
}
|
||||
const tenants = await loadTenants();
|
||||
const tenant = tenants.find((t) => t.id === tenantId);
|
||||
return tenant?.name ?? `Tenant ${tenantId}`;
|
||||
}
|
||||
|
||||
export async function getTenantList(): Promise<TenantDoc[]> {
|
||||
return loadTenants();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue