Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9ed0eb9

Browse files
34494 process courtOrders in correction (#4775)
1 parent 5c521ee commit 9ed0eb9

11 files changed

Lines changed: 442 additions & 24 deletions

File tree

legal-api/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "legal-api"
3-
version = "3.1.27"
3+
version = "3.1.28"
44
description = ""
55
authors = [
66
{name = "thor",email = "[email protected]"}

legal-api/src/legal_api/core/filing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ def _populate_completed_filing_documents( # noqa: PLR0913
603603
)
604604
if (
605605
not static_invisible and
606-
(static_docs := FilingMeta.get_static_documents(filing.storage, f"{base_url}{doc_url}/static"))
606+
(static_docs := FilingMeta.get_static_documents(business, filing.storage, f"{base_url}{doc_url}/static"))
607607
):
608608
documents["documents"]["staticDocuments"] = static_docs
609609

legal-api/src/legal_api/core/meta/filing.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from enum import Enum, auto
1919
from typing import Final
2020

21+
from flask import current_app, url_for
22+
2123
from business_model.models import Business, DocumentType
2224
from business_model.models import Filing as FilingStorage
2325
from legal_api.services import VersionedBusinessDetailsService as VersionService
@@ -1070,15 +1072,18 @@ def alter_outputs_special_resolution(filing, outputs):
10701072
return outputs
10711073

10721074
@staticmethod
1073-
def get_static_documents(filing, url_prefix):
1075+
def get_static_documents(business, filing, url_prefix):
10741076
"""Get static documents."""
10751077
outputs = []
10761078
if filing.filing_type == "continuationIn":
10771079
FilingMeta._get_continuation_in_static_documents(filing, url_prefix, outputs)
10781080
elif filing.filing_type == "continuationOut":
10791081
FilingMeta._get_continuation_out_static_documents(filing, url_prefix, outputs)
10801082
elif filing.filing_type == "courtOrder":
1081-
FilingMeta._get_court_order_static_documents(filing, url_prefix, outputs)
1083+
court_order = filing.meta_data.get("courtOrder", {})
1084+
FilingMeta._get_court_order_static_documents(court_order, url_prefix, outputs)
1085+
elif filing.filing_type == "correction":
1086+
FilingMeta._get_correction_static_documents(business, filing, outputs)
10821087
return outputs
10831088

10841089
@staticmethod
@@ -1108,8 +1113,7 @@ def _get_continuation_out_static_documents(filing, url_prefix, outputs):
11081113
})
11091114

11101115
@staticmethod
1111-
def _get_court_order_static_documents(filing, url_prefix, outputs):
1112-
court_order = filing.meta_data.get("courtOrder", {})
1116+
def _get_court_order_static_documents(court_order, url_prefix, outputs):
11131117
if files := court_order.get("files"):
11141118
for file in files:
11151119
file_key = file.get("fileKey")
@@ -1124,6 +1128,18 @@ def _get_court_order_static_documents(filing, url_prefix, outputs):
11241128
"documentType": document_type
11251129
})
11261130

1131+
@staticmethod
1132+
def _get_correction_static_documents(business, filing, outputs):
1133+
base_url = current_app.config.get("BUSINESS_API_GW_URL")
1134+
court_orders = filing.meta_data.get("courtOrders", [])
1135+
for court_order in court_orders:
1136+
doc_url = url_for("API2.get_documents",
1137+
identifier=business.identifier,
1138+
filing_id=court_order.get("filingId"),
1139+
legal_filing_name=None)
1140+
url_prefix = f"{base_url}{doc_url}/static"
1141+
FilingMeta._get_court_order_static_documents(court_order, url_prefix, outputs)
1142+
11271143
@staticmethod
11281144
def get_display_name(legal_type: str, filing_type: str, filing_sub_type: str | None = None) -> str:
11291145
"""Return display name for filing."""

