File tree 1 file changed +17
-11
lines changed
1 file changed +17
-11
lines changed Original file line number Diff line number Diff line change
1
+ 'use server' ;
1
2
import { getUser } from '@/actions/user/authed/get-user' ;
2
3
import { prisma } from '@/lib/prisma' ;
3
4
4
5
/**
5
- * Get's the answer for a given question uid
6
+ * Gets the answer for a given question uid
6
7
* and user uid
7
8
*
8
- * @param opts
9
- * @returns
9
+ * @param opts - Options containing the questionUid
10
+ * @returns - The user's answer or null if not found
10
11
*/
11
12
export const getUserAnswer = async ( opts : { questionUid : string } ) => {
12
13
const { questionUid } = opts ;
13
14
14
15
const user = await getUser ( ) ;
15
16
16
17
if ( ! user ) {
17
- return false ;
18
+ return null ; // Return null instead of false for better type consistency
18
19
}
19
20
20
- // find the answer to the question
21
- return await prisma . answers . findFirst ( {
22
- where : {
23
- questionUid,
24
- userUid : user ?. uid ,
25
- } ,
26
- } ) ;
21
+ try {
22
+ // Find the answer to the question
23
+ return await prisma . answers . findFirst ( {
24
+ where : {
25
+ questionUid,
26
+ userUid : user . uid , // user?.uid is unnecessary since we check for user existence
27
+ } ,
28
+ } ) ;
29
+ } catch ( error ) {
30
+ console . error ( 'Error fetching user answer:' , error ) ;
31
+ throw new Error ( 'Could not fetch user answer. Please try again later.' ) ; // Handle the error gracefully
32
+ }
27
33
} ;
You can’t perform that action at this time.
0 commit comments