Make contacts in dedications into usable json
This commit is contained in:
parent
10b6a8ba18
commit
6bbdfc4ebc
8 changed files with 139 additions and 31 deletions
|
@ -21,7 +21,7 @@ function view(state) {
|
||||||
, type: 'radio'
|
, type: 'radio'
|
||||||
, id: radioId1
|
, id: radioId1
|
||||||
, value: 'honor'
|
, value: 'honor'
|
||||||
, selected: data.dedication_type === 'honor'
|
, selected: !data.dedication_type || data.dedication_type === 'honor'
|
||||||
}})
|
}})
|
||||||
, h('label', {props: {htmlFor: radioId1}}, I18n.t('nonprofits.donate.dedication.in_honor_label'))
|
, h('label', {props: {htmlFor: radioId1}}, I18n.t('nonprofits.donate.dedication.in_honor_label'))
|
||||||
])
|
])
|
||||||
|
|
|
@ -127,7 +127,9 @@ const setDonationDedication = (don, dedication) => {
|
||||||
, JSON.stringify({
|
, JSON.stringify({
|
||||||
supporter_id: dedication.supporter.id
|
supporter_id: dedication.supporter.id
|
||||||
, name: dedication.supporter.name
|
, name: dedication.supporter.name
|
||||||
, contact: R.join(" - ", [dedication.supporter.email, dedication.supporter.phone, dedication.supporter.address])
|
, contact: {email: dedication.supporter.email,
|
||||||
|
phone: dedication.supporter.phone,
|
||||||
|
address: dedication.supporter.address}
|
||||||
, note: dedication.note
|
, note: dedication.note
|
||||||
, type: dedication.type
|
, type: dedication.type
|
||||||
})
|
})
|
||||||
|
|
|
@ -148,14 +148,17 @@ appl.def('format_dedication', function(dedic, node) {
|
||||||
var json
|
var json
|
||||||
try { json = JSON.parse(dedic) } catch(e) {}
|
try { json = JSON.parse(dedic) } catch(e) {}
|
||||||
if(json) {
|
if(json) {
|
||||||
|
let supporter_link = (json.supporter_id && json.supporter_id != '') ?
|
||||||
|
`<a href='/nonprofits/${app.nonprofit_id}/supporters?sid=${json.supporter_id}'>${json.name}</a>` :
|
||||||
|
json.name
|
||||||
inner = `
|
inner = `
|
||||||
Donation made in ${dedic.type || 'honor'} of
|
Donation made in ${json.type || 'honor'} of
|
||||||
<a href='/nonprofits/${app.nonprofit_id}/supporters?sid=${json.supporter_id}'>${json.name}</a>.
|
${supporter_link}.
|
||||||
${json.note ? `<br>Note: <em>${json.note}</em>.` : ''}
|
${json.note ? `<br>Note: <em>${json.note}</em>.` : ''}
|
||||||
`
|
`
|
||||||
} else {
|
} else {
|
||||||
// Print plaintext dedication
|
// Print plaintext dedication
|
||||||
inner = dedic
|
inner = ''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
td.innerHTML = inner
|
td.innerHTML = inner
|
||||||
|
|
|
@ -16,33 +16,12 @@ language plpgsql
|
||||||
immutable;
|
immutable;
|
||||||
SQL
|
SQL
|
||||||
|
|
||||||
result = Qx.select('id', 'dedication').from(:donations)
|
dedications = MaintainDedications.retrieve_non_json_dedications
|
||||||
.where("dedication IS NOT NULL AND dedication != ''")
|
|
||||||
.and_where("NOT is_valid_json(dedication)").ex
|
|
||||||
result.map do |i|
|
|
||||||
output = {id: i['id']}
|
|
||||||
if i['dedication'] =~ /(((in (loving )?)?memory of|in memorium)\:? )(.+)/i
|
|
||||||
output[:dedication] = JSON.generate({type: 'memory', note: $+ })
|
|
||||||
elsif i['dedication'] =~ /((in honor of|honor of)\:? )(.+)/i
|
|
||||||
output[:dedication] = JSON.generate({type: 'honor', note: $+ })
|
|
||||||
else
|
|
||||||
output[:dedication] = JSON.generate({type: 'honor', note: i['dedication'] })
|
|
||||||
end
|
|
||||||
output
|
|
||||||
end.each do |i|
|
|
||||||
Qx.update(:donations).where('id = $id', {id: i[:id]}).set({dedication: i[:dedication]}).ex
|
|
||||||
end
|
|
||||||
|
|
||||||
#
|
MaintainDedications.create_json_dedications_from_plain_text(dedications)
|
||||||
# result = Qx.select('id', 'dedication').from(:donations)
|
|
||||||
# .where("id IN ($ids)", ids: result.map{|i| i['id']}).ex
|
|
||||||
#
|
|
||||||
# puts result
|
|
||||||
|
|
||||||
|
dedications = MaintainDedications.retrieve_json_dedications
|
||||||
execute <<-SQL
|
MaintainDedications.add_honor_to_any_json_dedications_without_type(dedications)
|
||||||
drop function is_valid_json(text);
|
|
||||||
SQL
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def down
|
def down
|
||||||
|
|
52
db/migrate/20181003212559_correct_dedication_contacts.rb
Normal file
52
db/migrate/20181003212559_correct_dedication_contacts.rb
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
class CorrectDedicationContacts < ActiveRecord::Migration
|
||||||
|
def up
|
||||||
|
json_dedications = Qx.select('id', 'dedication').from(:donations)
|
||||||
|
.where("dedication IS NOT NULL AND dedication != ''")
|
||||||
|
.and_where("is_valid_json(dedication)").ex
|
||||||
|
parsed_dedications = json_dedications.map{|i| {id: i['id'], dedication: JSON.parse(i['dedication'])}}
|
||||||
|
with_contact_to_correct = parsed_dedications.select {|i| !i[:dedication]['contact'].blank? && i[:dedication]['contact'].is_a?(String)}
|
||||||
|
really_icky_dedications, easy_to_split_strings = with_contact_to_correct.partition{|i| i[:dedication]['contact'].split(" - ").count > 3}
|
||||||
|
|
||||||
|
easy_to_split_strings.map do |i|
|
||||||
|
split_contact = i[:dedication]['contact'].split(' - ')
|
||||||
|
i[:dedication]['contact'] = {
|
||||||
|
email: split_contact[0],
|
||||||
|
phone:split_contact[1],
|
||||||
|
address: split_contact[2],
|
||||||
|
}
|
||||||
|
puts i
|
||||||
|
i
|
||||||
|
end.each_with_index do |i, index|
|
||||||
|
unless (i[:id])
|
||||||
|
raise Error("Item at index #{index} is invalid. Object:#{i}")
|
||||||
|
end
|
||||||
|
Qx.update(:donations).where("id = $id", id:i[:id]).set(dedication: JSON.generate(i[:dedication])).ex
|
||||||
|
end
|
||||||
|
|
||||||
|
puts "Corrected #{easy_to_split_strings.count} records."
|
||||||
|
|
||||||
|
puts ""
|
||||||
|
puts ""
|
||||||
|
puts "You must manually fix the following dedications: "
|
||||||
|
really_icky_dedications.each do |i|
|
||||||
|
puts i
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def down
|
||||||
|
json_dedications = Qx.select('id', 'dedication').from(:donations)
|
||||||
|
.where("dedication IS NOT NULL AND dedication != ''")
|
||||||
|
.and_where("is_valid_json(dedication)").ex
|
||||||
|
|
||||||
|
parsed_dedications = json_dedications.map{|i| {'id' => i['id'], 'dedication' => JSON.parse(i['dedication'])}}
|
||||||
|
|
||||||
|
with_contact_to_correct = parsed_dedications.select {|i| i['dedication']['contact'].is_a?(Hash)}
|
||||||
|
|
||||||
|
puts "#{with_contact_to_correct.count} to revert"
|
||||||
|
with_contact_to_correct.each do |i|
|
||||||
|
contact_string = "#{i['dedication']['contact']['email']} - #{i['dedication']['contact']['phone']} - #{i['dedication']['contact']['address']}"
|
||||||
|
i['dedication']['contact'] = contact_string
|
||||||
|
Qx.update(:donations).where("id = $id", id:i['id']).set(dedication: JSON.generate(i['dedication'])).ex
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -3,7 +3,7 @@
|
||||||
--
|
--
|
||||||
|
|
||||||
-- Dumped from database version 9.6.5
|
-- Dumped from database version 9.6.5
|
||||||
-- Dumped by pg_dump version 9.6.9
|
-- Dumped by pg_dump version 9.6.10
|
||||||
|
|
||||||
SET statement_timeout = 0;
|
SET statement_timeout = 0;
|
||||||
SET lock_timeout = 0;
|
SET lock_timeout = 0;
|
||||||
|
@ -57,6 +57,22 @@ CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA public;
|
||||||
COMMENT ON EXTENSION "uuid-ossp" IS 'generate universally unique identifiers (UUIDs)';
|
COMMENT ON EXTENSION "uuid-ossp" IS 'generate universally unique identifiers (UUIDs)';
|
||||||
|
|
||||||
|
|
||||||
|
--
|
||||||
|
-- Name: is_valid_json(text); Type: FUNCTION; Schema: public; Owner: -
|
||||||
|
--
|
||||||
|
|
||||||
|
CREATE FUNCTION public.is_valid_json(p_json text) RETURNS boolean
|
||||||
|
LANGUAGE plpgsql IMMUTABLE
|
||||||
|
AS $$
|
||||||
|
begin
|
||||||
|
return (p_json::json is not null);
|
||||||
|
exception
|
||||||
|
when others then
|
||||||
|
return false;
|
||||||
|
end;
|
||||||
|
$$;
|
||||||
|
|
||||||
|
|
||||||
--
|
--
|
||||||
-- Name: update_supporter_assoc_search_vectors(); Type: FUNCTION; Schema: public; Owner: -
|
-- Name: update_supporter_assoc_search_vectors(); Type: FUNCTION; Schema: public; Owner: -
|
||||||
--
|
--
|
||||||
|
@ -4327,3 +4343,7 @@ INSERT INTO schema_migrations (version) VALUES ('20180713215825');
|
||||||
|
|
||||||
INSERT INTO schema_migrations (version) VALUES ('20180713220028');
|
INSERT INTO schema_migrations (version) VALUES ('20180713220028');
|
||||||
|
|
||||||
|
INSERT INTO schema_migrations (version) VALUES ('20181002160627');
|
||||||
|
|
||||||
|
INSERT INTO schema_migrations (version) VALUES ('20181003212559');
|
||||||
|
|
||||||
|
|
44
lib/maintain/maintain_dedications.rb
Normal file
44
lib/maintain/maintain_dedications.rb
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
|
||||||
|
module MaintainDedications
|
||||||
|
def self.retrieve_json_dedications
|
||||||
|
return Qx.select('id', 'dedication').from(:donations)
|
||||||
|
.where("is_valid_json(dedication)").ex
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.retrieve_non_json_dedications(include_blank=false)
|
||||||
|
temp = Qx.select('id', 'dedication').from(:donations)
|
||||||
|
temp = temp.where("dedication IS NOT NULL AND dedication != ''") unless include_blank
|
||||||
|
temp = temp.and_where("NOT is_valid_json(dedication)")
|
||||||
|
return temp.ex
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.create_json_dedications_from_plain_text(dedications)
|
||||||
|
dedications.map do |i|
|
||||||
|
output = {id: i['id']}
|
||||||
|
if i['dedication'] =~ /(((in (loving )?)?memory of|in memorium)\:? )(.+)/i
|
||||||
|
output[:dedication] = JSON.generate({type: 'memory', note: $+ })
|
||||||
|
elsif i['dedication'] =~ /((in honor of|honor of)\:? )(.+)/i
|
||||||
|
output[:dedication] = JSON.generate({type: 'honor', note: $+ })
|
||||||
|
else
|
||||||
|
output[:dedication] = JSON.generate({type: 'honor', note: i['dedication'] })
|
||||||
|
end
|
||||||
|
output
|
||||||
|
end.each do |i|
|
||||||
|
Qx.update(:donations).where('id = $id', {id: i[:id]}).set({dedication: i[:dedication]}).ex
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.add_honor_to_any_json_dedications_without_type(json_dedications)
|
||||||
|
json_dedications.map{|i| {'id' => i['id'], 'dedication' => JSON::parse(i['dedication']) }}
|
||||||
|
.select{|i| !%w(honor memory).include?(i['dedication']['type'])}
|
||||||
|
.map{|i| i['dedication']['type'] = 'honor'; i }
|
||||||
|
.each do |i|
|
||||||
|
Qx.update(:donations).where('id = $id', id: i['id'])
|
||||||
|
.set(dedication: JSON.generate(i['dedication'])).ex
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
end
|
8
spec/lib/maintain/maintain_dedications_spec.rb
Normal file
8
spec/lib/maintain/maintain_dedications_spec.rb
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
describe maintaindedications do
|
||||||
|
it 'is untested' do
|
||||||
|
pending 'add tests here'
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Reference in a new issue