Sha256: a07579aff6eb7bb3f916160a4c14499ce64d8259ce94080f2f15ed4dfa60824d

Contents?: true

Size: 1.7 KB

Versions: 9

Compression:

Stored size: 1.7 KB

Contents

class Eco::API::UseCases::Default::People::Amend::CleanUnknownTags < Eco::API::Common::Loaders::UseCase
  name "clean-unknown-tags"
  type :transform

  REGISTER_TAGS = %w[
    EVENT INJURY RISK CONTRACTOR PERMIT
    AUDIT JSEA
    TRAINING INDUCTION
    MEETING PPE CHEMICAL
    PLANT ASSET
    POLICY IDEA REPORTS
  ].freeze

  def main(people, session, options, usecase)
    update = session.new_job("main", "update", :update, usecase)
    people.each do |person|
      unknown_tags = person.filter_tags.reject { |tag| tag?(tag) }
      person.filter_tags -= unknown_tags
      removed_tag!(*unknown_tags)
      if (tag = default_tag(person)) && !tag?(tag)
        removed_tag!(tag)
        micro.refresh_default_tag(person.entry, person, options)
      end
      update.add(person)
    end

    log(:info) {
      msg  = "Here list of the #{removed_tags.count} removed locations..."
      msg << "\n  • "
      msg << removed_tags.sort.join("\n  • ")
      msg
    }
  end

  private

  def default_tag(person)
    return unless (account = person.account)
    account.default_tag
  end

  def register_tags
    @register_tags ||= self.class::REGISTER_TAGS.compact.map(&:upcase)
  end

  def removed_tag!(*tags)
    @removed_tags = removed_tags | tags
  end

  def removed_tags
    @removed_tags ||= []
  end

  def tag?(value)
    return false if value.nil?
    return true  if tagtree.tag?(value)
    return false if clean_register_tags?
    register_tags.any? { |reg| value.upcase == reg }
  end

  def tagtree
    @tagtree ||= session.tagtree(live: true, merge: true)
  end

  def clean_register_tags?
    options.dig(:usecase, :clear_register_tags)
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
eco-helpers-2.7.20 lib/eco/api/usecases/default/people/amend/clean_unknown_tags_case.rb
eco-helpers-2.7.19 lib/eco/api/usecases/default/people/amend/clean_unknown_tags_case.rb
eco-helpers-2.7.18 lib/eco/api/usecases/default/people/amend/clean_unknown_tags_case.rb
eco-helpers-2.7.17 lib/eco/api/usecases/default/people/amend/clean_unknown_tags_case.rb
eco-helpers-2.7.16 lib/eco/api/usecases/default/people/amend/clean_unknown_tags_case.rb
eco-helpers-2.7.15 lib/eco/api/usecases/default/people/amend/clean_unknown_tags_case.rb
eco-helpers-2.7.14 lib/eco/api/usecases/default/people/amend/clean_unknown_tags_case.rb
eco-helpers-2.7.13 lib/eco/api/usecases/default/people/amend/clean_unknown_tags_case.rb
eco-helpers-2.7.12 lib/eco/api/usecases/default/people/amend/clean_unknown_tags_case.rb