-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
71 lines (62 loc) 路 1.96 KB
/
Copy pathroute.ts
File metadata and controls
71 lines (62 loc) 路 1.96 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
import { headers } from 'next/headers';
import { auth } from '@/lib/auth';
import { deductCredits, getUserEntitlement, hasCredits } from '@/lib/user-entitlement';
import { getAiResponse } from '@/lib/ai';
export async function POST(request: Request) {
const session = await auth.api.getSession({
headers: await headers(),
});
if (!session) {
return Response.json(
{
code: 'unauthenticated',
message: 'You must be logged in to use this feature.',
},
{ status: 401 }
);
}
const entitlement = await getUserEntitlement(session.user.id);
if (!entitlement) {
return Response.json(
{
code: 'no_active_purchase',
message: 'You do not have an active license to use this feature.',
},
// 402 Payment Required
{ status: 402 }
);
}
if (!(await hasCredits(session.user.id, 100))) {
return Response.json(
{
code: 'insufficient_credits',
message: 'You do not have enough credits to use this feature.',
},
{ status: 402 }
);
}
/**
* Here you would implement the AI asset generation and credit consumption logic.
* For demonstration, we will just return a dummy response and deduct 100 credits.
*/
await deductCredits(session.user.id, 100);
const data = await request.json();
if (!data.message || typeof data.message !== 'string') {
return Response.json(
{
code: 'invalid_input',
message: 'Invalid input provided. Please provide a valid message.',
},
{ status: 400 }
);
}
return Response.json(
{
// Insert the actual AI asset generation logic here.
message: getAiResponse(data.message),
},
{
status: 200,
}
);
}