Skip to content

Commit

Permalink
21817 staff review models (bcgov#2797)
Browse files Browse the repository at this point in the history
  • Loading branch information
vysakh-menon-aot authored Jul 2, 2024
1 parent 4c0a3a1 commit 07b450f
Show file tree
Hide file tree
Showing 7 changed files with 367 additions and 0 deletions.
56 changes: 56 additions & 0 deletions legal-api/migrations/versions/ec9652171563_review.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""review
Revision ID: ec9652171563
Revises: 01b28a2bb730
Create Date: 2024-06-26 16:13:07.413995
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = 'ec9652171563'
down_revision = '01b28a2bb730'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('filings', sa.Column('resubmission_date', sa.DateTime(timezone=True), nullable=True))

op.create_table('reviews',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('nr_number', sa.String(length=15), nullable=True),
sa.Column('identifier', sa.String(length=50), nullable=True),
sa.Column('completing_party', sa.String(length=150), nullable=True),
sa.Column('status', sa.Enum('AWAITING_REVIEW', 'CHANGE_REQUESTED', 'RESUBMITTED', 'APPROVED', 'REJECTED', name='review_status'), nullable=False),
sa.Column('submission_date', sa.DateTime(timezone=True), nullable=True),
sa.Column('creation_date', sa.DateTime(timezone=True), nullable=True),
sa.Column('filing_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['filing_id'], ['filings.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('review_results',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('status', sa.Enum('AWAITING_REVIEW', 'CHANGE_REQUESTED', 'RESUBMITTED', 'APPROVED', 'REJECTED', name='review_status'), nullable=False),
sa.Column('comments', sa.Text(), nullable=True),
sa.Column('reviewer_id', sa.Integer(), nullable=True),
sa.Column('creation_date', sa.DateTime(timezone=True), nullable=True),
sa.Column('submission_date', sa.DateTime(timezone=True), nullable=True),
sa.Column('review_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['review_id'], ['reviews.id'], ),
sa.ForeignKeyConstraint(['reviewer_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('filings', 'resubmission_date')
op.drop_table('review_results')
op.drop_table('reviews')
op.execute("DROP TYPE review_status;")
# ### end Alembic commands ###
5 changes: 5 additions & 0 deletions legal-api/src/legal_api/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
from .registration_bootstrap import RegistrationBootstrap
from .request_tracker import RequestTracker
from .resolution import Resolution
from .review import Review, ReviewStatus
from .review_result import ReviewResult
from .share_class import ShareClass
from .share_series import ShareSeries
from .user import User, UserRoles
Expand Down Expand Up @@ -80,6 +82,9 @@
'RegistrationBootstrap',
'RequestTracker',
'Resolution',
'Review',
'ReviewResult',
'ReviewStatus',
'ShareClass',
'ShareSeries',
'User',
Expand Down
9 changes: 9 additions & 0 deletions legal-api/src/legal_api/models/filing.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ class Status(str, Enum):
PENDING = 'PENDING'
PENDING_CORRECTION = 'PENDING_CORRECTION'

# filings with staff review
APPROVED = 'APPROVED'
AWAITING_REVIEW = 'AWAITING_REVIEW'
CHANGE_REQUESTED = 'CHANGE_REQUESTED'
REJECTED = 'REJECTED'

class Source(Enum):
"""Render an Enum of the Filing Sources."""

Expand Down Expand Up @@ -201,6 +207,7 @@ class Source(Enum):
'name': 'continuationIn',
'title': 'Continuation In',
'temporaryCorpTypeCode': 'CTMP',
'staffApprovalRequired': True,
'codes': {
'C': 'CONTI',
'CBEN': 'CONTI',
Expand Down Expand Up @@ -438,6 +445,7 @@ class Source(Enum):
approval_type = db.Column('approval_type', db.String(15))
application_date = db.Column('application_date', db.DateTime(timezone=True))
notice_date = db.Column('notice_date', db.DateTime(timezone=True))
resubmission_date = db.Column('resubmission_date', db.DateTime(timezone=True))

# # relationships
transaction_id = db.Column('transaction_id', db.BigInteger,
Expand All @@ -458,6 +466,7 @@ class Source(Enum):
comments = db.relationship('Comment', lazy='dynamic')
documents = db.relationship('Document', lazy='dynamic')
filing_party_roles = db.relationship('PartyRole', lazy='dynamic')
review = db.relationship('Review', lazy='dynamic')

parent_filing_id = db.Column(db.Integer, db.ForeignKey('filings.id'))
parent_filing = db.relationship('Filing', remote_side=[id], backref=backref('children'))
Expand Down
77 changes: 77 additions & 0 deletions legal-api/src/legal_api/models/review.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Copyright © 2024 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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.
"""This module holds the data about review."""
from __future__ import annotations

from enum import auto

from legal_api.utils.base import BaseEnum
from legal_api.utils.datetime import datetime

from .db import db


class ReviewStatus(BaseEnum):
"""Render an Enum of the review status."""

AWAITING_REVIEW = auto()
CHANGE_REQUESTED = auto()
RESUBMITTED = auto()
APPROVED = auto()
REJECTED = auto()


class Review(db.Model): # pylint: disable=too-many-instance-attributes
"""This class manages the review."""

__tablename__ = 'reviews'

id = db.Column(db.Integer, primary_key=True)
nr_number = db.Column('nr_number', db.String(15))
identifier = db.Column('identifier', db.String(50))
completing_party = db.Column('completing_party', db.String(150))
status = db.Column('status', db.Enum(ReviewStatus), nullable=False)
submission_date = db.Column('submission_date',
db.DateTime(timezone=True),
default=datetime.utcnow) # last submission date
creation_date = db.Column('creation_date', db.DateTime(timezone=True), default=datetime.utcnow)

# parent keys
filing_id = db.Column('filing_id', db.Integer, db.ForeignKey('filings.id'), nullable=False)

# relationships
review_results = db.relationship('ReviewResult', lazy='dynamic')

def save(self):
"""Save the object to the database immediately."""
db.session.add(self)
db.session.commit()

@classmethod
def find_by_id(cls, review_id) -> Review:
"""Return review by the id."""
review = None
if review_id:
review = cls.query.filter_by(id=review_id).one_or_none()
return review

@classmethod
def get_review(cls, filing_id) -> Review:
"""Return review by the filing id."""
review = None
if filing_id:
review = (db.session.query(Review).
filter(Review.filing_id == filing_id).
one_or_none())
return review
71 changes: 71 additions & 0 deletions legal-api/src/legal_api/models/review_result.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Copyright © 2024 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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.
"""This module holds the data about review result."""
from __future__ import annotations

from typing import List

from sqlalchemy.orm import backref

from legal_api.utils.datetime import datetime

from .db import db
from .review import ReviewStatus


class ReviewResult(db.Model): # pylint: disable=too-many-instance-attributes
"""This class manages the review result."""

__tablename__ = 'review_results'

id = db.Column(db.Integer, primary_key=True)
status = db.Column('status', db.Enum(ReviewStatus), nullable=False)
comments = db.Column(db.Text)

reviewer_id = db.Column('reviewer_id', db.Integer, db.ForeignKey('users.id'))
reviewer = db.relationship('User',
backref=backref('reviewer', uselist=False),
foreign_keys=[reviewer_id])

creation_date = db.Column('creation_date', db.DateTime(timezone=True), default=datetime.utcnow)
submission_date = db.Column('submission_date', db.DateTime(timezone=True)) # submission/re-submission date

# parent keys
review_id = db.Column('review_id', db.Integer, db.ForeignKey('reviews.id'), nullable=False)

def save(self):
"""Save the object to the database immediately."""
db.session.add(self)
db.session.commit()

@ classmethod
def get_review_results(cls, review_id) -> List[ReviewResult]:
"""Return review results by the review id."""
review_results = None
if review_id:
review_results = (db.session.query(ReviewResult).
filter(ReviewResult.review_id == review_id).
all())
return review_results

@ classmethod
def get_last_review_result(cls, review_id) -> ReviewResult:
"""Return the last review result by the review id."""
review_result = None
if review_id:
review_result = (db.session.query(ReviewResult).
filter(ReviewResult.review_id == review_id).
order_by(ReviewResult.creation_date.desc()).
first())
return review_result
58 changes: 58 additions & 0 deletions legal-api/tests/unit/models/test_review.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright © 2024 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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.

"""Tests to assure the Review Model.
Test-Suite to ensure that the Review Model is working as expected.
"""
import copy
from registry_schemas.example_data import (
CONTINUATION_IN,
)

from legal_api.models import Review, ReviewStatus

from tests.unit.models import factory_filing


def test_review_save(session):
"""Assert that the review was saved."""
filing_dict = {
'filing': {
'header': {
'name': 'continuationIn',
'date': '2019-04-08',
'certifiedBy': 'full name',
'email': '[email protected]',
}
}
}
filing_dict['filing']['continuationIn'] = copy.deepcopy(CONTINUATION_IN)
filing = factory_filing(None, filing_dict)

review = Review()
review.filing_id = filing.id
review.nr_number = filing_dict['filing']['continuationIn']['nameRequest']['nrNumber']
review.identifier = filing_dict['filing']['continuationIn']['foreignJurisdiction']['identifier']
review.completing_party = 'completing party'
review.status = ReviewStatus.AWAITING_REVIEW
review.save()

assert review.id

review = Review.find_by_id(review.id)
assert review

review = Review.get_review(review.filing_id)
assert review
Loading

0 comments on commit 07b450f

Please sign in to comment.