Skip to content

Commit

Permalink
[kai/permitted-individual-create] Other Permitted Individual service (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kai-wei-mo authored Dec 8, 2022
1 parent 0298ef3 commit e46bb5b
Show file tree
Hide file tree
Showing 5 changed files with 276 additions and 0 deletions.
32 changes: 32 additions & 0 deletions backend/python/app/resources/other_permitted_individual_dto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class OtherPermittedIndividualDTO:
def __init__(self, **kwargs):
self.id = kwargs.get("id")
self.name = kwargs.get("name")
self.phone_number = kwargs.get("phone_number")
self.relationship_to_child = kwargs.get("relationship_to_child")
self.notes = kwargs.get("notes")
self.intake_id = kwargs.get("intake_id")


class CreateOtherPermittedIndividualDTO(OtherPermittedIndividualDTO):
def __init__(self, **kwargs):
super().__init__(**kwargs)

def validate(self):
error_list = []

if not self.name or not type(self.name) == str:
error_list.append("The name field is invalid")
if not self.phone_number or not type(self.phone_number) == str:
error_list.append("The phone_number field is invalid")
if (
not self.relationship_to_child
or not type(self.relationship_to_child) == str
):
error_list.append("The relationship_to_child field is invalid")
if not self.notes or not type(self.notes) == str:
error_list.append("The notes field is invalid")
if not self.intake_id or not type(self.intake_id) == int:
error_list.append("The intake_id field is invalid")

return error_list
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from ...models import db
from ...models.other_permitted_individual import OtherPermittedIndividual
from ...resources.other_permitted_individual_dto import (
CreateOtherPermittedIndividualDTO,
OtherPermittedIndividualDTO,
)
from ..interfaces.other_permitted_individual_service import (
IOtherPermittedIndividualService,
)


class OtherPermittedIndividualService(IOtherPermittedIndividualService):
def __init__(self, logger):
self.logger = logger

def get_all_other_permitted_individuals(self):
try:
return [
OtherPermittedIndividualDTO(**other_permitted_individual.__dict__)
for other_permitted_individual in OtherPermittedIndividual.query.all()
]
except Exception as error:
self.logger.error(str(error))
raise error

def create_new_other_permitted_individual(self, other_permitted_individual):
try:
if not other_permitted_individual:
raise Exception(
"Empty other_permitted_individual DTO/None passed to create_new_other_permitted_individual function"
)
if not isinstance(
other_permitted_individual, CreateOtherPermittedIndividualDTO
):
raise Exception(
"other_permitted_individual passed is not of CreateOtherPermittedIndividualDTO type"
)
error_list = other_permitted_individual.validate()
if error_list:
raise Exception(error_list)

new_other_permitted_individual_entry = OtherPermittedIndividual(
**other_permitted_individual.__dict__
)
db.session.add(new_other_permitted_individual_entry)
db.session.commit()

other_permitted_individual.id = new_other_permitted_individual_entry.id
return OtherPermittedIndividualDTO(**other_permitted_individual.__dict__)
except Exception as error:
db.session.rollback()
raise error

def delete_other_permitted_individual(self, other_permitted_individual_id):
try:
other_permitted_individual = OtherPermittedIndividual.query.get(
other_permitted_individual_id
)
if not other_permitted_individual:
raise Exception(
"No other_permitted_individual found with id {}".format(
other_permitted_individual_id
)
)
db.session.delete(other_permitted_individual)
db.session.commit()
except Exception as error:
db.session.rollback()
raise error
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from abc import ABC, abstractmethod


class IOtherPermittedIndividualService(ABC):
@abstractmethod
def get_all_other_permitted_individuals(self):
"""Get all other_permitted_individuals
:return: list of other_permitted_individuals
:rtype: list of OtherPermittedIndividualDTO
:raises: if error occurs in database query
"""

@abstractmethod
def create_new_other_permitted_individual(self, other_permitted_individual):
"""Adds the specified other_permitted_individual to the other_permitted_individual table and returns
a OtherPermittedIndividualDTO of the new entry
:param other_permitted_individual: the other_permitted_individual to be added
:type other_permitted_individual: CreateOtherPermittedIndividualDTO
:return: OtherPermittedIndividualDTO
:rtype: OtherPermittedIndividualDTO
:raises Exception: if input CreateOtherPermittedIndividualDTO is not valid or if there was an error
during insertion
"""
pass

@abstractmethod
def delete_other_permitted_individual(self, other_permitted_individual_id):
"""Deletes the other_permitted_individual with the specified id
:param other_permitted_individual_id: the id of the other_permitted_individual to be deleted
:type other_permitted_individual_id: int
:raises Exception: if no other_permitted_individual with the specified id exists
"""
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import datetime

import pytest
from flask import current_app

from app.models import db
from app.models.intake import Intake
from app.models.other_permitted_individual import OtherPermittedIndividual
from app.models.user import User
from app.resources.other_permitted_individual_dto import (
CreateOtherPermittedIndividualDTO,
OtherPermittedIndividualDTO,
)
from app.services.implementations.other_permitted_individual_service import (
OtherPermittedIndividualService,
)

# opi = other permitted individual


@pytest.fixture
def opi_service():
opi = OtherPermittedIndividualService(current_app.logger)
seed_database()
yield opi
teardown_database()
OtherPermittedIndividual.query.delete()


USER_DATA = {
"first_name": "John",
"last_name": "Doe",
"auth_id": "auth0|123456789",
"role": "User",
"branch": "ALGOMA",
}

INTAKE_DATA = {
"user_id": 1,
"referring_worker_name": "John Doe",
"referring_worker_contact": "[email protected]",
"referral_date": datetime.date(2020, 1, 1),
"family_name": "Doe",
"cpin_number": "123456789",
"cpin_file_type": "ONGOING",
"court_status": "OTHER",
"court_order_file": "court_order.pdf",
"transportation_requirements": "car",
"scheduling_requirements": "flexible",
"suggested_start_date": datetime.date(2020, 1, 1),
}

OPI_DATA = {
"name": "Jim Halpert",
"phone_number": "1234567890",
"relationship_to_child": "sales",
"notes": "note note note",
"intake_id": 1,
}


def seed_database():
user = User(**USER_DATA)
db.session.add(user)
db.session.commit()

intake = Intake(**INTAKE_DATA)
db.session.add(intake)
db.session.commit()

opi = OtherPermittedIndividual(**OPI_DATA)
db.session.add(opi)
db.session.commit()


def teardown_database():
OtherPermittedIndividual.query.delete()
Intake.query.delete()
User.query.delete()
db.session.execute(
"ALTER SEQUENCE other_permitted_individuals_id_seq RESTART WITH 1"
)
db.session.execute("ALTER SEQUENCE intakes_id_seq RESTART WITH 1")
db.session.execute("ALTER SEQUENCE users_id_seq RESTART WITH 1")
db.session.commit()


def test_normal_case(opi_service):
param = CreateOtherPermittedIndividualDTO(
name="Angela Martin",
phone_number="1234567890",
relationship_to_child="accounting",
notes="note note note",
intake_id=1,
)
opi_instance = opi_service.create_new_other_permitted_individual(param)
assert type(opi_instance) is OtherPermittedIndividualDTO


def test_null_case(opi_service):
with pytest.raises(Exception):
opi_service.create_new_other_permitted_individual(None)


def test_empty_input_string(opi_service):
param = CreateOtherPermittedIndividualDTO(
name="", # name cannot be empty
phone_number="1234567890",
relationship_to_child="accounting",
notes="note note note",
intake_id=1,
)
with pytest.raises(Exception):
opi_service.create_new_other_permitted_individual(param)


def test_missing_field(opi_service):
param = CreateOtherPermittedIndividualDTO(
name="Angela Martin",
# some required fields are missing
)
with pytest.raises(Exception):
opi_service.create_new_other_permitted_individual(param)


def test_delete_existing_success(opi_service):
opi_service.delete_other_permitted_individual(1)
assert OtherPermittedIndividual.query.get(1) is None


def test_delete_non_existing(opi_service):
with pytest.raises(Exception):
opi_service.delete_other_permitted_individual(999)
9 changes: 9 additions & 0 deletions backend/python/tools/db_seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ def insert_test_data():
for value in values:
insert_values(db, "caregivers", ("name", "date_of_birth", "individual_considerations", "primary_phone_number", "secondary_phone_number", "email", "address", "relationship_to_child", "additional_contact_notes", "intake_id"), value)

# Other Permitted Individuals
values = [
('Yor Forger', '555-555-5555', 'relationship', 'notes', 1),
('Loid Forger', '777-777-7777', 'relationship', 'notes', 1)
]

for value in values:
insert_values(db, "other_permitted_individuals", ("name", "phone_number", "relationship_to_child", "notes", "intake_id"), value)

# Providers
values = [
('Provider One', '111', '555-555-5555', '777-777-7777', '[email protected]', 'address', 'KINSHIP_PROVIDER', 'NULL', 1),
Expand Down

0 comments on commit e46bb5b

Please sign in to comment.