Skip to content

Commit

Permalink
19347 Legal-api - update alternate name model + misc unit test updates (
Browse files Browse the repository at this point in the history
#2423)

* 19347

Signed-off-by: Hongjing Chen <[email protected]>

* fix some unit tests

Signed-off-by: Hongjing Chen <[email protected]>

* add relationships

Signed-off-by: Hongjing Chen <[email protected]>

* partially fix linting

Signed-off-by: Hongjing Chen <[email protected]>

---------

Signed-off-by: Hongjing Chen <[email protected]>
  • Loading branch information
chenhongjing authored Jan 30, 2024
1 parent 1b2c95f commit f90ba37
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 21 deletions.
4 changes: 2 additions & 2 deletions legal-api/src/legal_api/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

"""This exports all of the models and schemas used by the application."""
from .address import Address
from .amalgamating_business import AmalgamatingBusiness
from .amalgamation import Amalgamation
from .alias import Alias
from .alternate_name import AlternateName
from .amalgamating_business import AmalgamatingBusiness
from .amalgamation import Amalgamation
from .colin_entity import ColinEntity
from .colin_update import ColinLastUpdate
from .comment import Comment
Expand Down
29 changes: 26 additions & 3 deletions legal-api/src/legal_api/models/alternate_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

from sql_versioning import Versioned

from legal_api.utils.datetime import datetime

from ..utils.enum import BaseEnum, auto
from .db import db

Expand All @@ -28,6 +30,13 @@ class NameType(BaseEnum):

OPERATING = auto()

class State(BaseEnum):
"""Enum for the Business state."""

ACTIVE = auto()
HISTORICAL = auto()
LIQUIDATION = auto()

__tablename__ = "alternate_names"
__mapper_args__ = {
"include_properties": [
Expand All @@ -42,27 +51,41 @@ class NameType(BaseEnum):
"start_date",
"naics_key",
"naics_code",
"naics_description"
"naics_description",
"business_start_date",
"dissolution_date",
"state",
"state_filing_id",
"admin_freeze",
"last_modified",
]
}

id = db.Column(db.Integer, primary_key=True)
identifier = db.Column("identifier", db.String(10), nullable=True)
identifier = db.Column("identifier", db.String(10), nullable=True, index=True)
name_type = db.Column("name_type", db.Enum(NameType), nullable=False)
name = db.Column("name", db.String(1000), nullable=False)
name = db.Column("name", db.String(1000), nullable=False, index=True)
bn15 = db.Column("bn15", db.String(20), nullable=True)
start_date = db.Column("start_date", db.DateTime(timezone=True), nullable=False)
end_date = db.Column("end_date", db.DateTime(timezone=True), nullable=True)
naics_key = db.Column("naics_key", db.String(50), nullable=True)
naics_code = db.Column("naics_code", db.String(10), nullable=True)
naics_description = db.Column("naics_description", db.String(300), nullable=True)
business_start_date = db.Column("business_start_date", db.DateTime(timezone=True), default=datetime.utcnow)
dissolution_date = db.Column("dissolution_date", db.DateTime(timezone=True), default=None)
state = db.Column("state", db.Enum(State), default=State.ACTIVE.value)
admin_freeze = db.Column("admin_freeze", db.Boolean, unique=False, default=False)
last_modified = db.Column("last_modified", db.DateTime(timezone=True), default=datetime.utcnow)

# parent keys
legal_entity_id = db.Column("legal_entity_id", db.Integer, db.ForeignKey("legal_entities.id"))
change_filing_id = db.Column("change_filing_id", db.Integer, db.ForeignKey("filings.id"), index=True)
state_filing_id = db.Column("state_filing_id", db.Integer, db.ForeignKey("filings.id"))

# relationships
legal_entity = db.relationship("LegalEntity", back_populates="_alternate_names")
filings = db.relationship("Filing", lazy="dynamic", foreign_keys="Filing.alternate_name_id")
documents = db.relationship("Document", lazy="dynamic")

def save(self):
"""Save the object to the database immediately."""
Expand Down
4 changes: 3 additions & 1 deletion legal-api/src/legal_api/models/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Comment(db.Model):
"legal_entity_id",
"staff_id",
"timestamp",
"alternate_name_id"
]
}

Expand All @@ -53,6 +54,7 @@ class Comment(db.Model):
legal_entity_id = db.Column("legal_entity_id", db.Integer, db.ForeignKey("legal_entities.id"), index=True)
staff_id = db.Column("staff_id", db.Integer, db.ForeignKey("users.id"), index=True)
filing_id = db.Column("filing_id", db.Integer, db.ForeignKey("filings.id"), index=True)
alternate_name_id = db.Column("alternate_name_id", db.Integer, db.ForeignKey("alternate_names.id"), index=True)

