def METHOD_NAME(data):
    """Create a user and set the profile fields from a data.
    :param data: A dict containing a mix of patron and user data.
    :returns: The modified dict.
    """
    from .modules.users.api import User
    data = deepcopy(data)
    profile_fields = [
        'first_name', 'last_name', 'street', 'postal_code', 'gender',
        'city', 'birth_date', 'username', 'home_phone', 'business_phone',
        'mobile_phone', 'other_phone', 'keep_history', 'country', 'email',
        'password'
    ]
    user = User.get_by_username(data.get('username'))
    if not user:
        with db.session.begin_nested():
            # create the user
            password = data.get('password')
            if not password:
                length = current_app.config.get(
                    'RERO_ILS_PASSWORD_MIN_LENGTH', 8)
                special_char = current_app.config.get(
                    'RERO_ILS_PASSWORD_SPECIAL_CHAR')
                password = password_generator(
                    length=length, special_char=special_char)
            password = hash_password(password)
            user = BaseUser(password=password, profile=dict(), active=True)
            db.session.add(user)
            # set the user fields
            if data.get('email') is not None:
                user.email = data['email']
            profile = user.profile
            # set the profile
            for field in profile_fields:
                value = data.get(field)
                if value is not None:
                    if field == 'birth_date':
                        value = datetime.strptime(value, '%Y-%m-%d')
                    setattr(profile, field, value)
            db.session.merge(user)
        db.session.commit()
        confirm_user(user)
        user_id = user.id
    else:
        user_id = user.user.id
    # remove the user fields from the data
    for field in profile_fields:
        try:
            del data[field]
        except KeyError:
            pass
    data['user_id'] = user_id
    return data