Skip to content

Commit

Permalink
22051 Fix validation rules for filing, add a could file detection for…
Browse files Browse the repository at this point in the history
… if th… (bcgov#3063)
  • Loading branch information
BrandonSharratt authored Nov 19, 2024
1 parent fbf736d commit a6976fb
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 1 deletion.
26 changes: 25 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 @@ -34,7 +34,7 @@
RegistrationBootstrapService,
check_warnings,
) # noqa: I001;
from legal_api.services.authz import get_allowable_actions, get_allowed
from legal_api.services.authz import get_allowable_actions, get_allowed, get_could_files
from legal_api.utils.auth import jwt

from .bp import bp
Expand Down Expand Up @@ -64,6 +64,7 @@ def get_businesses(identifier: str):
business_json = business.json(slim=True)
# need to add the alternateNames array here because it is not a part of slim JSON
business_json['alternateNames'] = business.get_alternate_names()

return jsonify(business=business_json)

warnings = check_warnings(business)
Expand All @@ -75,6 +76,7 @@ def get_businesses(identifier: str):
business.allowable_actions = allowable_actions

business_json = business.json()

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']
Expand Down Expand Up @@ -212,3 +214,25 @@ def search_businesses():
current_app.logger.info(err)
current_app.logger.error('Error searching over business information for: %s', identifiers)
return {'error': 'Unable to retrieve businesses.'}, HTTPStatus.INTERNAL_SERVER_ERROR


@bp.route('/allowable/<string:business_type>/<string:business_state>', methods=['GET'])
@cross_origin(origin='*')
@jwt.requires_auth
def get_allowable_for_business_type(business_type: str, business_state: str):
"""Return a JSON object with information about what a user could theoretically file for a business type."""
business_state = business_state.upper()
business_type = business_type.upper()

bs_state = getattr(Business.State, business_state, False)
if not bs_state:
return {'message': babel('Invalid business state.')}, HTTPStatus.BAD_REQUEST

try:
_ = Business.LegalTypes(business_type)
except ValueError:
return {'message': babel('Invalid business type.')}, HTTPStatus.BAD_REQUEST

could_file = get_could_files(jwt, business_type, business_state)

