Class: CollavreSlack::Creatives::SlackIntegrationsController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- ApplicationController
- CollavreSlack::Creatives::SlackIntegrationsController
- Includes:
- Collavre::IntegrationPermission, Collavre::IntegrationSetup
- Defined in:
- app/controllers/collavre_slack/creatives/slack_integrations_controller.rb
Instance Method Summary collapse
Instance Method Details
#badge ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'app/controllers/collavre_slack/creatives/slack_integrations_controller.rb', line 62 def badge links = SlackChannelLink.where(creative: @origin) render json: { links: links.map { |link| { channel_name: link.channel_name } } } end |
#create ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/controllers/collavre_slack/creatives/slack_integrations_controller.rb', line 34 def create unless @creative.(Current.user, :feedback) render json: { success: false, error: }, status: :forbidden return end # Find the user's Slack account slack_account = if params[:slack_account_id].present? SlackAccount.find_by!(id: params[:slack_account_id], user: Current.user) else SlackAccount.find_by(user: Current.user) end unless slack_account render json: { success: false, error: I18n.t("collavre_slack.errors.no_slack_account") }, status: :unprocessable_entity return end service = SlackIntegrationService.new(user: Current.user, slack_account: slack_account) link = service.link_channel(creative: @origin, channel_id: params[:channel_id], channel_name: params[:channel_name]) if link.persisted? render json: { success: true, link: { id: link.id, channel_id: link.channel_id, channel_name: link.channel_name } }, status: :created else render json: { success: false, error: link.errors..join(", ") }, status: :unprocessable_entity end end |
#destroy ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'app/controllers/collavre_slack/creatives/slack_integrations_controller.rb', line 71 def destroy link = SlackChannelLink.find(params[:id]) unless link.creative_id == @origin.id render json: { success: false, error: I18n.t("collavre_slack.errors.not_found") }, status: :not_found return end unless @creative.(Current.user, :feedback) render json: { success: false, error: }, status: :forbidden return end SlackIntegrationService.new(user: Current.user, slack_account: link.slack_account).unlink_channel(link) render json: { success: true } end |
#index ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'app/controllers/collavre_slack/creatives/slack_integrations_controller.rb', line 10 def index @links = SlackChannelLink.where(creative: @origin) respond_to do |format| format.json do slack_account = Current.user ? SlackAccount.find_by(user: Current.user) : nil channels = slack_account ? fetch_channels(slack_account) : [] render json: { connected: slack_account.present?, channels: channels, links: @links.map { |link| { id: link.id, channel_id: link.channel_id, channel_name: link.channel_name, last_synced_at: link.last_synced_at } } } end format.html end end |