Skip to content

Commit

Permalink
Add data quality checker
Browse files Browse the repository at this point in the history
  • Loading branch information
hunchr committed Aug 20, 2024
1 parent 4aa9901 commit 9ccc668
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
52 changes: 52 additions & 0 deletions app/domain/people/data_quality_checker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

# Copyright (c) 2024, Schweizer Alpen-Club. This file is part of
# hitobito_sac_cas and licensed under the Affero General Public License version 3
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_sac_cas

class People::DataQualityChecker
ATTRIBUTES_TO_CHECK = %w[email street zip_code town first_name last_name
company_name phone_numbers birthday]

def initialize(person)
@person = person

check_invoice_recipient if @person.sac_membership_invoice?
check_stammsektion if @person.roles.exists?(type: SacCas::STAMMSEKTION_ROLES)

if @person.company?
check("warning", "company_name", "ist leer", invalid: @person.company_name.blank?)
else
check("error", "first_name", "ist leer", invalid: @person.first_name.blank?)
check("error", "last_name", "ist leer", invalid: @person.last_name.blank?)
end
end

private

def check_invoice_recipient
check("error", "street", "ist leer", invalid: @person.street.blank?)
check("error", "zip_code", "ist leer", invalid: @person.zip_code.blank?)
check("error", "town", "ist leer", invalid: @person.town.blank?)
check("warning", "email", "ist leer", invalid: @person.email.blank?)
check("warning", "phone_numbers", "sind leer", invalid: @person.phone_numbers.blank?)
end

def check_stammsektion
join_date = @person.roles.find_by(type: SacCas::STAMMSEKTION_ROLES).created_at

check("error", "birthday", "ist leer", invalid: @person.birthday.blank?)
check("warning", "birthday", "liegt weniger als 6 Jahre vor dem SAC-Eintritt",
invalid: @person.birthday.present? && (@person.birthday > join_date - 6.years))
end

def check(severity, attr, key, invalid: false)
if invalid
issue = @person.data_quality_issues.new(severity: severity, attr: attr, key: key)
issue.save! if issue.valid?
else
@person.data_quality_issues.find_by(severity: severity, attr: attr, key: key)&.destroy!
end
end
end
4 changes: 3 additions & 1 deletion app/models/person/data_quality_issue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
# index_person_data_quality_issues_on_person_and_attribute_and_key (key) UNIQUE

class Person::DataQualityIssue < ApplicationRecord
VALID_ATTRIBUTES = Person.column_names + ["phone_numbers"]

belongs_to :person

enum severity: {info: 1, warning: 2, error: 3}
Expand All @@ -43,6 +45,6 @@ def message
private

def person_attribute_exists
errors.add(:attr, :invalid) unless Person.column_names.include?(attr)
errors.add(:attr, :invalid) unless VALID_ATTRIBUTES.include?(attr)
end
end
9 changes: 9 additions & 0 deletions app/models/sac_cas/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module SacCas::Person
validates(*Person::SAC_REMARKS, format: {with: /\A[^\n\r]*\z/})

before_save :set_digital_correspondence, if: :password_initialized?
after_save :check_data_quality

delegate :salutation_label, to: :class

Expand Down Expand Up @@ -105,4 +106,12 @@ def data_quality
def data_quality=(value)
super(value.is_a?(Integer) ? value : DATA_QUALITIES.index(value.to_s))
end

private

def check_data_quality
return if (People::DataQualityChecker::ATTRIBUTES_TO_CHECK & saved_changes.keys).empty?

People::DataQualityChecker.new(self)
end
end

0 comments on commit 9ccc668

Please sign in to comment.