class RewardGradeService < ApplicationService
  attr_reader :user, :attrs

  def initialize(user, **attrs)
    @user  = user
    @attrs = attrs.slice(*%i[container_id container_type score])
  end

  def call
    return if user.grades.exists?(attrs)

    ActiveRecord::Base.transaction do
      grade = user.grades.create!(attrs)

      user.increment!(:grade, grade.score)
    end
  end
end