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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,11 @@ Prerequisites (first setup only)

To add an IP address to the IP allow lists, Ensure you're authenticated for access to AWS and run the following command.

make update-all-ip-allowlists
make update-all-ip-allowlists [WARNING: Not working the stack for this one needs fixing (likely SSO issue)]

To add an IP address to the IP allow lists and deploy the allow list to environment run the following command.The `PROFILE` delineates which environment to update with the latest IP allow list. Set `ENVIRONMENT` if you are changing an environment not linked to your branch

make update-ip-allowlists-and-deploy-allowlist PROFILE=dev
make update-ip-allowlists-and-deploy-allowlist PROFILE=dev [WARNING: Not working the stack for this one needs fixing (likely SSO issue)]

### DoS Database Connection

Expand Down
59 changes: 59 additions & 0 deletions application/common/dos.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from aws_lambda_powertools.logging import Logger
from psycopg import Connection
from psycopg.rows import DictRow

from .constants import (
DOS_ACTIVE_STATUS_ID,
Expand Down Expand Up @@ -221,6 +222,11 @@ def get_specified_opening_times_from_db(connection: Connection, service_id: int)
return specified_opening_times


def convert_db_to_specified_opening_times(db_specified_opening_times: list[DictRow]) -> list[SpecifiedOpeningTime]:
"""Retrieves specified opening times from DoS database."""
return db_rows_to_spec_open_times(db_specified_opening_times)


def get_standard_opening_times_from_db(connection: Connection, service_id: int) -> StandardOpeningTimes:
"""Retrieves standard opening times from DoS database.

Expand All @@ -244,6 +250,15 @@ def get_standard_opening_times_from_db(connection: Connection, service_id: int)
return standard_opening_times


def convent_db_to_standard_opening_times(db_std_opening_times: list[DictRow]) -> StandardOpeningTimes:
"""Retrieves standard opening times from DoS database.

If the service id does not even match any service this function will still return a blank StandardOpeningTime
with no opening periods.
"""
return db_rows_to_std_open_times(db_std_opening_times)


def db_rows_to_spec_open_times(db_rows: Iterable[dict]) -> list[SpecifiedOpeningTime]:
"""Turns a set of dos database rows into a list of SpecifiedOpenTime objects.

Expand All @@ -264,6 +279,32 @@ def db_rows_to_spec_open_times(db_rows: Iterable[dict]) -> list[SpecifiedOpening
return specified_opening_times


def db_big_rows_to_spec_open_times(db_rows: Iterable[dict]) -> list[SpecifiedOpeningTime]:
"""Turns a set of dos database rows into a list of SpecifiedOpenTime objects.

note: The rows must to be for the same service.
"""
specified_opening_times = []
specified_op_times_ids = []
db_rows_refined = []
for row in list(db_rows):
if row["ssot_id"] is not None and row["ssot_id"] not in specified_op_times_ids:
specified_op_times_ids.append(row["ssot_id"])
db_rows_refined.append(row)
date_sorted_rows = sorted(db_rows_refined, key=lambda row: (row["date"], row["date_starttime"]))
for date, db_rows_refined in groupby(date_sorted_rows, lambda row: row["date"]):
is_open = True
open_periods = []
for row in list(db_rows_refined):
if row["isclosed"] is True:
is_open = False
else:
open_periods.append(OpenPeriod(row["date_starttime"], row["date_endtime"]))
specified_opening_times.append(SpecifiedOpeningTime(open_periods, date, is_open))

return specified_opening_times


def db_rows_to_std_open_times(db_rows: Iterable[dict]) -> StandardOpeningTimes:
"""Turns a set of dos database rows into a StandardOpeningTime object.

Expand All @@ -279,6 +320,24 @@ def db_rows_to_std_open_times(db_rows: Iterable[dict]) -> StandardOpeningTimes:
return standard_opening_times


def db_big_rows_to_std_open_times(db_rows: Iterable[dict]) -> StandardOpeningTimes:
"""Turns a set of dos database rows into a StandardOpeningTime object.

note: The rows must be for the same service.
"""
standard_opening_times = StandardOpeningTimes()
std_op_times_ids = []
for row in db_rows:
if row["sdot_id"] is not None and row["sdot_id"] not in std_op_times_ids:
std_op_times_ids.append(row["sdot_id"])
weekday = row["name"].lower()
start = row["day_starttime"]
end = row["day_endtime"]
open_period = OpenPeriod(start, end)
standard_opening_times.add_open_period(open_period, weekday)
return standard_opening_times


def has_palliative_care(service: DoSService, connection: Connection) -> bool:
"""Checks if a service has palliative care.

Expand Down
171 changes: 171 additions & 0 deletions application/common/tests/test_dos.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from application.common.dos import (
DoSService,
db_big_rows_to_spec_open_times,
db_big_rows_to_std_open_times,
db_rows_to_spec_open_times,
db_rows_to_std_open_times,
get_dos_locations,
Expand Down Expand Up @@ -584,6 +586,91 @@ def test_db_rows_to_spec_open_times() -> None:
assert spec_open_times == expected_spec_open_times


def test_db_big_rows_to_spec_open_times() -> None:
db_rows = [
{
"serviceid": 1,
"date": date(2019, 5, 6),
"date_starttime": time(8, 0, 0),
"date_endtime": time(20, 0, 0),
"isclosed": False,
"ssot_id": 123,
},
{
"serviceid": 1,
"date": date(2019, 5, 6),
"date_starttime": time(21, 0, 0),
"date_endtime": time(22, 0, 0),
"isclosed": False,
"ssot_id": 324,
},
{
"serviceid": 1,
"date": date(2019, 5, 27),
"date_starttime": time(8, 0, 0),
"date_endtime": time(20, 0, 0),
"isclosed": False,
"ssot_id": 768,
},
{
"serviceid": 1,
"date": date(2019, 8, 26),
"date_starttime": time(8, 0, 0),
"date_endtime": time(20, 0, 0),
"isclosed": False,
"ssot_id": 987,
},
{
"serviceid": 1,
"date": date(2019, 9, 20),
"date_starttime": None,
"date_endtime": None,
"isclosed": True,
"ssot_id": 567,
},
{
"serviceid": 1,
"date": date(2020, 5, 6),
"date_starttime": time(6, 0, 0),
"date_endtime": time(7, 0, 0),
"isclosed": False,
"ssot_id": 876,
},
{
"serviceid": 1,
"date": date(2020, 5, 6),
"date_starttime": time(6, 0, 0),
"date_endtime": time(7, 0, 0),
"isclosed": False,
"ssot_id": 876,
},
{
"serviceid": 1,
"date": None,
"date_starttime": None,
"date_endtime": None,
"isclosed": None,
"ssot_id": None,
},
]

spec_open_times = db_big_rows_to_spec_open_times(db_rows)

expected_spec_open_times = [
SpecifiedOpeningTime(
[OpenPeriod.from_string_times("08:00", "20:00"), OpenPeriod.from_string_times("21:00", "22:00")],
date(2019, 5, 6),
True,
),
SpecifiedOpeningTime([OpenPeriod.from_string_times("08:00", "20:00")], date(2019, 5, 27), True),
SpecifiedOpeningTime([OpenPeriod.from_string_times("08:00", "20:00")], date(2019, 8, 26), True),
SpecifiedOpeningTime([], date(2019, 9, 20), False),
SpecifiedOpeningTime([OpenPeriod.from_string_times("06:00", "07:00")], date(2020, 5, 6), True),
]

assert spec_open_times == expected_spec_open_times


def test_db_rows_to_std_open_time() -> None:
db_rows = [
{"serviceid": 1, "dayid": 0, "name": "Monday", "starttime": time(8, 0, 0), "endtime": time(17, 0, 0)},
Expand Down Expand Up @@ -611,6 +698,90 @@ def test_db_rows_to_std_open_time() -> None:
assert actual_std_open_times == expected_std_open_times


def test_db_big_rows_to_std_open_time() -> None:
db_rows = [
{
"serviceid": 1,
"dayid": 0,
"name": "Monday",
"day_starttime": time(8, 0, 0),
"day_endtime": time(17, 0, 0),
"sdot_id": 230,
},
{
"serviceid": 1,
"dayid": 6,
"name": "Sunday",
"day_starttime": time(13, 0, 0),
"day_endtime": time(15, 30, 0),
"sdot_id": 231,
},
{
"serviceid": 1,
"dayid": 1,
"name": "Tuesday",
"day_starttime": time(13, 0, 0),
"day_endtime": time(18, 0, 0),
"sdot_id": 233,
},
{
"serviceid": 1,
"dayid": 4,
"name": "Friday",
"day_starttime": time(13, 0, 0),
"day_endtime": time(15, 30, 0),
"sdot_id": 232,
},
{
"serviceid": 1,
"dayid": 6,
"name": "Wednesday",
"day_starttime": time(7, 0, 0),
"day_endtime": time(15, 30, 0),
"sdot_id": 234,
},
{
"serviceid": 1,
"dayid": 1,
"name": "Tuesday",
"day_starttime": time(8, 0, 0),
"day_endtime": time(12, 0, 0),
"sdot_id": 287,
},
{
"serviceid": 1,
"dayid": 4,
"name": "Thursday",
"day_starttime": time(11, 0, 0),
"day_endtime": time(13, 30, 0),
"sdot_id": 238,
},
{
"serviceid": 1,
"dayid": 4,
"name": "Thursday",
"day_starttime": time(11, 0, 0),
"day_endtime": time(13, 30, 0),
"sdot_id": 238,
},
]

expected_std_open_times = StandardOpeningTimes()
expected_std_open_times.monday = [OpenPeriod.from_string_times("08:00", "17:00")]
expected_std_open_times.tuesday = [
OpenPeriod.from_string_times("08:00", "12:00"),
OpenPeriod.from_string_times("13:00", "18:00"),
]
expected_std_open_times.wednesday = [OpenPeriod.from_string_times("07:00", "15:30")]
expected_std_open_times.thursday = [OpenPeriod.from_string_times("11:00", "13:30")]
expected_std_open_times.friday = [OpenPeriod.from_string_times("13:00", "15:30")]
expected_std_open_times.sunday = [OpenPeriod.from_string_times("13:00", "15:30")]

actual_std_open_times = db_big_rows_to_std_open_times(db_rows)

assert actual_std_open_times == expected_std_open_times


def get_db_item(odscode: str = "FA9321", name: str = "fake name", id: int = 9999, typeid: int = 13) -> dict: # noqa: A002
return {
"id": id,
Expand Down
Loading