educoder/app/services/users/apply_professional_auth_ser...

64 lines
1.8 KiB

class Users::ApplyProfessionalAuthService < ApplicationService
Error = Class.new(StandardError)
attr_reader :user, :params
def initialize(user, params)
@user = user
@params = params
end
def call
raise Error, '请先完善基本信息' unless user.profile_completed?
Users::ApplyProfessionalAuthForm.new(params).validate!
raise Error, '您已经申请过职业认证了' if ApplyUserAuthentication.professional_auth.processing.exists?(user_id: user.id)
user.professional_certification = false
extension = user.user_extension
extension.school_id = params[:school_id]
extension.department_id = params[:department_id]
extension.identity = params[:identity]
extra = params[:extra].to_s.strip.presence
if extension.identity.to_s == 'student'
extension.technical_title = nil
extension.student_id = extra
else
extension.technical_title = extra
extension.student_id = nil
end
ActiveRecord::Base.transaction do
user.save!
extension.save!
user.apply_user_authentication.create!(auth_type: 2, status: 0)
move_image_file! unless params[:upload_image].to_s == 'false'
sms_notify_admin
end
end
private
def move_image_file!
image_url = ApplicationController.helpers.disk_professional_auth_filename(user.id)
temp_image_url = image_url + 'temp'
FileUtils.mv(temp_image_url, image_url, force: true) if File.exist?(temp_image_url)
rescue RuntimeError => ex
Util.logger_error(ex)
raise Error, '申请失败'
ensure
File.delete(temp_image_url) if File.exist?(temp_image_url)
end
def sms_notify_admin
Educoder::Sms.notify_admin(send_type: 'apply_pro_certification')
rescue => ex
Util.logger_error(ex)
end
end