-
Notifications
You must be signed in to change notification settings - Fork 5
fix: DCI envelope support for disability registry search (#217) #303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Tarekchehahde
wants to merge
1
commit into
OpenSPP:19.0
Choose a base branch
from
Tarekchehahde:fix/217-disability-dci-envelope-search
base: 19.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,37 +11,100 @@ | |
| """ | ||
|
|
||
| import logging | ||
| from datetime import datetime | ||
| import uuid | ||
| from datetime import UTC, datetime | ||
| from typing import Annotated | ||
|
|
||
| from odoo.api import Environment | ||
|
|
||
| from odoo.addons.fastapi.dependencies import odoo_env | ||
| from odoo.addons.spp_dci.schemas import DCICallbackHeader, DCIEnvelope | ||
| from odoo.addons.spp_dci.schemas.constants import MsgHeaderStatusReasonCode | ||
| from odoo.addons.spp_dci.schemas.search import ( | ||
| SearchRequest, | ||
| SearchResponse, | ||
| SearchResponseItem, | ||
| ) | ||
| from odoo.addons.spp_dci.services import get_sender_id | ||
|
|
||
| from fastapi import APIRouter, Depends, status | ||
| from fastapi import APIRouter, Depends, HTTPException, status | ||
|
|
||
| from ..middleware.signature import verify_bearer_token | ||
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def _build_stub_search_envelope( | ||
| request_envelope: DCIEnvelope, | ||
| env: Environment, | ||
| not_implemented_message: str, | ||
| ) -> DCIEnvelope: | ||
| """Return a DCI on-search envelope with per-item rjct stubs.""" | ||
| try: | ||
| search_request = SearchRequest.model_validate(request_envelope.message) | ||
| except Exception as e: | ||
| _logger.error("Invalid SearchRequest message: %s", str(e)) | ||
| raise HTTPException( | ||
| status_code=status.HTTP_400_BAD_REQUEST, | ||
| detail=f"Invalid search request message: {str(e)}", | ||
| ) from e | ||
|
|
||
| response_items = [] | ||
| for req_item in search_request.search_request: | ||
| response_items.append( | ||
| SearchResponseItem( | ||
| reference_id=req_item.reference_id, | ||
| timestamp=datetime.now(UTC), | ||
| status="rjct", | ||
| status_reason_code=MsgHeaderStatusReasonCode.ACTION_NOT_SUPPORTED.value, | ||
| status_reason_message=not_implemented_message, | ||
| data=None, | ||
| pagination=None, | ||
| locale=req_item.locale, | ||
| ) | ||
| ) | ||
|
|
||
| search_response = SearchResponse( | ||
| transaction_id=search_request.transaction_id, | ||
| correlation_id=str(uuid.uuid4()), | ||
| search_response=response_items, | ||
| ) | ||
|
|
||
| our_sender_id = get_sender_id(env) or "openspp" | ||
| total_count = len(response_items) | ||
|
|
||
| callback_header = DCICallbackHeader( | ||
| version=request_envelope.header.version, | ||
| message_id=str(uuid.uuid4()), | ||
| message_ts=datetime.now(UTC), | ||
| action=f"on-{request_envelope.header.action}", | ||
| sender_id=our_sender_id, | ||
| receiver_id=request_envelope.header.sender_id, | ||
| total_count=total_count, | ||
| status="rjct", | ||
| status_reason_code=MsgHeaderStatusReasonCode.ACTION_NOT_SUPPORTED.value, | ||
| status_reason_message=not_implemented_message, | ||
| completed_count=0, | ||
| ) | ||
|
|
||
| return DCIEnvelope( | ||
| signature="", | ||
| header=callback_header, | ||
| message=search_response.model_dump(mode="json", exclude_none=True), | ||
| ) | ||
|
|
||
|
|
||
| # Disability Registry router | ||
| disability_router = APIRouter(tags=["Disability Registry"], prefix="/disability/registry") | ||
|
|
||
|
|
||
| @disability_router.post( | ||
| "/sync/search", | ||
| response_model=SearchResponse, | ||
| response_model=DCIEnvelope, | ||
| response_model_exclude_none=True, | ||
| status_code=status.HTTP_501_NOT_IMPLEMENTED, | ||
| ) | ||
| async def disability_sync_search( | ||
| request: SearchRequest, | ||
| request_envelope: DCIEnvelope, | ||
| env: Annotated[Environment, Depends(odoo_env)], | ||
| _bearer_token: Annotated[str, Depends(verify_bearer_token)], | ||
| ): | ||
|
|
@@ -54,29 +117,10 @@ async def disability_sync_search( | |
| For now, returns "not implemented" response for SPDCI compliance testing. | ||
| """ | ||
| _logger.warning("Disability Registry search endpoint called but not yet implemented") | ||
|
|
||
| # Build response items with "not implemented" status | ||
| response_items = [] | ||
| for req_item in request.search_request: | ||
| response_items.append( | ||
| SearchResponseItem( | ||
| reference_id=req_item.reference_id, | ||
| timestamp=datetime.utcnow(), | ||
| status="rjct", | ||
| status_reason_code=MsgHeaderStatusReasonCode.ACTION_NOT_SUPPORTED.value, | ||
| status_reason_message=( | ||
| "Disability Registry not yet implemented. Will be available in spp_dci_server_disability module." | ||
| ), | ||
| data=None, | ||
| pagination=None, | ||
| locale=req_item.locale, | ||
| ) | ||
| ) | ||
|
|
||
| return SearchResponse( | ||
| transaction_id=request.transaction_id, | ||
| correlation_id=None, | ||
| search_response=response_items, | ||
| return _build_stub_search_envelope( | ||
| request_envelope, | ||
| env, | ||
| "Disability Registry not yet implemented. Will be available in spp_dci_server_disability module.", | ||
| ) | ||
|
|
||
|
|
||
|
|
@@ -91,15 +135,15 @@ async def disability_sync_notify( | |
| """ | ||
| SPDCI-compliant Disability Registry notification endpoint. | ||
|
|
||
| Path: /disability/registry/sync/notify | ||
| Path: /disability/registry/sync/search | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| This endpoint will be implemented when the Disability Registry module is added. | ||
| """ | ||
| _logger.warning("Disability Registry notify endpoint called but not yet implemented") | ||
| return { | ||
| "status": "rjct", | ||
| "message": "Disability Registry not yet implemented", | ||
| "timestamp": datetime.utcnow().isoformat(), | ||
| "timestamp": datetime.now(UTC).isoformat(), | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -109,12 +153,11 @@ async def disability_sync_notify( | |
|
|
||
| @crvs_router.post( | ||
| "/sync/search", | ||
| response_model=SearchResponse, | ||
| response_model=DCIEnvelope, | ||
| response_model_exclude_none=True, | ||
| status_code=status.HTTP_501_NOT_IMPLEMENTED, | ||
| ) | ||
| async def crvs_sync_search( | ||
| request: SearchRequest, | ||
| request_envelope: DCIEnvelope, | ||
| env: Annotated[Environment, Depends(odoo_env)], | ||
| _bearer_token: Annotated[str, Depends(verify_bearer_token)], | ||
| ): | ||
|
|
@@ -127,29 +170,10 @@ async def crvs_sync_search( | |
| For now, returns "not implemented" response for SPDCI compliance testing. | ||
| """ | ||
| _logger.warning("Civil Registry search endpoint called but not yet implemented") | ||
|
|
||
| # Build response items with "not implemented" status | ||
| response_items = [] | ||
| for req_item in request.search_request: | ||
| response_items.append( | ||
| SearchResponseItem( | ||
| reference_id=req_item.reference_id, | ||
| timestamp=datetime.utcnow(), | ||
| status="rjct", | ||
| status_reason_code=MsgHeaderStatusReasonCode.ACTION_NOT_SUPPORTED.value, | ||
| status_reason_message=( | ||
| "Civil Registry not yet implemented. Will be available in spp_dci_server_crvs module." | ||
| ), | ||
| data=None, | ||
| pagination=None, | ||
| locale=req_item.locale, | ||
| ) | ||
| ) | ||
|
|
||
| return SearchResponse( | ||
| transaction_id=request.transaction_id, | ||
| correlation_id=None, | ||
| search_response=response_items, | ||
| return _build_stub_search_envelope( | ||
| request_envelope, | ||
| env, | ||
| "Civil Registry not yet implemented. Will be available in spp_dci_server_crvs module.", | ||
| ) | ||
|
|
||
|
|
||
|
|
@@ -172,7 +196,7 @@ async def crvs_sync_notify( | |
| return { | ||
| "status": "rjct", | ||
| "message": "Civil Registry not yet implemented", | ||
| "timestamp": datetime.utcnow().isoformat(), | ||
| "timestamp": datetime.now(UTC).isoformat(), | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -182,12 +206,11 @@ async def crvs_sync_notify( | |
|
|
||
| @farmer_router.post( | ||
| "/sync/search", | ||
| response_model=SearchResponse, | ||
| response_model=DCIEnvelope, | ||
| response_model_exclude_none=True, | ||
| status_code=status.HTTP_501_NOT_IMPLEMENTED, | ||
| ) | ||
| async def farmer_sync_search( | ||
| request: SearchRequest, | ||
| request_envelope: DCIEnvelope, | ||
| env: Annotated[Environment, Depends(odoo_env)], | ||
| _bearer_token: Annotated[str, Depends(verify_bearer_token)], | ||
| ): | ||
|
|
@@ -200,29 +223,10 @@ async def farmer_sync_search( | |
| For now, returns "not implemented" response for SPDCI compliance testing. | ||
| """ | ||
| _logger.warning("Farmer Registry search endpoint called but not yet implemented") | ||
|
|
||
| # Build response items with "not implemented" status | ||
| response_items = [] | ||
| for req_item in request.search_request: | ||
| response_items.append( | ||
| SearchResponseItem( | ||
| reference_id=req_item.reference_id, | ||
| timestamp=datetime.utcnow(), | ||
| status="rjct", | ||
| status_reason_code=MsgHeaderStatusReasonCode.ACTION_NOT_SUPPORTED.value, | ||
| status_reason_message=( | ||
| "Farmer Registry not yet implemented. Will be available in spp_dci_server_farmer module." | ||
| ), | ||
| data=None, | ||
| pagination=None, | ||
| locale=req_item.locale, | ||
| ) | ||
| ) | ||
|
|
||
| return SearchResponse( | ||
| transaction_id=request.transaction_id, | ||
| correlation_id=None, | ||
| search_response=response_items, | ||
| return _build_stub_search_envelope( | ||
| request_envelope, | ||
| env, | ||
| "Farmer Registry not yet implemented. Will be available in spp_dci_server_farmer module.", | ||
| ) | ||
|
|
||
|
|
||
|
|
@@ -245,5 +249,5 @@ async def farmer_sync_notify( | |
| return { | ||
| "status": "rjct", | ||
| "message": "Farmer Registry not yet implemented", | ||
| "timestamp": datetime.utcnow().isoformat(), | ||
| "timestamp": datetime.now(UTC).isoformat(), | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of manually constructing the
DCICallbackHeaderand returning an unsignedDCIEnvelope(withsignature=""), you can leverage the existingbuild_signed_envelopehelper fromspp_dci.services.response_helpers. This reduces boilerplate, ensures consistency, and automatically signs the envelope if a signing key is configured in the environment.