Skip to content

Commit

Permalink
Add Reset by event ID endpoint for colin api requiring colin svc id (b…
Browse files Browse the repository at this point in the history
…cgov#2772)

* UNTESTED reset_filings_by_event

* tested the colin api reset for business ar (reset by event id)

* fix description of reset filings by event

* addressing feedback on the changes
  • Loading branch information
BrandonSharratt authored Jun 24, 2024
1 parent 0dc71c0 commit 6ab628d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
41 changes: 41 additions & 0 deletions colin-api/src/colin_api/models/reset.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,44 @@ def reset_filings(cls, start_date: str = None, end_date: str = None, identifiers
if con:
con.rollback()
raise err

@classmethod
def reset_filings_by_event(cls, event_ids: list = []):
"""Reset changes made for given event ids."""
# initialize reset object
reset_obj = Reset()

# place into lists that can be reset together
annual_report_events = []
events_info = []

for filing_info in reset_obj.get_filings_for_reset():
if filing_info['event_id'] in event_ids:
events_info.append(filing_info)
if filing_info['filing_typ_cd'] in Filing.FILING_TYPES['annualReport']['type_code_list']:
annual_report_events.append(filing_info['event_id'])

try:
if event_ids:
# setup db connection
con = DB.connection
con.begin()
cursor = con.cursor()

# reset data in oracle for events
# The commented out events do not seem to happen for AR so they are commented out.
new_corps = cls._get_incorporations_by_event(cursor, event_ids)
Party.reset_dirs_by_events(cursor=cursor, event_ids=event_ids)
Business.reset_corporations(cursor=cursor, event_info=events_info, event_ids=event_ids)

cls._delete_filing_user(cursor=cursor, event_ids=event_ids)
cls._delete_corp_state(cursor=cursor, corp_nums=list(new_corps.keys()))
cls._delete_events_and_filings(cursor=cursor, event_ids=event_ids)
con.commit()
return
except Exception as err:
current_app.logger.error('Error in reset_filings_by_event: failed to reset filings.'
' Rolling back any partial changes.')
if con:
con.rollback()
raise err
40 changes: 39 additions & 1 deletion colin-api/src/colin_api/resources/reset.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
Currently this only resets changes made to COOP data made with user COOPER
"""
import json
from flask import current_app, jsonify, request
from flask_restx import Namespace, Resource, cors
from flask_restx import Namespace, Resource, cors, fields

from colin_api.models.reset import Reset
from colin_api.utils.auth import COLIN_SVC_ROLE, jwt
Expand Down Expand Up @@ -55,3 +56,40 @@ def post():
# general catch-all exception
current_app.logger.error(err.with_traceback(None))
return jsonify({'message': 'Error when trying to reset COLIN'}), 500


@cors_preflight('POST')
@API.route('/by_event_id')
class ResetByEventId(Resource):
"""Reset filing(s) based on the provided event_id, or array of event_ids.
This is only tested to work on Annual Reports, ymmv"""

eventResetParser = API.parser()
eventResetParser.add_argument(
'event_ids',
type=list,
help='The list of event ids to reset. Can be one id',
location='json',
required=True)

@staticmethod
@cors.crossdomain(origin='*')
@jwt.requires_roles([COLIN_SVC_ROLE])
@API.expect(eventResetParser)
def post():
"""Reset filing(s) based on the provided event_id, or array of event_ids.
This is only tested to work on Annual Reports, ymmv"""
try:

event_ids = API.payload.get('event_ids', None)

Reset.reset_filings_by_event(
event_ids=event_ids
)

return jsonify({'message':"Reset for event ids "+json.dumps(event_ids)}), 200

except Exception as err: # pylint: disable=broad-except; want to catch all errors
# general catch-all exception
current_app.logger.error(err.with_traceback(None))
return jsonify({'message': 'Error when trying to reset COLIN by event ids'}), 500

0 comments on commit 6ab628d

Please sign in to comment.