Skip to content

Commit

Permalink
Add explicit #start_on/#end_on to Role, remove FutureRole, Role#delet…
Browse files Browse the repository at this point in the history
…e_on
  • Loading branch information
daniel-illi authored and amaierhofer committed Nov 15, 2024
1 parent 44c85b4 commit eadf84d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def up
FROM (
SELECT DISTINCT person_id
FROM roles
WHERE deleted_at IS NULL
OR deleted_at > NOW()
WHERE end_on IS NULL
OR end_on > NOW()
) active_roles
WHERE people.id = active_roles.person_id
OR manual_member_number >= 300000
Expand Down
20 changes: 10 additions & 10 deletions lib/tasks/import.rake
Original file line number Diff line number Diff line change
Expand Up @@ -108,35 +108,35 @@ namespace :import do
person_id: person.id,
group_id: group.id,
type: Group::Mitglieder::Aktivmitglied.sti_name,
created_at: import_row[:memberentrydate],
deleted_at: import_row[:memberexitdate],
start_on: import_row[:memberentrydate],
end_on: import_row[:memberexitdate],
updated_at: Time.zone.now
}

magazin_abo_attrs = {
person_id: person.id,
group_id: group.id,
type: Group::Mitglieder::MagazinAbonnent.sti_name,
created_at: import_row[:abo1start],
deleted_at: import_row[:abo1end],
start_on: import_row[:abo1start],
end_on: import_row[:abo1end],
updated_at: Time.zone.now
}

[mitglied_attrs, magazin_abo_attrs].each do |attrs|
attrs[:created_at] = DateTime.parse(attrs[:created_at]) if attrs[:created_at]
attrs[:deleted_at] = DateTime.parse(attrs[:deleted_at]) if attrs[:deleted_at]
attrs[:start_on] = DateTime.parse(attrs[:start_on]) if attrs[:start_on]
attrs[:end_on] = DateTime.parse(attrs[:end_on]) if attrs[:end_on]

if attrs[:deleted_at].present? && attrs[:created_at].nil?
attrs[:created_at] = attrs[:deleted_at].yesterday
if attrs[:end_on].present? && attrs[:start_on].nil?
attrs[:start_on] = attrs[:end_on].yesterday
end

next unless attrs[:created_at]
next unless attrs[:start_on]

# Because of a known issue of the acts_as_paranoid gem,
# you can not directly create a model in a deleted state.
# Thus we have to update it afterwards.
Role.insert(attrs)
Role.with_deleted.last.update(attrs)
Role.with_inactive.last.update(attrs)
end

