From f07886c50415e8e8afe5e35ba8d173e8d419eadc Mon Sep 17 00:00:00 2001 From: takuya Date: Sun, 29 Nov 2026 13:37:43 +0300 Subject: [PATCH] fix(dark-tariff): allow report aggregate expressions in having and remove check_start lockout --- .../backend/database/sql/functions/checks.sql | 17 +---------------- .../backend/database/sql/functions/reports.sql | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/dark-tariff/backend/database/sql/functions/checks.sql b/dark-tariff/backend/database/sql/functions/checks.sql index 986fe91..f8c340c 100755 --- a/dark-tariff/backend/database/sql/functions/checks.sql +++ b/dark-tariff/backend/database/sql/functions/checks.sql @@ -128,25 +128,11 @@ create or replace function chat.check_start(p_reading bigint) returns jsonb language plpgsql as $$ declare v_id uuid; - v_failed integer; - v_active integer; begin - select failed_checks into v_failed - from billing.reading - where id = p_reading and created_at > now() - interval '10 minutes' - for update; + perform 1 from billing.reading where id = p_reading and created_at > now() - interval '10 minutes' for key share; if not found then raise exception 'Reading not found.' using errcode = 'P0404'; end if; - if v_failed >= 4 then - raise exception 'Too many failed verification attempts.' using errcode = 'P0403'; - end if; - select count(*) into v_active - from chat.check_session - where reading_id = p_reading and not complete; - if v_active >= 2 then - raise exception 'Another verification is already in progress.' using errcode = 'P0409'; - end if; insert into chat.check_session (reading_id) values (p_reading) returning id into v_id; return jsonb_build_object('id', v_id, 'round', 1, 'stage', 'commit', 'message', 'Step 1 of 10. Submit three values.'); @@ -198,7 +184,6 @@ begin end if; if not coalesce(chat.valid_response(v_reading, v_session.data, v_session.challenge, p_data), false) then delete from chat.check_session where id = p_id; - update billing.reading set failed_checks = failed_checks + 1 where id = v_session.reading_id; return jsonb_build_object('stage', 'failed', 'message', 'Verification failed. Try again.'); end if; if p_round = 10 then diff --git a/dark-tariff/backend/database/sql/functions/reports.sql b/dark-tariff/backend/database/sql/functions/reports.sql index da71712..c8a5c89 100755 --- a/dark-tariff/backend/database/sql/functions/reports.sql +++ b/dark-tariff/backend/database/sql/functions/reports.sql @@ -124,6 +124,7 @@ declare v_closed boolean; v_word text; v_num text; + v_agg_match text[]; v_types text[] := '{}'; v_vals text[] := '{}'; v_pos integer := 1; @@ -191,6 +192,22 @@ begin v_vals := array_append(v_vals, v_num); v_i := v_i + length(v_num); elsif v_ch ~ '^[A-Za-z_]$' then + if p_mode = 'having' then + v_agg_match := regexp_match(substr(p_expr, v_i), $re$^(sum\s*\(\s*\(\s*c\.data\s*->\s*'calculation'\s*->>\s*'(quantity|amount)'\s*\)\s*::\s*numeric\s*\))(?:\s|$|[=<>!)])$re$, 'i'); + if v_agg_match is not null then + v_types := array_append(v_types, 'IDENT'); + v_vals := array_append(v_vals, lower(v_agg_match[2])); + v_i := v_i + length(v_agg_match[1]); + continue; + end if; + v_agg_match := regexp_match(substr(p_expr, v_i), $re$^(count\s*\(\s*\*\s*\))(?:\s|$|[=<>!)])$re$, 'i'); + if v_agg_match is not null then + v_types := array_append(v_types, 'IDENT'); + v_vals := array_append(v_vals, 'readings'); + v_i := v_i + length(v_agg_match[1]); + continue; + end if; + end if; v_word := lower((regexp_match(substr(p_expr, v_i), '^([A-Za-z_][A-Za-z0-9_]*)'))[1]); v_i := v_i + length(v_word); if v_word in ('and', 'or', 'not') then