diff --git a/src/server/BreCal/database/sql_utils.py b/src/server/BreCal/database/sql_utils.py index 6c99818..cd2606d 100644 --- a/src/server/BreCal/database/sql_utils.py +++ b/src/server/BreCal/database/sql_utils.py @@ -40,6 +40,53 @@ def get_port_ids_for_participant_id(participant_id:int): pdata = execute_sql_query_standalone(query=query, param={"participant_id":participant_id}) return pdata +def filter_berths_by_port(berths:list[model.Berth], participant:model.Participant)->list[model.Berth]: + """ + this function filters a list of berths by the participant's assigned ports. + + Uses: + berth.port_id (the port's int id of each berth) + Participant.ports (a list of int ids) + + returns: berths (filtered) + """ + berths = [berth for berth in berths if berth.port_id in participant.ports] + return berths + + +def filter_berths_by_users_assigned_ports(berths:list[model.Berth], user_data:dict, commands=None)->list[model.Berth]: + """ + this function uses the user_id to filter a list of berths. Only those berths, which belong to the Participant's assigned + port list are relevant. + + Uses: + berth.port_id (the port's int id of each berth) + + user_data 'id' to obtain Participant + Participant.ports (a list of int ids) + + returns: berths + """ + assert 'id' in list(user_data.keys()) + + if commands is None: + pooledConnection = local_db.getPoolConnection() + commands = pydapper.using(pooledConnection) + + # filter: return only the relevant berths to a user, which belong to his assigned ports + user_participant_query = SQLQuery.get_participant_by_user_id() + + # use the user's id to obtain the matching participant + sentinel = object() + participant = commands.query_single_or_default( + user_participant_query, default=sentinel, model=model.Participant, + param={"userid":user_data.get("id")}) + if participant is sentinel: + raise Exception(f"could not find a user with id {user_data.get('id')}") + berths = filter_berths_by_port(berths, participant) + return berths + + def filter_shipcalls_by_port(shipcalls:list[model.Shipcall], participant:model.Participant)->list[model.Shipcall]: """ this function filters a list of shipcalls by the participant's assigned ports. diff --git a/src/server/BreCal/impl/berths.py b/src/server/BreCal/impl/berths.py index f00a9e8..0920d71 100644 --- a/src/server/BreCal/impl/berths.py +++ b/src/server/BreCal/impl/berths.py @@ -4,6 +4,7 @@ import pydapper from ..schemas import model from .. import local_db +from BreCal.database.sql_utils import filter_berths_by_users_assigned_ports def GetBerths(options): """