-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathroute.ts
More file actions
138 lines (114 loc) · 4 KB
/
route.ts
File metadata and controls
138 lines (114 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { NextRequest } from "next/server";
import { db } from "@/db";
import { expenses } from "@/db/schema/finance";
import { eq, and } from "drizzle-orm";
import { expenseCreateSchema, expenseUpdateSchema } from "@/lib/validation";
import { createApiResponse, handleApiError, handleValidationError } from "@/lib/api-utils";
import { getSession } from "@/lib/auth-client";
import { z } from "zod";
// GET /api/finance/expenses - Get all expenses for current user
export async function GET(request: NextRequest) {
try {
const session = await getSession();
if (!session?.user) {
return createApiResponse(false, undefined, "Unauthorized", 401);
}
const userExpenses = await db
.select()
.from(expenses)
.where(eq(expenses.userId, session.user.id))
.orderBy(expenses.date);
return createApiResponse(true, userExpenses);
} catch (error) {
return handleApiError(error);
}
}
// POST /api/finance/expenses - Create a new expense
export async function POST(request: NextRequest) {
try {
const session = await getSession();
if (!session?.user) {
return createApiResponse(false, undefined, "Unauthorized", 401);
}
const body = await request.json();
const validatedData = expenseCreateSchema.parse(body);
const [newExpense] = await db
.insert(expenses)
.values({
id: crypto.randomUUID(),
userId: session.user.id,
description: validatedData.description,
amount: validatedData.amount.toString(),
category: validatedData.category,
date: new Date(validatedData.date),
isRecurring: validatedData.isRecurring,
recurringFrequency: validatedData.recurringFrequency,
notes: validatedData.notes,
})
.returning();
return createApiResponse(true, newExpense, undefined, 201);
} catch (error) {
if (error instanceof z.ZodError) {
return handleValidationError(error);
}
return handleApiError(error);
}
}
// PUT /api/finance/expenses - Update an expense
export async function PUT(request: NextRequest) {
try {
const session = await getSession();
if (!session?.user) {
return createApiResponse(false, undefined, "Unauthorized", 401);
}
const body = await request.json();
const { id, ...updateData } = body;
if (!id) {
return createApiResponse(false, undefined, "Expense ID is required", 400);
}
const validatedData = expenseUpdateSchema.parse(updateData);
const [updatedExpense] = await db
.update(expenses)
.set({
...validatedData,
amount: validatedData.amount ? validatedData.amount.toString() : undefined,
date: validatedData.date ? new Date(validatedData.date) : undefined,
updatedAt: new Date(),
})
.where(and(eq(expenses.id, id), eq(expenses.userId, session.user.id)))
.returning();
if (!updatedExpense) {
return createApiResponse(false, undefined, "Expense not found", 404);
}
return createApiResponse(true, updatedExpense);
} catch (error) {
if (error instanceof z.ZodError) {
return handleValidationError(error);
}
return handleApiError(error);
}
}
// DELETE /api/finance/expenses - Delete an expense
export async function DELETE(request: NextRequest) {
try {
const session = await getSession();
if (!session?.user) {
return createApiResponse(false, undefined, "Unauthorized", 401);
}
const { searchParams } = new URL(request.url);
const id = searchParams.get("id");
if (!id) {
return createApiResponse(false, undefined, "Expense ID is required", 400);
}
const [deletedExpense] = await db
.delete(expenses)
.where(and(eq(expenses.id, id), eq(expenses.userId, session.user.id)))
.returning();
if (!deletedExpense) {
return createApiResponse(false, undefined, "Expense not found", 404);
}
return createApiResponse(true, { message: "Expense deleted successfully" });
} catch (error) {
return handleApiError(error);
}
}