From ccb7f45c7edc84f0ceda242e515f3c763291010d Mon Sep 17 00:00:00 2001 From: Aleksandr Meshchryakov Date: Tue, 6 Oct 2026 14:43:21 +0200 Subject: [PATCH] feat: enrich saved leasing events with bounded Fedresurs details --- docs/sources/checko-sections.md | 3 + docs/sources/leasing-details.md | 74 +++ src/apps/parsers/checko_section_snapshots.py | 12 +- src/apps/parsers/fedresurs_browser.py | 6 +- src/apps/parsers/fedresurs_company_profile.py | 6 +- src/apps/parsers/leasing_details.py | 378 ++++++++++++++++ .../commands/enrich_leasing_details.py | 26 ++ src/apps/parsers/source_cards.py | 16 + src/apps/parsers/tasks_registry_snapshots.py | 45 +- src/core/openapi.json | 184 ++++++++ .../checko_section_payload_serializers.py | 45 ++ src/settings/base.py | 6 + .../test_openapi_runtime_fixtures.py | 10 + tests/apps/parsers/test_leasing_details.py | 427 ++++++++++++++++++ tools/openapi/source-record-extensions.json | 184 ++++++++ 15 files changed, 1418 insertions(+), 4 deletions(-) create mode 100644 docs/sources/leasing-details.md create mode 100644 src/apps/parsers/leasing_details.py create mode 100644 src/apps/parsers/management/commands/enrich_leasing_details.py create mode 100644 tests/apps/parsers/test_leasing_details.py diff --git a/docs/sources/checko-sections.md b/docs/sources/checko-sections.md index fddaaa6..740d3d3 100644 --- a/docs/sources/checko-sections.md +++ b/docs/sources/checko-sections.md @@ -1,5 +1,8 @@ # Разделы Checko: проверенные методы API 2.5 +Предметы и реквизиты договоров дополняются отдельным режимом без повторных +запросов Checko: [детали лизинга](leasing-details.md). + Проверено 5 октября 2026 по официальным страницам [API](https://checko.ru/integration/api), [company](https://checko.ru/integration/api/company), [fedresurs](https://checko.ru/integration/api/fedresurs) и [справочнику типов](https://checko.ru/uploads/datasets/message_types.xlsx). Старые /api адреса возвращают 404. Публичная документация подтверждает все методы на всех тарифах; фактический доступ выбранного аккаунта ещё не проверен. Ключи/личный кабинет не читались, платных запросов не было. ## Транспорт и полнота diff --git a/docs/sources/leasing-details.md b/docs/sources/leasing-details.md new file mode 100644 index 0000000..ad0cb5c --- /dev/null +++ b/docs/sources/leasing-details.md @@ -0,0 +1,74 @@ +# Детали лизинга из публичных сообщений Федресурса + +Checko `/fedresurs` остаётся источником списка событий и их GUID. Отдельный +режим `details` дополняет уже сохранённые события из публичной страницы сообщения: +номер и дата договора, сроки аренды, лизингодатели/лизингополучатели, предметы +(описание, классификатор и опубликованный идентификатор), причина/дата прекращения +и ссылки на связанные публикации. Стоимость и отсутствующие идентификаторы не +вычисляются. Связанные страницы автоматически не обходятся. + +## Запуск и API + +Для администратора: + +```http +POST /api/v1/sources/checko-leasing/refresh/ +Content-Type: application/json + +{"params":{"mode":"details"}} +``` + +Также поддерживается `POST /api/v1/parsers/run/checko_leasing/` с тем же телом. +Ответ 202 содержит task_id; статус и счётчики — в `/api/v1/jobs//`. +Пустое тело сохраняет прежний режим `collection`: месячный сбор списка Checko. +Режим `details` не обращается к Checko, не сбрасывает месячные claims, не создаёт +ParserLoadLog и не подменяет дату полного успешного сбора списка. + +Для точечной проверки одной существующей записи или порции: + +```sh +PYTHONPATH=src uv run python src/manage.py enrich_leasing_details --max-pages 1 --record-uid +PYTHONPATH=src uv run python src/manage.py enrich_leasing_details --max-pages 25 +``` + +Новые необязательные поля `payload.lease_details` и `payload.lease_details_status` +выдаются в list/detail `/api/v2/organization-source-records/`; прежний payload +без этих полей остаётся допустимым. Успех — `status=loaded`; отказ — `failed`, +`error_code`, `checked_at`, `next_retry_at`. Предыдущие детали при отказе сохраняются. +UID, load_batch, количество событий и данные Checko при дополнении не меняются. +Следующий штатный сбор списка сохраняет детали события с тем же external_id. + +## Ограничение нагрузки + +- `FEDRESURS_LEASING_MAX_PAGES`: по умолчанию 25 уникальных страниц за запуск, + абсолютный потолок 100; командный `--max-pages` не может превысить настройку. +- Успешные детали сохраняются в БД. Повторный запуск пропускает их; одно сообщение + с несколькими организациями загружается один раз и проверяется для каждой + организации по точной паре ИНН/ОГРН стороны договора. +- Между страницами не менее двух секунд; увеличивается существующей настройкой + `FEDRESURS_MIN_INTERVAL_SECONDS`. Браузер переиспользуется внутри порции. +- Ошибка страницы откладывает её повтор на 24 часа. 403, challenge, timeout или + ошибка браузера останавливают порцию; отказ источника включает общий cooldown + на 24 часа. Техническая ошибка не становится пустым договором. +- Сбор ограничен 30 минутами и защищён общим cache-lock; одновременно запущенный + collector возвращает `enrichment_already_running` до загрузки страницы. + +Лимит считает навигации к сообщениям, а не все HTTP-запросы браузера: ресурсы +приложения требуют дополнительных запросов. Повторного платного сбора Checko нет. +Данные дозаполняются явными порциями; нового автоматического расписания нет. + +`FEDRESURS_LEASING_USER_AGENT` задаёт обычный браузерный User-Agent с обозначением +`ECOS-LeasingCollector/1.0`. Значение по умолчанию соответствует Chrome 154 в +текущем release image. Cookies пользователя, stealth-флаги, private API и +обход challenge не используются; TLS-проверки остаются включёнными. + +Сборщик сверяет заявленное число предметов с полученной таблицей. Неполная таблица +(в том числе ещё не загруженные страницы длинной таблицы) даёт явную +`incomplete_leasing_subjects` и не публикуется как полный договор. Старые данные +сохраняются. Подписи/сертификаты не собираются; исходный текст содержит только +публичный блок договора. + +Проверка 6 октября 2026: dev browser с заданным User-Agent открыл сообщение +39082892 из пользовательского примера; текущий парсер прочитал договор 5429, +два предмета, обе стороны, сроки и ссылку на исходное сообщение. Это проверка +транспорта/разбора; публикация и развёртывание проверяются отдельно. diff --git a/src/apps/parsers/checko_section_snapshots.py b/src/apps/parsers/checko_section_snapshots.py index 743786a..72cdc0a 100644 --- a/src/apps/parsers/checko_section_snapshots.py +++ b/src/apps/parsers/checko_section_snapshots.py @@ -22,7 +22,7 @@ from apps.parsers.registry_snapshots import ( ) from django.conf import settings from django.db.models import Q -from organizations.models import Organization +from organizations.models import Organization, OrganizationSourceRecord def _client(): @@ -78,6 +78,16 @@ def _collect_organization(client, org, source, artifact, row_number, raw_file): raise SnapshotValidationError("checko_record_outside_claim_scope") count += 1 row_number += 1 + if source == "checko_leasing": + previous = OrganizationSourceRecord.objects.filter( + source=source, + extension__organization=org, + external_id=row.external_id, + ).first() + if previous is not None: + for key in ("lease_details", "lease_details_status"): + if key in previous.payload: + row.payload[key] = previous.payload[key] normalized = { "title": row.title, "record_date": row.record_date, diff --git a/src/apps/parsers/fedresurs_browser.py b/src/apps/parsers/fedresurs_browser.py index cde684b..13a73e7 100644 --- a/src/apps/parsers/fedresurs_browser.py +++ b/src/apps/parsers/fedresurs_browser.py @@ -34,13 +34,17 @@ def _wait_public_element(browser, by: str, value: str): @contextmanager -def public_browser() -> Iterator[webdriver.Chrome]: +def public_browser(*, user_agent: str | None = None) -> Iterator[webdriver.Chrome]: """Start a fresh browser, with no inherited profile, cookies, or stealth flags.""" options = webdriver.ChromeOptions() options.binary_location = settings.FEDRESURS_BROWSER_BINARY options.add_argument("--headless=new") options.add_argument("--no-sandbox") options.add_argument("--disable-dev-shm-usage") + if user_agent is not None: + if not user_agent or "\n" in user_agent or "\r" in user_agent: + raise FedresursUpstreamError("invalid_browser_user_agent") + options.add_argument(f"--user-agent={user_agent}") driver = webdriver.Chrome( service=Service(executable_path=settings.FEDRESURS_BROWSER_DRIVER), options=options, diff --git a/src/apps/parsers/fedresurs_company_profile.py b/src/apps/parsers/fedresurs_company_profile.py index 939c83a..26bd9e4 100644 --- a/src/apps/parsers/fedresurs_company_profile.py +++ b/src/apps/parsers/fedresurs_company_profile.py @@ -32,7 +32,11 @@ SECTION_NAMES = { "trades": "biddings", } CHALLENGE_WORDS = ("captcha", "qrator", "access denied", "проверка браузера") -ACCESS_BLOCK_WORDS = ("403 error", "access to fedresurs.ru is forbidden") +ACCESS_BLOCK_WORDS = ( + "403 error", + "403 forbidden", + "access to fedresurs.ru is forbidden", +) MAX_RENDERED_SECTION_BYTES = 4 * 1024 * 1024 PUBLIC_PAGE_ROOTS = { "publications": "app-company-card-publications", diff --git a/src/apps/parsers/leasing_details.py b/src/apps/parsers/leasing_details.py new file mode 100644 index 0000000..e1edfac --- /dev/null +++ b/src/apps/parsers/leasing_details.py @@ -0,0 +1,378 @@ +"""Bounded enrichment of saved lease events from public Fedresurs message pages.""" + +from __future__ import annotations + +import re +import time +import uuid +from datetime import timedelta +from urllib.parse import urljoin, urlsplit + +from apps.parsers.fedresurs_browser import public_browser +from apps.parsers.fedresurs_company_profile import ( + FEDRESURS_ORIGIN, + FedresursUpstreamError, + _challenge, + _iso_date, + _label_value, +) +from bs4 import BeautifulSoup +from django.conf import settings +from django.core.cache import cache +from django.db import transaction +from django.utils import timezone +from django.utils.dateparse import parse_datetime +from organizations.cache import invalidate_organization_api_cache +from organizations.models import OrganizationSourceRecord +from selenium.common.exceptions import TimeoutException, WebDriverException +from selenium.webdriver.common.by import By +from selenium.webdriver.support.ui import WebDriverWait + +SOURCE = "checko_leasing" +LOCK_KEY = "fedresurs:leasing:enrichment-lock" +COOLDOWN_KEY = "fedresurs:leasing:enrichment-cooldown" +LOCK_SECONDS = 3600 +SOURCE_ERRORS = frozenset( + { + "upstream_access_blocked", + "upstream_challenge", + "upstream_timeout", + "browser_failure", + } +) +CONTENT_TAG = re.compile( + r"^sfact-message-(?:financial-lease-contract|change-financial-lease-contract|stop-financial-lease-contract)(?:-2)?$" +) + + +def message_guid(url: str) -> str: + """Accept only anonymous public message URLs, including compact GUIDs.""" + parsed = urlsplit(url) + match = re.fullmatch(r"/sfactmessages?/([0-9a-fA-F-]{32,36})", parsed.path) + if ( + parsed.scheme != "https" + or parsed.netloc != "fedresurs.ru" + or parsed.query + or parsed.fragment + or match is None + ): + raise FedresursUpstreamError("invalid_leasing_message_url") + try: + return str(uuid.UUID(match.group(1))) + except ValueError as exc: + raise FedresursUpstreamError("invalid_leasing_message_url") from exc + + +def _parties(content, label: str) -> list[dict]: + for node in content.find_all(string=True): + if str(node).strip() != label: + continue + parent = node.parent.parent + return [ + { + "name": participant.select_one(".name").get_text(" ", strip=True) + if participant.select_one(".name") is not None + else None, + "inn": _label_value(participant, "ИНН") or None, + "ogrn": _label_value(participant, "ОГРН") or None, + } + for participant in parent.find_all("message-participant") + ] + return [] + + +def _lease_value(content, label: str) -> str: + """Labels may contain a span; find their value without depending on CSS layout.""" + for node in content.find_all(string=True): + if str(node).strip() != label: + continue + ancestor = node.parent + while ancestor is not None and ancestor is not content: + if ancestor.get_text(" ", strip=True) != label: + break + sibling = ancestor.find_next_sibling() + if sibling is not None: + return sibling.get_text(" ", strip=True) + ancestor = ancestor.parent + return "" + + +def _subjects(content) -> list[dict]: + subjects = [] + for table in content.find_all("table"): + if "Предмет финансовой аренды" not in table.get_text(" ", strip=True): + continue + for row in table.select("tbody > tr"): + cells = row.find_all("td", recursive=False) + if len(cells) != 3: + raise FedresursUpstreamError("incomplete_leasing_subjects") + identifier_raw = _label_value(cells[1], "Идентификатор") + classifier = _label_value(cells[1], "Классификатор") + code, separator, name = classifier.partition(",") + subjects.append( + { + "identifier": identifier_raw + if identifier_raw and identifier_raw.casefold() != "нет данных" + else None, + "identifier_raw": identifier_raw or None, + "classifier_code": code.strip() or None, + "classifier_name": name.strip() if separator else None, + "description": cells[2].get_text("\n", strip=True) or None, + } + ) + count = re.search( + r"Предметы финансовой аренды\s*\(лизинга\)\s*\((\d+)\)", + content.get_text(" ", strip=True), + ) + if not subjects or (count is not None and int(count.group(1)) != len(subjects)): + raise FedresursUpstreamError("incomplete_leasing_subjects") + return subjects + + +def parse_leasing_html(html: str, *, url: str) -> dict: + message_guid(url) + _challenge(html) + soup = BeautifulSoup(html, "html.parser") + root = soup.find("sfact-message-card") + content = root.find(CONTENT_TAG) if root else None + if content is None: + raise FedresursUpstreamError("incomplete_leasing_message") + contract_raw = _lease_value(content, "Договор") + contract = re.fullmatch(r"(.+?)\s+от\s+(\d{2}\.\d{2}\.\d{4})", contract_raw) + dates = re.findall( + r"\d{2}\.\d{2}\.\d{4}", _label_value(content, "Срок финансовой аренды") + ) + related = {} + for link in root.select("sfact-message-card-msgs a[href]"): + target = urljoin(FEDRESURS_ORIGIN, link["href"]) + try: + guid = message_guid(target) + except FedresursUpstreamError: + continue + related[guid] = { + "guid": guid, + "url": target, + "label": link.get_text(" ", strip=True), + } + if contract is None or len(dates) != 2: + raise FedresursUpstreamError("incomplete_leasing_contract") + result = { + "contract_number": contract.group(1), + "contract_date": _iso_date(contract.group(2)), + "lease_start_date": _iso_date(dates[0]), + "lease_end_date": _iso_date(dates[1]), + "lessors": _parties(content, "Лизингодатели"), + "lessees": _parties(content, "Лизингополучатели"), + "subjects": _subjects(content), + "terminated_on": _iso_date(_label_value(content, "Дата прекращения")), + "termination_reason": _label_value(content, "Причина прекращения") or None, + "related_messages": list(related.values()), + "message_text": content.get_text("\n", strip=True), + "source_url": url, + "fetched_at": timezone.now().isoformat(), + } + if ( + not result["lessors"] + or not result["lessees"] + or any( + result[key] is None + for key in ("contract_date", "lease_start_date", "lease_end_date") + ) + ): + raise FedresursUpstreamError("incomplete_leasing_contract") + return result + + +def fetch_leasing_html(driver, url: str) -> str: + """Normal rendered UI navigation; no private endpoints or challenge workaround.""" + expected_guid = message_guid(url) + try: + driver.get(url) + _challenge(driver.page_source) + + def ready(browser): + _challenge(browser.page_source) + return browser.find_elements(By.TAG_NAME, "sfact-message-card") + + WebDriverWait(driver, min(settings.FEDRESURS_PAGE_TIMEOUT_SECONDS, 45)).until( + ready + ) + if message_guid(driver.current_url) != expected_guid: + raise FedresursUpstreamError("leasing_message_redirect_mismatch") + html = driver.page_source + if len(html.encode("utf-8")) > 4 * 1024 * 1024: + raise FedresursUpstreamError("leasing_message_too_large") + return html + except TimeoutException as exc: + raise FedresursUpstreamError("upstream_timeout") from exc + except WebDriverException as exc: + raise FedresursUpstreamError("browser_failure") from exc + + +def _due(payload: dict) -> bool: + state = payload.get("lease_details_status", {}) + if state.get("status") == "loaded" and payload.get("lease_details"): + return False + retry_at = parse_datetime(str(state.get("next_retry_at") or "")) + return retry_at is None or retry_at <= timezone.now() + + +def _save_details(records: list, *, details: dict | None, error: str | None) -> int: + updated = 0 + with transaction.atomic(): + for original in records: + obj = OrganizationSourceRecord.objects.select_for_update().get( + pk=original.pk + ) + if obj.payload.get("message_guid") != original.payload.get("message_guid"): + continue + payload = dict(obj.payload) + state = { + "status": "failed" if error else "loaded", + "checked_at": timezone.now().isoformat(), + } + if error: + state.update( + error_code=error, + next_retry_at=(timezone.now() + timedelta(hours=24)).isoformat(), + ) + elif details is not None: + identifiers = { + (p.get("inn"), p.get("ogrn")) + for p in details["lessors"] + details["lessees"] + } + org = obj.extension.organization + if (org.inn, org.ogrn) not in identifiers: + state.update( + status="failed", + error_code="leasing_party_mismatch", + next_retry_at=( + timezone.now() + timedelta(hours=24) + ).isoformat(), + ) + else: + payload["lease_details"] = details + updated += 1 + payload["lease_details_status"] = state + obj.payload = payload + obj.save(update_fields=("payload", "updated_at")) + return updated + + +def _candidate_groups(record_uid: str | None) -> dict[str, list]: + qs = OrganizationSourceRecord.objects.filter(source=SOURCE).select_related( + "extension__organization" + ) + target_guid = None + if record_uid: + try: + original = qs.get(uid=record_uid) + except OrganizationSourceRecord.DoesNotExist as exc: + raise ValueError("leasing_record_not_found") from exc + target_guid = message_guid(original.payload.get("source_url") or original.url) + groups = {} + for obj in qs.order_by("created_at", "uid").iterator(chunk_size=500): + try: + guid = message_guid(obj.payload.get("source_url") or obj.url) + if str(uuid.UUID(obj.payload.get("message_guid", ""))) != guid: + continue + if target_guid is not None and guid != target_guid: + continue + except (FedresursUpstreamError, ValueError, TypeError, AttributeError): + continue + groups.setdefault(guid, []).append(obj) + return groups + + +def _prepare_candidates(record_uid, max_pages, result): + candidates = [] + for records in _candidate_groups(record_uid).values(): + due = [obj for obj in records if _due(obj.payload)] + loaded = next( + ( + obj.payload["lease_details"] + for obj in records + if obj.payload.get("lease_details_status", {}).get("status") == "loaded" + and obj.payload.get("lease_details") + ), + None, + ) + if loaded is not None and due: + _apply_details(due, loaded, result) + elif due and len(candidates) < max_pages: + candidates.append(due) + return candidates + + +def _apply_details(records, details, result): + updated = _save_details(records, details=details, error=None) + result["records_updated"] += updated + if updated != len(records): + result["messages_failed"] += 1 + result["error_code"] = "leasing_party_mismatch" + + +def _enrich_candidates(candidates, owner, result): + # Bound wall time below the lock TTL even if the configured page budget grows. + deadline = time.monotonic() + 30 * 60 + with public_browser(user_agent=settings.FEDRESURS_LEASING_USER_AGENT) as driver: + driver.set_page_load_timeout(min(settings.FEDRESURS_PAGE_TIMEOUT_SECONDS, 45)) + for records in candidates: + if cache.get(LOCK_KEY) != owner or time.monotonic() >= deadline: + result["error_code"] = "enrichment_lock_or_time_limit" + break + if result["pages_requested"]: + time.sleep(max(2, float(settings.FEDRESURS_MIN_INTERVAL_SECONDS))) + url = records[0].payload.get("source_url") or records[0].url + result["pages_requested"] += 1 + try: + details = parse_leasing_html(fetch_leasing_html(driver, url), url=url) + except FedresursUpstreamError as exc: + code = str(exc) + _save_details(records, details=None, error=code) + result["messages_failed"] += 1 + result["error_code"] = code + if code in SOURCE_ERRORS: + cache.set(COOLDOWN_KEY, True, timeout=24 * 3600) + break + else: + _apply_details(records, details, result) + + +def enrich_leasing_details( + *, max_pages: int | None = None, record_uid: str | None = None +) -> dict: + """Persist immutable message details once, with a page budget and source cooldown.""" + limit = min(int(settings.FEDRESURS_LEASING_MAX_PAGES), 100) + max_pages = limit if max_pages is None else max_pages + if ( + isinstance(max_pages, bool) + or not isinstance(max_pages, int) + or not 1 <= max_pages <= limit + ): + raise ValueError("invalid_leasing_page_budget") + result = { + "pages_requested": 0, + "records_updated": 0, + "messages_failed": 0, + "error_code": None, + } + if cache.get(COOLDOWN_KEY): + return {**result, "error_code": "upstream_cooldown"} + owner = str(uuid.uuid4()) + if not cache.add(LOCK_KEY, owner, timeout=LOCK_SECONDS): + return {**result, "error_code": "enrichment_already_running"} + try: + candidates = _prepare_candidates(record_uid, max_pages, result) + if candidates: + try: + _enrich_candidates(candidates, owner, result) + except WebDriverException: + result["error_code"] = "browser_failure" + cache.set(COOLDOWN_KEY, True, timeout=24 * 3600) + return result + finally: + if result["records_updated"]: + invalidate_organization_api_cache() + if cache.get(LOCK_KEY) == owner: + cache.delete(LOCK_KEY) diff --git a/src/apps/parsers/management/commands/enrich_leasing_details.py b/src/apps/parsers/management/commands/enrich_leasing_details.py new file mode 100644 index 0000000..15dd227 --- /dev/null +++ b/src/apps/parsers/management/commands/enrich_leasing_details.py @@ -0,0 +1,26 @@ +"""Bounded backfill of existing lease events without any Checko calls.""" + +import json + +from apps.parsers.fedresurs_company_profile import FedresursUpstreamError +from apps.parsers.leasing_details import enrich_leasing_details +from django.core.management.base import BaseCommand, CommandError + + +class Command(BaseCommand): + help = "Дополнить сохранённые события лизинга из публичных сообщений Федресурса" + + def add_arguments(self, parser): + parser.add_argument("--max-pages", type=int, default=None) + parser.add_argument("--record-uid", default=None) + + def handle(self, *args, **options): + try: + result = enrich_leasing_details( + max_pages=options["max_pages"], record_uid=options["record_uid"] + ) + except (ValueError, FedresursUpstreamError) as exc: + raise CommandError(str(exc)) from exc + self.stdout.write(json.dumps(result, ensure_ascii=False, sort_keys=True)) + if result["error_code"]: + raise CommandError(result["error_code"]) diff --git a/src/apps/parsers/source_cards.py b/src/apps/parsers/source_cards.py index 696281c..efd4d0a 100644 --- a/src/apps/parsers/source_cards.py +++ b/src/apps/parsers/source_cards.py @@ -529,6 +529,13 @@ SOURCE_CARD_DEFINITIONS: tuple[SourceCardDefinition, ...] = ( ), ), refresh_interval=timedelta(days=30), + refresh_params=( + RefreshParamDefinition( + name="mode", + label="Режим обновления", + description="collection: список Checko; details: дополнить сохранённые события из Федресурса без запросов Checko.", + ), + ), ), SourceCardDefinition( slug="checko-messages", @@ -878,6 +885,13 @@ class SourceCardService: ) else: params = cls._validate_refresh_params(definition, params) + if slug == "checko-leasing" and params.get("mode", "collection") not in ( + "collection", + "details", + ): + raise APIValidationError( + message="Допустимые режимы: collection, details.", code="invalid_mode" + ) if mode is not None: allowed_modes = ( ("incremental", "full") @@ -1449,6 +1463,8 @@ class SourceCardService: ), ) elif definition.slug == "checko-leasing": + if "mode" in params: + kwargs["mode"] = params["mode"] task_specs = ( ( parse_checko_leasing, diff --git a/src/apps/parsers/tasks_registry_snapshots.py b/src/apps/parsers/tasks_registry_snapshots.py index bed55bd..e805656 100644 --- a/src/apps/parsers/tasks_registry_snapshots.py +++ b/src/apps/parsers/tasks_registry_snapshots.py @@ -379,7 +379,13 @@ def parse_fssp_enforcement_proceedings( soft_time_limit=8 * 3600, time_limit=8 * 3600 + 300, ) -def parse_checko_leasing(self, *, requested_by_id: int | None = None) -> dict: +def parse_checko_leasing( + self, *, requested_by_id: int | None = None, mode: str = "collection" +) -> dict: + if mode == "details": + return _run_leasing_details(self, requested_by_id=requested_by_id) + if mode != "collection": + raise SnapshotValidationError("invalid_mode") from apps.parsers.checko_section_snapshots import refresh_checko_section return _run_snapshot( @@ -391,6 +397,43 @@ def parse_checko_leasing(self, *, requested_by_id: int | None = None) -> dict: ) +def _run_leasing_details(task, *, requested_by_id: int | None) -> dict: + """Detail-only job; never create a collection log or advance full snapshot success.""" + from apps.parsers.leasing_details import enrich_leasing_details + + job, _ = BackgroundJob.objects.get_or_create( + task_id=task.request.id or str(uuid.uuid4()), + defaults={ + "task_name": task.name, + "user_id": requested_by_id, + "meta": {"source": "checko_leasing", "mode": "details"}, + }, + ) + try: + if not _claim_snapshot_job(job, "checko_leasing", task.name): + job.refresh_from_db() + return job.result or {"status": job.status, "duplicate_delivery": True} + job.update_progress(5, "Дополнение сохранённых событий лизинга") + result = enrich_leasing_details() + job.refresh_from_db() + if job.status != JobStatus.STARTED: + raise SnapshotValidationError("snapshot_job_no_longer_active") + job.meta = {**job.meta, "mode": "details", **result} + job.result = {"source": "checko_leasing", "mode": "details", **result} + job.save(update_fields=("meta", "result", "updated_at")) + if result["error_code"] and not result["records_updated"]: + raise SnapshotValidationError(result["error_code"]) + status = "partial" if result["error_code"] else "success" + job.update_progress(100, "Порция дополнения лизинга завершена") + job.complete(result={"status": status, **job.result}) + if result["records_updated"]: + transaction.on_commit(_refresh_published_exports) + return job.result + except Exception as exc: + job.fail(error=_snapshot_error_message(exc)) + raise + + @shared_task( bind=True, name="parsers.checko_messages.refresh", diff --git a/src/core/openapi.json b/src/core/openapi.json index 2ab2658..8931848 100644 --- a/src/core/openapi.json +++ b/src/core/openapi.json @@ -22282,6 +22282,12 @@ }, "lease_event_type": { "type": "string" + }, + "lease_details": { + "$ref": "#/components/schemas/LeaseDetails" + }, + "lease_details_status": { + "$ref": "#/components/schemas/LeaseDetailsStatus" } }, "required": [ @@ -22512,6 +22518,184 @@ } ] }, + "LeaseParty": { + "type": "object", + "properties": { + "name": { + "type": "string", + "nullable": true + }, + "inn": { + "type": "string", + "nullable": true + }, + "ogrn": { + "type": "string", + "nullable": true + } + }, + "required": [ + "name", + "inn", + "ogrn" + ] + }, + "LeaseSubject": { + "type": "object", + "properties": { + "identifier": { + "type": "string", + "nullable": true + }, + "identifier_raw": { + "type": "string", + "nullable": true + }, + "classifier_code": { + "type": "string", + "nullable": true + }, + "classifier_name": { + "type": "string", + "nullable": true + }, + "description": { + "type": "string", + "nullable": true + } + }, + "required": [ + "identifier", + "identifier_raw", + "classifier_code", + "classifier_name", + "description" + ] + }, + "LeaseRelatedMessage": { + "type": "object", + "properties": { + "guid": { + "type": "string" + }, + "url": { + "type": "string" + }, + "label": { + "type": "string" + } + }, + "required": [ + "guid", + "url", + "label" + ] + }, + "LeaseDetails": { + "type": "object", + "properties": { + "contract_number": { + "type": "string" + }, + "contract_date": { + "type": "string", + "format": "date" + }, + "lease_start_date": { + "type": "string", + "format": "date" + }, + "lease_end_date": { + "type": "string", + "format": "date" + }, + "lessors": { + "type": "array", + "items": { + "$ref": "#/components/schemas/LeaseParty" + } + }, + "lessees": { + "type": "array", + "items": { + "$ref": "#/components/schemas/LeaseParty" + } + }, + "subjects": { + "type": "array", + "items": { + "$ref": "#/components/schemas/LeaseSubject" + } + }, + "terminated_on": { + "type": "string", + "nullable": true, + "format": "date" + }, + "termination_reason": { + "type": "string", + "nullable": true + }, + "related_messages": { + "type": "array", + "items": { + "$ref": "#/components/schemas/LeaseRelatedMessage" + } + }, + "message_text": { + "type": "string" + }, + "source_url": { + "type": "string" + }, + "fetched_at": { + "type": "string", + "format": "date-time" + } + }, + "required": [ + "contract_number", + "contract_date", + "lease_start_date", + "lease_end_date", + "lessors", + "lessees", + "subjects", + "terminated_on", + "termination_reason", + "related_messages", + "message_text", + "source_url", + "fetched_at" + ] + }, + "LeaseDetailsStatus": { + "type": "object", + "properties": { + "status": { + "type": "string", + "enum": [ + "loaded", + "failed" + ] + }, + "checked_at": { + "type": "string", + "format": "date-time" + }, + "error_code": { + "type": "string" + }, + "next_retry_at": { + "type": "string", + "format": "date-time" + } + }, + "required": [ + "status", + "checked_at" + ] + }, "ExchangeInstrumentRecordList": { "type": "object", "properties": { diff --git a/src/organizations/checko_section_payload_serializers.py b/src/organizations/checko_section_payload_serializers.py index 0e691b7..4e4e5b3 100644 --- a/src/organizations/checko_section_payload_serializers.py +++ b/src/organizations/checko_section_payload_serializers.py @@ -18,8 +18,53 @@ class CheckoMessagePayloadSerializer(serializers.Serializer): upstream = serializers.JSONField() +class LeasePartySerializer(serializers.Serializer): + name = serializers.CharField(allow_null=True) + inn = serializers.CharField(allow_null=True) + ogrn = serializers.CharField(allow_null=True) + + +class LeaseSubjectSerializer(serializers.Serializer): + identifier = serializers.CharField(allow_null=True) + identifier_raw = serializers.CharField(allow_null=True) + classifier_code = serializers.CharField(allow_null=True) + classifier_name = serializers.CharField(allow_null=True) + description = serializers.CharField(allow_null=True) + + +class LeaseRelatedMessageSerializer(serializers.Serializer): + guid = serializers.CharField() + url = serializers.CharField() + label = serializers.CharField() + + +class LeaseDetailsSerializer(serializers.Serializer): + contract_number = serializers.CharField() + contract_date = serializers.DateField() + lease_start_date = serializers.DateField() + lease_end_date = serializers.DateField() + lessors = LeasePartySerializer(many=True) + lessees = LeasePartySerializer(many=True) + subjects = LeaseSubjectSerializer(many=True) + terminated_on = serializers.DateField(allow_null=True) + termination_reason = serializers.CharField(allow_null=True) + related_messages = LeaseRelatedMessageSerializer(many=True) + message_text = serializers.CharField() + source_url = serializers.CharField() + fetched_at = RegistryUTCDateTimeField() + + +class LeaseDetailsStatusSerializer(serializers.Serializer): + status = serializers.ChoiceField(choices=("loaded", "failed")) + checked_at = RegistryUTCDateTimeField() + error_code = serializers.CharField(required=False) + next_retry_at = RegistryUTCDateTimeField(required=False) + + class CheckoLeasingPayloadSerializer(CheckoMessagePayloadSerializer): lease_event_type = serializers.CharField() + lease_details = LeaseDetailsSerializer(required=False) + lease_details_status = LeaseDetailsStatusSerializer(required=False) class CheckoLicensePayloadSerializer(serializers.Serializer): diff --git a/src/settings/base.py b/src/settings/base.py index 18805ea..5f8b6e2 100644 --- a/src/settings/base.py +++ b/src/settings/base.py @@ -346,6 +346,12 @@ FEDRESURS_BROWSER_DRIVER = os.getenv( FEDRESURS_PAGE_TIMEOUT_SECONDS = int(os.getenv("FEDRESURS_PAGE_TIMEOUT_SECONDS", "45")) FEDRESURS_MIN_INTERVAL_SECONDS = float(os.getenv("FEDRESURS_MIN_INTERVAL_SECONDS", "2")) FEDRESURS_MAX_RETRIES = int(os.getenv("FEDRESURS_MAX_RETRIES", "2")) +FEDRESURS_LEASING_MAX_PAGES = int(os.getenv("FEDRESURS_LEASING_MAX_PAGES", "25")) +FEDRESURS_LEASING_USER_AGENT = os.getenv( + "FEDRESURS_LEASING_USER_AGENT", + "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/154.0.0.0 Safari/537.36 ECOS-LeasingCollector/1.0", +) # Default primary key field type DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" diff --git a/tests/apps/organizations/test_openapi_runtime_fixtures.py b/tests/apps/organizations/test_openapi_runtime_fixtures.py index a01faed..21b918b 100644 --- a/tests/apps/organizations/test_openapi_runtime_fixtures.py +++ b/tests/apps/organizations/test_openapi_runtime_fixtures.py @@ -149,6 +149,16 @@ def _canonical_records(): else client_with(message_page, fixture("messages-page-2.json")) ) row = next(collect_checko_section(client, organization=fssp_org, source=source)) + if source == "checko_leasing": + from apps.parsers.leasing_details import parse_leasing_html + + from tests.apps.parsers.test_leasing_details import URL, page + + row.payload["lease_details"] = parse_leasing_html(page(), url=URL) + row.payload["lease_details_status"] = { + "status": "loaded", + "checked_at": timezone.now().isoformat(), + } row.payload["upstream"]["future_nested"] = { "keep": [None, False, 0, {"unknown": "0007"}] } diff --git a/tests/apps/parsers/test_leasing_details.py b/tests/apps/parsers/test_leasing_details.py new file mode 100644 index 0000000..88b207d --- /dev/null +++ b/tests/apps/parsers/test_leasing_details.py @@ -0,0 +1,427 @@ +"""Lease enrichment: real parsing/persistence, synthetic public-page transport.""" + +import importlib +from contextlib import nullcontext +from unittest.mock import Mock, patch + +import pytest +from apps.parsers.fedresurs_company_profile import FedresursUpstreamError +from django.core.cache import cache +from django.utils import timezone +from organizations.models import Organization, OrganizationSourceRecord +from organizations.source_ingestion import ( + OrganizationSourceIngestionService, + SourceRecordInput, +) +from rest_framework.test import APIClient + +from tests.apps.user.factories import UserFactory + +GUID = "12345678-1234-4234-8234-123456789abc" +URL = f"https://fedresurs.ru/sfactmessages/{GUID}" + + +def page(kind="stop-financial-lease-contract-2", count=2): + """Synthetic DOM based on the public message components inspected on 06.10.""" + return f""" +
12345678 от 17.09.2026 08:29:10 МСК
+ +
Договор
TEST-42 от 08.12.2025
+
Срок финансовой аренды
23.12.2025 - 15.09.2026
+
Дата прекращения
15.09.2026
+
Причина прекращения
Окончание договора
+
Лизингодатели
+
АО Тест 1
ИНН
7707083893
+
ОГРН
1027700132195
+
+
Лизингополучатели
+
АО Тест 2
ИНН
7702070139
+
ОГРН
1027700070518
+
+
Предметы финансовой аренды (лизинга) ({count})
+ + + + +
Предмет финансовой аренды (лизинга)Описание
1
Идентификатор
нет данных
+
Классификатор
0104, Оборудование
Станок с ЧПУ
2
Идентификатор
OBJ-TEST
+
Классификатор
0104, Оборудование
Станок с ЧПУ
+
+ Заключение договора +
""" + + +def module(): + try: + return importlib.import_module("apps.parsers.leasing_details") + except ImportError: + pytest.fail("Lease detail enrichment is not implemented") + + +@pytest.mark.parametrize( + "kind", + [ + "financial-lease-contract-2", + "change-financial-lease-contract-2", + "stop-financial-lease-contract-2", + "financial-lease-contract", + ], +) +def test_public_message_keeps_contract_parties_and_each_subject(kind): + result = module().parse_leasing_html(page(kind), url=URL) + assert result["contract_number"] == "TEST-42" + assert result["contract_date"] == "2025-12-08" + assert result["lease_start_date"] == "2025-12-23" + assert result["lease_end_date"] == "2026-09-15" + assert result["lessors"] == [ + {"name": "АО Тест 1", "inn": "7707083893", "ogrn": "1027700132195"} + ] + assert result["lessees"][0]["inn"] == "7702070139" + assert len(result["subjects"]) == 2 + assert result["subjects"][0]["identifier"] is None + assert result["subjects"][1]["identifier"] == "OBJ-TEST" + assert result["subjects"][0]["description"] == "Станок с ЧПУ" + assert result["termination_reason"] == "Окончание договора" + assert ( + result["related_messages"][0]["guid"] == "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa" + ) + + +@pytest.mark.parametrize( + "url", + [ + "http://fedresurs.ru/sfactmessages/" + GUID, + "https://fedresurs.ru.evil.test/sfactmessages/" + GUID, + "https://user@fedresurs.ru/sfactmessages/" + GUID, + "https://fedresurs.ru/companies/" + GUID, + ], +) +def test_only_exact_public_message_urls_are_accepted(url): + with pytest.raises(FedresursUpstreamError, match="invalid_leasing_message_url"): + module().parse_leasing_html(page(), url=url) + + +def test_partial_subject_table_is_not_published_as_complete(): + with pytest.raises(FedresursUpstreamError, match="incomplete_leasing_subjects"): + module().parse_leasing_html(page(count=21), url=URL) + + +def test_403_is_a_source_error_not_an_empty_contract(): + with pytest.raises(FedresursUpstreamError, match="upstream_access_blocked"): + module().parse_leasing_html("403 Forbidden", url=URL) + + +def test_browser_receives_explicit_user_agent_and_closes(): + from unittest.mock import Mock + + from apps.parsers.fedresurs_browser import public_browser + + driver = Mock() + with patch( + "apps.parsers.fedresurs_browser.webdriver.Chrome", return_value=driver + ) as chrome, public_browser(user_agent="Mozilla/5.0 ECOS-LeasingCollector/1.0"): + assert ( + "--user-agent=Mozilla/5.0 ECOS-LeasingCollector/1.0" + in chrome.call_args.kwargs["options"].arguments + ) + driver.quit.assert_called_once() + + +def seed(org, guid=GUID): + return OrganizationSourceIngestionService.save_records( + source="checko_leasing", + load_batch=1, + records=[ + SourceRecordInput( + external_id=f"{org.inn}:{guid}", + title="Заключение договора лизинга", + organization_uid=org.uid, + organization_name=org.name, + inn=org.inn, + ogrn=org.ogrn, + record_type="leasing_contract", + status="published", + url=f"https://fedresurs.ru/sfactmessages/{guid}", + payload={ + "message_guid": guid, + "message_type": "FinancialLeaseContract2", + "lease_event_type": "FinancialLeaseContract2", + "message_type_name": "Заключение договора лизинга", + "publication_raw": "2026-09-17", + "publication_date": "2026-09-17", + "publisher_identifier": org.ogrn, + "participant_identifiers": [org.ogrn], + "source_url": f"https://fedresurs.ru/sfactmessages/{guid}", + "fetched_at": timezone.now().isoformat(), + "upstream": {"GUID": guid}, + }, + ) + ], + ) + + +@pytest.fixture +def organizations(db): + cache.clear() + return [ + Organization.objects.create( + name=f"АО Тест {n}", + inn=inn, + ogrn=ogrn, + okpo=f"1234567{n}", + directory_imported_at=timezone.now(), + opk_registry_membership=True, + ) + for n, (inn, ogrn) in enumerate( + [("7707083893", "1027700132195"), ("7702070139", "1027700070518")] + ) + ] + + +def run_enrichment(fetch, **kwargs): + m = module() + with patch.object( + m, "public_browser", return_value=nullcontext(Mock()) + ), patch.object(m, "fetch_leasing_html", side_effect=fetch): + return m.enrich_leasing_details(**kwargs) + + +def test_one_page_per_guid_and_second_run_uses_persisted_details(organizations): + for org in organizations: + seed(org) + before = list(OrganizationSourceRecord.objects.values_list("uid", "load_batch")) + visits = [] + + def fetch(driver, url): + visits.append(url) + return page() + + first = run_enrichment(fetch, max_pages=1) + second = run_enrichment(fetch, max_pages=1) + assert first["pages_requested"] == 1 + assert first["records_updated"] == 2 + assert second["pages_requested"] == 0 + assert len(visits) == 1 + assert ( + list(OrganizationSourceRecord.objects.values_list("uid", "load_batch")) + == before + ) + for obj in OrganizationSourceRecord.objects.all(): + assert obj.payload["lease_details"]["subjects"][1]["identifier"] == "OBJ-TEST" + + +def test_page_budget_is_enforced_and_next_run_advances(organizations): + seed(organizations[0]) + seed(organizations[0], "bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb") + first = run_enrichment(lambda driver, url: page(), max_pages=1) + assert first["pages_requested"] == 1 + assert ( + OrganizationSourceRecord.objects.filter( + payload__lease_details_status__status="loaded" + ).count() + == 1 + ) + second = run_enrichment(lambda driver, url: page(), max_pages=1) + assert second["pages_requested"] == 1 + assert ( + OrganizationSourceRecord.objects.filter( + payload__lease_details_status__status="loaded" + ).count() + == 2 + ) + + +def test_block_stops_batch_and_cooldown_prevents_next_batch(organizations): + seed(organizations[0]) + seed(organizations[0], "bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb") + + def blocked(driver, url): + raise FedresursUpstreamError("upstream_access_blocked") + + result = run_enrichment(blocked, max_pages=2) + assert result["pages_requested"] == 1 + assert result["error_code"] == "upstream_access_blocked" + assert ( + OrganizationSourceRecord.objects.filter( + payload__lease_details_status__status="failed" + ).count() + == 1 + ) + again = run_enrichment( + lambda driver, url: pytest.fail("Cooldown must prevent network access"), + max_pages=2, + ) + assert again["pages_requested"] == 0 + assert again["error_code"] == "upstream_cooldown" + + +def test_subject_identity_mismatch_never_enriches_another_organization(organizations): + seed(organizations[0]) + result = run_enrichment( + lambda driver, url: page() + .replace("7707083893", "9999999999") + .replace("1027700132195", "1999999999999"), + max_pages=1, + ) + obj = OrganizationSourceRecord.objects.get() + assert "lease_details" not in obj.payload + assert result["records_updated"] == 0 + assert result["error_code"] == "leasing_party_mismatch" + assert obj.payload["lease_details_status"]["error_code"] == "leasing_party_mismatch" + + +def test_list_and_detail_expose_enriched_subjects_without_changing_old_payload( + organizations, +): + seed(organizations[0]) + client = APIClient() + client.force_authenticate(UserFactory.create_user()) + obj = OrganizationSourceRecord.objects.get() + url = f"/api/v2/organization-source-records/{obj.uid}/" + old = client.get(url) + assert old.status_code == 200, old.data + assert "lease_details" not in old.data.get("data", old.data)["payload"] + run_enrichment(lambda driver, url: page(), max_pages=1) + detail = client.get(url) + listing = client.get( + "/api/v2/organization-source-records/", {"source": "checko_leasing"} + ) + assert detail.status_code == listing.status_code == 200 + for body in [detail.data.get("data", detail.data), listing.data["data"][0]]: + assert body["payload"]["lease_details"]["contract_number"] == "TEST-42" + assert len(body["payload"]["lease_details"]["subjects"]) == 2 + + +def test_refresh_api_details_mode_dispatches_without_loading_checko(organizations): + from apps.parsers.tasks_registry_snapshots import parse_checko_leasing + + client = APIClient() + client.force_authenticate(UserFactory.create_user(is_staff=True)) + with patch.object(parse_checko_leasing, "apply_async") as dispatch: + response = client.post( + "/api/v1/sources/checko-leasing/refresh/", + {"params": {"mode": "details"}}, + format="json", + ) + assert response.status_code == 202, response.data + assert dispatch.call_args.kwargs["kwargs"]["mode"] == "details" + + +def test_refresh_rejects_invalid_mode_before_dispatch(organizations): + client = APIClient() + client.force_authenticate(UserFactory.create_user(is_staff=True)) + response = client.post( + "/api/v1/sources/checko-leasing/refresh/", + {"params": {"mode": "unsupported"}}, + format="json", + ) + assert response.status_code == 400 + + +def test_detail_job_does_not_change_collection_success_or_use_checko(organizations): + from apps.parsers.models import ParserLoadLog + from apps.parsers.tasks_registry_snapshots import parse_checko_leasing + + seed(organizations[0]) + m = module() + with patch.object( + m, "public_browser", return_value=nullcontext(Mock()) + ), patch.object(m, "fetch_leasing_html", return_value=page()), patch( + "apps.parsers.checko_section_snapshots._client", + side_effect=AssertionError("No Checko calls in details mode"), + ): + result = parse_checko_leasing.apply(kwargs={"mode": "details"}).get() + assert result["records_updated"] == 1 + assert result["mode"] == "details" + assert ParserLoadLog.objects.count() == 0 + + +def test_next_collection_keeps_details_for_same_event(organizations): + from tempfile import TemporaryFile + + from apps.parsers.checko_section_snapshots import _collect_organization + from apps.parsers.models import ParserSourceArtifact + + seed(organizations[0]) + run_enrichment(lambda driver, url: page(), max_pages=1) + existing = OrganizationSourceRecord.objects.get() + fresh = SourceRecordInput( + external_id=existing.external_id, + title="Договор", + organization_name=organizations[0].name, + record_type="leasing_contract", + inn=organizations[0].inn, + ogrn=organizations[0].ogrn, + payload={"message_guid": GUID}, + ) + artifact = ParserSourceArtifact.objects.create(source="checko_leasing") + with TemporaryFile() as raw, patch( + "apps.parsers.checko_section_snapshots.collect_checko_section", + return_value=iter([fresh]), + ): + _collect_organization( + None, organizations[0], "checko_leasing", artifact, 0, raw + ) + staged = artifact.staged_records.get() + assert ( + staged.normalized_data["payload"]["lease_details"]["contract_number"] + == "TEST-42" + ) + + +def test_parallel_enrichment_is_rejected_before_browser_start(organizations): + seed(organizations[0]) + cache.set(module().LOCK_KEY, "other-owner", timeout=3600) + result = run_enrichment( + lambda driver, url: pytest.fail("Second collector must not navigate"), + max_pages=1, + ) + assert result["pages_requested"] == 0 + assert result["error_code"] == "enrichment_already_running" + + +def test_successful_empty_run_does_not_open_browser(organizations): + m = module() + with patch.object( + m, "public_browser", side_effect=AssertionError("No browser for empty queue") + ): + assert m.enrich_leasing_details()["pages_requested"] == 0 + + +def test_targeted_run_deduplicates_compact_and_dashed_guid(organizations): + seed(organizations[0]) + seed(organizations[1], GUID.replace("-", "").upper()) + target = OrganizationSourceRecord.objects.get( + extension__organization=organizations[0] + ) + result = run_enrichment( + lambda driver, url: page(), max_pages=1, record_uid=str(target.uid) + ) + assert result["pages_requested"] == 1 + assert result["records_updated"] == 2 + + +def test_browser_startup_failure_has_explicit_result_and_cooldown(organizations): + from selenium.common.exceptions import WebDriverException + + seed(organizations[0]) + m = module() + with patch.object( + m, "public_browser", side_effect=WebDriverException("startup failed") + ): + result = m.enrich_leasing_details(max_pages=1) + assert result["error_code"] == "browser_failure" + assert result["pages_requested"] == 0 + assert cache.get(m.COOLDOWN_KEY) + assert not cache.get(m.LOCK_KEY) + + +def test_non_admin_cannot_launch_details(organizations): + client = APIClient() + client.force_authenticate(UserFactory.create_user()) + response = client.post( + "/api/v1/sources/checko-leasing/refresh/", + {"params": {"mode": "details"}}, + format="json", + ) + assert response.status_code == 403 diff --git a/tools/openapi/source-record-extensions.json b/tools/openapi/source-record-extensions.json index 92398a0..72ee97c 100644 --- a/tools/openapi/source-record-extensions.json +++ b/tools/openapi/source-record-extensions.json @@ -1083,6 +1083,12 @@ }, "lease_event_type": { "type": "string" + }, + "lease_details": { + "$ref": "#/components/schemas/LeaseDetails" + }, + "lease_details_status": { + "$ref": "#/components/schemas/LeaseDetailsStatus" } }, "required": [ @@ -1312,5 +1318,183 @@ "$ref": "#/components/schemas/ExchangeInstrumentListPayload" } ] + }, + "LeaseParty": { + "type": "object", + "properties": { + "name": { + "type": "string", + "nullable": true + }, + "inn": { + "type": "string", + "nullable": true + }, + "ogrn": { + "type": "string", + "nullable": true + } + }, + "required": [ + "name", + "inn", + "ogrn" + ] + }, + "LeaseSubject": { + "type": "object", + "properties": { + "identifier": { + "type": "string", + "nullable": true + }, + "identifier_raw": { + "type": "string", + "nullable": true + }, + "classifier_code": { + "type": "string", + "nullable": true + }, + "classifier_name": { + "type": "string", + "nullable": true + }, + "description": { + "type": "string", + "nullable": true + } + }, + "required": [ + "identifier", + "identifier_raw", + "classifier_code", + "classifier_name", + "description" + ] + }, + "LeaseRelatedMessage": { + "type": "object", + "properties": { + "guid": { + "type": "string" + }, + "url": { + "type": "string" + }, + "label": { + "type": "string" + } + }, + "required": [ + "guid", + "url", + "label" + ] + }, + "LeaseDetails": { + "type": "object", + "properties": { + "contract_number": { + "type": "string" + }, + "contract_date": { + "type": "string", + "format": "date" + }, + "lease_start_date": { + "type": "string", + "format": "date" + }, + "lease_end_date": { + "type": "string", + "format": "date" + }, + "lessors": { + "type": "array", + "items": { + "$ref": "#/components/schemas/LeaseParty" + } + }, + "lessees": { + "type": "array", + "items": { + "$ref": "#/components/schemas/LeaseParty" + } + }, + "subjects": { + "type": "array", + "items": { + "$ref": "#/components/schemas/LeaseSubject" + } + }, + "terminated_on": { + "type": "string", + "nullable": true, + "format": "date" + }, + "termination_reason": { + "type": "string", + "nullable": true + }, + "related_messages": { + "type": "array", + "items": { + "$ref": "#/components/schemas/LeaseRelatedMessage" + } + }, + "message_text": { + "type": "string" + }, + "source_url": { + "type": "string" + }, + "fetched_at": { + "type": "string", + "format": "date-time" + } + }, + "required": [ + "contract_number", + "contract_date", + "lease_start_date", + "lease_end_date", + "lessors", + "lessees", + "subjects", + "terminated_on", + "termination_reason", + "related_messages", + "message_text", + "source_url", + "fetched_at" + ] + }, + "LeaseDetailsStatus": { + "type": "object", + "properties": { + "status": { + "type": "string", + "enum": [ + "loaded", + "failed" + ] + }, + "checked_at": { + "type": "string", + "format": "date-time" + }, + "error_code": { + "type": "string" + }, + "next_retry_at": { + "type": "string", + "format": "date-time" + } + }, + "required": [ + "status", + "checked_at" + ] } }