2021-06-03 20:00:43 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Users
|
|
|
|
module Settings
|
|
|
|
class WebhooksController < ApplicationController
|
|
|
|
layout 'fluid'
|
|
|
|
|
|
|
|
before_action :can_manage_filters
|
2021-06-09 18:53:11 +08:00
|
|
|
before_action :load_filter, except: :index
|
|
|
|
before_action :load_webhook, only: %i(update destroy)
|
|
|
|
before_action :set_sort, except: :filter_info
|
2021-06-03 20:00:43 +08:00
|
|
|
|
|
|
|
def index
|
2021-06-09 18:53:11 +08:00
|
|
|
@activity_filters = ActivityFilter.includes(:webhooks).order(name: (@current_sort == 'atoz' ? :asc : :desc))
|
2021-06-03 20:00:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_filter
|
|
|
|
@filter.destroy
|
|
|
|
redirect_to users_settings_webhooks_path(sort: @current_sort)
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_info
|
|
|
|
render json: { filter_elements: load_filter_elements(@filter) }
|
|
|
|
end
|
|
|
|
|
2021-06-09 18:53:11 +08:00
|
|
|
def create
|
|
|
|
@webhook = @filter.webhooks.create(webhook_params)
|
|
|
|
if @webhook.errors.any?
|
|
|
|
render json: { errors: @webhook.errors.messages }, status: :unprocessable_entity
|
|
|
|
else
|
|
|
|
flash[:success] = t('webhooks.index.webhook_created')
|
|
|
|
redirect_to users_settings_webhooks_path(sort: @current_sort)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@webhook.update(webhook_params)
|
|
|
|
if @webhook.errors.any?
|
|
|
|
render json: { errors: @webhook.errors.messages }, status: :unprocessable_entity
|
|
|
|
else
|
|
|
|
flash[:success] = t('webhooks.index.webhook_updated')
|
|
|
|
redirect_to users_settings_webhooks_path(sort: @current_sort)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@webhook.destroy
|
|
|
|
flash[:success] = t('webhooks.index.webhook_deleted')
|
|
|
|
redirect_to users_settings_webhooks_path(sort: @current_sort)
|
|
|
|
end
|
|
|
|
|
2021-06-03 20:00:43 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def can_manage_filters
|
|
|
|
render_403 && return unless can_create_acitivity_filters?
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_sort
|
|
|
|
@current_sort = params[:sort] || 'atoz'
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_filter
|
|
|
|
@filter = ActivityFilter.find_by(id: params[:filter_id])
|
|
|
|
|
|
|
|
render_404 && return unless @filter
|
|
|
|
end
|
|
|
|
|
2021-06-09 18:53:11 +08:00
|
|
|
def load_webhook
|
|
|
|
@webhook = Webhook.find_by(id: params[:id])
|
|
|
|
|
|
|
|
render_404 && return unless @webhook
|
|
|
|
end
|
|
|
|
|
|
|
|
def webhook_params
|
2021-11-03 22:04:13 +08:00
|
|
|
params.require(:webhook).permit(:http_method, :url, :active, :secret_key)
|
2021-06-09 18:53:11 +08:00
|
|
|
end
|
|
|
|
|
2021-06-03 20:00:43 +08:00
|
|
|
def load_filter_elements(filter)
|
|
|
|
result = []
|
|
|
|
filters = filter.filter
|
|
|
|
result += Team.where(id: filters['teams']).pluck(:name) if filters['teams']
|
|
|
|
result += User.where(id: filters['users']).pluck(:full_name) if filters['users']
|
|
|
|
|
|
|
|
if filters['types']
|
|
|
|
result += Activity.type_ofs.select { |_k, v| filters['types'].include?(v.to_s) }
|
|
|
|
.map { |k, _v| I18n.t("global_activities.activity_name.#{k}") }
|
|
|
|
end
|
|
|
|
|
|
|
|
if filters['to_date'] || filters['from_date']
|
|
|
|
result.push("#{t('global_activities.index.period_label')} #{filters['from_date']} - #{filters['to_date']}")
|
|
|
|
end
|
|
|
|
|
|
|
|
filters['subjects']&.each do |subject, ids|
|
|
|
|
result += subject.constantize.where(id: ids).pluck(:name)
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|