1
- import { authenticationError , notFoundError , authorizationError , argumentError } from "@/util/responses" ;
1
+ import { authenticationError , authorizationError , argumentError } from "@/util/responses" ;
2
2
import { auth } from "@/auth" ;
3
3
import { NextResponse , NextRequest } from "next/server" ;
4
4
import { z } from "zod" ;
5
- import { zfd } from "zod-form-data" ; // <-- Added import from zod-form-data
6
- import { PrismaClient } from "@prisma/client" ;
7
-
8
- // new Prisma Client
9
- const prisma = new PrismaClient ( ) ;
5
+ import { zfd } from "zod-form-data" ;
6
+ import { db } from "@/db" ;
10
7
11
8
// Zod schema
12
9
const PartnerDetailsFormSchema = zfd . formData ( {
@@ -16,68 +13,52 @@ const PartnerDetailsFormSchema = zfd.formData({
16
13
17
14
/**
18
15
* Updates a user's partner details.
19
- * Accepts parameters as FormData .
20
- *
21
- * @param { NextRequest } req The incoming request object.
22
- * @param { Object } context The route parameters containing the user ID.
23
- *
24
- * @returns { NextResponse } 401 if request is not authenticated.
25
- * @returns { NextResponse } 403 if the session user is a PARTNER modifying another user's details.
26
- * @returns { NextResponse } 400 if the request body is invalid.
27
- * @returns { NextResponse } 404 if no PartnerDetails record is found.
28
- * @returns { NextResponse } 200 with the updated partner details.
16
+ * Parameters are passed as form data .
17
+ *
18
+ * @param numberOfPatients The number of patients associated with the partner
19
+ * @param organizationType The type of the organization (NON_PROFIT, FOR_PROFIT, RELIGIOUS)
20
+ * @param userId The ID of the user whose partner details are being updated
21
+ *
22
+ * @returns 401 if the request is not authenticated
23
+ * @returns 403 if a PARTNER user attempts to modify another user's details
24
+ * @returns 404 if no PartnerDetails record is found
25
+ * @returns 200 with the updated partner details
29
26
*/
30
27
export async function POST (
31
28
req : NextRequest ,
32
29
{ params } : { params : Promise < { userId : string } > }
33
30
) {
34
- try {
31
+
35
32
// authenticate the user session
36
33
const session = await auth ( ) ;
37
- if ( ! session || ! session . user ) {
34
+ if ( ! session ? .user ) {
38
35
return authenticationError ( "Session required" ) ;
39
36
}
40
37
const { user } = session ;
41
38
42
39
// await params and ensure params.userId exists
43
40
const { userId } = await params ;
44
41
if ( ! userId ) {
45
- console . error ( "Missing user ID parameter in request" ) ;
46
42
return argumentError ( "Missing user ID parameter" ) ;
47
43
}
48
44
49
- const userIdNumber = parseInt ( userId , 10 ) ;
50
- if ( isNaN ( userIdNumber ) ) {
51
- console . error ( "Invalid user ID parameter" ) ;
52
- return argumentError ( "Invalid user ID" ) ;
53
- }
54
-
55
45
// partner users can only modify their own details
56
- if ( user . type === "PARTNER" && String ( user . id ) !== String ( userIdNumber ) ) {
46
+ if ( user . type === "PARTNER" && user . id !== userId ) {
57
47
return authorizationError ( "You are not allowed to modify this record" ) ;
58
48
}
59
49
60
50
// parse FormData
61
51
const formData = await req . formData ( ) ;
62
52
const parsedData = PartnerDetailsFormSchema . safeParse ( formData ) ;
63
53
if ( ! parsedData . success ) {
64
- console . error ( "Invalid form data:" , parsedData . error ) ;
65
54
return argumentError ( "Invalid form data" ) ;
66
55
}
67
56
68
57
const { numberOfPatients, organizationType } = parsedData . data ;
69
58
70
- // check if partnerDetails exists
71
- const partnerDetails = await prisma . partnerDetails . findUnique ( {
72
- where : { userId : userIdNumber } ,
73
- } ) ;
74
- if ( ! partnerDetails ) {
75
- console . error ( "Partner details not found for user ID:" , userIdNumber ) ;
76
- return notFoundError ( "Partner details not found" ) ;
77
- }
78
-
79
59
// update PartnerDetails record
80
- const updatedPartnerDetails = await prisma . partnerDetails . update ( {
60
+ const userIdNumber = Number ( userId ) ; //db schema accepts a number
61
+ const updatedPartnerDetails = await db . partnerDetails . update ( {
81
62
where : { userId : userIdNumber } ,
82
63
data : {
83
64
numberOfPatients,
@@ -86,8 +67,4 @@ export async function POST(
86
67
} ) ;
87
68
88
69
return NextResponse . json ( updatedPartnerDetails , { status : 200 } ) ;
89
- } catch ( error ) {
90
- console . error ( "Error updating PartnerDetails:" , error ) ;
91
- return NextResponse . json ( { message : "Internal Server Error" } , { status : 500 } ) ;
92
- }
93
70
}
0 commit comments