From 46ffefbbb91606012cd5bf3a219655076d789d46 Mon Sep 17 00:00:00 2001 From: MartinBraquet Date: Sat, 18 Oct 2025 00:53:35 +0200 Subject: [PATCH] Add anonymous option for votes --- backend/api/src/create-vote.ts | 3 ++- backend/supabase/vote_results.sql | 3 +++ backend/supabase/votes.sql | 1 + common/src/api/schema.ts | 1 + common/src/supabase/schema.ts | 12 ++++++++---- web/components/votes/vote-info.tsx | 14 ++++++++++++++ web/components/votes/vote-item.tsx | 2 +- 7 files changed, 30 insertions(+), 6 deletions(-) diff --git a/backend/api/src/create-vote.ts b/backend/api/src/create-vote.ts index 05fd291..42c0741 100644 --- a/backend/api/src/create-vote.ts +++ b/backend/api/src/create-vote.ts @@ -6,7 +6,7 @@ import { tryCatch } from 'common/util/try-catch' export const createVote: APIHandler< 'create-vote' -> = async ({ title, description }, auth) => { +> = async ({ title, description, isAnonymous }, auth) => { const creator = await getUser(auth.uid) if (!creator) throw new APIError(401, 'Your account was not found') @@ -17,6 +17,7 @@ export const createVote: APIHandler< creator_id: creator.id, title, description, + is_anonymous: isAnonymous, }) ) diff --git a/backend/supabase/vote_results.sql b/backend/supabase/vote_results.sql index 045511a..835bc02 100644 --- a/backend/supabase/vote_results.sql +++ b/backend/supabase/vote_results.sql @@ -34,6 +34,7 @@ DROP INDEX IF EXISTS idx_vote_results_vote_choice; CREATE INDEX idx_vote_results_vote_choice ON vote_results (vote_id, choice); +drop function if exists get_votes_with_results; create or replace function get_votes_with_results() returns table ( id BIGINT, @@ -41,6 +42,7 @@ create or replace function get_votes_with_results() description jsonb, created_time timestamptz, creator_id TEXT, + is_anonymous boolean, votes_for int, votes_against int, votes_abstain int, @@ -53,6 +55,7 @@ SELECT v.description, v.created_time, v.creator_id, + v.is_anonymous, COALESCE(SUM(CASE WHEN r.choice = 1 THEN 1 ELSE 0 END), 0) AS votes_for, COALESCE(SUM(CASE WHEN r.choice = -1 THEN 1 ELSE 0 END), 0) AS votes_against, COALESCE(SUM(CASE WHEN r.choice = 0 THEN 1 ELSE 0 END), 0) AS votes_abstain, diff --git a/backend/supabase/votes.sql b/backend/supabase/votes.sql index 20c2aa5..ca85a98 100644 --- a/backend/supabase/votes.sql +++ b/backend/supabase/votes.sql @@ -3,6 +3,7 @@ CREATE TABLE IF NOT EXISTS votes ( created_time TIMESTAMPTZ DEFAULT now() NOT NULL, creator_id TEXT NOT NULL, title TEXT NOT NULL, + is_anonymous BOOLEAN NOT NULL, description JSONB ); diff --git a/common/src/api/schema.ts b/common/src/api/schema.ts index 028fea5..b18fc2d 100644 --- a/common/src/api/schema.ts +++ b/common/src/api/schema.ts @@ -520,6 +520,7 @@ export const API = (_apiTypeCheck = { returns: {} as any, props: z.object({ title: z.string().min(1), + isAnonymous: z.boolean(), description: contentSchema, }), }, diff --git a/common/src/supabase/schema.ts b/common/src/supabase/schema.ts index 024e06e..c1d46bf 100644 --- a/common/src/supabase/schema.ts +++ b/common/src/supabase/schema.ts @@ -10,7 +10,7 @@ export type Database = { // Allows to automatically instantiate createClient with right options // instead of createClient(URL, KEY) __InternalSupabase: { - PostgrestVersion: '13.0.5' + PostgrestVersion: '13.0.4' } public: { Tables: { @@ -471,7 +471,7 @@ export type Database = { geodb_city_id?: string | null has_kids?: number | null height_in_inches?: number | null - id?: never + id?: number is_smoker?: boolean | null is_vegetarian_or_vegan?: boolean | null last_modification_time?: string @@ -519,7 +519,7 @@ export type Database = { geodb_city_id?: string | null has_kids?: number | null height_in_inches?: number | null - id?: never + id?: number is_smoker?: boolean | null is_vegetarian_or_vegan?: boolean | null last_modification_time?: string @@ -748,6 +748,7 @@ export type Database = { creator_id: string description: Json | null id: number + is_anonymous: boolean | null title: string } Insert: { @@ -755,6 +756,7 @@ export type Database = { creator_id: string description?: Json | null id?: never + is_anonymous?: boolean | null title: string } Update: { @@ -762,6 +764,7 @@ export type Database = { creator_id?: string description?: Json | null id?: never + is_anonymous?: boolean | null title?: string } Relationships: [ @@ -814,6 +817,7 @@ export type Database = { creator_id: string description: Json id: number + is_anonymous: boolean priority: number title: string votes_abstain: number @@ -874,7 +878,7 @@ export type Database = { Returns: Json } ts_to_millis: { - Args: { ts: string } + Args: { ts: string } | { ts: string } Returns: number } } diff --git a/web/components/votes/vote-info.tsx b/web/components/votes/vote-info.tsx index 919782f..1eb031f 100644 --- a/web/components/votes/vote-info.tsx +++ b/web/components/votes/vote-info.tsx @@ -25,6 +25,7 @@ export function VoteComponent() { const [title, setTitle] = useState('') const [editor, setEditor] = useState(null) + const [isAnonymous, setIsAnonymous] = useState(false) const hideButton = title.length == 0 @@ -53,6 +54,16 @@ export function VoteComponent() { setEditor(e)} /> + + setIsAnonymous(e.target.checked)} + className="h-4 w-4 rounded-md border-gray-300 text-blue-600 focus:ring-blue-500" + /> + + {!hideButton && (