class Traces::BaseController < ApplicationController

  helper_method :observed_logged_user?, :observed_user
  before_action :require_login
  before_action :check_trace_system

  def observed_user
    @_observed_user ||= (User.find_by_login(params[:user_id]) || User.find_by_id(params[:user_id]))
  end

  def observed_logged_user?
    observed_user.id == User.current&.id
  end

  protected
  def check_auth 
    return render_forbidden unless current_user.admin? || observed_logged_user?
  end

  def check_trace_system
    code, data, error = Trace::SystemInfoService.call(current_user.trace_token)
    return render_ok({code: 501, data: {operate_time: data['operate_time']}, message: '系统维护中'}) if data['status'] === 0
  rescue 
    # 这里根据需求跳转到404
    return render_not_found
  end
end