import { Injectable, ServiceUnavailableException } from '@nestjs/common'; import { load } from 'cheerio'; import { LRUCache } from 'lru-cache'; const registryUrl = 'https://ratings.ru/ratings/issues/'; export type NcrRating = { agency: 'NCR'; value: string | null; status: 'active' | 'withdrawn'; date: string; sourceUrl: string; observedAt: string; }; @Injectable() export class NcrRatingsClient { // Public registry contains no user data. Share one download across all issues. private readonly cache = new LRUCache< string, Promise> >({ max: 1, ttl: 6 * 60 * 60_000 }); async rating(isin: string): Promise { const key = isin.trim().toUpperCase(); if (!/^[A-Z]{2}[A-Z0-9]{9}\d$/.test(key)) return null; let pending = this.cache.get('registry'); if (!pending) { pending = this.registry().catch(() => { // Bound outage traffic too; never present a failed download as no rating. if (this.cache.get('registry') === pending) this.cache.set('registry', pending, { ttl: 60_000 }); throw new ServiceUnavailableException({ code: 'NCR_UNAVAILABLE' }); }); this.cache.set('registry', pending); } return (await pending).get(key) ?? null; } private async registry(): Promise> { const response = await fetch(registryUrl, { signal: AbortSignal.timeout(10_000), }); if (!response.ok) throw new Error('NCR unavailable'); const $ = load(await response.text()); const table = $('#issues-table'); const headings = table .find('thead th') .map((_, cell) => $(cell).text().trim()) .get(); if ( table.length !== 1 || headings.length !== 6 || headings[2] !== 'Рейтинг' || headings[3] !== 'ISIN' || headings[5] !== 'Дата' ) throw new Error('NCR registry changed'); const observedAt = new Date().toISOString(); const result = new Map(); table.find('tbody tr').each((_, row) => { const cells = $(row).find('td'); if (cells.length !== 6) throw new Error('NCR row changed'); const isin = cells.eq(3).text().trim().toUpperCase(); // Unassigned issues cannot safely be matched to a held instrument. if (!/^[A-Z]{2}[A-Z0-9]{9}\d$/.test(isin)) return; const raw = cells.eq(2).text().replace(/\s+/g, ' ').trim(); const withdrawn = raw.toLowerCase() === 'рейтинг отозван'; if ( !withdrawn && !/^(AAA|AA[+-]?|A[+-]?|BBB[+-]?|BB[+-]?|B[+-]?|CCC[+-]?|CC|C|D)\.ru$/.test( raw, ) ) throw new Error('NCR rating changed'); const dateParts = /^(\d{2})\.(\d{2})\.(\d{4})$/.exec( cells.eq(5).text().trim(), ); const href = cells.eq(3).find('a').attr('href'); if (!dateParts || !href || !/^\/ratings\/issues\/\d+\/$/.test(href)) throw new Error('NCR identity missing'); const date = `${dateParts[3]}-${dateParts[2]}-${dateParts[1]}`; if ( !Number.isFinite(Date.parse(date)) || new Date(date).toISOString().slice(0, 10) !== date ) throw new Error('NCR date invalid'); const rating: NcrRating = { agency: 'NCR', value: withdrawn ? null : raw, status: withdrawn ? 'withdrawn' : 'active', date, sourceUrl: `https://ratings.ru${href}`, observedAt, }; const previous = result.get(isin); if ( previous && previous.date === date && (previous.value !== rating.value || previous.status !== rating.status) ) throw new Error('NCR rating ambiguous'); if (!previous || previous.date < date) result.set(isin, rating); }); if (!result.size) throw new Error('NCR registry empty'); return result; } }