# Relationships - Users
staff = db.relationship("User", backref=backref("staff_comments"), foreign_keys=[staff_id])
Expand All @@ -67,7 +69,7 @@ def json(self):
"submitterDisplayName": user.display_name if user else None,
"comment": self.comment,
"filingId": self.filing_id,
"businessId": self.legal_entity_id,
"businessId": self.legal_entity_id or self.alternate_name_id,
"timestamp": self.timestamp.isoformat(),
}
}
Expand Down
4 changes: 3 additions & 1 deletion legal-api/src/legal_api/models/dc_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ class State(Enum):

legal_entity_id = db.Column("legal_entity_id", db.Integer, db.ForeignKey("legal_entities.id"))

alternate_name_id = db.Column("alternate_name_id", db.Integer, db.ForeignKey("alternate_names.id"))

@property
def json(self):
"""Return a dict of this object, with keys in JSON format."""
dc_connection = {
"id": self.id,
"businessId": self.legal_entity_id,
"businessId": self.legal_entity_id or self.alternate_name_id,
"connectionId": self.connection_id,
"invitationUrl": self.invitation_url,
"isActive": self.is_active,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class DCIssuedBusinessUserCredential(db.Model): # pylint: disable=too-many-inst

user_id = db.Column('user_id', db.Integer, db.ForeignKey('users.id'))
legal_entity_id = db.Column("legal_entity_id", db.Integer, db.ForeignKey("legal_entities.id"))
alternate_name_id = db.Column("alternate_name_id", db.Integer, db.ForeignKey("alternate_names.id"))

def save(self):
"""Save the object to the database immediately."""
Expand Down
2 changes: 2 additions & 0 deletions legal-api/src/legal_api/models/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Document(Versioned, db.Model):
"filing_id",
"legal_entity_id",
"type",
"alternate_name_id"
]
}

Expand All @@ -55,6 +56,7 @@ class Document(Versioned, db.Model):
# parent keys
legal_entity_id = db.Column("legal_entity_id", db.Integer, db.ForeignKey("legal_entities.id"), index=True)
filing_id = db.Column("filing_id", db.Integer, db.ForeignKey("filings.id"), index=True)
alternate_name_id = db.Column("alternate_name_id", db.Integer, db.ForeignKey("alternate_names.id"), index=True)

def save(self):
"""Save the object to the database immediately."""
Expand Down
2 changes: 2 additions & 0 deletions legal-api/src/legal_api/models/filing.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ class Source(Enum):
"tech_correction_json",
"temp_reg",
"transaction_id",
"alternate_name_id"
]
}

Expand Down Expand Up @@ -342,6 +343,7 @@ class Source(Enum):
# transaction_id = db.Column('transaction_id', db.BigInteger,
# db.ForeignKey('transaction.id'))
legal_entity_id = db.Column("legal_entity_id", db.Integer, db.ForeignKey("legal_entities.id"))
alternate_name_id = db.Column("alternate_name_id", db.Integer, db.ForeignKey("alternate_names.id"))
temp_reg = db.Column("temp_reg", db.String(10), db.ForeignKey("registration_bootstrap.identifier"))
submitter_id = db.Column("submitter_id", db.Integer, db.ForeignKey("users.id"))

Expand Down
29 changes: 18 additions & 11 deletions legal-api/src/legal_api/models/legal_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
from .alternate_name import ( # noqa: F401 pylint: disable=unused-import; needed by SQLAlchemy relationship
AlternateName,
)
from .amalgamation import Amalgamation # noqa: F401 pylint: disable=unused-import; needed by SQLAlchemy relationship
from .amalgamation import ( # noqa: F401 pylint: disable=unused-import; needed by SQLAlchemy relationship
Amalgamation,
)
from .db import db # noqa: I001
from .entity_role import ( # noqa: F401 pylint: disable=unused-import; needed by the SQLAlchemy relationship
EntityRole,
Expand Down Expand Up @@ -267,7 +269,6 @@ class AssociationTypes(Enum):
last_ar_reminder_year = db.Column("last_ar_reminder_year", db.Integer)
association_type = db.Column("association_type", db.String(50))
state = db.Column("state", db.Enum(State), default=State.ACTIVE.value)
state_filing_id = db.Column("state_filing_id", db.Integer)
admin_freeze = db.Column("admin_freeze", db.Boolean, unique=False, default=False)
submitter_userid = db.Column("submitter_userid", db.Integer, db.ForeignKey("users.id"))
submitter = db.relationship("User", backref=backref("submitter", uselist=False), foreign_keys=[submitter_userid])
Expand All @@ -294,6 +295,7 @@ class AssociationTypes(Enum):
delivery_address_id = db.Column("delivery_address_id", db.Integer, db.ForeignKey("addresses.id"))
mailing_address_id = db.Column("mailing_address_id", db.Integer, db.ForeignKey("addresses.id"))
change_filing_id = db.Column("change_filing_id", db.Integer, db.ForeignKey("filings.id"), index=True)
state_filing_id = db.Column("state_filing_id", db.Integer, db.ForeignKey("filings.id"))

# relationships
change_filing = db.relationship("Filing", foreign_keys=[change_filing_id])
Expand Down Expand Up @@ -326,8 +328,8 @@ class AssociationTypes(Enum):
foreign_keys="Resolution.signing_legal_entity_id",
lazy="dynamic",
)
amalgamating_businesses = db.relationship('AmalgamatingBusiness', lazy='dynamic')
amalgamation = db.relationship('Amalgamation', lazy='dynamic')
amalgamating_businesses = db.relationship("AmalgamatingBusiness", lazy="dynamic")
amalgamation = db.relationship("Amalgamation", lazy="dynamic")

