mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-01-24 21:57:53 -05:00
29 lines
817 B
TypeScript
29 lines
817 B
TypeScript
import { createSupabaseDirectClient } from 'shared/supabase/init'
|
|
import { getUser } from 'shared/utils'
|
|
import { APIHandler, APIError } from './helpers/endpoint'
|
|
import { insert } from 'shared/supabase/utils'
|
|
import { tryCatch } from 'common/util/try-catch'
|
|
|
|
export const createVote: APIHandler<
|
|
'create-vote'
|
|
> = async ({ title, description, isAnonymous }, auth) => {
|
|
const creator = await getUser(auth.uid)
|
|
if (!creator) throw new APIError(401, 'Your account was not found')
|
|
|
|
const pg = createSupabaseDirectClient()
|
|
|
|
const { data, error } = await tryCatch(
|
|
insert(pg, 'votes', {
|
|
creator_id: creator.id,
|
|
title,
|
|
description,
|
|
is_anonymous: isAnonymous,
|
|
status: 'voting_open',
|
|
})
|
|
)
|
|
|
|
if (error) throw new APIError(401, 'Error creating question')
|
|
|
|
return { data }
|
|
}
|