Skip to content

Commit

Permalink
21120 new get business endpoint to access from UI (bcgov#2684)
Browse files Browse the repository at this point in the history
  • Loading branch information
vysakh-menon-aot authored May 13, 2024
1 parent fe2015d commit f8568af
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
2 changes: 2 additions & 0 deletions colin-api/requirements/prod.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
cx_Oracle
datedelta
gunicorn
Flask
Flask-Script
Flask-Moment
Flask-RESTX
flask-jwt-oidc>=0.1.5
jsonschema
launchdarkly-server-sdk
pycountry
python-dotenv
psycopg2-binary
Expand Down
45 changes: 41 additions & 4 deletions colin-api/src/colin_api/models/business.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ class LearBusinessTypes(Enum):
COOP = 'CP'
BCOMP = 'BEN'
BC_COMP = 'BC'
EXTRA_PRO_A = 'A'

class TypeCodes(Enum):
"""Render an Enum of the Corporation Type Codes."""

EXTRA_PRO_A = 'A'
COOP = 'CP'
BCOMP = 'BEN'
BC_COMP = 'BC'
Expand Down Expand Up @@ -81,7 +82,10 @@ class CorpStateTypes(Enum):
TypeCodes.BC_COMP.value,
TypeCodes.ULC_COMP.value,
TypeCodes.CCC_COMP.value
]
],
LearBusinessTypes.EXTRA_PRO_A.value: [
TypeCodes.EXTRA_PRO_A.value
],
}

business_number = None
Expand All @@ -94,6 +98,9 @@ class CorpStateTypes(Enum):
founding_date = None
good_standing = None
jurisdiction = None
home_recogn_dt = None
home_juris_num = None
home_company_nme = None
last_agm_date = None
last_ar_date = None
last_ledger_timestamp = None
Expand All @@ -114,6 +121,9 @@ def as_dict(self) -> Dict:
'goodStanding': self.good_standing,
'identifier': self.corp_num,
'jurisdiction': self.jurisdiction,
'homeRecognitionDate': self.home_recogn_dt,
'homeJurisdictionNumber': self.home_juris_num,
'homeCompanyName': self.home_company_nme,
'lastAgmDate': self.last_agm_date,
'lastArDate': self.last_ar_date,
'lastLedgerTimestamp': self.last_ledger_timestamp,
Expand All @@ -123,6 +133,25 @@ def as_dict(self) -> Dict:
}
}

def as_slim_dict(self) -> Dict:
"""Return slim dict version of self."""
return {
'business': {
'businessNumber': self.business_number,
'corpState': self.corp_state,
'corpStateClass': self.corp_state_class,
'foundingDate': self.founding_date,
'identifier': self.corp_num,
'jurisdiction': self.jurisdiction,
'homeRecognitionDate': self.home_recogn_dt,
'homeJurisdictionNumber': self.home_juris_num,
'homeCompanyName': self.home_company_nme,
'legalName': self.corp_name,
'legalType': self.corp_type,
'status': self.status
}
}

@classmethod
def _get_bn_15s(cls, cursor, identifiers: List) -> Dict:
"""Return a dict of idenifiers mapping to their bn_15 numbers."""
Expand Down Expand Up @@ -194,7 +223,7 @@ def _get_last_ar_dates_for_reset(cls, cursor, event_info: List, event_ids: List)
return dates_by_corp_num

@classmethod
def find_by_identifier(cls, identifier: str, corp_types: List, con=None) -> Business:
def find_by_identifier(cls, identifier: str, corp_types: List = None, con=None) -> Business:
"""Return a Business by identifier."""
business = None
try:
Expand All @@ -203,10 +232,15 @@ def find_by_identifier(cls, identifier: str, corp_types: List, con=None) -> Busi
con = DB.connection
# con.begin()

corp_type_condition = ''
if corp_types:
corp_type_condition = f'corp.corp_typ_cd in ({stringify_list(corp_types)}) and '

cursor = con.cursor()
cursor.execute(
f"""
select corp.corp_num, corp.corp_typ_cd, recognition_dts, bn_15, can_jur_typ_cd, othr_juris_desc,
home_recogn_dt, home_juris_num, home_company_nme,
filing.period_end_dt, last_agm_date, corp_op_state.full_desc as state, admin_email,
corp_state.state_typ_cd as corp_state, corp_op_state.op_state_typ_cd as corp_state_class,
corp.last_ar_filed_dt, corp.transition_dt, ct.corp_class
Expand All @@ -217,7 +251,7 @@ def find_by_identifier(cls, identifier: str, corp_types: List, con=None) -> Busi
join corp_type ct on ct.corp_typ_cd = corp.corp_typ_cd
join event on corp.corp_num = event.corp_num
left join filing on event.event_id = filing.event_id and filing.filing_typ_cd in ('OTANN', 'ANNBC')
where corp.corp_typ_cd in ({stringify_list(corp_types)}) and corp.corp_num=:corp_num
where {corp_type_condition} corp.corp_num=:corp_num
order by filing.period_end_dt desc nulls last
""",
corp_num=identifier
Expand Down Expand Up @@ -273,6 +307,9 @@ def find_by_identifier(cls, identifier: str, corp_types: List, con=None) -> Busi
business_obj.founding_date = convert_to_json_datetime(business['recognition_dts'])
business_obj.good_standing = cls.is_in_good_standing(business, cursor)
business_obj.jurisdiction = business['jurisdiction']
business_obj.home_recogn_dt = convert_to_json_datetime(business['home_recogn_dt'])
business_obj.home_juris_num = business['home_juris_num']
business_obj.home_company_nme = business['home_company_nme']
business_obj.last_agm_date = convert_to_json_date(business['last_agm_date'])
business_obj.last_ar_date = convert_to_json_date(business['period_end_dt']) if business['period_end_dt'] \
else convert_to_json_date(business['last_agm_date'])
Expand Down
31 changes: 31 additions & 0 deletions colin-api/src/colin_api/resources/business.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,37 @@
API = Namespace('businesses', description='Colin API Services - Businesses')


@cors_preflight('GET')
@API.route('/<string:identifier>/public', methods=['GET'])
class BusinessPublicInfo(Resource):
"""Meta information about the overall service."""

@staticmethod
@cors.crossdomain(origin='*')
@jwt.requires_auth
def get(identifier: str):
"""Return the complete business info."""
try:
# strip prefix BC
if identifier.startswith('BC'):
identifier = identifier[-7:]

# get business
business = Business.find_by_identifier(identifier)
if not business:
return jsonify({'message': f'{identifier} not found'}), HTTPStatus.NOT_FOUND
return jsonify(business.as_slim_dict()), HTTPStatus.OK

except GenericException as err: # pylint: disable=duplicate-code
return jsonify({'message': err.error}), err.status_code

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 retrieve business record from COLIN'}
), HTTPStatus.INTERNAL_SERVER_ERROR

@cors_preflight('GET, POST')
@API.route('/<string:legal_type>/<string:identifier>', methods=['GET'])
@API.route('/<string:legal_type>', methods=['POST'])
Expand Down

0 comments on commit f8568af

Please sign in to comment.