Class: CollavreNotion::NotionSyncJob

Inherits:
ApplicationJob
  • Object
show all
Defined in:
app/jobs/collavre_notion/notion_sync_job.rb

Instance Method Summary collapse

Instance Method Details

#perform(creative, notion_account, page_id) ⇒ Object



5
6
7
8
9
10
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
36
37
38
39
40
41
42
43
44
45
# File 'app/jobs/collavre_notion/notion_sync_job.rb', line 5

def perform(creative, , page_id)
  service = CollavreNotion::NotionService.new(user: .user)

  begin
    # Find the existing link
    link = CollavreNotion::NotionPageLink.find_by(
      creative: creative,
      notion_account: ,
      page_id: page_id
    )

    unless link
      Rails.logger.error("No Notion page link found for creative #{creative.id} and page #{page_id}")
      return
    end

    # Update the existing Notion page with children as blocks
    title = ActionController::Base.helpers.strip_tags(creative.description).strip.presence || "Untitled Creative"
    children = creative.children.to_a
    Rails.logger.info("NotionSyncJob: Syncing creative #{creative.id} as page title with #{children.count} children as blocks")
    blocks = children.any? ? NotionCreativeExporter.new(creative).export_tree_blocks(children, 1, 0) : []

    properties = {
      title: {
        title: [ { text: { content: title } } ]
      }
    }

    service.update_page(page_id, properties: properties, blocks: blocks)
    link.update!(page_title: title)
    link.mark_synced!

    Rails.logger.info("Successfully synced creative #{creative.id} to Notion page #{page_id}")
  rescue NotionError => e
    Rails.logger.error("Notion sync failed for creative #{creative.id}: #{e.message}")
    raise e
  rescue StandardError => e
    Rails.logger.error("Unexpected error during Notion sync for creative #{creative.id}: #{e.message}")
    raise e
  end
end