queue_services/business-filer/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "business-filer"
3-
version = "3.0.31"
3+
version = "3.0.32"
44
description = "Business Registry Filer Service"
55
authors = [
66
{name = "thor",email = "[email protected]"}

queue_services/business-filer/src/business_filer/filing_processors/filing_components/correction.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ def correct_business_data(business: Business, # noqa: PLR0915
155155
update_relationship_entity_info(relationships, business)
156156
_set_lear_only(correction_filing, correction_filing_rec, relationships, business)
157157

158+
# update court orders, if any is present
159+
with suppress(IndexError, KeyError, TypeError):
160+
court_orders_json = dpath.get(correction_filing, "/correction/courtOrders")
161+
filings.update_court_orders(business, court_orders_json, filing_meta)
162+
158163
# update court order, if any is present
159164
with suppress(IndexError, KeyError, TypeError):
160165
court_order_json = dpath.get(correction_filing, "/correction/courtOrder")

queue_services/business-filer/src/business_filer/filing_processors/filing_components/filings.py

Lines changed: 105 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,19 @@
3737
from business_common.utils import datetime
3838
from business_model.models import Business, CourtOrder, Filing
3939
from business_model.models.types.filings import FilingTypes
40-
from flask_babel import _ as babel
4140

4241
from business_filer.filing_meta import FilingMeta
42+
from business_filer.filing_processors.filing_components import documents
43+
from business_filer.services.utils import is_same_str
4344

4445

45-
def create_court_order(filing_submission: Filing,
46+
def create_court_order(filing: Filing,
4647
court_order: dict,
47-
filing_meta: FilingMeta,
48+
filing_meta: FilingMeta = None,
4849
new_business: Business = None) -> dict | None:
4950
"""Create a court order."""
50-
if not filing_submission:
51-
return {"error": babel("Filing is required before a court order can be created.")}
52-
5351
if not (file_number := court_order.get("fileNumber")):
54-
return
52+
return None
5553

5654
court_order_obj = CourtOrder(
5755
file_number=file_number,
@@ -61,20 +59,111 @@ def create_court_order(filing_submission: Filing,
6159
with suppress(IndexError, KeyError, TypeError, ValueError):
6260
court_order_obj.order_date = datetime.fromisoformat(court_order.get("orderDate"))
6361

64-
filing_meta.court_order = {"fileNumber": file_number}
62+
court_order_meta = {"fileNumber": file_number}
6563
if court_order_obj.effect_of_order:
66-
filing_meta.court_order["effectOfOrder"] = court_order_obj.effect_of_order
64+
court_order_meta["effectOfOrder"] = court_order_obj.effect_of_order
6765
if court_order.get("orderDate"):
68-
filing_meta.court_order["orderDate"] = court_order.get("orderDate")
69-
if filing_submission.filing_type == FilingTypes.COURTORDER.value:
66+
court_order_meta["orderDate"] = court_order.get("orderDate")
67+
if filing.filing_type == FilingTypes.COURTORDER:
7068
# Only add order details for court orders
71-
filing_meta.court_order["orderDetails"] = court_order_obj.order_details
69+
court_order_meta["orderDetails"] = court_order_obj.order_details
70+
71+
if filing_meta:
72+
filing_meta.court_order = court_order_meta
7273

7374
if new_business:
74-
court_order_obj.filing_id = filing_submission.id
75+
court_order_obj.filing_id = filing.id
7576
new_business.court_orders.append(court_order_obj)
7677
else:
77-
court_order_obj.business_id = filing_submission.business_id
78-
filing_submission.court_orders.append(court_order_obj)
78+
court_order_obj.business_id = filing.business_id
79+
filing.court_orders.append(court_order_obj)
80+
81+
return court_order_meta
82+
83+
84+
def update_court_order(court_order: dict, filing: Filing, court_order_meta: dict) -> bool:
85+
"""Update a court order."""
86+
has_changed = False
87+
file_number = court_order.get("fileNumber")
88+
effect_of_order = court_order.get("effectOfOrder")
89+
order_details = court_order.get("orderDetails")
90+
91+
court_order_obj = CourtOrder.get_by_id(court_order.get("id"))
92+
if not is_same_str(court_order_obj.file_number, file_number):
93+
court_order_meta["fileNumber"] = file_number
94+
has_changed = True
95+
if not is_same_str(court_order_obj.effect_of_order, effect_of_order):
96+
court_order_meta["effectOfOrder"] = effect_of_order
97+
has_changed = True
98+
if not is_same_str(court_order_obj.order_details, order_details):
99+
court_order_meta["orderDetails"] = order_details
100+
has_changed = True
101+
102+
if has_changed:
103+
court_order_obj.file_number = file_number
104+
court_order_obj.effect_of_order = effect_of_order
105+
court_order_obj.order_details = order_details
106+
filing.court_orders.append(court_order_obj)
107+
108+
return has_changed
109+
110+
111+
def update_court_orders(business: Business,
112+
court_orders: list[dict],
113+
filing_meta: FilingMeta) -> None:
114+
"""Update court orders."""
115+
court_orders_meta = []
116+
for court_order in court_orders:
117+
filing = Filing.find_by_id(court_order.get("filingId"))
118+
119+
has_changed = False
120+
court_order_meta = {"filingId": filing.id}
121+
if court_order.get("id"):
122+
has_changed = update_court_order(court_order, filing, court_order_meta)
123+
else:
124+
court_order_meta = {
125+
**court_order_meta,
126+
**create_court_order(filing, court_order)
127+
}
128+
has_changed = True
129+
130+
documents_changed = update_court_order_documents(court_order, filing, business, court_order_meta)
131+
if has_changed or documents_changed:
132+
# Only add court order if there are any changes
133+
court_orders_meta.append(court_order_meta)
134+
135+
if court_orders_meta:
136+
filing_meta.court_orders = court_orders_meta
137+
138+
139+
def update_court_order_documents(court_order: dict, filing: Filing, business: Business, court_order_meta: dict) -> bool:
140+
"""Update court order documents if the filing is a court order."""
141+
if (
142+
filing.filing_type != FilingTypes.COURTORDER or
143+
"files" not in court_order
144+
):
145+
return False
146+
147+
documents_changed = False
148+
files = court_order.get("files", [])
149+
new_files = []
150+
if court_order_documents := filing.documents.all():
151+
existing_file_keys = [document.file_key for document in court_order_documents]
152+
new_files = [file for file in files if file.get("fileKey") not in existing_file_keys]
153+
deleted_documents = [
154+
document
155+
for document in court_order_documents
156+
if document.file_key not in [file.get("fileKey") for file in files]
157+
]
158+
if deleted_documents:
159+
for document in deleted_documents:
160+
filing.documents.remove(document)
161+
documents_changed = True
162+
else:
163+
new_files = files
79164

80-
return None
165+
if new_files:
166+
file_list = documents.create_filing_documents(new_files, business, filing)
167+
court_order_meta["files"] = file_list
168+
documents_changed = True
169+
return documents_changed

queue_services/business-filer/src/business_filer/services/filer.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,4 +347,13 @@ def process_filing(filing_message: FilingMessage): # noqa: PLR0915, PLR0912
347347
current_app.logger.warning(err.with_traceback(None))
348348
current_app.logger.warning(f"Failed to create DRS Record for {filing_submission.id}.")
349349

350+
if filing_type == FilingTypes.CORRECTION:
351+
try:
352+
# Update DRS record for court orders
353+
PublishEvent.publish_drs_update_message_court_orders(current_app, business, filing_submission)
354+
except Exception as err:
355+
# log error for ops, but don't prevent filing from completing
356+
current_app.logger.warning(err.with_traceback(None))
357+
current_app.logger.warning(f"Failed to update DRS Record for court orders in {filing_submission.id}.")
358+
350359
PublishEvent.publish_event(current_app, business, filing_submission)

queue_services/business-filer/src/business_filer/services/publish_event.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,37 @@ def publish_drs_update_message(app: Flask, business: Business, filing: Filing):
109109
except Exception as err: # pylint: disable=broad-except;
110110
raise PublishException(err) from err
111111

112+
@staticmethod
113+
def publish_drs_update_message_court_orders(app: Flask, business: Business, filing: Filing):
114+
"""Publish a drs update record message for each DRS document uploaded with the filing.
115+
116+
Updates the document record(s) with filing/business information once a filing completes,
117+
so the document shows up correctly in the ledger and is searchable in the DRS UI.
118+
"""
119+
try:
120+
subject = app.config.get("DOC_UPDATE_REC_TOPIC")
121+
for court_order in filing.meta_data.get("courtOrders", []):
122+
if not (files := court_order.get("files")):
123+
continue
124+
125+
for file in files:
126+
if not PublishEvent._is_drs_document(file.get("fileKey")):
127+
continue
128+
data = {
129+
"accountId": "business-api",
130+
"fileKey": file.get("fileKey"),
131+
"businessIdentifier": business.identifier if business else None,
132+
"filingDate": filing.completion_date.isoformat() if filing.completion_date else None,
133+
"filingId": court_order.get("filingId")
134+
}
135+
data = {k: v for k, v in data.items() if v is not None}
136+
137+
ce = PublishEvent._create_cloud_event(app, business, filing, subject, data)
138+
gcp_queue.publish(subject, to_queue_message(ce))
139+
140+
except Exception as err: # pylint: disable=broad-except;
141+
raise PublishException(err) from err
142+
112143
@staticmethod
113144
def _is_drs_document(file_key: str) -> bool:
114145
"""Return True if the file_key is a DRS key."""

queue_services/business-filer/src/business_filer/services/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,13 @@ def get_int(filing: dict, path: str) -> str:
8585
return int(raw)
8686
except (IndexError, KeyError, TypeError, ValueError):
8787
return None
88+
89+
90+
def normalize_str(value: str) -> str:
91+
"""Convert None or empty values to a stripped uppercase string."""
92+
return (value or "").strip().upper()
93+
94+
95+
def is_same_str(str1: str, str2: str) -> bool:
96+
"""Check if two strings are the same after normalization."""
97+
return normalize_str(str1) == normalize_str(str2)

queue_services/business-filer/tests/unit/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,7 @@ def factory_completed_filing(business, data_dict, filing_date=FROZEN_DATETIME, p
612612
filing.business_id = business.id
613613
filing.filing_date = filing_date
614614
filing.filing_json = data_dict
615+
filing._filing_type = data_dict.get('filing', {}).get('header', {}).get('name', '')
615616
filing.save()
616617

617618
transaction_id = VersioningProxy.get_transaction_id(db.session())

0 commit comments

Comments
 (0)