def get(self, scope): try: if scope.upper() == "ALL": all_users = [] for i in UserCreateUpdateService.get_all_users(): all_users.append( UserConverter.convert_user_dto_to_public_response_dto( i)) return {'response': all_users}, 200 elif scope.upper() == "SINGLE": request = UserFetchResource.parser.parse_args() user = get_jwt_identity() if not user: return { 'error': ErrorEnums.SESSION_NOT_FOUND_ERROR.value }, 403 if not request.get('Id'): return ErrorEnums.ID_MISSING_ERROR.value, 400 if len(request.get('Id')) != 24: return ErrorEnums.INVALID_ID_FORMAT_ERROR.value, 400 user = UserCreateUpdateService.get_existing_user_by_id( request.get('Id')) if 'error' in user.keys(): return user, 404 return UserConverter.convert_user_dto_to_public_response_dto( user), 200 else: return { 'error': 'Unknown scope encountered. Please check your input.' }, 400 except Exception as e: return {'error': 'Exception - {} - occurred.'.format(e.args)}, 400
def update_email(identity, new_em): user = UserDatabaseService.get_user_by_id(identity.get('id')) try: user.update(set__email=new_em, set__last_updated_at=datetime.datetime.now(), set__last_updated_by=user) updated_user = dataStateAccess.UserDTO.user_dto( UserDatabaseService.get_user_by_id(identity.get('id'))) is_update_verified = verify_email_update_for_user( updated_user, new_em) if not is_update_verified: return [{ 'error': 'There was some error while updating email. Error Code: {}' .format(str(int(time.time() * 1000))) }, 500] response = UserUtils.convert_user_dto_to_public_response_dto( updated_user) return [{ 'Success': 'Email updated successfully.', 'updatedUserDetails': response }, 200] except Exception as e: return [{ "error": "There was some error. Exception: {}, Error Code: {}".format( e, str(int(time.time() * 1000))) }, 500]
def generate_session_token(self, user): self.session_dao = SessionHistoryDatabaseService() active_token = self.get_active_token_record(user) if active_token: return active_token session_token = self.generate_fresh_session_token( UserConverter.convert_user_dto_to_public_response_dto(user)) self.session_dao.insert_login_session(user.get('email'), session_token) return session_token
def update_username(identity, new_username): user = UserDatabaseService.get_user_by_id(identity.get('id')) user.update(set__username=new_username, set__last_updated_at=datetime.datetime.now(), set__last_updated_by=user) updated_user = dataStateAccess.UserDTO.user_dto( UserDatabaseService.get_user_by_id(identity.get('id'))) is_validated = verify_username_update_for_user(updated_user, new_username) if is_validated: return [{ 'Success': 'Username successfully updated.', 'updatedUserDetails': UserUtils.convert_user_dto_to_public_response_dto(updated_user) }, 200] return [{ 'Error': 'There was some error. Please retry again. Error code: {}'.format( str(int(time.time() * 1000))) }, 500]