Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
48 lines
1.8 KiB
TypeScript
48 lines
1.8 KiB
TypeScript
// Квартиры для страницы /ru/apartments — из CRM (crm.yandp.tech), организация AREV.
|
|
// Ключ CRM знает только сервер лендинга: браузер ходит за фото и заявками через /api/flats/*.
|
|
|
|
const CRM = (process.env.CRM_URL ?? 'https://crm.yandp.tech').replace(/\/$/, '');
|
|
const KEY = process.env.CRM_BAREV_KEY ?? '';
|
|
|
|
export interface Flat {
|
|
id: number;
|
|
/** «Описание для клиента» из карточки объекта в CRM */
|
|
text: string;
|
|
dealType: 'rent' | 'sale' | null;
|
|
propertyType: string | null;
|
|
district: string | null;
|
|
price: number | null;
|
|
currency: string | null;
|
|
rooms: number | null;
|
|
area: number | null;
|
|
floor: number | null;
|
|
floorsTotal: number | null;
|
|
/** сколько фото; само фото — /api/flats/photo/<id>/<номер> */
|
|
photos: number;
|
|
}
|
|
|
|
export const crmConfigured = () => Boolean(KEY);
|
|
|
|
export function crmFetch(path: string, init: RequestInit & { next?: { revalidate: number } } = {}) {
|
|
return fetch(`${CRM}${path}`, {
|
|
...init,
|
|
headers: { ...(init.headers ?? {}), 'x-barev-key': KEY },
|
|
signal: init.signal ?? AbortSignal.timeout(20_000),
|
|
});
|
|
}
|
|
|
|
/** Объекты, по которым агент дозвонился и указал комиссию. null — CRM недоступна. */
|
|
export async function getFlats(): Promise<Flat[] | null> {
|
|
if (!KEY) return null;
|
|
try {
|
|
const res = await crmFetch('/api/barev/objects', {
|
|
next: { revalidate: 60 },
|
|
});
|
|
if (!res.ok) throw new Error(String(res.status));
|
|
return (await res.json()) as Flat[];
|
|
} catch (e) {
|
|
console.error('[flats] CRM недоступна:', e instanceof Error ? e.message : e);
|
|
return null;
|
|
}
|
|
}
|