<commit_before>class UsersController < AuthenticatedController
  skip_before_action :authenticate_user!, only: [:new, :create]
  skip_before_action :ensure_user_paid!

  before_action :set_user

  def new
    if current_user.present? && !current_user.guest?
      redirect_to dashboard_path
    end
    @user = User.new
  end

  def create
    if current_user
      @user = current_user
      @user.assign_attributes(user_params)
      @user.guest = false
    else
      @user = User.new(user_params)
    end

    @user.password_confirmation = user_params[:password]
    @user.timezone = Time.zone.tzinfo.name

    valid = @user.save && create_customer_and_subscription

    if valid
      login(@user)
      redirect_to welcome_path
    else
      render :new
    end
  end

  def edit
    if @user.stripe_customer
      @existing_card = current_user.stripe_customer.sources.first
    end
  end

  def update
    @user.assign_attributes(user_params)

    valid = @user.save && add_card_to_user

    if valid
      flash[:success] = 'Settings updated'
      redirect_to dashboard_path
    else
      flash[:error] = @user.errors.full_messages.join(', ')
      redirect_to settings_path
    end
  end

  def destroy
    @credit_card_service = CreditCardService.new(user: @user)

    if @credit_card_service.cancel_subscription || @user.destroy
      flash[:success] = 'Account deleted. Sorry to see you go!'
    else
      flash[:error] = 'Something went wrong.'
    end

    redirect_to new_user_path
  end

  def cancel_subscription
    @credit_card_service = CreditCardService.new(user: @user)

    if @credit_card_service.cancel_subscription
      flash[:success] = 'Subscription cancelled.'
    else
      flash[:error] = 'Something went wrong.'
    end

    redirect_to settings_path
  end

  private

  def create_customer_and_subscription
    @credit_card_service = CreditCardService.new(user: @user)

    @credit_card_service.create_customer &&
      @credit_card_service.create_subscription
  end

  def add_card_to_user
    @credit_card_service = CreditCardService.new(user: @user)

    if params[:stripeToken]
      @credit_card_service.add_card_to_customer(token: params[:stripeToken])
    else
      true
    end
  end

  def set_user
    @user = current_user
  end

  def user_params
    params.require(:user).permit(
      :name,
      :email,
      :phone,
      :email_reminders,
      :sms_reminders,
      :track_weekends,
      :password
    )
  end
end<commit_msg>Fix references to payment service in controller.<commit_after>class UsersController < AuthenticatedController
  skip_before_action :authenticate_user!, only: [:new, :create]
  skip_before_action :ensure_user_paid!

  before_action :set_user

  def new
    if current_user.present? && !current_user.guest?
      redirect_to dashboard_path
    end
    @user = User.new
  end

  def create
    if current_user
      @user = current_user
      @user.assign_attributes(user_params)
      @user.guest = false
    else
      @user = User.new(user_params)
    end

    @user.password_confirmation = <PASSWORD>_<PASSWORD>[:password]
    @user.timezone = Time.zone.tzinfo.name

    valid = @user.save && create_customer_and_subscription

    if valid
      login(@user)
      redirect_to welcome_path
    else
      render :new
    end
  end

  def edit
    if @user.stripe_customer
      @existing_card = current_user.stripe_customer.sources.first
    end
  end

  def update
    @user.assign_attributes(user_params)

    valid = @user.save && add_card_to_user

    if valid
      flash[:success] = 'Settings updated'
      redirect_to dashboard_path
    else
      flash[:error] = @user.errors.full_messages.join(', ')
      redirect_to settings_path
    end
  end

  def destroy
    @payment_service = PaymentService.new(user: @user)

    if @payment_service.cancel_subscription || @user.destroy
      flash[:success] = 'Account deleted. Sorry to see you go!'
    else
      flash[:error] = 'Something went wrong.'
    end

    redirect_to new_user_path
  end

  def cancel_subscription
    @payment_service = PaymentService.new(user: @user)

    if @payment_service.cancel_subscription
      flash[:success] = 'Subscription cancelled.'
    else
      flash[:error] = 'Something went wrong.'
    end

    redirect_to settings_path
  end

  private

  def create_customer_and_subscription
    @payment_service = PaymentService.new(user: @user)

    @payment_service.create_customer &&
      @payment_service.create_subscription
  end

  def add_card_to_user
    @payment_service = PaymentService.new(user: @user)

    if params[:stripeToken]
      @payment_service.add_card_to_customer(token: params[:stripeToken])
    else
      true
    end
  end

  def set_user
    @user = current_user
  end

  def user_params
    params.require(:user).permit(
      :name,
      :email,
      :phone,
      :email_reminders,
      :sms_reminders,
      :track_weekends,
      :password
    )
  end
end
{'fraction_non_alphanumeric': 0.08153815180189249, 'fraction_numerical': 0.0, 'mean_word_length': 3.025931928687196, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5610390', 'n_tokens_mistral': 1594, 'n_tokens_neox': 1544, 'n_words': 333}