-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathroute.ts
98 lines (86 loc) · 2.42 KB
/
route.ts
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
import { revalidatePath } from 'next/cache'
import { NextRequest } from 'next/server'
import { PutObjectCommand } from '@aws-sdk/client-s3'
import { getSignedUrl } from '@aws-sdk/s3-request-presigner'
import { ZodError } from 'zod'
import { TaskSchema } from '@/lib/api/schema/tasks'
import prisma from '@/lib/prisma'
import { s3Client } from '@/lib/s3Client'
import { getServerUser } from '@/lib/session'
import {
badRequest,
forbidden,
internalServerError,
json,
unauthorized
} from '@/utils/apiResponse'
export async function GET() {
const user = await getServerUser()
const task = await prisma.task.findMany({
...(!user?.admin && { where: { private: false } })
})
return json(task)
}
export async function POST(req: NextRequest) {
const user = await getServerUser()
if (!user) return unauthorized()
if (!user.admin) return forbidden()
try {
const task = TaskSchema.parse(await req.json())
const path = task.categoryId.split('/')
for (let i = 0; i < path.length; i++) {
await prisma.category.upsert({
where: { id: path.slice(0, i + 1).join('/') },
update: {},
create: {
id: path.slice(0, i + 1).join('/'),
name: path[i],
parentCategoryId: path.slice(0, i).join('/') || null
}
})
}
const uploadUrl = []
if (task.files) {
for (const file of task.files) {
const Key =
file.type === 'application/pdf'
? `statements/pdf/${task.id}.pdf`
: file.type === 'application/zip'
? `statements/attachments/${task.id}.zip`
: `testcases/${task.id}/${file.path}`
const url = await getSignedUrl(
s3Client,
new PutObjectCommand({
Bucket: process.env.BUCKET_NAME,
Key,
ContentType: file.type
}),
{ expiresIn: 15 * 60 }
)
uploadUrl.push({ path: file.path, url })
}
}
delete task['files']
await prisma.task.create({
data: {
...task,
tags: {
connectOrCreate: task.tags.map(tag => ({
where: { name: tag },
create: { name: tag }
}))
}
}
})
revalidatePath('/')
revalidatePath('/tasks')
return json(uploadUrl)
} catch (err) {
console.log(err)
if (err instanceof ZodError) {
return badRequest()
} else {
return internalServerError()
}
}
}