@hybrid_property
def identifier(self):
Expand Down Expand Up @@ -479,7 +481,10 @@ def legal_name(self):
func.nullif(related_le_alias.first_name, ""),
),
),
(related_le_alias.entity_type == "organization", related_le_alias._legal_name), # pylint: disable=protected-access # noqa: E501
(
related_le_alias.entity_type == "organization",
related_le_alias._legal_name,
), # pylint: disable=protected-access # noqa: E501
else_=None,
).label("sortName"),
case(
Expand All @@ -492,7 +497,10 @@ def legal_name(self):
func.nullif(related_le_alias.last_name, ""),
),
),
(related_le_alias.entity_type == "organization", related_le_alias._legal_name), # pylint: disable=protected-access # noqa: E501
(
related_le_alias.entity_type == "organization",
related_le_alias._legal_name,
), # pylint: disable=protected-access # noqa: E501
else_=None,
).label("legalName"),
)
Expand Down Expand Up @@ -656,8 +664,9 @@ def _extend_json(self, d):
if self.fiscal_year_end_date:
d["fiscalYearEndDate"] = datetime.date(self.fiscal_year_end_date).isoformat()
if self.state_filing_id:
if (self.state == LegalEntity.State.HISTORICAL and
(amalgamating_business := self.amalgamating_businesses.one_or_none())):
if self.state == LegalEntity.State.HISTORICAL and (
amalgamating_business := self.amalgamating_businesses.one_or_none()
):
amalgamation = Amalgamation.find_by_id(amalgamating_business.amalgamation_id)
d["amalgamatedInto"] = amalgamation.json()
else:
Expand Down Expand Up @@ -794,9 +803,7 @@ def find_by_legal_name(cls, legal_name: str = None):
@classmethod
def find_by_identifier(cls, identifier: str = None):
"""Return a Business by the id assigned by the Registrar."""
if not identifier or not cls.validate_identifier(
entity_type=None, identifier=identifier
):
if not identifier or not cls.validate_identifier(entity_type=None, identifier=identifier):
return None

legal_entity = None
Expand Down
1 change: 1 addition & 0 deletions legal-api/src/legal_api/models/request_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class RequestType(BaseEnum):
# parent keys
legal_entity_id = db.Column("legal_entity_id", db.Integer, db.ForeignKey("legal_entities.id"), index=True)
filing_id = db.Column("filing_id", db.Integer, db.ForeignKey("filings.id"), index=True)
alternate_name_id = db.Column("alternate_name_id", db.Integer, db.ForeignKey("alternate_names.id"), index=True)

@property
def json(self) -> dict:
Expand Down
6 changes: 3 additions & 3 deletions legal-api/tests/unit/models/test_amalgamating_business.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_valid_amalgamating_business_save(session):

amalgamation = Amalgamation(
amalgamation_type=Amalgamation.AmalgamationTypes.horizontal,
business_id=b.id,
legal_entity_id=b.id,
filing_id=filing.id,
amalgamation_date=datetime.utcnow(),
court_approval=True
Expand All @@ -60,7 +60,7 @@ def test_valid_amalgamating_business_save(session):
foreign_jurisdiction_region="AB",
foreign_name="Testing123",
foreign_corp_num="123456789",
business_id=b.id,
legal_entity_id=b.id,
amalgamation_id=amalgamation.id
)
amalgamating_business_1.save()
Expand All @@ -71,7 +71,7 @@ def test_valid_amalgamating_business_save(session):
foreign_jurisdiction_region="AB",
foreign_name="Testing123",
foreign_corp_num="123456789",
business_id=b.id,
legal_entity_id=b.id,
amalgamation_id=amalgamation.id
)
amalgamating_business_2.save()
Expand Down

0 comments on commit f90ba37

Please sign in to comment.