Skip to content

Commit 39d84d2

Browse files
authored
Merge pull request #1187 from trycompai/mariano/qa-endpoint
[dev] [Marfuen] mariano/qa-endpoint
2 parents bd7c645 + 89a6d16 commit 39d84d2

1 file changed

Lines changed: 112 additions & 0 deletions

File tree

  • apps/app/src/app/api/qa/delete-user
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { db } from '@db';
2+
import { type NextRequest, NextResponse } from 'next/server';
3+
4+
/**
5+
* POST /api/qa/delete-user
6+
*
7+
* Deletes a user and all associated data.
8+
* This is an internal endpoint for QA team.
9+
*
10+
* Headers:
11+
* - Authorization: Bearer {QA_SECRET}
12+
*
13+
* Body:
14+
* - userId: string - The ID of the user to delete.
15+
*
16+
* Returns:
17+
* - 200: { success: true, message: "User deleted successfully", userId: string }
18+
* - 400: { success: false, error: "Missing userId in request body" }
19+
* - 401: { success: false, error: "Unauthorized" }
20+
* - 500: { success: false, error: "Failed to delete user" }
21+
*/
22+
export async function POST(request: NextRequest) {
23+
const authHeader = request.headers.get('authorization');
24+
const qaSecret = process.env.QA_SECRET;
25+
26+
if (!qaSecret) {
27+
console.error('QA_SECRET is not set in environment variables.');
28+
return NextResponse.json(
29+
{
30+
success: false,
31+
error: 'Internal server configuration error.',
32+
},
33+
{ status: 500 },
34+
);
35+
}
36+
37+
const token = authHeader?.split(' ')[1];
38+
39+
if (!token || token !== qaSecret) {
40+
return NextResponse.json(
41+
{
42+
success: false,
43+
error: 'Unauthorized',
44+
},
45+
{ status: 401 },
46+
);
47+
}
48+
49+
let body;
50+
try {
51+
body = await request.json();
52+
} catch (error) {
53+
return NextResponse.json(
54+
{
55+
success: false,
56+
error: 'Invalid JSON in request body',
57+
},
58+
{ status: 400 },
59+
);
60+
}
61+
62+
const { userId } = body;
63+
64+
if (!userId) {
65+
return NextResponse.json(
66+
{
67+
success: false,
68+
error: 'Missing userId in request body',
69+
},
70+
{ status: 400 },
71+
);
72+
}
73+
74+
try {
75+
// Check if user exists
76+
const existingUser = await db.user.findUnique({
77+
where: { id: userId },
78+
});
79+
80+
if (!existingUser) {
81+
return NextResponse.json(
82+
{
83+
success: false,
84+
error: 'Failed to delete user',
85+
},
86+
{ status: 500 },
87+
);
88+
}
89+
90+
// Delete the user (cascading deletes will handle related records)
91+
await db.user.delete({
92+
where: { id: userId },
93+
});
94+
95+
console.log(`QA: User ${userId} deleted successfully`);
96+
97+
return NextResponse.json({
98+
success: true,
99+
message: 'User deleted successfully',
100+
userId: userId,
101+
});
102+
} catch (error) {
103+
console.error('Error deleting user:', error);
104+
return NextResponse.json(
105+
{
106+
success: false,
107+
error: 'Failed to delete user',
108+
},
109+
{ status: 500 },
110+
);
111+
}
112+
}

0 commit comments

Comments
 (0)