|
| 1 | +-- Ad network: a self-deal click is free-tier delivery, not a paid click that |
| 2 | +-- happened to fail. |
| 3 | +-- |
| 4 | +-- ad_charge_click refuses to bill when the same profile owns the slot and the |
| 5 | +-- campaign — correct, there is no money to move. But that branch recorded the |
| 6 | +-- row as `valid=false, tier='paid'`, and reporting reads exactly two kinds of |
| 7 | +-- click: |
| 8 | +-- |
| 9 | +-- clicks = valid -- billed |
| 10 | +-- free_clicks = not valid and tier='free' -- real, unbillable |
| 11 | +-- |
| 12 | +-- `not valid and tier='paid'` is neither, and it is what the bot/duplicate/ |
| 13 | +-- forged path writes. So every self-deal click landed in the bucket reserved |
| 14 | +-- for fraud and disappeared from the dashboard entirely. The two branches |
| 15 | +-- either side of it already write 'free' for the same situation — a real click |
| 16 | +-- nobody can be charged for — so this was an inconsistency, not a policy. |
| 17 | +-- |
| 18 | +-- serveAd makes the matching call on the impression side: a self-owned |
| 19 | +-- campaign is demoted to the free tier rather than dropped (see the comment in |
| 20 | +-- lib/ads/serve.ts). This aligns the click side with it. |
| 21 | +-- |
| 22 | +-- Backfill included, because the misclassification is recent and total: while |
| 23 | +-- every slot and every campaign belong to one account, 100% of clicks take |
| 24 | +-- this branch, and 661 of them are currently invisible. The update is scoped |
| 25 | +-- narrowly enough not to touch a genuine fraud row: |
| 26 | +-- |
| 27 | +-- * valid = false and tier = 'paid' — the only rows in the wrong bucket; |
| 28 | +-- * charged_cents = 0 — never move a row that billed; |
| 29 | +-- * slot owner = campaign owner — the self-deal condition itself; |
| 30 | +-- * device is distinct from 'bot' — bots are rejected before this branch, |
| 31 | +-- so a bot row can only have come from |
| 32 | +-- the fraud path in resolveClick. |
| 33 | +-- |
| 34 | +-- Duplicate-click rows cannot be caught by mistake: that check requires an |
| 35 | +-- existing valid=true click on the campaign inside 6h, and the branch being |
| 36 | +-- fixed here is precisely why no such click exists. |
| 37 | + |
| 38 | +create or replace function public.ad_charge_click( |
| 39 | + p_campaign uuid, |
| 40 | + p_slot uuid, |
| 41 | + p_creative uuid, |
| 42 | + p_impression uuid, |
| 43 | + p_visitor text, |
| 44 | + p_ip_hash text, |
| 45 | + p_country text, |
| 46 | + p_device text, |
| 47 | + p_cpc_credits integer, |
| 48 | + p_platform_rate numeric |
| 49 | +) |
| 50 | +returns table(click_id uuid, charged_cents integer, publisher_earn_cents integer, valid boolean) |
| 51 | +language plpgsql |
| 52 | +security definer |
| 53 | +set search_path to 'public' |
| 54 | +as $function$ |
| 55 | +declare |
| 56 | + v_owner uuid; |
| 57 | + v_status text; |
| 58 | + v_daily int; |
| 59 | + v_spend int; |
| 60 | + v_date date; |
| 61 | + v_paid int; |
| 62 | + v_bonus int; |
| 63 | + v_promo int; |
| 64 | + v_from_bonus int; |
| 65 | + v_from_promo int; |
| 66 | + v_from_cash int; |
| 67 | + v_rest int; |
| 68 | + v_slot_owner uuid; |
| 69 | + v_charged int; |
| 70 | + v_earn int; |
| 71 | + v_cut int; |
| 72 | + v_click uuid; |
| 73 | + v_rack_cents constant int := 5; |
| 74 | + v_floor_cents constant numeric := 2.0; |
| 75 | +begin |
| 76 | + select owner_id, status, daily_budget_cents, spend_today_cents, spend_date |
| 77 | + into v_owner, v_status, v_daily, v_spend, v_date |
| 78 | + from public.ad_campaigns where id = p_campaign for update; |
| 79 | + if not found then return; end if; |
| 80 | + |
| 81 | + v_charged := p_cpc_credits * v_rack_cents; |
| 82 | + if v_date is distinct from current_date then v_spend := 0; end if; |
| 83 | + |
| 84 | + -- Paused / archived campaign: this click should not have been servable at |
| 85 | + -- all, so it stays out of the free-tier figures as well as the paid ones. |
| 86 | + if v_status not in ('active', 'exhausted') then |
| 87 | + insert into public.ad_clicks(impression_id,slot_id,campaign_id,creative_id,visitor_id,ip_hash,geo_country,device,charged_cents,publisher_earn_cents,platform_cut_cents,valid,tier) |
| 88 | + values (p_impression,p_slot,p_campaign,p_creative,p_visitor,p_ip_hash,p_country,p_device,0,0,0,false,'paid') |
| 89 | + returning id into v_click; |
| 90 | + return query select v_click, 0, 0, false; |
| 91 | + return; |
| 92 | + end if; |
| 93 | + |
| 94 | + select owner_id into v_slot_owner from public.ad_slots where id = p_slot; |
| 95 | + |
| 96 | + -- Self-deal: one account on both sides, so nothing is billed and nothing is |
| 97 | + -- earned. Real delivery all the same — free tier, same as the two branches |
| 98 | + -- below. |
| 99 | + if v_slot_owner is not null and v_slot_owner = v_owner then |
| 100 | + insert into public.ad_clicks(impression_id,slot_id,campaign_id,creative_id,visitor_id,ip_hash,geo_country,device,charged_cents,publisher_earn_cents,platform_cut_cents,valid,tier) |
| 101 | + values (p_impression,p_slot,p_campaign,p_creative,p_visitor,p_ip_hash,p_country,p_device,0,0,0,false,'free') |
| 102 | + returning id into v_click; |
| 103 | + return query select v_click, 0, 0, false; |
| 104 | + return; |
| 105 | + end if; |
| 106 | + |
| 107 | + if (v_spend + v_charged) > v_daily then |
| 108 | + insert into public.ad_clicks(impression_id,slot_id,campaign_id,creative_id,visitor_id,ip_hash,geo_country,device,charged_cents,publisher_earn_cents,platform_cut_cents,valid,tier) |
| 109 | + values (p_impression,p_slot,p_campaign,p_creative,p_visitor,p_ip_hash,p_country,p_device,0,0,0,false,'free') |
| 110 | + returning id into v_click; |
| 111 | + return query select v_click, 0, 0, false; |
| 112 | + return; |
| 113 | + end if; |
| 114 | + |
| 115 | + select credits_balance, |
| 116 | + coalesce(ad_bonus_credits, 0), |
| 117 | + least(coalesce(promo_credits, 0), credits_balance) |
| 118 | + into v_paid, v_bonus, v_promo |
| 119 | + from public.profiles where id = v_owner for update; |
| 120 | + |
| 121 | + if coalesce(v_paid, 0) + coalesce(v_bonus, 0) < p_cpc_credits then |
| 122 | + insert into public.ad_clicks(impression_id,slot_id,campaign_id,creative_id,visitor_id,ip_hash,geo_country,device,charged_cents,publisher_earn_cents,platform_cut_cents,valid,tier) |
| 123 | + values (p_impression,p_slot,p_campaign,p_creative,p_visitor,p_ip_hash,p_country,p_device,0,0,0,false,'free') |
| 124 | + returning id into v_click; |
| 125 | + return query select v_click, 0, 0, false; |
| 126 | + return; |
| 127 | + end if; |
| 128 | + |
| 129 | + v_from_bonus := least(v_bonus, p_cpc_credits); |
| 130 | + v_rest := p_cpc_credits - v_from_bonus; |
| 131 | + v_from_promo := least(v_promo, v_rest); |
| 132 | + v_from_cash := v_rest - v_from_promo; |
| 133 | + |
| 134 | + update public.profiles |
| 135 | + set ad_bonus_credits = ad_bonus_credits - v_from_bonus, |
| 136 | + credits_balance = credits_balance - (v_from_promo + v_from_cash), |
| 137 | + promo_credits = greatest(0, coalesce(promo_credits, 0) - v_from_promo) |
| 138 | + where id = v_owner; |
| 139 | + |
| 140 | + v_earn := floor(v_from_cash * (1 - p_platform_rate) * v_floor_cents); |
| 141 | + v_cut := v_charged - v_earn; |
| 142 | + |
| 143 | + update public.ad_campaigns |
| 144 | + set spend_today_cents = v_spend + v_charged, |
| 145 | + spend_date = current_date, |
| 146 | + total_spent_cents = coalesce(total_spent_cents,0) + v_charged |
| 147 | + where id = p_campaign; |
| 148 | + |
| 149 | + insert into public.ad_clicks(impression_id,slot_id,campaign_id,creative_id,visitor_id,ip_hash,geo_country,device,charged_cents,publisher_earn_cents,platform_cut_cents,valid,tier) |
| 150 | + values (p_impression,p_slot,p_campaign,p_creative,p_visitor,p_ip_hash,p_country,p_device,v_charged,v_earn,v_cut,true,'paid') |
| 151 | + returning id into v_click; |
| 152 | + |
| 153 | + if v_slot_owner is not null and v_earn > 0 then |
| 154 | + insert into public.ad_ledger(kind, owner_id, campaign_id, slot_id, amount_cents, ref_click_id) |
| 155 | + values ('publisher_accrual', v_slot_owner, p_campaign, p_slot, v_earn, v_click); |
| 156 | + end if; |
| 157 | + if v_cut > 0 then |
| 158 | + insert into public.ad_ledger(kind, owner_id, campaign_id, slot_id, amount_cents, ref_click_id) |
| 159 | + values ('platform_fee', null, p_campaign, p_slot, v_cut, v_click); |
| 160 | + end if; |
| 161 | + |
| 162 | + return query select v_click, v_charged, v_earn, true; |
| 163 | +end $function$; |
| 164 | + |
| 165 | +-- create or replace keeps the existing ACL, but state it anyway so a fresh |
| 166 | +-- database ends up where 20260731160000_ad_rpc_revoke_public.sql left this one: |
| 167 | +-- no PUBLIC execute on a security-definer money function. |
| 168 | +revoke execute on function public.ad_charge_click(uuid, uuid, uuid, uuid, text, text, text, text, integer, numeric) from public; |
| 169 | +grant execute on function public.ad_charge_click(uuid, uuid, uuid, uuid, text, text, text, text, integer, numeric) to service_role; |
| 170 | + |
| 171 | +-- Reclassify the rows the old branch mislabelled. See the header for why each |
| 172 | +-- clause is here; together they select self-deal clicks and nothing else. |
| 173 | +update public.ad_clicks c |
| 174 | + set tier = 'free' |
| 175 | + from public.ad_campaigns camp, public.ad_slots s |
| 176 | + where c.campaign_id = camp.id |
| 177 | + and c.slot_id = s.id |
| 178 | + and s.owner_id = camp.owner_id |
| 179 | + and c.valid = false |
| 180 | + and c.tier = 'paid' |
| 181 | + and c.charged_cents = 0 |
| 182 | + and c.device is distinct from 'bot'; |
0 commit comments