Skip to content

Commit

Permalink
Merge pull request #196 from mikepsinn/develop
Browse files Browse the repository at this point in the history
Conversation2Measurements, global variables, generic variables, measurements, etc.
  • Loading branch information
mikepsinn authored Apr 30, 2024
2 parents f15c528 + 612d5e1 commit b217262
Show file tree
Hide file tree
Showing 47 changed files with 1,030 additions and 108 deletions.
2 changes: 1 addition & 1 deletion apps/nextjs/app/api/chat-with-vision/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import OpenAI from 'openai';
import { OpenAIStream, StreamingTextResponse } from 'ai';

// Create an OpenAI API client (that's edge friendly!)
// Create an OpenAI API client (that's edge-friendly!)
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY || '',
});
Expand Down
37 changes: 37 additions & 0 deletions apps/nextjs/app/api/conversation2measurements/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { NextRequest, NextResponse } from 'next/server';
import { conversation2measurements } from "@/lib/conversation2measurements";
import {postMeasurements} from "@/lib/dfda";
import {getUserId} from "@/lib/getUserId";

export async function POST(request: NextRequest) {
let { statement, localDateTime, previousStatements } = await request.json();

try {
const measurements = await conversation2measurements(statement, localDateTime, previousStatements);
const userId = await getUserId();
if(userId){
await postMeasurements(measurements, userId)
}
return NextResponse.json({ success: true, measurements: measurements });
} catch (error) {
console.error('Error in conversation2measurements:', error);
return NextResponse.json({ success: false, message: 'Error in conversation2measurements' });
}
}

export async function GET(req: NextRequest) {
const urlParams = Object.fromEntries(new URL(req.url).searchParams);
const statement = urlParams.statement as string;
const previousStatements = urlParams.previousStatements as string | null | undefined;
const localDateTime = urlParams.localDateTime as string | null | undefined;

try {
const measurements = await conversation2measurements(statement, localDateTime, previousStatements);
const userId = await getUserId();
if(userId){await postMeasurements(measurements, userId)}
return NextResponse.json({ success: true, measurements: measurements });
} catch (error) {
console.error('Error sending request to OpenAI:', error);
return NextResponse.json({ success: false, message: 'Error sending request to OpenAI' });
}
}
12 changes: 5 additions & 7 deletions apps/nextjs/app/api/dfda/[dfdaPath]/route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { getServerSession } from 'next-auth/next';
import { z } from 'zod';

import { authOptions } from '@/lib/auth';
import { handleError } from '@/lib/errorHandler';
import {dfdaGET, dfdaPOST} from '@/lib/dfda';
import {getUserId} from "@/lib/getUserId";

