Skip to content

Commit 69e25ef

Browse files
committed
results: include donations to ops
1 parent 3a59668 commit 69e25ef

File tree

4 files changed

+109
-43
lines changed

4 files changed

+109
-43
lines changed

app/results/donations/monthly/outputs/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
dbClient,
33
dbRelease,
4-
getTransferOverview,
4+
getResultsMonthlyOutputs,
55
logError,
66
type TransferredDistribution,
77
} from "src";
@@ -12,7 +12,7 @@ export async function GET() {
1212
try {
1313
db = await dbClient();
1414

15-
const transfer_overview = await getTransferOverview(db);
15+
const transfer_overview = await getResultsMonthlyOutputs(db);
1616

1717
return Response.json({
1818
status: 200,

db/migrations/99999999999999_views.sql

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ ignored_renewals,
3535
kpi,
3636
pending_distribution,
3737
renew_donations_to_email,
38+
results_monthly_outputs,
3839
time_distribution_daily,
3940
time_distribution_monthly,
4041
transfer_overview,
@@ -1401,6 +1402,40 @@ grant
14011402
select
14021403
on transfer_overview to everyone;
14031404

1405+
--------------------------------------
1406+
create view results_monthly_outputs as
1407+
select
1408+
total_dkk,
1409+
earmark,
1410+
transferred_at,
1411+
unit,
1412+
unit_impact,
1413+
recipient
1414+
from
1415+
transfer_overview
1416+
union all
1417+
select
1418+
round(sum(amount)) as total_dkk,
1419+
earmark,
1420+
date_trunc('month', charged_at)::date::text as transferred_at,
1421+
'kr.' as unit,
1422+
round(sum(amount)) as unit_impact,
1423+
'Giv Effektivt' as recipient
1424+
from
1425+
charged_donations_by_transfer_internal
1426+
where
1427+
earmark = 'Giv Effektivts arbejde og vækst'
1428+
group by
1429+
earmark,
1430+
transferred_at
1431+
order by
1432+
transferred_at,
1433+
total_dkk desc;
1434+
1435+
grant
1436+
select
1437+
on results_monthly_outputs to everyone;
1438+
14041439
--------------------------------------
14051440
create view transferred_distribution as
14061441
select

db/schema.sql

Lines changed: 66 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2484,6 +2484,72 @@ CREATE VIEW giveffektivt.renew_donations_to_email AS
24842484
WHERE (d.emailed = 'renew-no'::giveffektivt.emailed_status);
24852485

24862486

2487+
--
2488+
-- Name: transfer_overview; Type: VIEW; Schema: giveffektivt; Owner: -
2489+
--
2490+
2491+
CREATE VIEW giveffektivt.transfer_overview AS
2492+
SELECT t.id,
2493+
t.earmark,
2494+
(
2495+
CASE
2496+
WHEN (t.created_at > now()) THEN 'Forventet: '::text
2497+
ELSE ''::text
2498+
END || t.recipient) AS recipient,
2499+
CASE
2500+
WHEN (t.recipient = 'Against Malaria Foundation'::giveffektivt.transfer_recipient) THEN 'Antimalaria myggenet'::text
2501+
WHEN (t.recipient = 'Malaria Consortium'::giveffektivt.transfer_recipient) THEN 'Malariabehandlinger'::text
2502+
WHEN (t.recipient = 'Helen Keller International'::giveffektivt.transfer_recipient) THEN 'A-vitamintilskud'::text
2503+
WHEN (t.recipient = 'New Incentives'::giveffektivt.transfer_recipient) THEN 'Vaccinationsprogrammer'::text
2504+
WHEN (t.recipient = 'Give Directly'::giveffektivt.transfer_recipient) THEN 'Dollars'::text
2505+
WHEN (t.recipient = 'SCI Foundation'::giveffektivt.transfer_recipient) THEN 'Ormekure'::text
2506+
ELSE NULL::text
2507+
END AS unit,
2508+
round(sum(cdt.amount)) AS total_dkk,
2509+
round((sum(cdt.amount) / max(t.exchange_rate))) AS total_usd,
2510+
round(max(t.unit_cost_external), 2) AS unit_cost_external,
2511+
round(max(t.unit_cost_conversion), 2) AS unit_cost_conversion,
2512+
round(((max(t.unit_cost_external) / max(t.unit_cost_conversion)) * max(t.exchange_rate)), 2) AS unit_cost_dkk,
2513+
round(((sum(cdt.amount) / max(t.exchange_rate)) / (max(t.unit_cost_external) / max(t.unit_cost_conversion))), 1) AS unit_impact,
2514+
round(max(t.life_cost_external), 2) AS life_cost_external,
2515+
round((max(t.life_cost_external) * max(t.exchange_rate)), 2) AS life_cost_dkk,
2516+
round(((sum(cdt.amount) / max(t.exchange_rate)) / max(t.life_cost_external)), 1) AS life_impact,
2517+
max(cdt.charged_at) AS computed_at,
2518+
CASE
2519+
WHEN (t.created_at > now()) THEN 'Næste overførsel'::text
2520+
ELSE to_char(t.created_at, 'yyyy-mm-dd'::text)
2521+
END AS transferred_at
2522+
FROM (giveffektivt.charged_donations_by_transfer_internal cdt
2523+
JOIN giveffektivt.transfer t ON (((cdt.transfer_id = t.id) OR ((cdt.transfer_id IS NULL) AND (cdt.earmark = t.earmark) AND (t.created_at > now())))))
2524+
GROUP BY t.id, t.earmark, t.recipient, t.created_at
2525+
ORDER BY t.created_at, (sum(cdt.amount)) DESC;
2526+
2527+
2528+
--
2529+
-- Name: results_monthly_outputs; Type: VIEW; Schema: giveffektivt; Owner: -
2530+
--
2531+
2532+
CREATE VIEW giveffektivt.results_monthly_outputs AS
2533+
SELECT transfer_overview.total_dkk,
2534+
transfer_overview.earmark,
2535+
transfer_overview.transferred_at,
2536+
transfer_overview.unit,
2537+
transfer_overview.unit_impact,
2538+
transfer_overview.recipient
2539+
FROM giveffektivt.transfer_overview
2540+
UNION ALL
2541+
SELECT round(sum(charged_donations_by_transfer_internal.amount)) AS total_dkk,
2542+
charged_donations_by_transfer_internal.earmark,
2543+
((date_trunc('month'::text, charged_donations_by_transfer_internal.charged_at))::date)::text AS transferred_at,
2544+
'kr.'::text AS unit,
2545+
round(sum(charged_donations_by_transfer_internal.amount)) AS unit_impact,
2546+
'Giv Effektivt'::text AS recipient
2547+
FROM giveffektivt.charged_donations_by_transfer_internal
2548+
WHERE (charged_donations_by_transfer_internal.earmark = 'Giv Effektivts arbejde og vækst'::giveffektivt.donation_recipient)
2549+
GROUP BY charged_donations_by_transfer_internal.earmark, ((date_trunc('month'::text, charged_donations_by_transfer_internal.charged_at))::date)::text
2550+
ORDER BY 3, 1 DESC;
2551+
2552+
24872553
--
24882554
-- Name: scanpay_seq; Type: TABLE; Schema: giveffektivt; Owner: -
24892555
--
@@ -3220,47 +3286,6 @@ CREATE VIEW giveffektivt.time_distribution_monthly AS
32203286
ORDER BY COALESCE(a.period, b.period) DESC;
32213287

32223288

3223-
--
3224-
-- Name: transfer_overview; Type: VIEW; Schema: giveffektivt; Owner: -
3225-
--
3226-
3227-
CREATE VIEW giveffektivt.transfer_overview AS
3228-
SELECT t.id,
3229-
t.earmark,
3230-
(
3231-
CASE
3232-
WHEN (t.created_at > now()) THEN 'Forventet: '::text
3233-
ELSE ''::text
3234-
END || t.recipient) AS recipient,
3235-
CASE
3236-
WHEN (t.recipient = 'Against Malaria Foundation'::giveffektivt.transfer_recipient) THEN 'Antimalaria myggenet'::text
3237-
WHEN (t.recipient = 'Malaria Consortium'::giveffektivt.transfer_recipient) THEN 'Malariabehandlinger'::text
3238-
WHEN (t.recipient = 'Helen Keller International'::giveffektivt.transfer_recipient) THEN 'A-vitamintilskud'::text
3239-
WHEN (t.recipient = 'New Incentives'::giveffektivt.transfer_recipient) THEN 'Vaccinationsprogrammer'::text
3240-
WHEN (t.recipient = 'Give Directly'::giveffektivt.transfer_recipient) THEN 'Dollars'::text
3241-
WHEN (t.recipient = 'SCI Foundation'::giveffektivt.transfer_recipient) THEN 'Ormekure'::text
3242-
ELSE NULL::text
3243-
END AS unit,
3244-
round(sum(cdt.amount)) AS total_dkk,
3245-
round((sum(cdt.amount) / max(t.exchange_rate))) AS total_usd,
3246-
round(max(t.unit_cost_external), 2) AS unit_cost_external,
3247-
round(max(t.unit_cost_conversion), 2) AS unit_cost_conversion,
3248-
round(((max(t.unit_cost_external) / max(t.unit_cost_conversion)) * max(t.exchange_rate)), 2) AS unit_cost_dkk,
3249-
round(((sum(cdt.amount) / max(t.exchange_rate)) / (max(t.unit_cost_external) / max(t.unit_cost_conversion))), 1) AS unit_impact,
3250-
round(max(t.life_cost_external), 2) AS life_cost_external,
3251-
round((max(t.life_cost_external) * max(t.exchange_rate)), 2) AS life_cost_dkk,
3252-
round(((sum(cdt.amount) / max(t.exchange_rate)) / max(t.life_cost_external)), 1) AS life_impact,
3253-
max(cdt.charged_at) AS computed_at,
3254-
CASE
3255-
WHEN (t.created_at > now()) THEN 'Næste overførsel'::text
3256-
ELSE to_char(t.created_at, 'yyyy-mm-dd'::text)
3257-
END AS transferred_at
3258-
FROM (giveffektivt.charged_donations_by_transfer_internal cdt
3259-
JOIN giveffektivt.transfer t ON (((cdt.transfer_id = t.id) OR ((cdt.transfer_id IS NULL) AND (cdt.earmark = t.earmark) AND (t.created_at > now())))))
3260-
GROUP BY t.id, t.earmark, t.recipient, t.created_at
3261-
ORDER BY t.created_at, (sum(cdt.amount)) DESC;
3262-
3263-
32643289
--
32653290
-- Name: transferred_distribution; Type: VIEW; Schema: giveffektivt; Owner: -
32663291
--

src/kpi/repository.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ export async function getTransferOverview(
2323
return (await client.query("select * from transfer_overview")).rows;
2424
}
2525

26+
export async function getResultsMonthlyOutputs(
27+
client: PoolClient,
28+
): Promise<TransferredDistribution[]> {
29+
return (await client.query("select * from results_monthly_outputs")).rows;
30+
}
31+
2632
export async function getTransferredDistribution(
2733
client: PoolClient,
2834
): Promise<TransferredDistribution[]> {

0 commit comments

Comments
 (0)