You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/app/services/users/bind_email_service.rb

28 lines
752 B

6 years ago
class Users::BindEmailService < ApplicationService
Error = Class.new(StandardError)
attr_reader :user, :params
def initialize(user, params)
@user = user
@params = params
end
def call
Users::BindEmailForm.new(params).validate!
raise Error, '该邮箱已被绑定' if User.where.not(id: user.id).exists?(mail: params[:email])
code = VerificationCode.where(mail: params[:email], code: params[:code], code_type: 4).last
raise Error, '验证码无效' unless code&.effective?
ActiveRecord::Base.transaction do
if user.mail.blank?
RewardGradeService.call(user, container_id: user.id, container_type: 'Mail', score: 500)
end
user.mail = params[:email]
user.save!
end
end
end