summaryrefslogtreecommitdiff
path: root/app/models/onboarding_progress.rb
blob: 419bbd595e96a43828e07b696ffad0deeda447ff (plain)
1
2
3
4
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# frozen_string_literal: true

class OnboardingProgress < ApplicationRecord
  belongs_to :namespace, optional: false

  validate :namespace_is_root_namespace

  ACTIONS = [
    :git_pull,
    :git_write,
    :merge_request_created,
    :pipeline_created,
    :user_added,
    :trial_started,
    :subscription_created,
    :required_mr_approvals_enabled,
    :code_owners_enabled,
    :scoped_label_created,
    :security_scan_enabled,
    :issue_auto_closed,
    :repository_imported,
    :repository_mirrored
  ].freeze

  class << self
    def onboard(namespace)
      return unless root_namespace?(namespace)

      safe_find_or_create_by(namespace: namespace)
    end

    def register(namespace, action)
      return unless root_namespace?(namespace) && ACTIONS.include?(action)

      action_column = column_name(action)
      onboarding_progress = find_by(namespace: namespace, action_column => nil)
      onboarding_progress&.update!(action_column => Time.current)
    end

    def completed?(namespace, action)
      return unless root_namespace?(namespace) && ACTIONS.include?(action)

      action_column = column_name(action)
      where(namespace: namespace).where.not(action_column => nil).exists?
    end

    private

    def column_name(action)
      :"#{action}_at"
    end

    def root_namespace?(namespace)
      namespace && namespace.root?
    end
  end

  private

  def namespace_is_root_namespace
    return unless namespace

    errors.add(:namespace, _('must be a root namespace')) if namespace.has_parent?
  end
end