Beispiel #1
0
def validate_access_token(access_token: str,
                          user_role: str = 'user') -> Tuple[str, UserStored]:
    payload = jwt.decode(access_token,
                         AUTH_CONFIG['access_token']['secret'],
                         algorithms=AUTH_CONFIG['access_token']['sign_alg'])
    if user_role not in payload['roles']:
        raise errors.UnverifiedRoleException(
            'Required role not contained in jwt')

    user_id: str = payload['sub']
    try:
        user: UserStored = UsersDb.get_user_by_id(user_id)
        return user_id, user
    except LookupError:
        raise errors.UserNotExistsException(
            f'User with id {user_id} does not exist')
Beispiel #2
0
    def set_record(user_id: str, record: Record) -> Record:
        """Set medical record data.
        If the record already exists, its data is merged.

        :return: The updated record data.
        :raises LookupError: If no record exists for the requested user.
        """
        user_exists, user_ref = UsersDb.user_id_exists(user_id)
        if not user_exists:
            raise errors.UserNotExistsException(
                f'User with id {user_id} does not exist')
        date_str = convert_date_to_str(record.date)
        record_ref: DocumentReference = user_ref.collection(
            'records').document(date_str)
        record_ref.set(record.symptoms.dict(exclude_unset=True), merge=True)

        updated_record = record_ref.get().to_dict()
        return Record(date=datetime.datetime.strptime(date_str, DATE_FORMAT),
                      symptoms=Symptoms.parse_obj(updated_record))
Beispiel #3
0
    def get_one_by_user_id_and_date_str(user_id: str, date_str: str) -> Record:
        user_exists, user_ref = UsersDb.user_id_exists(user_id)
        if not user_exists:
            raise errors.UserNotExistsException(
                f'User with id {user_id} does not exist')
        records_ref: CollectionReference = user_ref.collection(u'records')

        record = None
        for doc_ref in records_ref.list_documents():
            doc_date = doc_ref.get().id
            if doc_date == date_str:
                symptoms = Symptoms.parse_obj(doc_ref.get().to_dict())
                record = Record(date=convert_str_to_date(doc_date),
                                symptoms=symptoms)
                break

        if not record:
            raise LookupError(f'No record for {date_str} could be found')
        else:
            return record
Beispiel #4
0
    def get_by_user_id(user_id: str) -> List[Record]:
        """Get medical record documents from the Firestore database.

        :param user_id: user id for whom the records shall be retrieved
        :return: all records for the user, empty if nothing found
        """
        user_exists, user_ref = UsersDb.user_id_exists(user_id)
        if not user_exists:
            raise errors.UserNotExistsException(
                f'User with id {user_id} does not exist')
        records_ref: CollectionReference = user_ref.collection(u'records')

        records = []
        for doc_ref in records_ref.list_documents(page_size=50):
            doc_snapshot = doc_ref.get()
            symptoms = Symptoms.parse_obj(doc_snapshot.to_dict())
            record = Record(date=convert_str_to_date(doc_snapshot.id),
                            symptoms=symptoms)
            records.append(record)
        return records
Beispiel #5
0
 def get_user_id(username: str) -> str:
     _, ref = UsersDb.username_exists(username)
     if not ref:
         raise errors.UserNotExistsException(
             f'User {username} does not exist')
     return ref.get().id