const routeContextSchema = z.object({
params: z.object({
Expand All @@ -14,9 +12,9 @@ const routeContextSchema = z.object({
export async function GET(req: Request, context: z.infer<typeof routeContextSchema>) {
const { params } = routeContextSchema.parse(context);
const urlParams = Object.fromEntries(new URL(req.url).searchParams);
const session = await getServerSession(authOptions);
const userId = await getUserId()
try {
const response = await dfdaGET(params.dfdaPath, urlParams, session?.user.id);
const response = await dfdaGET(params.dfdaPath, urlParams, userId);
const data = response.data ?? response;
return new Response(JSON.stringify(data), { status: 200, headers: { 'Content-Type': 'application/json' } });
} catch (error) {
Expand All @@ -28,9 +26,9 @@ export async function POST(req: Request, context: z.infer<typeof routeContextSch
const { params } = routeContextSchema.parse(context);
const urlParams = Object.fromEntries(new URL(req.url).searchParams);
const body = await req.json();
const session = await getServerSession(authOptions);
const userId = await getUserId()
try {
const response = await dfdaPOST(params.dfdaPath, body, session?.user.id, urlParams);
const response = await dfdaPOST(params.dfdaPath, body, userId, urlParams);
const data = response.data ?? response;
return new Response(JSON.stringify(data), { status: response.status, headers: { 'Content-Type': 'application/json' } });
} catch (error) {
Expand Down
13 changes: 6 additions & 7 deletions apps/nextjs/app/api/text2measurements/route.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { NextRequest, NextResponse } from 'next/server';
import { text2measurements } from "@/lib/text2measurements";
import {dfdaPOST} from "@/lib/dfda";
import {getServerSession} from "next-auth/next";
import {authOptions} from "@/lib/auth";
import { postMeasurements} from "@/lib/dfda";
import {getUserId} from "@/lib/getUserId";

export async function POST(request: NextRequest) {
let { statement, localDateTime, text } = await request.json();
statement = statement || text;

try {
const measurements = await text2measurements(statement, localDateTime);
const session = await getServerSession(authOptions);
const userId = session?.user.id;
const userId = await getUserId();
if(userId){
await dfdaPOST('/v3/measurements', measurements, session?.user.id);
await postMeasurements(measurements, userId)
}
return NextResponse.json({ success: true, measurements: measurements });
} catch (error) {
Expand All @@ -30,7 +28,8 @@ export async function GET(req: NextRequest) {

try {
const measurements = await text2measurements(statement, localDateTime);
// If you want to save them, uncomment await dfdaPOST('/v3/measurements', measurements, session?.user.id);
const userId = await getUserId();
await postMeasurements(measurements, userId);
return NextResponse.json({ success: true, measurements: measurements });
} catch (error) {
console.error('Error sending request to OpenAI:', error);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Metadata } from "next"
import { notFound, redirect } from "next/navigation"

import { authOptions } from "@/lib/auth"
import { getCurrentUser } from "@/lib/session"
import { Shell } from "@/components/layout/shell"
import { DashboardHeader } from "@/components/pages/dashboard/dashboard-header"
import { GlobalVariableCharts } from '@/components/globalVariables/global-variable-charts';

export const metadata: Metadata = {
title: "Global Variable Charts",
}

interface GlobalVariableEditProps {
params: { variableId: string }
}

export default async function GlobalVariableChart({ params }: GlobalVariableEditProps) {
const user = await getCurrentUser()

if (!user) {
redirect(authOptions?.pages?.signIn || "/signin")
}
const variableId = parseInt(params.variableId)
return (
<Shell>
<div className="grid grid-cols-1 gap-10">
<GlobalVariableCharts
variableId={variableId}
/>
</div>
</Shell>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Link from "next/link"

import { buttonVariants } from "@/components/ui/button"
import { EmptyPlaceholder } from "@/components/empty-placeholder"
import { Icons } from "@/components/icons"

export default function NotFound() {
return (
<div className="flex items-center justify-center md:p-4 lg:p-8">
<EmptyPlaceholder className="mx-auto max-w-[800px]">
<div className="flex h-20 w-20 items-center justify-center rounded-full bg-muted">
<Icons.warning className="h-10 w-10" />
</div>
<EmptyPlaceholder.Title>Not Found</EmptyPlaceholder.Title>
<EmptyPlaceholder.Description>
This globalVariable could not be found. Please try again.
</EmptyPlaceholder.Description>
<Link
href="/dashboard"
className={buttonVariants({ variant: "outline" })}
>
Go to Dashboard
</Link>
</EmptyPlaceholder>
</div>
)
}
51 changes: 51 additions & 0 deletions apps/nextjs/app/dashboard/globalVariables/[variableId]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Metadata } from "next"
import { redirect } from "next/navigation"

import { authOptions } from "@/lib/auth"
import { getCurrentUser } from "@/lib/session"
import { Shell } from "@/components/layout/shell"
import { GlobalVariableOverview } from "@/components/globalVariables/global-variable-overview";


interface GlobalVariablePageProps {
params: { variableId: number }
searchParams: { from: string; to: string }
}

// export async function generateMetadata({
// params,
// }: GlobalVariablePageProps): Promise<Metadata> {
// const user = await getCurrentUser()
//
// if (!user) {
// redirect(authOptions?.pages?.signIn || "/signin")
// }
// const response = await fetch(`/api/dfda/globalVariables?variableId=${params.variableId}&includeCharts=0`);
// const globalVariables = await response.json();
// const globalVariable = globalVariables[0];
//
// return {
// title: globalVariable?.name || "Not Found",
// description: globalVariable?.description,
// }
// }

export default async function GlobalVariablePage({
params,
searchParams,
}: GlobalVariablePageProps) {
const user = await getCurrentUser()

if (!user) {
redirect(authOptions?.pages?.signIn || "/signin")
}

return (
<Shell>
<GlobalVariableOverview variableId={params.variableId} user={user} measurementsDateRange={{
from: searchParams.from,
to: searchParams.to
}} />
</Shell>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Metadata } from "next"
import { notFound, redirect } from "next/navigation"

import { authOptions } from "@/lib/auth"
import { getCurrentUser } from "@/lib/session"
import { GlobalVariableEditForm } from "@/components/globalVariables/global-variable-edit-form"
import { Shell } from "@/components/layout/shell"
import { DashboardHeader } from "@/components/pages/dashboard/dashboard-header"

export const metadata: Metadata = {
title: "GlobalVariable Settings",
}

interface GlobalVariableEditProps {
params: { variableId: string }
}

export default async function GlobalVariableEdit({ params }: GlobalVariableEditProps) {
const user = await getCurrentUser()

if (!user) {
redirect(authOptions?.pages?.signIn || "/signin")
}

const response = await fetch(
`/api/dfda/globalVariables?variableId=${params.variableId}&includeCharts=0`)
const globalVariables = await response.json()
const globalVariable = globalVariables[0]

if (!globalVariable) {
notFound()
}

return (
<Shell>
<DashboardHeader
heading={globalVariable.name + " Settings"}
text="Modify globalVariable details."
/>
<div className="grid grid-cols-1 gap-10">
<GlobalVariableEditForm
globalVariable={{
id: globalVariable.id,
name: globalVariable.name,
description: globalVariable.description
}}
/>
</div>
</Shell>
)
}
41 changes: 41 additions & 0 deletions apps/nextjs/app/dashboard/globalVariables/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Metadata } from "next"
import { redirect } from "next/navigation"

import { authOptions } from "@/lib/auth"
import { getCurrentUser } from "@/lib/session"
import { GlobalVariableAddButton } from "@/components/globalVariables/global-variable-add-button"
import { Shell } from "@/components/layout/shell"
import { DashboardHeader } from "@/components/pages/dashboard/dashboard-header"
import {GenericVariableList} from "@/components/genericVariables/generic-variable-list";


export const metadata: Metadata = {
title: "Your Variables",
description: "Manage your treatments, symptoms, and other variables.",
}

export default async function GlobalVariablesPage() {
const user = await getCurrentUser()

if (!user) {
redirect(authOptions?.pages?.signIn || "/signin")
}

// Define search parameters
const searchParams = {
includePublic: false,
sort: 'createdAt',
limit: 10,
offset: 0,
searchPhrase: "",
};

return (
<Shell>
<DashboardHeader heading="Your Variables" text="Manage your treatments, symptoms, and other variables.">
<GlobalVariableAddButton />
</DashboardHeader>
<GenericVariableList user={user} searchParams={searchParams} />
</Shell>
)
}
2 changes: 1 addition & 1 deletion apps/nextjs/app/dashboard/measurements/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getCurrentUser } from "@/lib/session"
import { Shell } from "@/components/layout/shell"
import {DashboardHeader} from "@/components/pages/dashboard/dashboard-header";
import {DateRangePicker} from "@/components/date-range-picker";
import {MeasurementsList} from "@/components/userVariable/measurements/measurements-list";
import {MeasurementsList} from "@/components/measurements/measurements-list";


interface MeasurementsPageProps {
Expand Down
6 changes: 3 additions & 3 deletions apps/nextjs/app/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getCurrentUser } from "@/lib/session"

import { Shell } from "@/components/layout/shell"
import { DashboardHeader } from "@/components/pages/dashboard/dashboard-header"
import {UserVariableSearch} from "@/components/userVariable/user-variable-search";
import {GenericVariableSearch} from "@/components/genericVariables/generic-variable-search";

export const metadata: Metadata = {
title: "Dashboard",
Expand All @@ -25,10 +25,10 @@ export default async function Dashboard() {

return (
<Shell>
<DashboardHeader heading="Your Data" text="Monitor your symptoms and factors.">
<DashboardHeader heading="Variables" text="Record measurements, add reminders or view your measurement history">
</DashboardHeader>
<div className={layout}>
<UserVariableSearch user={user}/>
<GenericVariableSearch user={user}/>
</div>
</Shell>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { authOptions } from "@/lib/auth"
import { getCurrentUser } from "@/lib/session"
import { Shell } from "@/components/layout/shell"
import { DashboardHeader } from "@/components/pages/dashboard/dashboard-header"
import { UserVariableCharts } from '@/components/userVariable/user-variable-charts';
import { UserVariableCharts } from '@/components/userVariables/user-variable-charts';

export const metadata: Metadata = {
title: "UserVariable Charts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { redirect } from "next/navigation"
import { authOptions } from "@/lib/auth"
import { getCurrentUser } from "@/lib/session"
import { Shell } from "@/components/layout/shell"
import { UserVariableOverview } from "@/components/userVariable/user-variable-overview";
import { UserVariableOverview } from "@/components/userVariables/user-variable-overview";


interface UserVariablePageProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { notFound, redirect } from "next/navigation"

import { authOptions } from "@/lib/auth"
import { getCurrentUser } from "@/lib/session"
import { UserVariableEditForm } from "@/components/userVariable/user-variable-edit-form"
import { UserVariableEditForm } from "@/components/userVariables/user-variable-edit-form"
import { Shell } from "@/components/layout/shell"
import { DashboardHeader } from "@/components/pages/dashboard/dashboard-header"

Expand Down
Loading

0 comments on commit b217262

Please sign in to comment.