class NoticesController < ApplicationController

  def create
    return tip_exception("参数有误") if params["source"].blank?
    user_id = params[:user_id]

    if params["source"] == "CompetitionBegin"
      competition_id = params[:competition_id]
      SendTemplateMessageJob.perform_later('CompetitionBegin', user_id, competition_id)
    elsif params["source"] == "CompetitionResult"
      competition_id = params[:competition_id]
      SendTemplateMessageJob.perform_later('CompetitionResult', user_id, competition_id)
    elsif params["source"] == "CompetitionReview"
      competition_id = params[:competition_id]
      SendTemplateMessageJob.perform_later('CompetitionReview', user_id, competition_id)
    elsif params["source"] == "CustomTip"
      users_id = params[:users_id]
      props = params[:props].to_unsafe_hash
      return tip_exception("参数有误") unless props.is_a?(Hash) && users_id.is_a?(Array)
      template_id = params[:template_id]
      SendTemplateMessageJob.perform_later('CustomTip', users_id, template_id, props)
    else
      tip_exception("#{params["source"]}未配置")
    end
    render_ok
  end


  private 
  def params_props
    params.require(:notice).permit(:props)
  end
end
