|
9 | 9 |
|
10 | 10 | from odoo.addons.spp_dci_client.services import DCIClient |
11 | 11 |
|
| 12 | +from .dr_parsing import extract_disability_data, extract_functional_scores, unwrap_search_data |
| 13 | + |
12 | 14 | _logger = logging.getLogger(__name__) |
13 | 15 |
|
14 | 16 |
|
@@ -122,14 +124,11 @@ def get_disability_status(self, partner) -> dict | None: |
122 | 124 |
|
123 | 125 | # Extract first result |
124 | 126 | search_response = message["search_response"][0] |
125 | | - if "data" not in search_response or not search_response["data"]: |
| 127 | + records = unwrap_search_data(search_response.get("data")) |
| 128 | + if not records: |
126 | 129 | return None |
127 | 130 |
|
128 | | - record_data = ( |
129 | | - search_response["data"][0] if isinstance(search_response["data"], list) else search_response["data"] |
130 | | - ) |
131 | | - |
132 | | - # Extract disability information |
| 131 | + record_data = records[0] |
133 | 132 | disability_data = self._extract_disability_data(record_data) |
134 | 133 |
|
135 | 134 | _logger.info( |
@@ -207,14 +206,11 @@ def get_functional_assessment(self, identifier_type: str, identifier_value: str) |
207 | 206 |
|
208 | 207 | # Extract first result |
209 | 208 | search_response = message["search_response"][0] |
210 | | - if "data" not in search_response or not search_response["data"]: |
| 209 | + records = unwrap_search_data(search_response.get("data")) |
| 210 | + if not records: |
211 | 211 | return None |
212 | 212 |
|
213 | | - record_data = ( |
214 | | - search_response["data"][0] if isinstance(search_response["data"], list) else search_response["data"] |
215 | | - ) |
216 | | - |
217 | | - # Extract functional scores |
| 213 | + record_data = records[0] |
218 | 214 | scores = self._extract_functional_scores(record_data) |
219 | 215 |
|
220 | 216 | _logger.info( |
@@ -389,103 +385,29 @@ def _get_partner_identifier(self, partner): |
389 | 385 | return None |
390 | 386 |
|
391 | 387 | def _extract_disability_data(self, record_data: dict) -> dict: |
392 | | - """Extract disability information from DCI record data. |
| 388 | + """Extract disability information from a DCI v1.0.0 record. |
| 389 | +
|
| 390 | + Delegates to the stateless module-level helper in dr_parsing. |
393 | 391 |
|
394 | 392 | Args: |
395 | | - record_data: DCI record data from search response |
| 393 | + record_data: A single record dict from reg_records |
396 | 394 |
|
397 | 395 | Returns: |
398 | 396 | dict: Extracted disability data |
399 | 397 | """ |
400 | | - disability_data = { |
401 | | - "has_disability": False, |
402 | | - "disability_types": [], |
403 | | - "functional_scores": {}, |
404 | | - "raw_data": record_data, |
405 | | - } |
406 | | - |
407 | | - # Check for disability flag |
408 | | - if "has_disability" in record_data: |
409 | | - disability_data["has_disability"] = bool(record_data["has_disability"]) |
410 | | - elif "is_pwd" in record_data: |
411 | | - disability_data["has_disability"] = bool(record_data["is_pwd"]) |
412 | | - |
413 | | - # Extract disability types |
414 | | - if "disability_types" in record_data: |
415 | | - types_data = record_data["disability_types"] |
416 | | - if isinstance(types_data, list): |
417 | | - disability_data["disability_types"] = types_data |
418 | | - elif isinstance(types_data, str): |
419 | | - # Handle comma-separated string |
420 | | - disability_data["disability_types"] = [t.strip() for t in types_data.split(",") if t.strip()] |
421 | | - |
422 | | - # Extract functional scores |
423 | | - disability_data["functional_scores"] = self._extract_functional_scores(record_data) |
424 | | - |
425 | | - # Extract assessment date |
426 | | - if "assessment_date" in record_data: |
427 | | - disability_data["assessment_date"] = record_data["assessment_date"] |
428 | | - elif "disability_assessment_date" in record_data: |
429 | | - disability_data["assessment_date"] = record_data["disability_assessment_date"] |
430 | | - |
431 | | - # Extract source registry |
432 | | - if "source_registry" in record_data: |
433 | | - disability_data["source_registry"] = record_data["source_registry"] |
434 | | - elif "registry_name" in record_data: |
435 | | - disability_data["source_registry"] = record_data["registry_name"] |
436 | | - |
437 | | - return disability_data |
| 398 | + return extract_disability_data(record_data) |
438 | 399 |
|
439 | 400 | def _extract_functional_scores(self, record_data: dict) -> dict: |
440 | | - """Extract functional assessment scores from record data. |
| 401 | + """Return functional assessment scores from a DCI v1.0.0 record. |
| 402 | +
|
| 403 | + Delegates to the stateless module-level helper in dr_parsing. |
| 404 | + The DCI v1.0.0 spec has no numeric functional scores, so this always |
| 405 | + returns ``{}``. |
441 | 406 |
|
442 | 407 | Args: |
443 | | - record_data: DCI record data |
| 408 | + record_data: A single record dict from reg_records |
444 | 409 |
|
445 | 410 | Returns: |
446 | | - dict: Functional scores by domain |
447 | | - Example: {'Vision': 3, 'Hearing': 1, 'Mobility': 4, ...} |
| 411 | + dict: Always ``{}`` |
448 | 412 | """ |
449 | | - scores = {} |
450 | | - |
451 | | - # Try to extract from functional_scores field |
452 | | - if "functional_scores" in record_data: |
453 | | - scores_data = record_data["functional_scores"] |
454 | | - if isinstance(scores_data, dict): |
455 | | - scores = scores_data |
456 | | - elif isinstance(scores_data, str): |
457 | | - # Try to parse JSON string |
458 | | - try: |
459 | | - scores = json.loads(scores_data) |
460 | | - except json.JSONDecodeError: |
461 | | - _logger.warning("Failed to parse functional_scores JSON") |
462 | | - |
463 | | - # Try to extract individual domain scores |
464 | | - functional_domains = [ |
465 | | - "Vision", |
466 | | - "Hearing", |
467 | | - "Mobility", |
468 | | - "Cognition", |
469 | | - "SelfCare", |
470 | | - "Communication", |
471 | | - ] |
472 | | - |
473 | | - for domain in functional_domains: |
474 | | - # Try various field name formats |
475 | | - for field_name in [ |
476 | | - f"functional_{domain.lower()}", |
477 | | - f"{domain.lower()}_score", |
478 | | - domain.lower(), |
479 | | - domain, |
480 | | - ]: |
481 | | - if field_name in record_data and record_data[field_name]: |
482 | | - try: |
483 | | - scores[domain] = int(record_data[field_name]) |
484 | | - except (ValueError, TypeError): |
485 | | - _logger.warning( |
486 | | - "Invalid functional score for %s: %s", |
487 | | - domain, |
488 | | - record_data[field_name], |
489 | | - ) |
490 | | - |
491 | | - return scores |
| 413 | + return extract_functional_scores(record_data) |
0 commit comments