|
|
|
class Users::UpdateAccountService < ApplicationService
|
|
|
|
|
|
|
|
attr_reader :user, :params
|
|
|
|
|
|
|
|
def initialize(user, params)
|
|
|
|
@user = user
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def call
|
|
|
|
form = Users::UpdateAccountForm.new(params.merge(user: user))
|
|
|
|
form.validate!
|
|
|
|
|
|
|
|
first_full_reward = false
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
first_full_reward = user.nickname.blank?
|
|
|
|
|
|
|
|
extension = user.user_extension || user.build_user_extension
|
|
|
|
|
|
|
|
user.assign_attributes(user_attributes)
|
|
|
|
extension.assign_attributes(user_extension_attributes)
|
|
|
|
|
|
|
|
# 未认证下才能修改姓名
|
|
|
|
if !user.authentication? && user.process_real_name_apply.blank?
|
|
|
|
user.lastname = params[:name]
|
|
|
|
user.firstname = ''
|
|
|
|
extension.gender = params[:gender]
|
|
|
|
end
|
|
|
|
|
|
|
|
if extension.student?
|
|
|
|
extension.student_id = params[:student_id]
|
|
|
|
extension.technical_title = nil
|
|
|
|
else
|
|
|
|
extension.student_id = nil
|
|
|
|
extension.technical_title = params[:technical_title]
|
|
|
|
end
|
|
|
|
|
|
|
|
# 职业、学校变动需要重新进行职业认证
|
|
|
|
if extension.identity_changed? || extension.school_id_changed?
|
|
|
|
user.professional_certification = false
|
|
|
|
# 撤销之前的职业认证
|
|
|
|
user.apply_user_authentication.professional_auth.passed.update_all(status: 3)
|
|
|
|
end
|
|
|
|
|
|
|
|
# 表示资料完整
|
|
|
|
user.profile_completed = true
|
|
|
|
|
|
|
|
extension.save!
|
|
|
|
user.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
if first_full_reward
|
|
|
|
RewardGradeService.call(user, container_id: user.id, container_type: 'Account', score: 500)
|
|
|
|
end
|
|
|
|
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def user_attributes
|
|
|
|
params.slice(*%i[nickname show_realname])
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_extension_attributes
|
|
|
|
params.slice(*%i[location location_city identity student_id technical_title school_id department_id])
|
|
|
|
end
|
|
|
|
end
|