Class: CollavreNotion::Creatives::NotionIntegrationsController

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

Instance Method Summary collapse

Instance Method Details

#destroyObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/controllers/collavre_notion/creatives/notion_integrations_controller.rb', line 82

def destroy
  unless @creative.has_permission?(Current.user, :write)
    render json: { error: integration_forbidden_message }, status: :forbidden
    return
  end

   = Current.user.
  unless 
    render json: { error: "not_connected" }, status: :unprocessable_entity
    return
  end

  page_id = params[:page_id]

  if page_id
    # Remove specific page link
    link = linked_page_links().find_by(page_id: page_id)
    unless link
      render json: { error: "not_found" }, status: :not_found
      return
    end

    link.destroy!
  else
    # Remove all page links for this creative
    linked_page_links().destroy_all
  end

  links = linked_page_links()
  render json: {
    success: true,
    linked_pages: links.map do |link|
      {
        page_id: link.page_id,
        page_title: link.page_title,
        page_url: link.page_url,
        last_synced_at: link.last_synced_at
      }
    end
  }
end

#showObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/collavre_notion/creatives/notion_integrations_controller.rb', line 11

def show
   = Current.user.
  links = linked_page_links()

  Rails.logger.info("Notion Integration: Showing status for user #{Current.user.id}, connected: #{.present?}")

  render json: {
    connected: .present?,
    creative_title: helpers.strip_tags(@creative.effective_origin.description.to_s).strip.presence || "Untitled Creative",
    account:  && {
      workspace_name: .workspace_name,
      workspace_id: .workspace_id,
      bot_id: .bot_id
    },
    linked_pages: links.map do |link|
      {
        page_id: link.page_id,
        page_title: link.page_title,
        page_url: link.page_url,
        last_synced_at: link.last_synced_at
      }
    end,
    available_pages: .present? ? fetch_available_pages() : []
  }
end

#updateObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/collavre_notion/creatives/notion_integrations_controller.rb', line 37

def update
   = Current.user.
  unless 
    render json: { error: "not_connected" }, status: :unprocessable_entity
    return
  end

  Rails.logger.info("Notion Integration Update: Full params = #{params.to_unsafe_h}")

  integration_attributes = integration_params
  Rails.logger.info("Notion Integration Update: integration_params = #{integration_attributes}")

  request_action = request.request_parameters[:action]
  action = integration_attributes[:action]
  if action.blank? || action.to_s == action_name
    action = request_action.presence
  end
  parent_page_id = integration_attributes[:parent_page_id]

  Rails.logger.info("Notion Integration Update: action=#{action}, parent_page_id=#{parent_page_id}")

  begin
    case action
    when "export"
      # Export creative to Notion
      CollavreNotion::NotionExportJob.perform_later(@creative, , parent_page_id)
      render json: { success: true, message: "Export started" }
    when "sync"
      # Sync existing page
      link = linked_page_links().first
      if link
        CollavreNotion::NotionSyncJob.perform_later(@creative, , link.page_id)
        render json: { success: true, message: "Sync started" }
      else
        render json: { error: "no_linked_page" }, status: :unprocessable_entity
      end
    else
      render json: { error: "invalid_action" }, status: :unprocessable_entity
    end
  rescue StandardError => e
    Rails.logger.error("Notion integration error: #{e.message}")
    render json: { error: "operation_failed", message: e.message }, status: :internal_server_error
  end
end