Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 23 additions & 67 deletions app/api/family-notes/[family]/route.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,6 @@
import { NextRequest, NextResponse } from 'next/server';
import { auth } from '@/lib/auth';
import { getGraphQLClient } from '@/lib/graphql/client';

interface FamilyResearchNote {
id: string;
familyName: string;
content: string;
updatedAt: string;
}

interface FamilyNoteQueryResponse {
familyResearchNote: FamilyResearchNote | null;
}

interface UpsertFamilyNoteMutationResponse {
upsertFamilyResearchNote: FamilyResearchNote;
}

const FAMILY_NOTE_QUERY = `
query FamilyResearchNote($familyName: String!) {
familyResearchNote(familyName: $familyName) {
id
familyName
content
updatedAt
}
}
`;

const UPSERT_FAMILY_NOTE_MUTATION = `
mutation UpsertFamilyResearchNote($input: FamilyResearchNoteInput!) {
upsertFamilyResearchNote(input: $input) {
id
familyName
content
updatedAt
}
}
`;
import { pool } from '@/lib/pool';

export async function GET(
request: NextRequest,
Expand All @@ -50,30 +13,25 @@ export async function GET(
}

const { family } = await params;
const client = getGraphQLClient();
const { data, errors } = await client.request<FamilyNoteQueryResponse>(FAMILY_NOTE_QUERY, {
familyName: family.toUpperCase(),
});
const familyName = family.toUpperCase();

if (errors) {
console.error('GraphQL errors:', errors);
return NextResponse.json(
{ error: 'Failed to fetch family note' },
{ status: 500 }
);
}
const { rows } = await pool.query(
`SELECT family_name, content, updated_at FROM family_research_notes WHERE family_name = $1`,
[familyName]
);

if (!data?.familyResearchNote) {
if (rows.length === 0) {
return NextResponse.json(
{ error: 'Family notes not found' },
{ status: 404 }
);
}

const note = rows[0];
return NextResponse.json({
family: data.familyResearchNote.familyName,
content: data.familyResearchNote.content,
lastModified: data.familyResearchNote.updatedAt,
family: note.family_name,
content: note.content,
lastModified: note.updated_at.toISOString(),
});
} catch (error: any) {
console.error('Error reading family notes:', error);
Expand All @@ -95,6 +53,7 @@ export async function PUT(
}

const { family } = await params;
const familyName = family.toUpperCase();
const { content } = await request.json();

if (typeof content !== 'string') {
Expand All @@ -104,27 +63,24 @@ export async function PUT(
);
}

const client = getGraphQLClient();
const { data, errors } = await client.request<UpsertFamilyNoteMutationResponse>(UPSERT_FAMILY_NOTE_MUTATION, {
input: {
familyName: family.toUpperCase(),
content,
},
}, {
'x-user-id': session.user.id!,
});
const { rows } = await pool.query(
`UPDATE family_research_notes
SET content = $1, updated_by = $2, updated_at = NOW()
WHERE family_name = $3
RETURNING updated_at`,
[content, session.user.id, familyName]
);

if (errors) {
console.error('GraphQL errors:', errors);
if (rows.length === 0) {
return NextResponse.json(
{ error: 'Failed to update family notes' },
{ status: 500 }
{ error: 'Family notes not found' },
{ status: 404 }
);
}

return NextResponse.json({
success: true,
lastModified: data?.upsertFamilyResearchNote.updatedAt,
lastModified: rows[0].updated_at.toISOString(),
});
} catch (error) {
console.error('Error updating family notes:', error);
Expand Down
5 changes: 3 additions & 2 deletions app/family-notes/[family]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const metadata: Metadata = {
title: 'Family Research Notes',
};

export default function FamilyNotePage({ params }: { params: { family: string } }) {
return <FamilyNoteViewerClient family={params.family} />;
export default async function FamilyNotePage({ params }: { params: Promise<{ family: string }> }) {
const { family } = await params;
return <FamilyNoteViewerClient family={family} />;
}
Loading