-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathroute.ts
More file actions
136 lines (112 loc) · 3.89 KB
/
route.ts
File metadata and controls
136 lines (112 loc) · 3.89 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
import { NextRequest } from "next/server";
import { db } from "@/db";
import { assets } from "@/db/schema/finance";
import { eq, and } from "drizzle-orm";
import { assetCreateSchema, assetUpdateSchema } from "@/lib/validation";
import { createApiResponse, handleApiError, handleValidationError } from "@/lib/api-utils";
import { getSession } from "@/lib/auth-client";
import { z } from "zod";
// GET /api/finance/assets - Get all assets for current user
export async function GET(request: NextRequest) {
try {
const session = await getSession();
if (!session?.user) {
return createApiResponse(false, undefined, "Unauthorized", 401);
}
const userAssets = await db
.select()
.from(assets)
.where(eq(assets.userId, session.user.id))
.orderBy(assets.createdAt);
return createApiResponse(true, userAssets);
} catch (error) {
return handleApiError(error);
}
}
// POST /api/finance/assets - Create a new asset
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 = assetCreateSchema.parse(body);
const [newAsset] = await db
.insert(assets)
.values({
id: crypto.randomUUID(),
userId: session.user.id,
name: validatedData.name,
type: validatedData.type,
value: validatedData.value.toString(),
purchaseDate: validatedData.purchaseDate ? new Date(validatedData.purchaseDate) : undefined,
description: validatedData.description,
})
.returning();
return createApiResponse(true, newAsset, undefined, 201);
} catch (error) {
if (error instanceof z.ZodError) {
return handleValidationError(error);
}
return handleApiError(error);
}
}
// PUT /api/finance/assets - Update an asset
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, "Asset ID is required", 400);
}
const validatedData = assetUpdateSchema.parse(updateData);
const [updatedAsset] = await db
.update(assets)
.set({
...validatedData,
value: validatedData.value ? validatedData.value.toString() : undefined,
purchaseDate: validatedData.purchaseDate ? new Date(validatedData.purchaseDate) : undefined,
updatedAt: new Date(),
})
.where(and(eq(assets.id, id), eq(assets.userId, session.user.id)))
.returning();
if (!updatedAsset) {
return createApiResponse(false, undefined, "Asset not found", 404);
}
return createApiResponse(true, updatedAsset);
} catch (error) {
if (error instanceof z.ZodError) {
return handleValidationError(error);
}
return handleApiError(error);
}
}
// DELETE /api/finance/assets - Delete an asset
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, "Asset ID is required", 400);
}
const [deletedAsset] = await db
.delete(assets)
.where(and(eq(assets.id, id), eq(assets.userId, session.user.id)))
.returning();
if (!deletedAsset) {
return createApiResponse(false, undefined, "Asset not found", 404);
}
return createApiResponse(true, { message: "Asset deleted successfully" });
} catch (error) {
return handleApiError(error);
}
}