This commit is contained in:
@@ -7,8 +7,8 @@ async function getUserFromToken() {
|
||||
const token = await getTokenCookie();
|
||||
if (!token) return null;
|
||||
|
||||
const [user] = await db.select().from(users).where(eq(users.token, token));
|
||||
return user;
|
||||
const userRows = await db.select().from(users).where(eq(users.token, token));
|
||||
return userRows.length > 0 ? userRows[0] : null;
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest, { params }: { params: Promise<{ id: string }> }) {
|
||||
@@ -26,20 +26,20 @@ export async function POST(request: NextRequest, { params }: { params: Promise<{
|
||||
}
|
||||
|
||||
// Verify habit belongs to user
|
||||
const [habit] = await db
|
||||
const habitRows = await db
|
||||
.select()
|
||||
.from(habits)
|
||||
.where(and(eq(habits.id, habitId), eq(habits.userId, user.id)));
|
||||
|
||||
if (!habit) {
|
||||
if (habitRows.length === 0) {
|
||||
return NextResponse.json({ error: 'Habit not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const body = (await request.json()) as { note?: string };
|
||||
const { note } = body;
|
||||
|
||||
// Create log entry
|
||||
const [log] = await db
|
||||
const logRows = await db
|
||||
.insert(habitLogs)
|
||||
.values({
|
||||
habitId,
|
||||
@@ -47,6 +47,11 @@ export async function POST(request: NextRequest, { params }: { params: Promise<{
|
||||
})
|
||||
.returning();
|
||||
|
||||
if (logRows.length === 0) {
|
||||
throw new Error('Failed to create log entry');
|
||||
}
|
||||
|
||||
const log = logRows[0];
|
||||
return NextResponse.json({ log });
|
||||
} catch (error) {
|
||||
console.error('Log habit error:', error);
|
||||
|
@@ -7,11 +7,11 @@ async function getUserFromToken() {
|
||||
const token = await getTokenCookie();
|
||||
if (!token) return null;
|
||||
|
||||
const [user] = await db.select().from(users).where(eq(users.token, token));
|
||||
return user;
|
||||
const userRows = await db.select().from(users).where(eq(users.token, token));
|
||||
return userRows.length > 0 ? userRows[0] : null;
|
||||
}
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
export async function GET() {
|
||||
try {
|
||||
const user = await getUserFromToken();
|
||||
if (!user) {
|
||||
@@ -83,7 +83,13 @@ export async function POST(request: NextRequest) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const body = (await request.json()) as {
|
||||
name: string;
|
||||
type: string;
|
||||
targetFrequency?: { value: number; period: 'day' | 'week' | 'month' };
|
||||
color?: string;
|
||||
icon?: string;
|
||||
};
|
||||
const { name, type, targetFrequency, color, icon } = body;
|
||||
|
||||
if (!name || !type) {
|
||||
@@ -95,18 +101,33 @@ export async function POST(request: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
const [newHabit] = await db
|
||||
// Validate type is one of the allowed enum values
|
||||
if (!['positive', 'neutral', 'negative'].includes(type)) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Type must be one of: positive, neutral, negative',
|
||||
},
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
const newHabitRows = await db
|
||||
.insert(habits)
|
||||
.values({
|
||||
userId: user.id,
|
||||
name,
|
||||
type,
|
||||
type: type as 'positive' | 'neutral' | 'negative',
|
||||
targetFrequency,
|
||||
color,
|
||||
icon,
|
||||
})
|
||||
.returning();
|
||||
|
||||
if (newHabitRows.length === 0) {
|
||||
throw new Error('Failed to create habit');
|
||||
}
|
||||
|
||||
const newHabit = newHabitRows[0];
|
||||
return NextResponse.json({ habit: newHabit });
|
||||
} catch (error) {
|
||||
console.error('Create habit error:', error);
|
||||
|
Reference in New Issue
Block a user