-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3051e42
commit 63088f7
Showing
7 changed files
with
504 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
"use server"; | ||
|
||
import { notFound } from "next/navigation"; | ||
import { cookies } from "next/headers"; | ||
import { createServerClient } from "@supabase/ssr"; | ||
import { Database, Json } from "@/data/supabaseTypes"; | ||
import { Status } from "@/app/query/servers"; | ||
import NewQueryId from "@/app/query/haikunator"; | ||
|
||
type QueryRow = Database["public"]["Tables"]["queries"]["Row"]; | ||
type QueryType = Database["public"]["Enums"]["query_type"]; | ||
|
||
export interface Query { | ||
uuid: string; | ||
displayId: string; | ||
type: QueryType; | ||
status: Status; | ||
formData: Json; | ||
createdAt: string; | ||
startedAt: string | null; | ||
endedAt: string | null; | ||
} | ||
|
||
function processQueryData(data: QueryRow): Query { | ||
return { | ||
uuid: data.uuid, | ||
displayId: data.display_id, | ||
type: data.type as QueryType, | ||
status: data.status as Status, | ||
formData: data.form_data, | ||
createdAt: data.created_at, | ||
startedAt: data.started_at, | ||
endedAt: data.ended_at, | ||
}; | ||
} | ||
|
||
export async function getQuery(displayId: string): Promise<Query> { | ||
const cookieStore = cookies(); | ||
|
||
const supabase = createServerClient<Database>( | ||
process.env.NEXT_PUBLIC_SUPABASE_URL!, | ||
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, | ||
{ | ||
cookies: { | ||
get(name: string) { | ||
return cookieStore.get(name)?.value; | ||
}, | ||
}, | ||
}, | ||
); | ||
|
||
const { status, data, error } = await supabase | ||
.from("queries") | ||
.select("*") | ||
.eq("display_id", displayId) | ||
.limit(1) | ||
.maybeSingle(); | ||
|
||
if (error) { | ||
console.error(error); | ||
} else if (status === 200) { | ||
if (data) { | ||
return processQueryData(data); | ||
} else { | ||
notFound(); | ||
} | ||
} | ||
throw new Error(`${displayId} not found.`); | ||
} | ||
|
||
export async function createNewQuery( | ||
formData: FormData, | ||
queryType: QueryType, | ||
): Promise<Query> { | ||
const json = JSON.stringify(Object.fromEntries(formData)); | ||
const cookieStore = cookies(); | ||
|
||
const supabase = createServerClient<Database>( | ||
process.env.NEXT_PUBLIC_SUPABASE_URL!, | ||
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, | ||
{ | ||
cookies: { | ||
get(name: string) { | ||
return cookieStore.get(name)?.value; | ||
}, | ||
}, | ||
}, | ||
); | ||
|
||
const newQueryId = NewQueryId(); | ||
|
||
const { data: uniqueDisplayId, error: rpcError } = await supabase.rpc( | ||
"generate_unique_display_id", | ||
{ p_display_id: newQueryId }, | ||
); | ||
|
||
if (rpcError) { | ||
throw new Error(`${rpcError}`); | ||
} | ||
|
||
const { data: queryRow, error: insertError } = await supabase | ||
.from("queries") | ||
.insert({ | ||
display_id: uniqueDisplayId, | ||
status: "QUEUED", | ||
type: queryType, | ||
form_data: json, | ||
}) | ||
.select() | ||
.returns<QueryRow>() | ||
.single(); | ||
|
||
if (insertError) { | ||
throw new Error(`${insertError}`); | ||
} | ||
|
||
const query: Query = processQueryData(queryRow); | ||
return query; | ||
} |
Oops, something went wrong.