Skip to content
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

24195 tweaks on filing queries #3166

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion legal-api/src/legal_api/core/filing.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def get_filings_by_status(business_id: int, status: list, after_date: date = Non
@staticmethod
def get_most_recent_filing_json(business_id: str, filing_type: str = None, jwt: JwtManager = None):
"""Return the most recent filing json."""
if storage := FilingStorage.get_most_recent_legal_filing(business_id, filing_type):
if storage := FilingStorage.get_most_recent_filing(business_id, filing_type):
submitter_displayname = REDACTED_STAFF_SUBMITTER
if (submitter := storage.filing_submitter) \
and submitter.username and jwt \
Expand Down
1 change: 0 additions & 1 deletion legal-api/src/legal_api/models/business.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,6 @@ def json(self, slim=False):
'lastLedgerTimestamp': self.last_ledger_timestamp.isoformat(),
'lastAddressChangeDate': '',
'lastDirectorChangeDate': '',
'lastModified': self.last_modified.isoformat(),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

included in _slim_json

'naicsKey': self.naics_key,
'naicsCode': self.naics_code,
'naicsDescription': self.naics_description,
Expand Down
43 changes: 20 additions & 23 deletions legal-api/src/legal_api/models/filing.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ def get_temp_reg_filing(temp_reg_id: str, filing_id: str = None):
q = db.session.query(Filing).filter(Filing.temp_reg == temp_reg_id)

if filing_id:
q.filter(Filing.id == filing_id)
q = q.filter(Filing.id == filing_id)

filing = q.one_or_none()
return filing
Expand Down Expand Up @@ -896,7 +896,7 @@ def get_filings_by_types(business_id: int, filing_types):
filter(Filing.business_id == business_id). \
filter(Filing._filing_type.in_(filing_types)). \
filter(Filing._status == Filing.Status.COMPLETED.value). \
order_by(desc(Filing.effective_date)). \
order_by(desc(Filing.transaction_id)). \
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

order with transaction_id for COMPLETED filing queries

all()
return filings

Expand Down Expand Up @@ -956,23 +956,21 @@ def get_filings_by_type_pairs(business_id: int, filing_type_pairs: list, status:
return filings

@staticmethod
def get_a_businesses_most_recent_filing_of_a_type(business_id: int, filing_type: str, filing_sub_type: str = None):
"""Return the filings of a particular type."""
max_filing = db.session.query(db.func.max(Filing._filing_date).label('last_filing_date')).\
filter(Filing._filing_type == filing_type). \
filter(Filing.business_id == business_id)
if filing_sub_type:
max_filing = max_filing.filter(Filing._filing_sub_type == filing_sub_type)
max_filing = max_filing.subquery()
def get_most_recent_filing(business_id: str, filing_type: str = None, filing_sub_type: str = None):
"""Return the most recent filing.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplified and generalize this function


filing = Filing.query.join(max_filing, Filing._filing_date == max_filing.c.last_filing_date). \
filing_type is required, if filing_sub_type is provided, it will be used to filter the query.
"""
query = db.session.query(Filing). \
filter(Filing.business_id == business_id). \
filter(Filing._filing_type == filing_type). \
filter(Filing._status == Filing.Status.COMPLETED.value)
if filing_sub_type:
filing = filing.filter(Filing._filing_sub_type == filing_sub_type)
if filing_type:
query = query.filter(Filing._filing_type == filing_type)
if filing_sub_type:
query = query.filter(Filing._filing_sub_type == filing_sub_type)

return filing.one_or_none()
query = query.order_by(Filing.transaction_id.desc())
return query.first()

@staticmethod
def get_most_recent_legal_filing(business_id: str, filing_type: str = None):
Expand Down Expand Up @@ -1037,15 +1035,14 @@ def get_all_filings_by_status(status):
@staticmethod
def get_previous_completed_filing(filing):
"""Return the previous completed filing."""
filings = db.session.query(Filing). \
query = db.session.query(Filing). \
filter(Filing.business_id == filing.business_id). \
filter(Filing._status == Filing.Status.COMPLETED.value). \
filter(Filing.id < filing.id). \
filter(Filing.effective_date < filing.effective_date). \
order_by(Filing.effective_date.desc()).all()
if filings:
return filings[0]
return None
filter(Filing._status == Filing.Status.COMPLETED.value)

if filing.transaction_id: # transaction_id will be None for the pending filings (intermediate state)
query = query.filter(Filing.transaction_id < filing.transaction_id)

return query.order_by(Filing.transaction_id.desc()).first()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

order with transaction_id for COMPLETED filing queries


@staticmethod
def has_completed_filing(business_id: int, filing_type: str) -> bool:
Expand Down
1 change: 0 additions & 1 deletion legal-api/src/legal_api/resources/v2/business/business.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def get_businesses(identifier: str):
recent_filing_json = CoreFiling.get_most_recent_filing_json(business.id, None, jwt)
if recent_filing_json:
business_json['submitter'] = recent_filing_json['filing']['header']['submitter']
business_json['lastModified'] = recent_filing_json['filing']['header']['date']
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not required to override. lastModified will be updated in entity-filer while processing a filing


allowed_filings = str(request.args.get('allowed_filings', None)).lower() == 'true'
if allowed_filings:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ def validate(business: Business, restoration: Dict) -> Optional[Error]:
restoration_type = get_str(restoration, '/filing/restoration/type')
limited_restoration = None
if restoration_type in ('limitedRestorationExtension', 'limitedRestorationToFull'):
limited_restoration = Filing.get_a_businesses_most_recent_filing_of_a_type(business.id,
'restoration',
'limitedRestoration')
limited_restoration = Filing.get_most_recent_filing(business.id,
'restoration',
'limitedRestoration')
if restoration_type in ('limitedRestoration', 'limitedRestorationExtension'):
msg.extend(validate_expiry_date(business, restoration, restoration_type))
elif restoration_type in ('fullRestoration', 'limitedRestorationToFull'):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,10 @@ def check_parties(legal_type: str, business: Business) -> list:
firm_party_roles = business.party_roles.filter(PartyRole.cessation_date.is_(None))
result.extend(check_firm_parties(legal_type, firm_party_roles))

completing_party_filing = Filing \
.get_most_recent_legal_filing(business.id, 'conversion')
completing_party_filing = Filing.get_most_recent_filing(business.id, 'conversion')

if not completing_party_filing:
completing_party_filing = Filing \
.get_most_recent_legal_filing(business.id, 'registration')
completing_party_filing = Filing.get_most_recent_filing(business.id, 'registration')

result.extend(check_completing_party_for_filing(completing_party_filing))
return result
Expand Down
4 changes: 2 additions & 2 deletions legal-api/tests/unit/models/test_filing.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ def test_get_completed_filings_for_colin(session, client, jwt):
assert len(filings) == 0


def test_get_a_businesses_most_recent_filing_of_a_type(session):
def test_get_most_recent_filing(session):
"""Assert that the most recent completed filing of a specified type is returned."""
from legal_api.models import Filing
from tests.unit.models import factory_completed_filing
Expand All @@ -577,7 +577,7 @@ def test_get_a_businesses_most_recent_filing_of_a_type(session):
filing = factory_completed_filing(b, ar, filing_date)
filings.append(filing)
# test
filing = Filing.get_a_businesses_most_recent_filing_of_a_type(b.id, Filing.FILINGS['annualReport']['name'])
filing = Filing.get_most_recent_filing(b.id, Filing.FILINGS['annualReport']['name'])

# assert that we get the last filing
assert filings[4] == filing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def execute_test_restoration_nr(mocker, filing_sub_type, legal_type, nr_number,
mock_nr_response = MockResponse(temp_nr_response, HTTPStatus.OK)

mocker.patch('legal_api.services.NameXService.query_nr_number', return_value=mock_nr_response)
with patch.object(Filing, 'get_a_businesses_most_recent_filing_of_a_type',
with patch.object(Filing, 'get_most_recent_filing',
return_value=limited_restoration_filing):
err = validate(business, filing)

Expand Down Expand Up @@ -180,7 +180,7 @@ def test_validate_relationship(session, test_status, restoration_type, expected_
elif test_status == 'SUCCESS' and restoration_type in ('fullRestoration', 'limitedRestorationToFull'):
filing['filing']['restoration']['relationships'] = relationships

with patch.object(Filing, 'get_a_businesses_most_recent_filing_of_a_type',
with patch.object(Filing, 'get_most_recent_filing',
return_value=limited_restoration_filing):
err = validate(business, filing)

Expand Down Expand Up @@ -231,7 +231,7 @@ def test_validate_expiry_date(session, test_name, restoration_type, delta_date,
filing['filing']['restoration']['type'] = restoration_type
if delta_date:
filing['filing']['restoration']['expiry'] = expiry_date.strftime(date_format)
with patch.object(Filing, 'get_a_businesses_most_recent_filing_of_a_type',
with patch.object(Filing, 'get_most_recent_filing',
return_value=limited_restoration_filing):
err = validate(business, filing)

Expand Down Expand Up @@ -281,7 +281,7 @@ def test_approval_type(session, test_status, restoration_types, legal_types, app
filing['filing']['restoration']['applicationDate'] = '2023-03-30'
filing['filing']['restoration']['noticeDate'] = '2023-03-30'

with patch.object(Filing, 'get_a_businesses_most_recent_filing_of_a_type',
with patch.object(Filing, 'get_most_recent_filing',
return_value=limited_restoration_filing):
err = validate(business, filing)

Expand Down Expand Up @@ -337,7 +337,7 @@ def test_restoration_court_orders(session, test_status, restoration_types, legal
else:
del filing['filing']['restoration']['courtOrder']

with patch.object(Filing, 'get_a_businesses_most_recent_filing_of_a_type',
with patch.object(Filing, 'get_most_recent_filing',
return_value=limited_restoration_filing):
err = validate(business, filing)

Expand Down Expand Up @@ -394,7 +394,7 @@ def test_restoration_registrar(session, test_status, restoration_types, legal_ty
if notice_date:
filing['filing']['restoration']['noticeDate'] = notice_date

with patch.object(Filing, 'get_a_businesses_most_recent_filing_of_a_type',
with patch.object(Filing, 'get_most_recent_filing',
return_value=limited_restoration_filing):
err = validate(business, filing)

Expand Down