return jsonify(couldFile=could_file)
65 changes: 65 additions & 0 deletions legal-api/src/legal_api/services/authz.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# pylint: disable=too-many-lines
"""This manages all of the authentication and authorization service."""
from datetime import datetime, timezone
from enum import Enum
Expand Down Expand Up @@ -515,6 +517,22 @@ def is_allowed(business: Business,
return False


def get_could_files(jwt: JwtManager, business_type: str, business_state: str):
"""Get allowable actions."""
is_competent_authority = has_product('CA_SEARCH', jwt.get_token_auth_header())
if is_competent_authority:
allowed_filings = []
else:
allowed_filings = get_could_file(business_type, business_state, jwt)

result = {
'filing': {
'filingTypes': allowed_filings
}
}
return result


def get_allowable_actions(jwt: JwtManager, business: Business):
"""Get allowable actions."""
is_competent_authority = has_product('CA_SEARCH', jwt.get_token_auth_header())
Expand All @@ -537,6 +555,53 @@ def get_allowable_actions(jwt: JwtManager, business: Business):
return result


def get_could_file(legal_type: str,
state: str,
jwt: JwtManager):
"""Get allowed type of filing types for the current user."""
# importing here to avoid circular dependencies
# pylint: disable=import-outside-toplevel
from legal_api.core.meta import FilingMeta

user_role = 'general'
if jwt.contains_role([STAFF_ROLE, SYSTEM_ROLE, COLIN_SVC_ROLE]):
user_role = 'staff'

bs_state = getattr(Business.State, state, '')

allowable_filings = get_allowable_filings_dict().get(user_role, {}).get(bs_state, {})
could_filing_types = []

for allowable_filing_key, allowable_filing_value in allowable_filings.items():
allowable_filing_legal_types = allowable_filing_value.get('legalTypes', [])

if allowable_filing_legal_types:
is_allowable = legal_type in allowable_filing_legal_types
allowable_filing_type = {'name': allowable_filing_key,
'displayName': FilingMeta.get_display_name(legal_type, allowable_filing_key)}
could_filing_types = add_allowable_filing_type(is_allowable,
could_filing_types,
allowable_filing_type)
continue

filing_sub_type_items = \
filter(lambda x: isinstance(x[1], dict) and legal_type in
x[1].get('legalTypes', []), allowable_filing_value.items())

for filing_sub_type_item_key, _ in filing_sub_type_items:

allowable_filing_sub_type = {'name': allowable_filing_key,
'type': filing_sub_type_item_key,
'displayName': FilingMeta.get_display_name(legal_type,
allowable_filing_key,
filing_sub_type_item_key)}
could_filing_types = add_allowable_filing_type(True,
could_filing_types,
allowable_filing_sub_type)

return could_filing_types


def get_allowed_filings(business: Business,
state: Business.State,
legal_type: str,
Expand Down
116 changes: 116 additions & 0 deletions legal-api/tests/unit/resources/v2/test_business.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,3 +544,119 @@ def test_post_affiliated_businesses_invalid(session, client, jwt):
json={},
headers=create_header(jwt, [SYSTEM_ROLE]))
assert rv.status_code == HTTPStatus.BAD_REQUEST


def test_get_could_file(session, client, jwt):
"""Assert that the cold file is returned."""
identifier = 'BC0000001'
rv = client.get('/api/v2/businesses/allowable/BC/ACTIVE',
headers=create_header(jwt, [STAFF_ROLE], identifier))

expected = [
{
"displayName": "Admin Freeze",
"name": "adminFreeze"
},
{
"displayName": "Request for AGM Extension",
"name": "agmExtension"
},
{
"displayName": "AGM Location Change",
"name": "agmLocationChange"
},
{
"displayName": "Alteration",
"name": "alteration"
},
{
"displayName": "Amalgamation Application (Regular)",
"name": "amalgamationApplication",
"type": "regular"
},
{
"displayName": "Amalgamation Application Short-form (Vertical)",
"name": "amalgamationApplication",
"type": "vertical"
},
{
"displayName": "Amalgamation Application Short-form (Horizontal)",
"name": "amalgamationApplication",
"type": "horizontal"
},
{
"displayName": "Annual Report",
"name": "annualReport"
},
{
"displayName": "Address Change",
"name": "changeOfAddress"
},
{
"displayName": "Director Change",
"name": "changeOfDirectors"
},
{
"displayName": "6-Month Consent to Continue Out",
"name": "consentContinuationOut"
},
{
"displayName": "Continuation Out",
"name": "continuationOut"
},
{
"displayName": "Register Correction Application",
"name": "correction"
},
{
"displayName": "Court Order",
"name": "courtOrder"
},
{
"displayName": "Voluntary Dissolution",
"name": "dissolution",
"type": "voluntary"
},
{
"displayName": "Administrative Dissolution",
"name": "dissolution",
"type": "administrative"
},
{
"displayName": "BC Limited Company Incorporation Application",
"name": "incorporationApplication"
},
{
"displayName": "Registrar's Notation",
"name": "registrarsNotation"
},
{
"displayName": "Registrar's Order",
"name": "registrarsOrder"
},
{
"displayName": "Transition Application",
"name": "transition"
},
{
"displayName": "Limited Restoration Extension Application",
"name": "restoration",
"type": "limitedRestorationExtension"
},
{
"displayName": "Conversion to Full Restoration Application",
"name": "restoration",
"type": "limitedRestorationToFull"
},
{
"displayName": "Notice of Withdrawal",
"name": "noticeOfWithdrawal"
}
]

assert rv.status_code == HTTPStatus.OK
assert rv.json['couldFile']
assert rv.json['couldFile']['filing']
assert rv.json['couldFile']['filing']['filingTypes']
assert len(rv.json['couldFile']['filing']['filingTypes']) > 0
assert rv.json['couldFile']['filing']['filingTypes'] == expected

0 comments on commit a6976fb

Please sign in to comment.