89 lines
No EOL
2.9 KiB
TypeScript
89 lines
No EOL
2.9 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { sql } from "@/db";
|
|
|
|
interface LogEntry {
|
|
type: "feed" | "diaper" | "sleep";
|
|
childId: string;
|
|
subType: string;
|
|
amountMl?: number;
|
|
notes?: string;
|
|
startedAt?: string;
|
|
endedAt?: string;
|
|
}
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const body: LogEntry = await request.json();
|
|
const { type, childId, subType, amountMl, notes, startedAt, endedAt } = body;
|
|
|
|
if (!type || !childId || !subType) {
|
|
return NextResponse.json({ error: "Missing required fields" }, { status: 400 });
|
|
}
|
|
|
|
const now = new Date();
|
|
|
|
if (type === "feed") {
|
|
await sql.unsafe(
|
|
`INSERT INTO feeds (child_id, type, method, amount_ml, notes, logged_at) VALUES ($1, $2, $3, $4, $5, $6)`,
|
|
[childId, subType, subType.includes("breast") ? subType : "bottle", amountMl || null, notes || null, now]
|
|
);
|
|
} else if (type === "diaper") {
|
|
await sql.unsafe(
|
|
`INSERT INTO diapers_logs (child_id, type, notes, logged_at) VALUES ($1, $2, $3, $4)`,
|
|
[childId, subType, notes || null, now]
|
|
);
|
|
} else if (type === "sleep") {
|
|
const startTime = startedAt ? new Date(startedAt) : now;
|
|
const endTime = endedAt ? new Date(endedAt) : null;
|
|
const durationMinutes = endTime
|
|
? Math.round((endTime.getTime() - startTime.getTime()) / 60000)
|
|
: null;
|
|
|
|
await sql.unsafe(
|
|
`INSERT INTO sleeps (child_id, type, started_at, ended_at, duration_minutes, notes, logged_at) VALUES ($1, $2, $3, $4, $5, $6, $7)`,
|
|
[childId, subType, startTime, endTime, durationMinutes, notes || null, now]
|
|
);
|
|
}
|
|
|
|
return NextResponse.json({ success: true });
|
|
} catch (error) {
|
|
console.error(error);
|
|
return NextResponse.json({ error: String(error) }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function GET(request: Request) {
|
|
const { searchParams } = new URL(request.url);
|
|
const childId = searchParams.get("childId");
|
|
const type = searchParams.get("type");
|
|
const limit = parseInt(searchParams.get("limit") || "20");
|
|
|
|
if (!childId || !type) {
|
|
return NextResponse.json({ error: "childId and type required" }, { status: 400 });
|
|
}
|
|
|
|
try {
|
|
let results: any[] = [];
|
|
if (type === "feed") {
|
|
results = await sql.unsafe(
|
|
`SELECT * FROM feeds WHERE child_id = $1 ORDER BY logged_at DESC LIMIT $2`,
|
|
[childId, limit]
|
|
);
|
|
} else if (type === "diaper") {
|
|
results = await sql.unsafe(
|
|
`SELECT * FROM diapers_logs WHERE child_id = $1 ORDER BY logged_at DESC LIMIT $2`,
|
|
[childId, limit]
|
|
);
|
|
} else if (type === "sleep") {
|
|
results = await sql.unsafe(
|
|
`SELECT * FROM sleeps WHERE child_id = $1 ORDER BY logged_at DESC LIMIT $2`,
|
|
[childId, limit]
|
|
);
|
|
}
|
|
|
|
return NextResponse.json({ entries: results || [] });
|
|
} catch (error) {
|
|
console.error(error);
|
|
return NextResponse.json({ error: String(error) }, { status: 500 });
|
|
}
|
|
} |