tagging_attrs = {taggable_id: person.id, taggable_type: Person.sti_name, context: "tags"}
Expand Down
29 changes: 24 additions & 5 deletions spec/support/graphiti/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2321,8 +2321,14 @@
"writable": false,
"description": null
},
"deleted_at": {
"type": "datetime",
"start_on": {
"type": "date",
"readable": true,
"writable": false,
"description": null
},
"end_on": {
"type": "date",
"readable": true,
"writable": false,
"description": null
Expand Down Expand Up @@ -2361,7 +2367,9 @@
},
"updated_at": {
},
"deleted_at": {
"start_on": {
},
"end_on": {
},
"person_id": {
},
Expand Down Expand Up @@ -2406,8 +2414,19 @@
"lte"
]
},
"deleted_at": {
"type": "datetime",
"start_on": {
"type": "date",
"operators": [
"eq",
"not_eq",
"gt",
"gte",
"lt",
"lte"
]
},
"end_on": {
"type": "date",
"operators": [
"eq",
"not_eq",
Expand Down
40 changes: 20 additions & 20 deletions spec/tasks/import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,18 @@
expect(person.magazin_abo_number).to eq(1000)
expect(person.name_add_on).to eq('Mustermann')
expect(person.email).to eq('[email protected]')
expect(person.roles.with_deleted.count).to eq(2)
expect(person.roles.with_inactive.count).to eq(2)

mitglied = person.roles.first
magazin_abo = person.roles.with_deleted.last
magazin_abo = person.roles.with_inactive.last

expect(mitglied.type).to eq(Group::Mitglieder::Aktivmitglied.sti_name)
expect(mitglied.created_at).to eq(DateTime.new(1977, 1, 1))
expect(mitglied.deleted_at).to be_nil
expect(mitglied.start_on).to eq(DateTime.new(1977, 1, 1))
expect(mitglied.end_on).to be_nil

expect(magazin_abo.type).to eq(Group::Mitglieder::MagazinAbonnent.sti_name)
expect(magazin_abo.created_at).to eq(DateTime.new(1990, 10, 12))
expect(magazin_abo.deleted_at).to eq(DateTime.new(2006, 2, 12))
expect(magazin_abo.start_on).to eq(DateTime.new(1990, 10, 12))
expect(magazin_abo.end_on).to eq(DateTime.new(2006, 2, 12))

expect(person.taggings.count).to eq(3)

Expand Down Expand Up @@ -107,30 +107,30 @@
expect(person.additional_emails.first.email).to eq('[email protected]')
end

it "sets role created_at on a day before deleted_at if not set" do
it "sets role start_on on a day before end_on if not set" do
expect do
Rake::Task["import:people_fo"].invoke(groups(:berner_wanderwege).id)
end.to output("Successfully imported 5/5 rows\n").to_stdout

person = Person.find_by(alabus_id: 'dcwe1-vbsdw2-2cib1kbs-p-g2bnbw1h-2sd')
expect(person).to be_present

expect(person.roles.without_deleted.count).to eq(0)
expect(person.roles.with_deleted.count).to eq(2)
expect(person.roles.active.count).to eq(0)
expect(person.roles.with_inactive.count).to eq(2)

mitglied = person.roles.with_deleted.first
magazin_abo = person.roles.with_deleted.last
mitglied = person.roles.with_inactive.first
magazin_abo = person.roles.with_inactive.last

expect(mitglied.type).to eq(Group::Mitglieder::Aktivmitglied.sti_name)
expect(mitglied.created_at).to eq(DateTime.new(2002, 11, 29))
expect(mitglied.deleted_at).to eq(DateTime.new(2002, 11, 30))
expect(mitglied.start_on).to eq(DateTime.new(2002, 11, 29))
expect(mitglied.end_on).to eq(DateTime.new(2002, 11, 30))

expect(magazin_abo.type).to eq(Group::Mitglieder::MagazinAbonnent.sti_name)
expect(magazin_abo.created_at).to eq(DateTime.new(1998, 12, 31))
expect(magazin_abo.deleted_at).to eq(DateTime.new(1999, 1, 1))
expect(magazin_abo.start_on).to eq(DateTime.new(1998, 12, 31))
expect(magazin_abo.end_on).to eq(DateTime.new(1999, 1, 1))
end

it "imports role only if created_at can be set" do
it "imports role only if start_on can be set" do
expect do
Rake::Task["import:people_fo"].invoke(groups(:berner_wanderwege).id)
end.to output("Successfully imported 5/5 rows\n").to_stdout
Expand All @@ -139,9 +139,9 @@
person_with_one_role = Person.find_by(alabus_id: '1s23w-b52n1x-2ciw2kjn-g-g213bwvh-1x7')
person_without_roles = Person.find_by(alabus_id: 'bew31-axzcd1-jbhox23z-z-jtxn23wd1-k3g')

expect(person_with_two_roles.roles.with_deleted.count).to eq(2)
expect(person_with_one_role.roles.with_deleted.count).to eq(1)
expect(person_without_roles.roles.with_deleted.count).to eq(0)
expect(person_with_two_roles.roles.with_inactive.count).to eq(2)
expect(person_with_one_role.roles.with_inactive.count).to eq(1)
expect(person_without_roles.roles.with_inactive.count).to eq(0)
end

it "assigns Schweiz as fallback country" do
Expand Down Expand Up @@ -381,7 +381,7 @@
expect(person.language).to eq('fr')
expect(person.valid_password?('great_password')).to eq(true)

expect(person.roles.with_deleted.count).to eq(1)
expect(person.roles.with_inactive.count).to eq(1)

role = Group::Benutzerkonten::Benutzerkonto.find_by(person_id: person.id,
group_id: benutzerkonten_group.id)
Expand Down

0 comments on commit eadf84d

Please sign in to comment.