Skip to content

Commit

Permalink
improve gavebrev checkins creation
Browse files Browse the repository at this point in the history
  • Loading branch information
maximbaz committed Dec 30, 2024
1 parent 03ef7ad commit 4a8fe43
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-- migrate:up
drop view gavebrev_checkins_to_create;

create view gavebrev_checkins_to_create as
select
*
from ( select distinct on (g.donor_id)
g.donor_id as donor_id,
coalesce(c.year + 1, date_part('year', g.created_at)::numeric) as year,
coalesce(c.income_verified, coalesce(c.income_preliminary, coalesce(c.income_inferred, 0))) as income_inferred
from
gavebrev g
left join gavebrev_checkin c on g.donor_id = c.donor_id
where
g.status = 'signed'
and g.stopped_at >= now()
order by
g.donor_id,
c.year desc) s
where
year <= date_part('year', now());

grant select on gavebrev_checkins_to_create to reader_sensitive;

select
cron.schedule('create-gavebrev-checkins', '0 4 * * *', 'insert into gavebrev_checkin(donor_id, year, income_inferred) select * from gavebrev_checkins_to_create');

-- migrate:down
21 changes: 11 additions & 10 deletions db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,11 @@ CREATE TABLE giveffektivt._transfer (
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
deleted_at timestamp with time zone,
earmark giveffektivt.donation_recipient NOT NULL,
unit_cost_conversion numeric,
unit_cost_external numeric,
life_cost_external numeric,
exchange_rate numeric
exchange_rate numeric,
earmark giveffektivt.donation_recipient NOT NULL,
unit_cost_conversion numeric
);


Expand Down Expand Up @@ -1158,13 +1158,13 @@ CREATE VIEW giveffektivt.gavebrev_checkins_to_create AS
SELECT donor_id,
year,
income_inferred
FROM ( SELECT DISTINCT ON (c.donor_id) c.donor_id,
(c.year + (1)::numeric) AS year,
COALESCE(c.income_verified, COALESCE(c.income_preliminary, c.income_inferred)) AS income_inferred
FROM (giveffektivt.gavebrev_checkin c
JOIN giveffektivt.gavebrev g ON ((g.donor_id = c.donor_id)))
FROM ( SELECT DISTINCT ON (g.donor_id) g.donor_id,
COALESCE((c.year + (1)::numeric), (date_part('year'::text, g.created_at))::numeric) AS year,
COALESCE(c.income_verified, COALESCE(c.income_preliminary, COALESCE(c.income_inferred, (0)::numeric))) AS income_inferred
FROM (giveffektivt.gavebrev g
LEFT JOIN giveffektivt.gavebrev_checkin c ON ((g.donor_id = c.donor_id)))
WHERE ((g.status = 'signed'::giveffektivt.gavebrev_status) AND (g.stopped_at >= now()))
ORDER BY c.donor_id, c.year DESC) s
ORDER BY g.donor_id, c.year DESC) s
WHERE ((year)::double precision <= date_part('year'::text, now()));


Expand Down Expand Up @@ -1973,4 +1973,5 @@ INSERT INTO giveffektivt.schema_migrations (version) VALUES
('20241121213227'),
('20241123140526'),
('20241211133732'),
('20241212214448');
('20241212214448'),
('20241230123042');

0 comments on commit 4a8fe43

Please sign in to comment.