Class: Collavre::Tools::CreativeUpdateService

Inherits:
Object
  • Object
show all
Extended by:
T::Sig, ToolMeta
Includes:
DescriptionNormalizable
Defined in:
app/services/collavre/tools/creative_update_service.rb

Instance Method Summary collapse

Instance Method Details

#call(id:, description: nil, progress: nil, parent_id: nil) ⇒ Object



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
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
81
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
# File 'app/services/collavre/tools/creative_update_service.rb', line 19

def call(id:, description: nil, progress: nil, parent_id: nil)
  raise "Current.user is required" unless Current.user

  creative = Creative.find_by(id: id)
  unless creative
    return { error: "Creative not found", id: id }
  end

  unless creative.has_permission?(Current.user, :write)
    return { error: "No write permission on this Creative", id: id }
  end

  # Get the effective origin for updating content
  base = creative.effective_origin

  updates = {}
  parent_updates = {}

  # Handle description update
  if description.present?
    updates[:description] = normalize_description(description)
  end

  # Handle progress update
  if progress.present?
    progress_value = progress.to_f.clamp(0.0, 1.0)

    # Only 100% (1.0) progress updates are allowed via tools
    unless progress_value == 1.0
      return { error: "Only progress of 1.0 (100%) is allowed. Partial progress updates are not supported.", id: id }
    end

    # Only leaf creatives (no children) can have progress updated directly
    if base.children.exists?
      return { error: "Cannot update progress on a parent Creative. Only leaf Creatives (with no children) can be marked complete.", id: id }
    end

    updates[:progress] = progress_value
  end

  # Handle parent change (on the creative itself, not base)
  # Skip if parent_id is nil or 0 — these mean "don't change parent"
  if parent_id.present? && parent_id != 0
    new_parent_id = parent_id

    # Prevent self-reference
    if new_parent_id == creative.id
      return { error: "Cannot set a Creative as its own parent", id: id }
    end

    new_parent = Creative.find_by(id: new_parent_id)
    unless new_parent
      return { error: "New parent Creative not found", parent_id: new_parent_id }
    end
    unless new_parent.has_permission?(Current.user, :write)
      return { error: "No write permission on new parent Creative", parent_id: new_parent_id }
    end
    # Prevent circular reference
    if new_parent.self_and_ancestors.include?(creative)
      return { error: "Cannot move Creative under its own descendant", parent_id: new_parent_id }
    end

    parent_updates[:parent_id] = new_parent_id
  end

  # Apply updates
  success = true

  # Update parent on the creative (for linked creatives, parent is on the link, not origin)
  if parent_updates.present?
    success &&= creative.update(parent_updates)
  end

  # Update content on the base/origin
  if updates.present?
    success &&= base.update(updates)

    # Note: progress updates are only allowed on leaf Creatives (validated above).
    # Parent progress is automatically calculated from children.
  end

  if success
    creative.reload
    base.reload
    {
      success: true,
      id: creative.id,
      description: base.description,
      parent_id: creative.parent_id,
      progress: base.progress
    }
  else
    { error: "Failed to update Creative", details: creative.errors.full_messages + base.errors.full_messages }
  end
end