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 committed Sep 16, 2024
1 parent 3793e0d commit fb19a46
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 42 deletions.
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
54 changes: 27 additions & 27 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 @@ -231,7 +231,7 @@
expect(invoice.state).to eq('issued')
expect(invoice.esr_number).to eq('00 37592 44815 05725 00000 00013')
expect(invoice.sent_at).to eq(DateTime.new(2022, 3, 28))
expect(invoice.created_at).to eq(DateTime.new(2022, 3, 28))
expect(invoice.start_on).to eq(DateTime.new(2022, 3, 28))

expect(invoice.invoice_items.count).to eq(1)

Expand All @@ -257,7 +257,7 @@

expected_output = ['Successfully imported 2/2 rows',
'ROWS WITH STATUS OTHER THAN "Offen":',
"esr_number: 00 34519 87043 97732 00000 00013, sent_at: 10.02.2022, created_at: 10.02.2022, alabus_id: wi2bn3f-tfbw3v-js1swvzh-h-jx75x634-beje, amount: 75\n"].join("\n")
"esr_number: 00 34519 87043 97732 00000 00013, sent_at: 10.02.2022, start_on: 10.02.2022, alabus_id: wi2bn3f-tfbw3v-js1swvzh-h-jx75x634-beje, amount: 75\n"].join("\n")

expect do
Rake::Task["import:invoices_fo"].invoke(groups(:berner_wanderwege).id)
Expand Down Expand Up @@ -308,12 +308,12 @@

expected_output = ['Successfully imported 0/4 rows',
'ROWS WITH STATUS OTHER THAN "Offen":',
'esr_number: 00 34519 87043 97732 00000 00013, sent_at: 10.02.2022, created_at: 10.02.2022, alabus_id: wi2bn3f-tfbw3v-js1swvzh-h-jx75x634-beje, amount: 75',
'esr_number: 00 34519 87043 97732 00000 00013, sent_at: 10.02.2022, start_on: 10.02.2022, alabus_id: wi2bn3f-tfbw3v-js1swvzh-h-jx75x634-beje, amount: 75',
'FAILED ROWS:',
'esr_number: 00 37592 44815 05725 00000 00013, sent_at: 28.03.2022, created_at: 28.03.2022, alabus_id: 5c2o3xc-twcwrv-js1wkcxh-h-jsax76d7-bew1, amount: 75, failing_note: person not found',
"esr_number: 00 65823 21284 96217 00000 00013, sent_at: 13.03.2022, created_at: 13.03.2022, alabus_id: wi2bhef-tfcdxv-vcewwcvh-h-jx23x667-ghee, amount: , failing_note: invoice not found after reload",
"esr_number: 00 43914 69124 312592 00000 00013, sent_at: 25.03.2022, created_at: 25.03.2022, alabus_id: , amount: 100, failing_note: id not present",
"esr_number: , sent_at: , created_at: , alabus_id: wi2523f-t431xg-57eww221-h-j634x6sd-ghae, amount: 100, failing_note: Gültigkeitsprüfung ist fehlgeschlagen: Referenz Nummer muss ausgefüllt werden\n",
'esr_number: 00 37592 44815 05725 00000 00013, sent_at: 28.03.2022, start_on: 28.03.2022, alabus_id: 5c2o3xc-twcwrv-js1wkcxh-h-jsax76d7-bew1, amount: 75, failing_note: person not found',
"esr_number: 00 65823 21284 96217 00000 00013, sent_at: 13.03.2022, start_on: 13.03.2022, alabus_id: wi2bhef-tfcdxv-vcewwcvh-h-jx23x667-ghee, amount: , failing_note: invoice not found after reload",
"esr_number: 00 43914 69124 312592 00000 00013, sent_at: 25.03.2022, start_on: 25.03.2022, alabus_id: , amount: 100, failing_note: id not present",
"esr_number: , sent_at: , start_on: , alabus_id: wi2523f-t431xg-57eww221-h-j634x6sd-ghae, amount: 100, failing_note: Gültigkeitsprüfung ist fehlgeschlagen: Referenz Nummer muss ausgefüllt werden\n",
"nothing was imported due to errors. Please fix import source file and try again.\n"].join("\n")


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 fb19a46

Please sign in to comment.