diff --git a/service/src/main/inse/works/logic.py b/service/src/main/inse/works/logic.py index eb36f5a9..c2f7b52a 100644 --- a/service/src/main/inse/works/logic.py +++ b/service/src/main/inse/works/logic.py @@ -71,6 +71,7 @@ async def get_author_works( ) elements, total = _parse_full_work_search_response(data) + await _enrich_work_topic_descriptions(elements) offset = (page - 1) * size last_page = offset + len(elements) >= total @@ -202,9 +203,45 @@ async def get_work( elements, _ = _parse_full_work_search_response(response_by_title) if not elements: return None + await _enrich_work_topic_descriptions(elements[:1]) return elements[0] +async def _enrich_work_topic_descriptions(works: List[Work]) -> None: + """Fill in topic descriptions missing from raw work documents. + """ + topic_ids = set() + + for work in works: + for topic in work.topics or []: + if topic.id and not topic.description: + topic_ids.add(topic.id) + + primary = work.primary_topic + if primary and primary.id and not primary.description: + topic_ids.add(primary.id) + + if not topic_ids: + return + + details = await _get_topic_details(list(topic_ids)) + + if not details: + return + + for work in works: + for topic in work.topics or []: + info = details.get(topic.id) + if info and not topic.description: + topic.description = info.get("description") + + primary = work.primary_topic + if primary and primary.id: + info = details.get(primary.id) + if info and not primary.description: + primary.description = info.get("description") + + async def _get_topic_details(topic_ids: List[str]) -> dict[str, dict]: if not topic_ids: return {}