def get_sessions():
    """
    handles session retrievals based on email,session_id and course_id
    :return: session details
    """

    body = session_app.current_request.json_body
    if not body:
        return Response({'message': 'empty body, missing required details'},
                        status_code=422)

    log.info('retrieving session details')
    if body.get('sessionId', False) and body['sessionId'] is not None:
        log.info('connecting to session db with session id')
        current_session = global_db_handler.get_session_db().get_item_details(
            body['sessionId'])
        current_session = global_db_handler.replace_decimals(current_session)
        return Response(body=current_session, status_code=200)
    if body.get('courseId', False):
        if not body.get('email'):
            body['email'] = global_auth.get_authorized_email(
                session_app.current_request)
        user_session_data = global_db_handler.get_user_db().list_items_by_mail(
            body['email'])
        log.info('current user data ')
        if len(user_session_data) <= 0:
            return Response(body=[], status_code=200)
        session_id_list = []
        for each_session in user_session_data:
            session_id_list.append(each_session['sessionId'])
        log.info('sessions found for current request')
        session_details = global_db_handler.get_session_db().list_items_course(
            session_id_list, body['courseId'])
        log.info("session details")
        session_details = global_db_handler.replace_decimals(session_details)
        return Response(body=session_details, status_code=200)
    if not body.get('email', False):
        log.info('no email found. decoding auth token')
        body['email'] = global_auth.get_authorized_email(
            session_app.current_request)
    log.info('getting session details by email')
    user_session_data = global_db_handler.get_user_db().list_items_by_mail(
        body['email'])
    log.info('response from user table')
    if len(user_session_data) <= 0:
        return Response(body=[], status_code=200)
    session_id_list = []
    for each_session in user_session_data:
        session_id_list.append(each_session['sessionId'])
    log.info('getting all sessions which match session ids')
    session_details = global_db_handler.get_session_db().list_items_mail(
        session_id_list)
    log.info(f"returning session details {session_details}")
    session_details = global_db_handler.replace_decimals(session_details)
    return Response(body=session_details, status_code=200)
def session_types():
    global _SESSION_ROLES
    if _SESSION_ROLES is None:
        log.info("cache miss for session roles, connecting to db")
        _SESSION_ROLES = global_db_handler.get_session_roles_db().list_all_items()
        _SESSION_ROLES = global_db_handler.replace_decimals(_SESSION_ROLES)
    return _SESSION_ROLES
def session_roles():
    global _SESSION_ROLES
    if _SESSION_ROLES is None:
        log.info("cache miss for session roles, connecting to db")
        _SESSION_ROLES = global_db_handler.get_session_roles_db().list_all_items()
        _SESSION_ROLES = global_db_handler.replace_decimals(_SESSION_ROLES)
    return Response(body=_SESSION_ROLES, status_code = 200)
def user_roles():
    global _USER_ROLES
    if _USER_ROLES is None:
        log.info("cache miss for user roles, connecting to db")
        _USER_ROLES = global_db_handler.get_user_roles_db().list_all_items()
        _USER_ROLES = global_db_handler.replace_decimals(_USER_ROLES)

    return _USER_ROLES
def get_session_audit(session_id):
    log.info(f"getting session audit event for sessionId {session_id}")
    response = global_db_handler.get_session_audit_db().get_session_item(
        session_id)
    if len(response) == 0:
        return Response(body={"message": "session not found"}, status_code=400)
    response = global_db_handler.replace_decimals(response)
    return Response(body=response, status_code=200)
def get_user_audit(user_id):
    log.info(f"getting user audit event for userId {user_id}")
    response = global_db_handler.get_user_audit_db().get_user_item(
        str(user_id))

    if len(response) == 0:
        return Response(body={"message": "user not found"}, status_code=400)
    response = global_db_handler.replace_decimals(response)
    return Response(body=response, status_code=200)
Esempio n. 7
0
def get_feedback(session_id):

    response = global_db_handler.get_feedback_db().get_item(session_id)

    if len(response) == 0:
        return Response(body={"message": "unable to find session id"},
                        status_code=400)

    response = global_db_handler.replace_decimals(response)
    return Response(body=response, status_code=200)
def test_jwt_session():
    user_id = global_auth.get_authorized_user_id(session_app.current_request)
    session_id = global_auth.get_authorized_session_id(
        session_app.current_request)
    user_data = global_auth.get_authorized_user_data(
        session_app.current_request)
    email = global_auth.get_authorized_email(session_app.current_request)
    response = {
        "sessionId": session_id,
        "userId": user_id,
        "user_data": user_data,
        "email": email
    }
    response = global_db_handler.replace_decimals(response)
    return {"message": response}
def update_session_duration():
    body = session_app.current_request.json_body
    log.info(f"session status update {body}")

    if not body or not body.get('duration', False):
        return Response(
            body={'message': 'empty body, duration field is required'},
            status_code=422)

    if not isinstance(body['duration'], int):
        return Response(body={'message': 'duration field is int'},
                        status_code=422)

    if not body.get('sessionId', False):
        body['sessionId'] = global_auth.get_authorized_session_id(
            session_app.current_request)
    response = global_db_handler.get_session_db().update_duration(body)
    response = global_db_handler.replace_decimals(response)
    return Response(body=response, status_code=200)
def login():
    """"
    function to join session and generate jwt.
    :returns jwt token and session data (dict)
    """
    body = session_app.current_request.json_body
    if not body or not body.get('token'):
        return Response(body={'message': 'token is required'}, status_code=422)
    log.info(f"received session join request with {body['token']}")
    response = global_db_handler.get_user_db().validate_token(body['token'])
    log.info(f"response from db call for token  {body['token']} ")
    if len(response) <= 0:
        log.info("no session found with token")
        return Response(body={
            'message': 'no session found',
            'code': 'session_not_found'
        },
                        status_code=400)
    current_session_details = global_db_handler.get_session_db().get_item(
        response['sessionId'])
    if len(current_session_details) == 0:
        return Response(body={
            'message': 'no session found',
            'code': 'session_not_found'
        },
                        status_code=400)
    log.info(f'current session details for the token{current_session_details}')
    body['sessionDetails'] = {
        'email': response['email'],
        'firstName': response['firstName'],
        'lastName': response['lastName'],
        'token': body['token'],
        'role': response['role'],
        'userId': response['uid'],
        'sessionId': response['sessionId']
    }
    log.info('generating authentication token using jwt')
    jwt_token = auth.get_jwt_token(body['sessionDetails'])
    log.info('token generated, returning response ')
    session_audit_payload = {
        "sessionId": response['sessionId'],
        "statusId": 3,
        "status": "IN_PROGRESS",
        "timestamp": str(datetime.utcnow())
    }
    user_audit_payload = {
        "userId": response['uid'],
        "roleId": 3,
        "status": "JOINED",
        "timestamp": str(datetime.utcnow()),
        'email': response['email']
    }
    session_status_payload = {
        "sessionId": response['sessionId'],
        "status": "IN_PROGRESS"
    }
    audit_controller.update_audit("session", session_audit_payload)
    audit_controller.update_audit("user", user_audit_payload)
    update_session_status(session_status_payload)
    current_session_details = global_db_handler.replace_decimals(
        current_session_details)
    return Response(body={
        'token': jwt_token,
        'sessionDetails': current_session_details,
        'user': {
            'firstName': response['firstName'],
            'lastName': response['lastName'],
            'email': response['email'],
            'phoneNumber': response['phoneNumber'],
            'profilePicUrl': response['profilePicUrl']
        }
    },
                    status_code=200)