Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@
from http import HTTPStatus

import pycountry
from flask_babel import _ as babel # noqa: N813, I004, I001; importing camelcase '_' as a name
from flask_babel import _ as babel # importing camelcase '_' as a name

from legal_api.errors import Error
from legal_api.models import Address, Business, Filing
from legal_api.services.filings.validations.common_validations import validate_parties_addresses
from legal_api.services.filings.validations.common_validations import PARTY_NAME_MAX_LENGTH, validate_parties_addresses
from legal_api.services.utils import get_str
from legal_api.utils.datetime import date, datetime
from legal_api.utils.legislation_datetime import LegislationDatetime

# noqa: I003; needed as the linter gets confused from the babel override above.


def validate(business: Business, cod: dict) -> Error:
"""Validate the Change of Directors filing."""
Expand All @@ -41,6 +39,10 @@ def validate(business: Business, cod: dict) -> Error:
if msg_appointment_date:
msg += msg_appointment_date

msg_directors_names = validate_directors_name(cod)
if msg_directors_names:
msg += msg_directors_names

msg_directors_addresses = validate_directors_addresses(business, cod)
if msg_directors_addresses:
msg += msg_directors_addresses
Expand Down Expand Up @@ -267,3 +269,62 @@ def validate_effective_date(business: Business, cod: dict) -> list:
msg.append({"error": babel("Effective date cannot be before another Change of Director filing.")})

return msg

def validate_directors_name(cod: dict) -> list:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There is a validate_party_name function in common validations, but this one has differences than a party name (though posted as basically the same format from the UI) in that there's the prev* fields, the role isn't variable, and there's the actions to take into account.

Could maybe generalize a "validate person name" that is shared but there's also the 20 character limit on the name fields, so probably consider any refactoring in context with https://app.zenhub.com/workspaces/entities-team-65af15f59e89f5043c2911f7/issues/gh/bcgov/entity/31979 if that gets addressed at some point.

"""Return error messages if a director's name fields are invalid.

Rules:
- firstName and lastName are required (non-empty) for all directors.
- prevFirstName and prevLastName are required when "nameChanged" is in actions.
- No leading or trailing whitespace.
- All name fields have a maximum length of 30 characters.
"""
msg = []
filing_type = "changeOfDirectors"
directors = cod["filing"][filing_type]["directors"]

name_fields = ["firstName", "middleInitial", "lastName",
"prevFirstName", "prevMiddleInitial", "prevLastName"]
required_fields = ["firstName", "lastName"]
name_changed_required_fields = ["prevFirstName", "prevLastName"]

for idx, director in enumerate(directors):
officer = director.get("officer", {})
actions = director.get("actions", [])
is_name_changed = "nameChanged" in actions

for field in name_fields:
value = officer.get(field)
path = f"/filing/changeOfDirectors/directors/{idx}/officer/{field}"

if field in required_fields and (not value or not value.strip()):
msg.append({
"error": babel(f"Director {field} is required."),
"path": path
})
continue

# Check prev first/last required when nameChanged
if field in name_changed_required_fields and is_name_changed and (not value or not value.strip()):
msg.append({
"error": babel(f"Director {field} is required when name has changed."),
"path": path
})
continue

if value:
# No leading or trailing whitespace
if value != value.strip():
Copy link
Collaborator

Choose a reason for hiding this comment

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

This may have impact on PROD if existing data has space ("prevFirstName", "prevMiddleInitial", "prevLastName"), Checked and removed trailing space from PROD. So if dev/test has issues with existing data no need to be concerned about it.

msg.append({
"error": babel(f"Director {field} cannot have leading or trailing whitespace."),
"path": path
})

# Max length
if len(value) > PARTY_NAME_MAX_LENGTH:
msg.append({
"error": babel(f"Director {field} cannot be longer than {PARTY_NAME_MAX_LENGTH} characters."),
"path": path
})

return msg
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"postalCode",
)

PARTY_NAME_MAX_LENGTH = 30

# Share structure constants
EXCLUDED_WORDS_FOR_CLASS = ["share", "shares", "value"]
Expand Down Expand Up @@ -407,7 +408,7 @@ def validate_party_name(party: dict, party_path: str, legal_type: str) -> list:
msg = []

custom_allowed_max_length = 20
last_name_max_length = 30
last_name_max_length = PARTY_NAME_MAX_LENGTH
officer = party["officer"]
party_type = officer["partyType"]
party_roles = [x.get("roleType") for x in party["roles"]]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import copy
import pytest
from http import HTTPStatus
from legal_api.services.filings.validations.change_of_directors import validate_directors_name

# Minimal valid director structure for reuse
def make_director(**kwargs):
base = {
"actions": [],
"officer": {
"firstName": "John",
"middleInitial": "Q",
"lastName": "Public",
"prevFirstName": "",
"prevMiddleInitial": "",
"prevLastName": ""
}
}
for k, v in kwargs.items():
if k == "officer":
base["officer"].update(v)
else:
base[k] = v
return base

@pytest.mark.parametrize(
"test_name,directors,expected_msgs",
[
("valid_minimal", [make_director()], []),
("missing_firstName", [make_director(officer={"firstName": ""})], [
{"error": "Director firstName is required.", "path": "/filing/changeOfDirectors/directors/0/officer/firstName"}
]),
("missing_lastName", [make_director(officer={"lastName": ""})], [
{"error": "Director lastName is required.", "path": "/filing/changeOfDirectors/directors/0/officer/lastName"}
]),
("nameChanged_missing_prevFirstName", [make_director(actions=["nameChanged"], officer={"prevFirstName": "", "prevLastName": "Smith"})], [
{"error": "Director prevFirstName is required when name has changed.", "path": "/filing/changeOfDirectors/directors/0/officer/prevFirstName"}
]),
("nameChanged_missing_prevLastName", [make_director(actions=["nameChanged"], officer={"prevFirstName": "Jane", "prevLastName": ""})], [
{"error": "Director prevLastName is required when name has changed.", "path": "/filing/changeOfDirectors/directors/0/officer/prevLastName"}
]),
("leading_whitespace", [make_director(officer={"firstName": " John"})], [
{"error": "Director firstName cannot have leading or trailing whitespace.", "path": "/filing/changeOfDirectors/directors/0/officer/firstName"}
]),
("trailing_whitespace", [make_director(officer={"lastName": "Public "})], [
{"error": "Director lastName cannot have leading or trailing whitespace.", "path": "/filing/changeOfDirectors/directors/0/officer/lastName"}
]),
("over_max_length", [make_director(officer={"firstName": "A"*31})], [
{"error": "Director firstName cannot be longer than 30 characters.", "path": "/filing/changeOfDirectors/directors/0/officer/firstName"}
]),
("multiple_directors", [
make_director(),
make_director(officer={"firstName": "", "lastName": ""})
], [
{"error": "Director firstName is required.", "path": "/filing/changeOfDirectors/directors/1/officer/firstName"},
{"error": "Director lastName is required.", "path": "/filing/changeOfDirectors/directors/1/officer/lastName"}
]),
]
)
def test_validate_directors_name(test_name, directors, expected_msgs):
cod = {
"filing": {
"changeOfDirectors": {
"directors": directors
}
}
}
msgs = validate_directors_name(cod)
msgs_simple = [
{"error": m["error"].replace("Director ", "Director "), "path": m["path"]} for m in msgs
]
assert msgs_simple == expected_msgs