@mccaskie-labs/workbench-fundraising 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/README.md +63 -2
- package/dist/cli.d.ts +12 -0
- package/dist/cli.js +161 -0
- package/dist/config.d.ts +7 -0
- package/dist/config.js +137 -0
- package/dist/draw.d.ts +26 -0
- package/dist/draw.js +86 -0
- package/dist/errors.d.ts +5 -0
- package/dist/errors.js +12 -0
- package/dist/fundraising.d.ts +238 -0
- package/dist/fundraising.js +495 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +15 -0
- package/dist/law.d.ts +81 -0
- package/dist/law.js +406 -0
- package/dist/migration.d.ts +4 -0
- package/dist/migration.js +902 -0
- package/dist/money.d.ts +4 -0
- package/dist/money.js +10 -0
- package/dist/starters.d.ts +22 -0
- package/dist/starters.js +13 -0
- package/dist/styles.d.ts +33 -0
- package/dist/styles.js +108 -0
- package/dist/types.d.ts +278 -0
- package/dist/types.js +5 -0
- package/dist/ui.d.ts +96 -0
- package/dist/ui.js +236 -0
- package/dist/words.d.ts +4 -0
- package/dist/words.js +250 -0
- package/package.json +55 -4
|
@@ -0,0 +1,902 @@
|
|
|
1
|
+
// The app's own fundraising tables (docs/capabilities/31-fundraising.md,
|
|
2
|
+
// "Data stored"), copied into the project's supabase/migrations as one file
|
|
3
|
+
// and its down file.
|
|
4
|
+
//
|
|
5
|
+
// Only the service role reaches them: the functions are granted to
|
|
6
|
+
// service_role alone and the tables have RLS on with no policies. The app
|
|
7
|
+
// calls createFundraising with its service client, server side.
|
|
8
|
+
//
|
|
9
|
+
// A gift is recorded once per payment (its ref is unique), so a webhook that
|
|
10
|
+
// arrives twice counts once. Raffle tickets are held, paid and numbered under
|
|
11
|
+
// a lock on the raffle's row, so two buyers at the same moment can't both
|
|
12
|
+
// take the last ticket, and paid tickets are numbered 1, 2, 3 … in the order
|
|
13
|
+
// they were paid for. The draw is stored once, with its seed, and only from a
|
|
14
|
+
// raffle whose sales have closed and that has no unpaid holds.
|
|
15
|
+
//
|
|
16
|
+
// It is a Workbench tool's own migration: new tables and functions only (no
|
|
17
|
+
// OR REPLACE, no SECURITY DEFINER, nothing called while it runs), with REVOKE
|
|
18
|
+
// and GRANT on its own objects to service_role. So Control applies it on
|
|
19
|
+
// staging without asking (hard rule 6); production asks. The down file drops
|
|
20
|
+
// everything the up file made.
|
|
21
|
+
const SIGNATURES = [
|
|
22
|
+
"fundraising_campaign_save(jsonb, text, timestamptz)",
|
|
23
|
+
"fundraising_campaign_status(uuid, text, text, timestamptz)",
|
|
24
|
+
"fundraising_campaign_get(text)",
|
|
25
|
+
"fundraising_campaigns(text)",
|
|
26
|
+
"fundraising_totals(uuid)",
|
|
27
|
+
"fundraising_pledge_start(uuid, bigint, text, boolean, boolean, text, text, text, timestamptz)",
|
|
28
|
+
"fundraising_pledge_end(uuid, timestamptz)",
|
|
29
|
+
"fundraising_gift_record(uuid, text, bigint, timestamptz)",
|
|
30
|
+
"fundraising_gift_refund(text, timestamptz)",
|
|
31
|
+
"fundraising_cash_add(uuid, text, bigint, text, text, text, timestamptz)",
|
|
32
|
+
"fundraising_supporters(uuid, integer)",
|
|
33
|
+
"fundraising_raffle_save(jsonb, text, timestamptz)",
|
|
34
|
+
"fundraising_raffle_move(uuid, text, text, text, text, timestamptz)",
|
|
35
|
+
"fundraising_raffle_get(uuid)",
|
|
36
|
+
"fundraising_raffles(uuid)",
|
|
37
|
+
"fundraising_tickets_hold(uuid, text, integer, text, text, text, timestamptz, timestamptz)",
|
|
38
|
+
"fundraising_tickets_confirm(text, text, bigint, timestamptz)",
|
|
39
|
+
"fundraising_tickets_release(text, timestamptz)",
|
|
40
|
+
"fundraising_tickets_void(text, timestamptz)",
|
|
41
|
+
"fundraising_order_get(text)",
|
|
42
|
+
"fundraising_draw_state(uuid)",
|
|
43
|
+
"fundraising_raffle_draw(uuid, text, integer, integer[], text, jsonb, text, jsonb, timestamptz)",
|
|
44
|
+
"fundraising_raffle_redraw(uuid, integer, integer, jsonb, text, text, timestamptz)",
|
|
45
|
+
"fundraising_prize_claimed(uuid, integer, text, timestamptz)",
|
|
46
|
+
"fundraising_winner_contacts(uuid)",
|
|
47
|
+
"fundraising_refundable(uuid)",
|
|
48
|
+
"fundraising_tick(timestamptz)",
|
|
49
|
+
"fundraising_cleanup(timestamptz, integer)",
|
|
50
|
+
];
|
|
51
|
+
const JSON_FNS = [
|
|
52
|
+
"fundraising_campaign_json(public.fundraising_campaigns)",
|
|
53
|
+
"fundraising_raffle_json(public.fundraising_raffles)",
|
|
54
|
+
"fundraising_order_json(public.fundraising_ticket_orders)",
|
|
55
|
+
"fundraising_gift_json(public.fundraising_gifts)",
|
|
56
|
+
];
|
|
57
|
+
const up = `create table public.fundraising_campaigns (
|
|
58
|
+
id uuid primary key default gen_random_uuid(),
|
|
59
|
+
slug text not null unique check (slug ~ '^[a-z0-9]([a-z0-9-]{0,78}[a-z0-9])?$'),
|
|
60
|
+
title text not null check (char_length(title) between 1 and 200),
|
|
61
|
+
story text not null default '' check (char_length(story) <= 20000),
|
|
62
|
+
image_url text check (image_url ~ '^https://' and char_length(image_url) <= 2000),
|
|
63
|
+
goal_cents bigint check (goal_cents between 1 and 100000000000),
|
|
64
|
+
currency text not null check (currency ~ '^[A-Z]{3}$'),
|
|
65
|
+
starts_at timestamptz,
|
|
66
|
+
ends_at timestamptz,
|
|
67
|
+
status text not null default 'draft' check (status in ('draft', 'live', 'closed')),
|
|
68
|
+
created_by text not null check (char_length(created_by) between 1 and 200),
|
|
69
|
+
closed_at timestamptz,
|
|
70
|
+
created_at timestamptz not null default now(),
|
|
71
|
+
updated_at timestamptz not null default now(),
|
|
72
|
+
check (ends_at is null or starts_at is null or ends_at > starts_at)
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
comment on table public.fundraising_campaigns is 'Fundraising campaigns: a goal, dates, a story and a picture (workbench-fundraising). Kept with the gifts. Service role only.';
|
|
76
|
+
|
|
77
|
+
create table public.fundraising_pledges (
|
|
78
|
+
id uuid primary key default gen_random_uuid(),
|
|
79
|
+
campaign_id uuid not null references public.fundraising_campaigns (id) on delete restrict,
|
|
80
|
+
amount_cents bigint not null check (amount_cents between 1 and 100000000),
|
|
81
|
+
currency text not null check (currency ~ '^[A-Z]{3}$'),
|
|
82
|
+
monthly boolean not null,
|
|
83
|
+
show_name boolean not null default false,
|
|
84
|
+
name text check (char_length(name) between 1 and 100),
|
|
85
|
+
message text check (char_length(message) between 1 and 500),
|
|
86
|
+
supporter text check (char_length(supporter) between 1 and 200),
|
|
87
|
+
status text not null default 'pending' check (status in ('pending', 'given', 'active', 'ended')),
|
|
88
|
+
ended_at timestamptz,
|
|
89
|
+
cleared_at timestamptz,
|
|
90
|
+
created_at timestamptz not null default now(),
|
|
91
|
+
check (show_name or name is null)
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
comment on table public.fundraising_pledges is 'What a giver chose before paying: amount, once or monthly, and (only if they ticked the box) the name and message shown on the page. Personal details: name, message, supporter (the app''s contact id), cleared after the retention period. Service role only.';
|
|
95
|
+
|
|
96
|
+
create table public.fundraising_gifts (
|
|
97
|
+
id uuid primary key default gen_random_uuid(),
|
|
98
|
+
campaign_id uuid not null references public.fundraising_campaigns (id) on delete restrict,
|
|
99
|
+
pledge_id uuid references public.fundraising_pledges (id) on delete restrict,
|
|
100
|
+
kind text not null check (kind in ('online', 'cash')),
|
|
101
|
+
ref text not null unique check (char_length(ref) between 1 and 200),
|
|
102
|
+
amount_cents bigint not null check (amount_cents between 1 and 100000000),
|
|
103
|
+
currency text not null check (currency ~ '^[A-Z]{3}$'),
|
|
104
|
+
monthly boolean not null default false,
|
|
105
|
+
note text check (char_length(note) <= 500),
|
|
106
|
+
by text check (char_length(by) between 1 and 200),
|
|
107
|
+
refunded_at timestamptz,
|
|
108
|
+
created_at timestamptz not null default now(),
|
|
109
|
+
check ((kind = 'online') = (pledge_id is not null))
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
comment on table public.fundraising_gifts is 'Gifts that count towards a campaign: one row per confirmed payment (or monthly cycle) and per cash amount the owner adds. No personal details. Kept with the campaign as the organisation''s record. Service role only.';
|
|
113
|
+
|
|
114
|
+
create index fundraising_gifts_campaign on public.fundraising_gifts (campaign_id) where refunded_at is null;
|
|
115
|
+
create index fundraising_pledges_campaign on public.fundraising_pledges (campaign_id, created_at);
|
|
116
|
+
|
|
117
|
+
create table public.fundraising_raffles (
|
|
118
|
+
id uuid primary key default gen_random_uuid(),
|
|
119
|
+
campaign_id uuid references public.fundraising_campaigns (id) on delete restrict,
|
|
120
|
+
title text not null check (char_length(title) between 1 and 200),
|
|
121
|
+
place text not null check (place ~ '^[A-Z]{2}(-[A-Z]{2,3})?$'),
|
|
122
|
+
currency text not null check (currency ~ '^[A-Z]{3}$'),
|
|
123
|
+
ticket_cents bigint not null check (ticket_cents between 50 and 100000000),
|
|
124
|
+
max_tickets integer not null check (max_tickets between 1 and 1000000),
|
|
125
|
+
sold integer not null default 0 check (sold >= 0),
|
|
126
|
+
held integer not null default 0 check (held >= 0),
|
|
127
|
+
next_number integer not null default 1 check (next_number >= 1),
|
|
128
|
+
sales_start timestamptz not null,
|
|
129
|
+
sales_end timestamptz not null,
|
|
130
|
+
draw_at timestamptz not null,
|
|
131
|
+
draw_place text not null check (char_length(draw_place) between 1 and 300),
|
|
132
|
+
prizes jsonb not null check (jsonb_typeof(prizes) = 'array' and jsonb_array_length(prizes) between 1 and 100),
|
|
133
|
+
permits jsonb not null default '{}' check (jsonb_typeof(permits) = 'object'),
|
|
134
|
+
adults_only boolean not null default true,
|
|
135
|
+
terms text check (char_length(terms) <= 5000),
|
|
136
|
+
unclaimed_months integer not null check (unclaimed_months between 0 and 24),
|
|
137
|
+
records_years integer not null check (records_years between 1 and 10),
|
|
138
|
+
status text not null default 'draft' check (status in ('draft', 'selling', 'closed', 'drawn', 'cancelled')),
|
|
139
|
+
seed text check (seed ~ '^[0-9a-f]{64}$'),
|
|
140
|
+
issued integer,
|
|
141
|
+
void_numbers integer[] not null default '{}',
|
|
142
|
+
tickets_hash text check (tickets_hash ~ '^[0-9a-f]{64}$'),
|
|
143
|
+
winners jsonb not null default '[]' check (jsonb_typeof(winners) = 'array'),
|
|
144
|
+
claimed integer[] not null default '{}',
|
|
145
|
+
witnesses jsonb not null default '[]' check (jsonb_typeof(witnesses) = 'array'),
|
|
146
|
+
drawn_by text check (char_length(drawn_by) between 1 and 200),
|
|
147
|
+
drawn_at timestamptz,
|
|
148
|
+
cancel_reason text check (char_length(cancel_reason) <= 500),
|
|
149
|
+
created_by text not null check (char_length(created_by) between 1 and 200),
|
|
150
|
+
created_at timestamptz not null default now(),
|
|
151
|
+
updated_at timestamptz not null default now(),
|
|
152
|
+
check (sales_end > sales_start and draw_at >= sales_end),
|
|
153
|
+
check (sold + held <= max_tickets),
|
|
154
|
+
check (next_number - 1 >= sold)
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
comment on table public.fundraising_raffles is 'Raffles: prizes, tickets, dates, permit numbers, and the recorded draw with its seed (workbench-fundraising). Kept for the place''s records period. Service role only.';
|
|
158
|
+
|
|
159
|
+
create table public.fundraising_ticket_orders (
|
|
160
|
+
ref text primary key check (char_length(ref) between 1 and 200),
|
|
161
|
+
raffle_id uuid not null references public.fundraising_raffles (id) on delete restrict,
|
|
162
|
+
count integer not null check (count between 1 and 500),
|
|
163
|
+
amount_cents bigint not null check (amount_cents >= 50),
|
|
164
|
+
status text not null default 'held' check (status in ('held', 'paid', 'released', 'void')),
|
|
165
|
+
first_number integer check (first_number >= 1),
|
|
166
|
+
buyer_name text check (char_length(buyer_name) between 1 and 100),
|
|
167
|
+
buyer_email text check (char_length(buyer_email) between 3 and 254),
|
|
168
|
+
customer text check (char_length(customer) between 1 and 200),
|
|
169
|
+
payment_ref text unique check (char_length(payment_ref) between 1 and 200),
|
|
170
|
+
expires_at timestamptz not null,
|
|
171
|
+
paid_at timestamptz,
|
|
172
|
+
voided_at timestamptz,
|
|
173
|
+
cleared_at timestamptz,
|
|
174
|
+
created_at timestamptz not null default now(),
|
|
175
|
+
check ((status in ('paid', 'void')) = (payment_ref is not null) or status = 'released'),
|
|
176
|
+
check (status <> 'paid' or first_number is not null)
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
comment on table public.fundraising_ticket_orders is 'Raffle ticket orders: how many, the numbers once paid, and the buyer''s name and email to tell a winner. Personal details: buyer_name, buyer_email, customer, cleared after the place''s records period. Service role only.';
|
|
180
|
+
|
|
181
|
+
create index fundraising_orders_raffle on public.fundraising_ticket_orders (raffle_id, status);
|
|
182
|
+
create index fundraising_orders_held on public.fundraising_ticket_orders (expires_at) where status = 'held';
|
|
183
|
+
|
|
184
|
+
alter table public.fundraising_campaigns enable row level security;
|
|
185
|
+
alter table public.fundraising_pledges enable row level security;
|
|
186
|
+
alter table public.fundraising_gifts enable row level security;
|
|
187
|
+
alter table public.fundraising_raffles enable row level security;
|
|
188
|
+
alter table public.fundraising_ticket_orders enable row level security;
|
|
189
|
+
|
|
190
|
+
revoke all on public.fundraising_campaigns, public.fundraising_pledges, public.fundraising_gifts, public.fundraising_raffles, public.fundraising_ticket_orders from anon, authenticated;
|
|
191
|
+
|
|
192
|
+
create function public.fundraising_campaign_json(c public.fundraising_campaigns)
|
|
193
|
+
returns jsonb
|
|
194
|
+
language sql
|
|
195
|
+
immutable
|
|
196
|
+
security invoker
|
|
197
|
+
set search_path = ''
|
|
198
|
+
as $$
|
|
199
|
+
select jsonb_build_object('id', c.id, 'slug', c.slug, 'title', c.title, 'story', c.story, 'image_url', c.image_url, 'goal_cents', c.goal_cents,
|
|
200
|
+
'currency', c.currency, 'starts_at', c.starts_at, 'ends_at', c.ends_at, 'status', c.status, 'created_at', c.created_at);
|
|
201
|
+
$$;
|
|
202
|
+
|
|
203
|
+
create function public.fundraising_raffle_json(r public.fundraising_raffles)
|
|
204
|
+
returns jsonb
|
|
205
|
+
language sql
|
|
206
|
+
immutable
|
|
207
|
+
security invoker
|
|
208
|
+
set search_path = ''
|
|
209
|
+
as $$
|
|
210
|
+
select jsonb_build_object('id', r.id, 'campaign_id', r.campaign_id, 'title', r.title, 'place', r.place, 'currency', r.currency,
|
|
211
|
+
'ticket_cents', r.ticket_cents, 'max_tickets', r.max_tickets, 'sold', r.sold, 'held', r.held,
|
|
212
|
+
'sales_start', r.sales_start, 'sales_end', r.sales_end, 'draw_at', r.draw_at, 'draw_place', r.draw_place,
|
|
213
|
+
'prizes', r.prizes, 'permits', r.permits, 'adults_only', r.adults_only, 'terms', r.terms, 'status', r.status,
|
|
214
|
+
'unclaimed_months', r.unclaimed_months, 'records_years', r.records_years,
|
|
215
|
+
'seed', r.seed, 'issued', r.issued, 'void_numbers', to_jsonb(r.void_numbers), 'tickets_hash', r.tickets_hash,
|
|
216
|
+
'winners', r.winners, 'claimed', to_jsonb(r.claimed), 'witnesses', r.witnesses, 'drawn_by', r.drawn_by,
|
|
217
|
+
'drawn_at', r.drawn_at, 'cancel_reason', r.cancel_reason, 'created_at', r.created_at);
|
|
218
|
+
$$;
|
|
219
|
+
|
|
220
|
+
create function public.fundraising_order_json(o public.fundraising_ticket_orders)
|
|
221
|
+
returns jsonb
|
|
222
|
+
language sql
|
|
223
|
+
immutable
|
|
224
|
+
security invoker
|
|
225
|
+
set search_path = ''
|
|
226
|
+
as $$
|
|
227
|
+
select jsonb_build_object('ref', o.ref, 'raffle_id', o.raffle_id, 'count', o.count, 'amount_cents', o.amount_cents,
|
|
228
|
+
'status', o.status, 'first_number', o.first_number, 'expires_at', o.expires_at, 'payment_ref', o.payment_ref);
|
|
229
|
+
$$;
|
|
230
|
+
|
|
231
|
+
create function public.fundraising_gift_json(g public.fundraising_gifts)
|
|
232
|
+
returns jsonb
|
|
233
|
+
language sql
|
|
234
|
+
immutable
|
|
235
|
+
security invoker
|
|
236
|
+
set search_path = ''
|
|
237
|
+
as $$
|
|
238
|
+
select jsonb_build_object('id', g.id, 'campaign_id', g.campaign_id, 'pledge_id', g.pledge_id, 'kind', g.kind, 'ref', g.ref,
|
|
239
|
+
'amount_cents', g.amount_cents, 'currency', g.currency, 'monthly', g.monthly, 'refunded', g.refunded_at is not null, 'created_at', g.created_at);
|
|
240
|
+
$$;
|
|
241
|
+
|
|
242
|
+
-- Makes a campaign (no id) or changes one (with its id). A closed campaign
|
|
243
|
+
-- can't change.
|
|
244
|
+
create function public.fundraising_campaign_save(p jsonb, p_by text, p_now timestamptz)
|
|
245
|
+
returns jsonb
|
|
246
|
+
language plpgsql
|
|
247
|
+
security invoker
|
|
248
|
+
set search_path = ''
|
|
249
|
+
as $$
|
|
250
|
+
declare
|
|
251
|
+
c public.fundraising_campaigns;
|
|
252
|
+
begin
|
|
253
|
+
if p ? 'id' and p->>'id' is not null then
|
|
254
|
+
update public.fundraising_campaigns set
|
|
255
|
+
slug = p->>'slug', title = p->>'title', story = coalesce(p->>'story', ''), image_url = p->>'image_url',
|
|
256
|
+
goal_cents = (p->>'goal_cents')::bigint, starts_at = (p->>'starts_at')::timestamptz, ends_at = (p->>'ends_at')::timestamptz,
|
|
257
|
+
updated_at = p_now
|
|
258
|
+
where id = (p->>'id')::uuid and status <> 'closed'
|
|
259
|
+
returning * into c;
|
|
260
|
+
if c.id is null then raise exception using errcode = 'P0001', message = 'fundraising_not_found'; end if;
|
|
261
|
+
else
|
|
262
|
+
insert into public.fundraising_campaigns (slug, title, story, image_url, goal_cents, currency, starts_at, ends_at, created_by, created_at, updated_at)
|
|
263
|
+
values (p->>'slug', p->>'title', coalesce(p->>'story', ''), p->>'image_url', (p->>'goal_cents')::bigint, p->>'currency',
|
|
264
|
+
(p->>'starts_at')::timestamptz, (p->>'ends_at')::timestamptz, p_by, p_now, p_now)
|
|
265
|
+
returning * into c;
|
|
266
|
+
end if;
|
|
267
|
+
return public.fundraising_campaign_json(c);
|
|
268
|
+
end;
|
|
269
|
+
$$;
|
|
270
|
+
|
|
271
|
+
-- draft → live → closed. A closed campaign stays closed.
|
|
272
|
+
create function public.fundraising_campaign_status(p_id uuid, p_status text, p_by text, p_now timestamptz)
|
|
273
|
+
returns jsonb
|
|
274
|
+
language plpgsql
|
|
275
|
+
security invoker
|
|
276
|
+
set search_path = ''
|
|
277
|
+
as $$
|
|
278
|
+
declare
|
|
279
|
+
c public.fundraising_campaigns;
|
|
280
|
+
begin
|
|
281
|
+
select * into c from public.fundraising_campaigns where id = p_id for update;
|
|
282
|
+
if not found then raise exception using errcode = 'P0001', message = 'fundraising_not_found'; end if;
|
|
283
|
+
if c.status = p_status then return public.fundraising_campaign_json(c); end if;
|
|
284
|
+
if c.status = 'closed' or (c.status = 'draft' and p_status not in ('live', 'closed')) or (c.status = 'live' and p_status <> 'closed') then
|
|
285
|
+
raise exception using errcode = 'P0001', message = 'fundraising_wrong_state';
|
|
286
|
+
end if;
|
|
287
|
+
update public.fundraising_campaigns set status = p_status, updated_at = p_now,
|
|
288
|
+
closed_at = case when p_status = 'closed' then p_now else closed_at end
|
|
289
|
+
where id = p_id returning * into c;
|
|
290
|
+
return public.fundraising_campaign_json(c);
|
|
291
|
+
end;
|
|
292
|
+
$$;
|
|
293
|
+
|
|
294
|
+
create function public.fundraising_campaign_get(p_key text)
|
|
295
|
+
returns jsonb
|
|
296
|
+
language sql
|
|
297
|
+
stable
|
|
298
|
+
security invoker
|
|
299
|
+
set search_path = ''
|
|
300
|
+
as $$
|
|
301
|
+
select public.fundraising_campaign_json(c) from public.fundraising_campaigns c
|
|
302
|
+
where c.slug = p_key or c.id::text = p_key
|
|
303
|
+
limit 1;
|
|
304
|
+
$$;
|
|
305
|
+
|
|
306
|
+
create function public.fundraising_campaigns(p_status text)
|
|
307
|
+
returns jsonb
|
|
308
|
+
language sql
|
|
309
|
+
stable
|
|
310
|
+
security invoker
|
|
311
|
+
set search_path = ''
|
|
312
|
+
as $$
|
|
313
|
+
select coalesce(jsonb_agg(public.fundraising_campaign_json(c) order by c.created_at desc), '[]'::jsonb)
|
|
314
|
+
from public.fundraising_campaigns c where p_status is null or c.status = p_status;
|
|
315
|
+
$$;
|
|
316
|
+
|
|
317
|
+
-- What's been raised: confirmed gifts and cash, less refunds, and paid raffle
|
|
318
|
+
-- tickets of the campaign's raffles.
|
|
319
|
+
create function public.fundraising_totals(p_campaign uuid)
|
|
320
|
+
returns jsonb
|
|
321
|
+
language sql
|
|
322
|
+
stable
|
|
323
|
+
security invoker
|
|
324
|
+
set search_path = ''
|
|
325
|
+
as $$
|
|
326
|
+
select jsonb_build_object(
|
|
327
|
+
'campaign_id', c.id,
|
|
328
|
+
'goal_cents', c.goal_cents,
|
|
329
|
+
'gifts_cents', coalesce((select sum(g.amount_cents) from public.fundraising_gifts g where g.campaign_id = c.id and g.kind = 'online' and g.refunded_at is null), 0),
|
|
330
|
+
'cash_cents', coalesce((select sum(g.amount_cents) from public.fundraising_gifts g where g.campaign_id = c.id and g.kind = 'cash' and g.refunded_at is null), 0),
|
|
331
|
+
'raffle_cents', coalesce((select sum(o.amount_cents) from public.fundraising_ticket_orders o join public.fundraising_raffles r on r.id = o.raffle_id
|
|
332
|
+
where r.campaign_id = c.id and o.status = 'paid'), 0),
|
|
333
|
+
'gifts', (select count(*) from public.fundraising_gifts g where g.campaign_id = c.id and g.refunded_at is null),
|
|
334
|
+
'monthly_givers', (select count(*) from public.fundraising_pledges p where p.campaign_id = c.id and p.monthly and p.status = 'active'))
|
|
335
|
+
from public.fundraising_campaigns c where c.id = p_campaign;
|
|
336
|
+
$$;
|
|
337
|
+
|
|
338
|
+
-- What a giver chose, before they pay. Only on a live campaign within its dates.
|
|
339
|
+
create function public.fundraising_pledge_start(
|
|
340
|
+
p_campaign uuid, p_amount bigint, p_currency text, p_monthly boolean, p_show boolean, p_name text, p_message text, p_supporter text, p_now timestamptz
|
|
341
|
+
)
|
|
342
|
+
returns jsonb
|
|
343
|
+
language plpgsql
|
|
344
|
+
security invoker
|
|
345
|
+
set search_path = ''
|
|
346
|
+
as $$
|
|
347
|
+
declare
|
|
348
|
+
c public.fundraising_campaigns;
|
|
349
|
+
v public.fundraising_pledges;
|
|
350
|
+
begin
|
|
351
|
+
select * into c from public.fundraising_campaigns where id = p_campaign;
|
|
352
|
+
if not found then raise exception using errcode = 'P0001', message = 'fundraising_not_found'; end if;
|
|
353
|
+
if c.status <> 'live' or (c.starts_at is not null and c.starts_at > p_now) or (c.ends_at is not null and c.ends_at <= p_now) then
|
|
354
|
+
return jsonb_build_object('ok', false, 'reason', 'not_live');
|
|
355
|
+
end if;
|
|
356
|
+
if c.currency <> p_currency then raise exception using errcode = '22023', message = 'fundraising: the campaign is in another currency'; end if;
|
|
357
|
+
insert into public.fundraising_pledges (campaign_id, amount_cents, currency, monthly, show_name, name, message, supporter, created_at)
|
|
358
|
+
values (p_campaign, p_amount, p_currency, p_monthly, p_show, case when p_show then p_name end, p_message, p_supporter, p_now)
|
|
359
|
+
returning * into v;
|
|
360
|
+
return jsonb_build_object('ok', true, 'pledge', jsonb_build_object('id', v.id, 'campaign_id', v.campaign_id, 'amount_cents', v.amount_cents,
|
|
361
|
+
'monthly', v.monthly, 'show_name', v.show_name, 'status', v.status, 'created_at', v.created_at));
|
|
362
|
+
end;
|
|
363
|
+
$$;
|
|
364
|
+
|
|
365
|
+
-- A monthly gift stopped (cancelled, or its retries ran out).
|
|
366
|
+
create function public.fundraising_pledge_end(p_pledge uuid, p_now timestamptz)
|
|
367
|
+
returns boolean
|
|
368
|
+
language plpgsql
|
|
369
|
+
security invoker
|
|
370
|
+
set search_path = ''
|
|
371
|
+
as $$
|
|
372
|
+
begin
|
|
373
|
+
update public.fundraising_pledges set status = 'ended', ended_at = p_now where id = p_pledge and monthly and status in ('pending', 'active');
|
|
374
|
+
return found;
|
|
375
|
+
end;
|
|
376
|
+
$$;
|
|
377
|
+
|
|
378
|
+
-- Records one confirmed payment against its pledge, once per payment ref:
|
|
379
|
+
-- the same ref again answers the first gift (repeat). A one-off pledge takes
|
|
380
|
+
-- one gift; a monthly one takes one per cycle.
|
|
381
|
+
create function public.fundraising_gift_record(p_pledge uuid, p_ref text, p_amount bigint, p_now timestamptz)
|
|
382
|
+
returns jsonb
|
|
383
|
+
language plpgsql
|
|
384
|
+
security invoker
|
|
385
|
+
set search_path = ''
|
|
386
|
+
as $$
|
|
387
|
+
declare
|
|
388
|
+
v public.fundraising_pledges;
|
|
389
|
+
g public.fundraising_gifts;
|
|
390
|
+
begin
|
|
391
|
+
select * into v from public.fundraising_pledges where id = p_pledge for update;
|
|
392
|
+
if not found then raise exception using errcode = 'P0001', message = 'fundraising_not_found'; end if;
|
|
393
|
+
select * into g from public.fundraising_gifts where ref = p_ref;
|
|
394
|
+
if found then
|
|
395
|
+
if g.pledge_id is distinct from p_pledge or g.amount_cents <> p_amount then
|
|
396
|
+
raise exception using errcode = 'P0001', message = 'fundraising_ref_reused';
|
|
397
|
+
end if;
|
|
398
|
+
return public.fundraising_gift_json(g) || jsonb_build_object('repeat', true);
|
|
399
|
+
end if;
|
|
400
|
+
if p_amount <> v.amount_cents then raise exception using errcode = 'P0001', message = 'fundraising_amount_differs'; end if;
|
|
401
|
+
if not v.monthly and v.status = 'given' then raise exception using errcode = 'P0001', message = 'fundraising_ref_reused'; end if;
|
|
402
|
+
insert into public.fundraising_gifts (campaign_id, pledge_id, kind, ref, amount_cents, currency, monthly, created_at)
|
|
403
|
+
values (v.campaign_id, v.id, 'online', p_ref, p_amount, v.currency, v.monthly, p_now)
|
|
404
|
+
returning * into g;
|
|
405
|
+
update public.fundraising_pledges set status = case when monthly then (case when status = 'ended' then 'ended' else 'active' end) else 'given' end
|
|
406
|
+
where id = v.id;
|
|
407
|
+
return public.fundraising_gift_json(g) || jsonb_build_object('repeat', false);
|
|
408
|
+
end;
|
|
409
|
+
$$;
|
|
410
|
+
|
|
411
|
+
-- A refunded payment stops counting. Twice is a no-op.
|
|
412
|
+
create function public.fundraising_gift_refund(p_ref text, p_now timestamptz)
|
|
413
|
+
returns jsonb
|
|
414
|
+
language plpgsql
|
|
415
|
+
security invoker
|
|
416
|
+
set search_path = ''
|
|
417
|
+
as $$
|
|
418
|
+
declare
|
|
419
|
+
g public.fundraising_gifts;
|
|
420
|
+
begin
|
|
421
|
+
select * into g from public.fundraising_gifts where ref = p_ref for update;
|
|
422
|
+
if not found then return jsonb_build_object('refunded', false, 'repeat', false); end if;
|
|
423
|
+
if g.refunded_at is not null then return jsonb_build_object('refunded', true, 'repeat', true); end if;
|
|
424
|
+
update public.fundraising_gifts set refunded_at = p_now where id = g.id;
|
|
425
|
+
return jsonb_build_object('refunded', true, 'repeat', false);
|
|
426
|
+
end;
|
|
427
|
+
$$;
|
|
428
|
+
|
|
429
|
+
-- Cash or a cheque the owner counted, once per key.
|
|
430
|
+
create function public.fundraising_cash_add(p_campaign uuid, p_key text, p_amount bigint, p_currency text, p_note text, p_by text, p_now timestamptz)
|
|
431
|
+
returns jsonb
|
|
432
|
+
language plpgsql
|
|
433
|
+
security invoker
|
|
434
|
+
set search_path = ''
|
|
435
|
+
as $$
|
|
436
|
+
declare
|
|
437
|
+
c public.fundraising_campaigns;
|
|
438
|
+
g public.fundraising_gifts;
|
|
439
|
+
begin
|
|
440
|
+
select * into c from public.fundraising_campaigns where id = p_campaign;
|
|
441
|
+
if not found then raise exception using errcode = 'P0001', message = 'fundraising_not_found'; end if;
|
|
442
|
+
if c.currency <> p_currency then raise exception using errcode = '22023', message = 'fundraising: the campaign is in another currency'; end if;
|
|
443
|
+
insert into public.fundraising_gifts (campaign_id, kind, ref, amount_cents, currency, note, by, created_at)
|
|
444
|
+
values (p_campaign, 'cash', 'cash:' || p_key, p_amount, p_currency, p_note, p_by, p_now)
|
|
445
|
+
on conflict (ref) do nothing
|
|
446
|
+
returning * into g;
|
|
447
|
+
if g.id is null then
|
|
448
|
+
select * into g from public.fundraising_gifts where ref = 'cash:' || p_key;
|
|
449
|
+
if g.amount_cents <> p_amount or g.campaign_id <> p_campaign then raise exception using errcode = 'P0001', message = 'fundraising_ref_reused'; end if;
|
|
450
|
+
return public.fundraising_gift_json(g) || jsonb_build_object('repeat', true);
|
|
451
|
+
end if;
|
|
452
|
+
return public.fundraising_gift_json(g) || jsonb_build_object('repeat', false);
|
|
453
|
+
end;
|
|
454
|
+
$$;
|
|
455
|
+
|
|
456
|
+
-- Givers who ticked the box, newest first, whose gift still counts. No amounts.
|
|
457
|
+
create function public.fundraising_supporters(p_campaign uuid, p_limit integer)
|
|
458
|
+
returns jsonb
|
|
459
|
+
language sql
|
|
460
|
+
stable
|
|
461
|
+
security invoker
|
|
462
|
+
set search_path = ''
|
|
463
|
+
as $$
|
|
464
|
+
select coalesce(jsonb_agg(jsonb_build_object('name', s.name, 'message', s.message, 'at', s.created_at) order by s.created_at desc), '[]'::jsonb)
|
|
465
|
+
from (
|
|
466
|
+
select p.name, p.message, p.created_at from public.fundraising_pledges p
|
|
467
|
+
where p.campaign_id = p_campaign and p.show_name and p.name is not null and p.status <> 'pending'
|
|
468
|
+
and exists (select 1 from public.fundraising_gifts g where g.pledge_id = p.id and g.refunded_at is null)
|
|
469
|
+
order by p.created_at desc
|
|
470
|
+
limit greatest(least(p_limit, 500), 1)
|
|
471
|
+
) s;
|
|
472
|
+
$$;
|
|
473
|
+
|
|
474
|
+
-- Makes a raffle (no id) or changes a draft one. Once it's selling, its
|
|
475
|
+
-- prizes, price, tickets and dates are fixed: they're on the tickets.
|
|
476
|
+
create function public.fundraising_raffle_save(p jsonb, p_by text, p_now timestamptz)
|
|
477
|
+
returns jsonb
|
|
478
|
+
language plpgsql
|
|
479
|
+
security invoker
|
|
480
|
+
set search_path = ''
|
|
481
|
+
as $$
|
|
482
|
+
declare
|
|
483
|
+
r public.fundraising_raffles;
|
|
484
|
+
begin
|
|
485
|
+
if p ? 'id' and p->>'id' is not null then
|
|
486
|
+
update public.fundraising_raffles set
|
|
487
|
+
campaign_id = (p->>'campaign_id')::uuid, title = p->>'title', place = p->>'place', ticket_cents = (p->>'ticket_cents')::bigint,
|
|
488
|
+
max_tickets = (p->>'max_tickets')::integer, sales_start = (p->>'sales_start')::timestamptz, sales_end = (p->>'sales_end')::timestamptz,
|
|
489
|
+
draw_at = (p->>'draw_at')::timestamptz, draw_place = p->>'draw_place', prizes = p->'prizes', permits = coalesce(p->'permits', '{}'::jsonb),
|
|
490
|
+
adults_only = (p->>'adults_only')::boolean, terms = p->>'terms', unclaimed_months = (p->>'unclaimed_months')::integer,
|
|
491
|
+
records_years = (p->>'records_years')::integer, updated_at = p_now
|
|
492
|
+
where id = (p->>'id')::uuid and status = 'draft'
|
|
493
|
+
returning * into r;
|
|
494
|
+
if r.id is null then raise exception using errcode = 'P0001', message = 'fundraising_wrong_state'; end if;
|
|
495
|
+
else
|
|
496
|
+
insert into public.fundraising_raffles (campaign_id, title, place, currency, ticket_cents, max_tickets, sales_start, sales_end, draw_at, draw_place,
|
|
497
|
+
prizes, permits, adults_only, terms, unclaimed_months, records_years, created_by, created_at, updated_at)
|
|
498
|
+
values ((p->>'campaign_id')::uuid, p->>'title', p->>'place', p->>'currency', (p->>'ticket_cents')::bigint, (p->>'max_tickets')::integer,
|
|
499
|
+
(p->>'sales_start')::timestamptz, (p->>'sales_end')::timestamptz, (p->>'draw_at')::timestamptz, p->>'draw_place',
|
|
500
|
+
p->'prizes', coalesce(p->'permits', '{}'::jsonb), (p->>'adults_only')::boolean, p->>'terms',
|
|
501
|
+
(p->>'unclaimed_months')::integer, (p->>'records_years')::integer, p_by, p_now, p_now)
|
|
502
|
+
returning * into r;
|
|
503
|
+
end if;
|
|
504
|
+
return public.fundraising_raffle_json(r);
|
|
505
|
+
end;
|
|
506
|
+
$$;
|
|
507
|
+
|
|
508
|
+
-- Moves a raffle on: draft → selling (opened), selling → closed (sales
|
|
509
|
+
-- ended early), or draft, selling or closed → cancelled. p_from must be the
|
|
510
|
+
-- state it's in, so two moves at once can't both win.
|
|
511
|
+
create function public.fundraising_raffle_move(p_id uuid, p_from text, p_to text, p_by text, p_reason text, p_now timestamptz)
|
|
512
|
+
returns jsonb
|
|
513
|
+
language plpgsql
|
|
514
|
+
security invoker
|
|
515
|
+
set search_path = ''
|
|
516
|
+
as $$
|
|
517
|
+
declare
|
|
518
|
+
r public.fundraising_raffles;
|
|
519
|
+
begin
|
|
520
|
+
if not ((p_from = 'draft' and p_to in ('selling', 'cancelled')) or (p_from = 'selling' and p_to in ('closed', 'cancelled')) or (p_from = 'closed' and p_to = 'cancelled')) then
|
|
521
|
+
raise exception using errcode = '22023', message = 'fundraising: a raffle can''t move from ' || p_from || ' to ' || p_to;
|
|
522
|
+
end if;
|
|
523
|
+
update public.fundraising_raffles set status = p_to, updated_at = p_now,
|
|
524
|
+
cancel_reason = case when p_to = 'cancelled' then p_reason else cancel_reason end
|
|
525
|
+
where id = p_id and status = p_from
|
|
526
|
+
returning * into r;
|
|
527
|
+
if r.id is null then raise exception using errcode = 'P0001', message = 'fundraising_wrong_state'; end if;
|
|
528
|
+
if p_to = 'cancelled' then
|
|
529
|
+
update public.fundraising_ticket_orders set status = 'released' where raffle_id = p_id and status = 'held';
|
|
530
|
+
update public.fundraising_raffles set held = 0 where id = p_id returning * into r;
|
|
531
|
+
end if;
|
|
532
|
+
return public.fundraising_raffle_json(r);
|
|
533
|
+
end;
|
|
534
|
+
$$;
|
|
535
|
+
|
|
536
|
+
create function public.fundraising_raffle_get(p_id uuid)
|
|
537
|
+
returns jsonb
|
|
538
|
+
language sql
|
|
539
|
+
stable
|
|
540
|
+
security invoker
|
|
541
|
+
set search_path = ''
|
|
542
|
+
as $$
|
|
543
|
+
select public.fundraising_raffle_json(r) from public.fundraising_raffles r where r.id = p_id;
|
|
544
|
+
$$;
|
|
545
|
+
|
|
546
|
+
create function public.fundraising_raffles(p_campaign uuid)
|
|
547
|
+
returns jsonb
|
|
548
|
+
language sql
|
|
549
|
+
stable
|
|
550
|
+
security invoker
|
|
551
|
+
set search_path = ''
|
|
552
|
+
as $$
|
|
553
|
+
select coalesce(jsonb_agg(public.fundraising_raffle_json(r) order by r.draw_at desc), '[]'::jsonb)
|
|
554
|
+
from public.fundraising_raffles r where p_campaign is null or r.campaign_id = p_campaign;
|
|
555
|
+
$$;
|
|
556
|
+
|
|
557
|
+
-- Holds tickets for an order while it's paid for, under the raffle's lock:
|
|
558
|
+
-- holds that have lapsed are let go first, then the order fits or is refused.
|
|
559
|
+
-- The same ref again answers the first hold.
|
|
560
|
+
create function public.fundraising_tickets_hold(
|
|
561
|
+
p_raffle uuid, p_ref text, p_count integer, p_name text, p_email text, p_customer text, p_expires timestamptz, p_now timestamptz
|
|
562
|
+
)
|
|
563
|
+
returns jsonb
|
|
564
|
+
language plpgsql
|
|
565
|
+
security invoker
|
|
566
|
+
set search_path = ''
|
|
567
|
+
as $$
|
|
568
|
+
declare
|
|
569
|
+
r public.fundraising_raffles;
|
|
570
|
+
o public.fundraising_ticket_orders;
|
|
571
|
+
v_freed integer;
|
|
572
|
+
v_left integer;
|
|
573
|
+
begin
|
|
574
|
+
select * into r from public.fundraising_raffles where id = p_raffle for update;
|
|
575
|
+
if not found then raise exception using errcode = 'P0001', message = 'fundraising_not_found'; end if;
|
|
576
|
+
select * into o from public.fundraising_ticket_orders where ref = p_ref;
|
|
577
|
+
if found then
|
|
578
|
+
if o.raffle_id <> p_raffle or o.count <> p_count then raise exception using errcode = 'P0001', message = 'fundraising_ref_reused'; end if;
|
|
579
|
+
return jsonb_build_object('ok', true, 'order', public.fundraising_order_json(o) || jsonb_build_object('repeat', true));
|
|
580
|
+
end if;
|
|
581
|
+
if r.status <> 'selling' then return jsonb_build_object('ok', false, 'reason', case when r.status = 'draft' then 'not_selling' else 'ended' end); end if;
|
|
582
|
+
if p_now < r.sales_start then return jsonb_build_object('ok', false, 'reason', 'not_started'); end if;
|
|
583
|
+
if p_now >= r.sales_end then return jsonb_build_object('ok', false, 'reason', 'ended'); end if;
|
|
584
|
+
with gone as (
|
|
585
|
+
update public.fundraising_ticket_orders set status = 'released'
|
|
586
|
+
where raffle_id = p_raffle and status = 'held' and expires_at <= p_now
|
|
587
|
+
returning count
|
|
588
|
+
) select coalesce(sum(count), 0) into v_freed from gone;
|
|
589
|
+
r.held := r.held - v_freed;
|
|
590
|
+
v_left := r.max_tickets - r.sold - r.held;
|
|
591
|
+
if v_left <= 0 then
|
|
592
|
+
update public.fundraising_raffles set held = r.held where id = p_raffle;
|
|
593
|
+
return jsonb_build_object('ok', false, 'reason', 'sold_out', 'left', 0);
|
|
594
|
+
end if;
|
|
595
|
+
if p_count > v_left then
|
|
596
|
+
update public.fundraising_raffles set held = r.held where id = p_raffle;
|
|
597
|
+
return jsonb_build_object('ok', false, 'reason', 'not_enough_left', 'left', v_left);
|
|
598
|
+
end if;
|
|
599
|
+
insert into public.fundraising_ticket_orders (ref, raffle_id, count, amount_cents, buyer_name, buyer_email, customer, expires_at, created_at)
|
|
600
|
+
values (p_ref, p_raffle, p_count, p_count * r.ticket_cents, p_name, p_email, p_customer, p_expires, p_now)
|
|
601
|
+
returning * into o;
|
|
602
|
+
update public.fundraising_raffles set held = r.held + p_count, updated_at = p_now where id = p_raffle;
|
|
603
|
+
return jsonb_build_object('ok', true, 'order', public.fundraising_order_json(o) || jsonb_build_object('repeat', false));
|
|
604
|
+
end;
|
|
605
|
+
$$;
|
|
606
|
+
|
|
607
|
+
-- The order's payment is confirmed: its tickets get the next numbers. A
|
|
608
|
+
-- payment that arrives after its hold lapsed still gets tickets while there
|
|
609
|
+
-- are some and the raffle isn't drawn or cancelled; otherwise it answers
|
|
610
|
+
-- too_late and the app refunds it.
|
|
611
|
+
create function public.fundraising_tickets_confirm(p_ref text, p_payment text, p_amount bigint, p_now timestamptz)
|
|
612
|
+
returns jsonb
|
|
613
|
+
language plpgsql
|
|
614
|
+
security invoker
|
|
615
|
+
set search_path = ''
|
|
616
|
+
as $$
|
|
617
|
+
declare
|
|
618
|
+
v_raffle uuid;
|
|
619
|
+
r public.fundraising_raffles;
|
|
620
|
+
o public.fundraising_ticket_orders;
|
|
621
|
+
v_was_held boolean;
|
|
622
|
+
begin
|
|
623
|
+
select raffle_id into v_raffle from public.fundraising_ticket_orders where ref = p_ref;
|
|
624
|
+
if v_raffle is null then raise exception using errcode = 'P0001', message = 'fundraising_not_found'; end if;
|
|
625
|
+
select * into r from public.fundraising_raffles where id = v_raffle for update;
|
|
626
|
+
select * into o from public.fundraising_ticket_orders where ref = p_ref for update;
|
|
627
|
+
if o.payment_ref is not null then
|
|
628
|
+
if o.payment_ref <> p_payment then raise exception using errcode = 'P0001', message = 'fundraising_ref_reused'; end if;
|
|
629
|
+
return jsonb_build_object('ok', true, 'order', public.fundraising_order_json(o) || jsonb_build_object('repeat', true));
|
|
630
|
+
end if;
|
|
631
|
+
if p_amount <> o.amount_cents then raise exception using errcode = 'P0001', message = 'fundraising_amount_differs'; end if;
|
|
632
|
+
if r.status not in ('selling', 'closed') or (o.status = 'released' and r.max_tickets - r.sold - r.held < o.count) then
|
|
633
|
+
return jsonb_build_object('ok', false, 'reason', 'too_late');
|
|
634
|
+
end if;
|
|
635
|
+
v_was_held := o.status = 'held';
|
|
636
|
+
update public.fundraising_ticket_orders set status = 'paid', payment_ref = p_payment, first_number = r.next_number, paid_at = p_now
|
|
637
|
+
where ref = p_ref returning * into o;
|
|
638
|
+
update public.fundraising_raffles set
|
|
639
|
+
sold = sold + o.count,
|
|
640
|
+
held = case when v_was_held then held - o.count else held end,
|
|
641
|
+
next_number = next_number + o.count, updated_at = p_now
|
|
642
|
+
where id = r.id;
|
|
643
|
+
return jsonb_build_object('ok', true, 'order', public.fundraising_order_json(o) || jsonb_build_object('repeat', false));
|
|
644
|
+
end;
|
|
645
|
+
$$;
|
|
646
|
+
|
|
647
|
+
-- The order's payment failed or expired: its held tickets go back.
|
|
648
|
+
create function public.fundraising_tickets_release(p_ref text, p_now timestamptz)
|
|
649
|
+
returns boolean
|
|
650
|
+
language plpgsql
|
|
651
|
+
security invoker
|
|
652
|
+
set search_path = ''
|
|
653
|
+
as $$
|
|
654
|
+
declare
|
|
655
|
+
v_raffle uuid;
|
|
656
|
+
v_count integer;
|
|
657
|
+
begin
|
|
658
|
+
select raffle_id into v_raffle from public.fundraising_ticket_orders where ref = p_ref;
|
|
659
|
+
if v_raffle is null then return false; end if;
|
|
660
|
+
perform 1 from public.fundraising_raffles where id = v_raffle for update;
|
|
661
|
+
update public.fundraising_ticket_orders set status = 'released' where ref = p_ref and status = 'held' returning count into v_count;
|
|
662
|
+
if v_count is null then return false; end if;
|
|
663
|
+
update public.fundraising_raffles set held = greatest(held - v_count, 0), updated_at = p_now where id = v_raffle;
|
|
664
|
+
return true;
|
|
665
|
+
end;
|
|
666
|
+
$$;
|
|
667
|
+
|
|
668
|
+
-- A paid order was refunded before the draw: its numbers leave the draw.
|
|
669
|
+
-- After the draw a ticket stays in it. Twice is a no-op.
|
|
670
|
+
create function public.fundraising_tickets_void(p_ref text, p_now timestamptz)
|
|
671
|
+
returns jsonb
|
|
672
|
+
language plpgsql
|
|
673
|
+
security invoker
|
|
674
|
+
set search_path = ''
|
|
675
|
+
as $$
|
|
676
|
+
declare
|
|
677
|
+
v_raffle uuid;
|
|
678
|
+
r public.fundraising_raffles;
|
|
679
|
+
o public.fundraising_ticket_orders;
|
|
680
|
+
begin
|
|
681
|
+
select raffle_id into v_raffle from public.fundraising_ticket_orders where ref = p_ref or payment_ref = p_ref;
|
|
682
|
+
if v_raffle is null then raise exception using errcode = 'P0001', message = 'fundraising_not_found'; end if;
|
|
683
|
+
select * into r from public.fundraising_raffles where id = v_raffle for update;
|
|
684
|
+
select * into o from public.fundraising_ticket_orders where ref = p_ref or payment_ref = p_ref for update;
|
|
685
|
+
if o.status = 'void' then return jsonb_build_object('voided', true, 'repeat', true, 'amount_cents', o.amount_cents); end if;
|
|
686
|
+
if o.status <> 'paid' then raise exception using errcode = 'P0001', message = 'fundraising_wrong_state'; end if;
|
|
687
|
+
if r.status = 'drawn' then raise exception using errcode = 'P0001', message = 'fundraising_drawn'; end if;
|
|
688
|
+
update public.fundraising_ticket_orders set status = 'void', voided_at = p_now where ref = o.ref;
|
|
689
|
+
update public.fundraising_raffles set sold = sold - o.count, updated_at = p_now where id = r.id;
|
|
690
|
+
return jsonb_build_object('voided', true, 'repeat', false, 'amount_cents', o.amount_cents);
|
|
691
|
+
end;
|
|
692
|
+
$$;
|
|
693
|
+
|
|
694
|
+
create function public.fundraising_order_get(p_ref text)
|
|
695
|
+
returns jsonb
|
|
696
|
+
language sql
|
|
697
|
+
stable
|
|
698
|
+
security invoker
|
|
699
|
+
set search_path = ''
|
|
700
|
+
as $$
|
|
701
|
+
select public.fundraising_order_json(o) from public.fundraising_ticket_orders o where o.ref = p_ref;
|
|
702
|
+
$$;
|
|
703
|
+
|
|
704
|
+
-- What the draw draws from: the highest number issued, the numbers voided,
|
|
705
|
+
-- and whether any order is still being paid for.
|
|
706
|
+
create function public.fundraising_draw_state(p_raffle uuid)
|
|
707
|
+
returns jsonb
|
|
708
|
+
language sql
|
|
709
|
+
stable
|
|
710
|
+
security invoker
|
|
711
|
+
set search_path = ''
|
|
712
|
+
as $$
|
|
713
|
+
select jsonb_build_object('status', r.status, 'issued', r.next_number - 1, 'held', r.held, 'sales_end', r.sales_end,
|
|
714
|
+
'void_numbers', coalesce((select jsonb_agg(n order by n) from public.fundraising_ticket_orders o, generate_series(o.first_number, o.first_number + o.count - 1) n
|
|
715
|
+
where o.raffle_id = r.id and o.status = 'void'), '[]'::jsonb))
|
|
716
|
+
from public.fundraising_raffles r where r.id = p_raffle;
|
|
717
|
+
$$;
|
|
718
|
+
|
|
719
|
+
-- Stores the draw once, from a closed raffle with no unpaid holds, as long
|
|
720
|
+
-- as the tickets are still the ones the winners were drawn from.
|
|
721
|
+
create function public.fundraising_raffle_draw(
|
|
722
|
+
p_raffle uuid, p_seed text, p_issued integer, p_void integer[], p_hash text, p_winners jsonb, p_by text, p_witnesses jsonb, p_now timestamptz
|
|
723
|
+
)
|
|
724
|
+
returns jsonb
|
|
725
|
+
language plpgsql
|
|
726
|
+
security invoker
|
|
727
|
+
set search_path = ''
|
|
728
|
+
as $$
|
|
729
|
+
declare
|
|
730
|
+
r public.fundraising_raffles;
|
|
731
|
+
v_void integer[];
|
|
732
|
+
begin
|
|
733
|
+
select * into r from public.fundraising_raffles where id = p_raffle for update;
|
|
734
|
+
if not found then raise exception using errcode = 'P0001', message = 'fundraising_not_found'; end if;
|
|
735
|
+
if r.status <> 'closed' then raise exception using errcode = 'P0001', message = 'fundraising_wrong_state'; end if;
|
|
736
|
+
if r.held > 0 then raise exception using errcode = 'P0001', message = 'fundraising_pending'; end if;
|
|
737
|
+
select coalesce(array_agg(n order by n), '{}') into v_void
|
|
738
|
+
from public.fundraising_ticket_orders o, generate_series(o.first_number, o.first_number + o.count - 1) n
|
|
739
|
+
where o.raffle_id = r.id and o.status = 'void';
|
|
740
|
+
if r.next_number - 1 <> p_issued or v_void <> coalesce(p_void, '{}') then raise exception using errcode = 'P0001', message = 'fundraising_changed'; end if;
|
|
741
|
+
update public.fundraising_raffles set status = 'drawn', seed = p_seed, issued = p_issued, void_numbers = v_void, tickets_hash = p_hash,
|
|
742
|
+
winners = p_winners, witnesses = p_witnesses, drawn_by = p_by, drawn_at = p_now, updated_at = p_now
|
|
743
|
+
where id = r.id returning * into r;
|
|
744
|
+
return public.fundraising_raffle_json(r);
|
|
745
|
+
end;
|
|
746
|
+
$$;
|
|
747
|
+
|
|
748
|
+
-- Adds a redraw of one unclaimed prize, once its holding period is over.
|
|
749
|
+
-- p_expected is how many winners the redraw was worked out from.
|
|
750
|
+
create function public.fundraising_raffle_redraw(p_raffle uuid, p_rank integer, p_expected integer, p_winner jsonb, p_by text, p_reason text, p_now timestamptz)
|
|
751
|
+
returns jsonb
|
|
752
|
+
language plpgsql
|
|
753
|
+
security invoker
|
|
754
|
+
set search_path = ''
|
|
755
|
+
as $$
|
|
756
|
+
declare
|
|
757
|
+
r public.fundraising_raffles;
|
|
758
|
+
begin
|
|
759
|
+
select * into r from public.fundraising_raffles where id = p_raffle for update;
|
|
760
|
+
if not found then raise exception using errcode = 'P0001', message = 'fundraising_not_found'; end if;
|
|
761
|
+
if r.status <> 'drawn' or p_rank = any (r.claimed) then raise exception using errcode = 'P0001', message = 'fundraising_wrong_state'; end if;
|
|
762
|
+
if jsonb_array_length(r.winners) <> p_expected then raise exception using errcode = 'P0001', message = 'fundraising_changed'; end if;
|
|
763
|
+
if r.drawn_at + make_interval(months => r.unclaimed_months) > p_now then raise exception using errcode = 'P0001', message = 'fundraising_too_soon'; end if;
|
|
764
|
+
update public.fundraising_raffles set winners = winners || jsonb_build_array(p_winner || jsonb_build_object('by', p_by, 'reason', p_reason)), updated_at = p_now
|
|
765
|
+
where id = r.id returning * into r;
|
|
766
|
+
return public.fundraising_raffle_json(r);
|
|
767
|
+
end;
|
|
768
|
+
$$;
|
|
769
|
+
|
|
770
|
+
create function public.fundraising_prize_claimed(p_raffle uuid, p_rank integer, p_by text, p_now timestamptz)
|
|
771
|
+
returns jsonb
|
|
772
|
+
language plpgsql
|
|
773
|
+
security invoker
|
|
774
|
+
set search_path = ''
|
|
775
|
+
as $$
|
|
776
|
+
declare
|
|
777
|
+
r public.fundraising_raffles;
|
|
778
|
+
begin
|
|
779
|
+
select * into r from public.fundraising_raffles where id = p_raffle for update;
|
|
780
|
+
if not found then raise exception using errcode = 'P0001', message = 'fundraising_not_found'; end if;
|
|
781
|
+
if r.status <> 'drawn' then raise exception using errcode = 'P0001', message = 'fundraising_wrong_state'; end if;
|
|
782
|
+
if not (p_rank = any (r.claimed)) then
|
|
783
|
+
update public.fundraising_raffles set claimed = array_append(claimed, p_rank), updated_at = p_now where id = r.id returning * into r;
|
|
784
|
+
end if;
|
|
785
|
+
return public.fundraising_raffle_json(r);
|
|
786
|
+
end;
|
|
787
|
+
$$;
|
|
788
|
+
|
|
789
|
+
-- Each prize's current winner (its latest draw) with the buyer to tell
|
|
790
|
+
-- privately.
|
|
791
|
+
create function public.fundraising_winner_contacts(p_raffle uuid)
|
|
792
|
+
returns jsonb
|
|
793
|
+
language sql
|
|
794
|
+
stable
|
|
795
|
+
security invoker
|
|
796
|
+
set search_path = ''
|
|
797
|
+
as $$
|
|
798
|
+
select coalesce(jsonb_agg(jsonb_build_object('rank', w.rank, 'number', w.number, 'name', o.buyer_name, 'email', o.buyer_email, 'order_ref', o.ref) order by w.rank), '[]'::jsonb)
|
|
799
|
+
from (
|
|
800
|
+
select distinct on ((x->>'rank')::integer) (x->>'rank')::integer as rank, (x->>'number')::integer as number
|
|
801
|
+
from public.fundraising_raffles r, jsonb_array_elements(r.winners) x
|
|
802
|
+
where r.id = p_raffle
|
|
803
|
+
order by (x->>'rank')::integer, (x->>'redraw')::integer desc
|
|
804
|
+
) w
|
|
805
|
+
join public.fundraising_ticket_orders o on o.raffle_id = p_raffle and o.status = 'paid' and w.number between o.first_number and o.first_number + o.count - 1;
|
|
806
|
+
$$;
|
|
807
|
+
|
|
808
|
+
-- A cancelled raffle's paid orders, for the app to refund.
|
|
809
|
+
create function public.fundraising_refundable(p_raffle uuid)
|
|
810
|
+
returns jsonb
|
|
811
|
+
language sql
|
|
812
|
+
stable
|
|
813
|
+
security invoker
|
|
814
|
+
set search_path = ''
|
|
815
|
+
as $$
|
|
816
|
+
select coalesce(jsonb_agg(jsonb_build_object('order_ref', o.ref, 'payment_ref', o.payment_ref, 'amount_cents', o.amount_cents) order by o.paid_at), '[]'::jsonb)
|
|
817
|
+
from public.fundraising_ticket_orders o where o.raffle_id = p_raffle and o.status = 'paid';
|
|
818
|
+
$$;
|
|
819
|
+
|
|
820
|
+
-- The daily work: lapsed holds go back, raffles whose sales have ended
|
|
821
|
+
-- close, and campaigns past their end date close.
|
|
822
|
+
create function public.fundraising_tick(p_now timestamptz)
|
|
823
|
+
returns jsonb
|
|
824
|
+
language plpgsql
|
|
825
|
+
security invoker
|
|
826
|
+
set search_path = ''
|
|
827
|
+
as $$
|
|
828
|
+
declare
|
|
829
|
+
v_released integer;
|
|
830
|
+
v_raffles jsonb;
|
|
831
|
+
v_campaigns integer;
|
|
832
|
+
begin
|
|
833
|
+
with gone as (
|
|
834
|
+
update public.fundraising_ticket_orders set status = 'released' where status = 'held' and expires_at <= p_now
|
|
835
|
+
returning raffle_id, count
|
|
836
|
+
), per as (
|
|
837
|
+
select raffle_id, sum(count) as n from gone group by raffle_id
|
|
838
|
+
), fixed as (
|
|
839
|
+
update public.fundraising_raffles r set held = greatest(r.held - per.n, 0), updated_at = p_now from per where r.id = per.raffle_id
|
|
840
|
+
returning r.id
|
|
841
|
+
) select (select coalesce(sum(n), 0) from per) into v_released;
|
|
842
|
+
with closed as (
|
|
843
|
+
update public.fundraising_raffles set status = 'closed', updated_at = p_now where status = 'selling' and sales_end <= p_now
|
|
844
|
+
returning id
|
|
845
|
+
) select coalesce(jsonb_agg(id), '[]'::jsonb) into v_raffles from closed;
|
|
846
|
+
with ended as (
|
|
847
|
+
update public.fundraising_campaigns set status = 'closed', closed_at = p_now, updated_at = p_now where status = 'live' and ends_at is not null and ends_at <= p_now
|
|
848
|
+
returning id
|
|
849
|
+
) select count(*) into v_campaigns from ended;
|
|
850
|
+
return jsonb_build_object('released', v_released, 'closed_raffles', v_raffles, 'closed_campaigns', v_campaigns);
|
|
851
|
+
end;
|
|
852
|
+
$$;
|
|
853
|
+
|
|
854
|
+
-- Clears personal details once they're no longer needed: givers' names and
|
|
855
|
+
-- messages p_months after their campaign closed, and ticket buyers' details
|
|
856
|
+
-- after the raffle's records period (or p_months, if longer). Unpaid
|
|
857
|
+
-- pledges and lapsed holds older than 30 days are deleted.
|
|
858
|
+
create function public.fundraising_cleanup(p_now timestamptz, p_months integer)
|
|
859
|
+
returns jsonb
|
|
860
|
+
language plpgsql
|
|
861
|
+
security invoker
|
|
862
|
+
set search_path = ''
|
|
863
|
+
as $$
|
|
864
|
+
declare
|
|
865
|
+
v_pledges integer;
|
|
866
|
+
v_orders integer;
|
|
867
|
+
v_deleted integer;
|
|
868
|
+
v_more integer;
|
|
869
|
+
begin
|
|
870
|
+
update public.fundraising_pledges p set name = null, message = null, supporter = null, show_name = false, cleared_at = p_now
|
|
871
|
+
from public.fundraising_campaigns c
|
|
872
|
+
where c.id = p.campaign_id and c.status = 'closed' and c.closed_at + make_interval(months => p_months) <= p_now and p.cleared_at is null;
|
|
873
|
+
get diagnostics v_pledges = row_count;
|
|
874
|
+
update public.fundraising_ticket_orders o set buyer_name = null, buyer_email = null, customer = null, cleared_at = p_now
|
|
875
|
+
from public.fundraising_raffles r
|
|
876
|
+
where r.id = o.raffle_id and o.cleared_at is null
|
|
877
|
+
and coalesce(r.drawn_at, case when r.status = 'cancelled' then r.updated_at end) + make_interval(months => greatest(r.records_years * 12, p_months)) <= p_now;
|
|
878
|
+
get diagnostics v_orders = row_count;
|
|
879
|
+
delete from public.fundraising_pledges p where p.status = 'pending' and p.created_at + interval '30 days' <= p_now
|
|
880
|
+
and not exists (select 1 from public.fundraising_gifts g where g.pledge_id = p.id);
|
|
881
|
+
get diagnostics v_deleted = row_count;
|
|
882
|
+
delete from public.fundraising_ticket_orders where status = 'released' and payment_ref is null and created_at + interval '30 days' <= p_now;
|
|
883
|
+
get diagnostics v_more = row_count;
|
|
884
|
+
return jsonb_build_object('cleared_pledges', v_pledges, 'cleared_orders', v_orders, 'deleted', v_deleted + v_more);
|
|
885
|
+
end;
|
|
886
|
+
$$;
|
|
887
|
+
|
|
888
|
+
${JSON_FNS.map((s) => `revoke execute on function public.${s} from public, anon, authenticated;`).join("\n")}
|
|
889
|
+
${SIGNATURES.map((s) => `revoke execute on function public.${s} from public, anon, authenticated;`).join("\n")}
|
|
890
|
+
${JSON_FNS.map((s) => `grant execute on function public.${s} to service_role;`).join("\n")}
|
|
891
|
+
${SIGNATURES.map((s) => `grant execute on function public.${s} to service_role;`).join("\n")}
|
|
892
|
+
grant select, insert, update, delete on public.fundraising_campaigns, public.fundraising_pledges, public.fundraising_gifts, public.fundraising_raffles, public.fundraising_ticket_orders to service_role;
|
|
893
|
+
`;
|
|
894
|
+
const down = `${[...SIGNATURES].reverse().map((s) => `drop function public.${s};`).join("\n")}
|
|
895
|
+
${[...JSON_FNS].reverse().map((s) => `drop function public.${s};`).join("\n")}
|
|
896
|
+
drop table public.fundraising_ticket_orders;
|
|
897
|
+
drop table public.fundraising_raffles;
|
|
898
|
+
drop table public.fundraising_gifts;
|
|
899
|
+
drop table public.fundraising_pledges;
|
|
900
|
+
drop table public.fundraising_campaigns;
|
|
901
|
+
`;
|
|
902
|
+
export const fundraisingMigration = { up, down };
|