def get_participant_role_time_seconds()

in src/dispatch/case_cost/service.py [0:0]


def get_participant_role_time_seconds(case: Case, participant_role: ParticipantRole) -> float:
    """Calculates the time spent by a participant in a case role starting from a given time.

    The participant's time spent in the case role is adjusted based on the role's engagement multiplier.

    Args:
        case: The case the participant is part of.
        participant_role: The role of the participant.
        start_at: Only time spent after this will be considered.

    Returns:
        float: The time spent by the participant in the case role in seconds.
    """
    if participant_role.role == ParticipantRoleType.observer:
        # skip calculating cost for participants with the observer role
        return 0

    # we set the renounced_at default time to the current time
    participant_role_renounced_at = datetime.now(tz=timezone.utc).replace(tzinfo=None)
    if participant_role.renounced_at:
        participant_role_renounced_at = participant_role.renounced_at
    elif case.status == CaseStatus.closed:
        if case.closed_at:
            participant_role_renounced_at = case.closed_at
    elif case.status == CaseStatus.escalated:
        if case.escalated_at:
            participant_role_renounced_at = case.escalated_at

    # the time the participant has spent in the case role since the last case cost update
    participant_role_time = participant_role_renounced_at - participant_role.assumed_at
    if participant_role_time.total_seconds() < 0:
        # the participant was added after the case was closed/escalated
        return 0

    # we calculate the number of hours the participant has spent in the case role
    participant_role_time_hours = participant_role_time.total_seconds() / SECONDS_IN_HOUR

    # we make the assumption that participants only spend 8 hours a day working on the case,
    # if the case goes past 24hrs
    if participant_role_time_hours > HOURS_IN_DAY:
        days, hours = divmod(participant_role_time_hours, HOURS_IN_DAY)
        participant_role_time_hours = ((days * HOURS_IN_DAY) / 3) + hours

    # we make the assumption that participants spend more or less time based on their role
    # and we adjust the time spent based on that
    return (
        participant_role_time_hours
        * SECONDS_IN_HOUR
        * get_engagement_multiplier(participant_role.role)
    )