Class: CollavreSlack::Creatives::SlackIntegrationsController

Inherits:
ApplicationController
  • Object
show all
Includes:
Collavre::IntegrationPermission, Collavre::IntegrationSetup
Defined in:
app/controllers/collavre_slack/creatives/slack_integrations_controller.rb

Instance Method Summary collapse

Instance Method Details

#badgeObject



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

#createObject



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.has_permission?(Current.user, :feedback)
    render json: { success: false, error: integration_forbidden_message }, status: :forbidden
    return
  end

  # Find the user's 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 
    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: )
  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.full_messages.join(", ") }, status: :unprocessable_entity
  end
end

#destroyObject



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.has_permission?(Current.user, :feedback)
    render json: { success: false, error: integration_forbidden_message }, status: :forbidden
    return
  end

  SlackIntegrationService.new(user: Current.user, slack_account: link.).unlink_channel(link)
  render json: { success: true }
end

#indexObject



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
       = Current.user ? SlackAccount.find_by(user: Current.user) : nil
      channels =  ? fetch_channels() : []

      render json: {
        connected: .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