Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

3.8 KB
Raw Blame
CREATE OR REPLACE FUNCTION public.finish_automation_send (
  p_id              text,
  p_attempt_id      text,
  p_status          text,
  p_outcome         text,
  p_reason          text                     DEFAULT NULL::text,
  p_error_message   text                     DEFAULT NULL::text,
  p_message_id      text                     DEFAULT NULL::text,
  p_error_code      integer                  DEFAULT NULL::integer,
  p_error_subcode   integer                  DEFAULT NULL::integer,
  p_http_status     integer                  DEFAULT NULL::integer,
  p_next_attempt_at timestamp with time zone DEFAULT NULL::timestamp WITH time zone,
  p_cooldown_until  timestamp with time zone DEFAULT NULL::timestamp WITH time zone,
  p_cooldown_reason text                     DEFAULT NULL::text,
  p_pause_reason    text                     DEFAULT NULL::text,
  p_recipient_id    text                     DEFAULT NULL::text,
  p_gate_status     text                     DEFAULT NULL::text
)
  RETURNS boolean
  LANGUAGE plpgsql
  SET search_path TO ''
  AS $function$
declare
    job public.automation_comments%rowtype;
begin
    update public.automation_comments c
    set status = p_status,
        status_reason = p_reason,
        error_message = p_error_message,
        message_id = coalesce(p_message_id, c.message_id),
        accepted_at = case when p_status = 'accepted' then now() else c.accepted_at end,
        recipient_id = coalesce(p_recipient_id, c.recipient_id),
        gate_status = coalesce(p_gate_status, c.gate_status),
        next_attempt_at = p_next_attempt_at,
        claimed_at = null
    where c.id = p_id and (c.status = 'sending' or (c.status = 'needs_review' and c.status_reason = 'interrupted'))
    returning * into job;
    -- a comment already settled keeps the attempt that settled it
    if not found then
        return false;
    end if;

    update public.automation_attempts t
    set outcome = p_outcome,
        error_code = p_error_code,
        error_subcode = p_error_subcode,
        http_status = p_http_status,
        finished_at = now()
    where t.id = p_attempt_id and t.automation_comment_id = p_id;

    if p_cooldown_until is not null then
        insert into public.automation_accounts (account_id, user_id, cooldown_until, cooldown_reason)
        values (job.account_id, job.user_id, p_cooldown_until, p_cooldown_reason)
        on conflict (account_id) do update
        set cooldown_until = greatest(coalesce(public.automation_accounts.cooldown_until, excluded.cooldown_until), excluded.cooldown_until),
            cooldown_reason = excluded.cooldown_reason;
    end if;

    if p_pause_reason is not null then
        insert into public.automation_accounts (account_id, user_id, paused, paused_reason)
        values (job.account_id, job.user_id, true, p_pause_reason)
        on conflict (account_id) do update set paused = true, paused_reason = excluded.paused_reason;
    end if;
    return true;
end;
$function$;

GRANT EXECUTE
  ON FUNCTION "public"."finish_automation_send"(text, text, text, text, text, text, text, integer, integer, integer, timestamp WITH time zone, timestamp
    WITH time zone, text, text, text, text)
  TO "service_role";

REVOKE ALL
  ON FUNCTION "public"."finish_automation_send"(text, text, text, text, text, text, text, integer, integer, integer, timestamp WITH time zone, timestamp
    WITH time zone, text, text, text, text)
  FROM "postgres";

GRANT EXECUTE
  ON FUNCTION "public"."finish_automation_send"(text, text, text, text, text, text, text, integer, integer, integer, timestamp WITH time zone, timestamp
    WITH time zone, text, text, text, text)
  TO "postgres";

REVOKE ALL
  ON FUNCTION "public"."finish_automation_send"(text, text, text, text, text, text, text, integer, integer, integer, timestamp WITH time zone, timestamp
    WITH time zone, text, text, text, text)
  FROM PUBLIC, "anon", "authenticated";