@@ -5,7 +5,7 @@ import { contactRequests, contacts, user as userTable } from "@call/db/schema";
5
5
import { createId } from "@paralleldrive/cuid2" ;
6
6
import { and , eq , or } from "drizzle-orm" ;
7
7
import type { ReqVariables } from "../../index.js" ;
8
- import { sendMail } from ' @call/auth/utils/send-mail' ;
8
+ import { sendMail } from " @call/auth/utils/send-mail" ;
9
9
10
10
const contactsRoutes = new Hono < { Variables : ReqVariables } > ( ) ;
11
11
@@ -26,38 +26,54 @@ contactsRoutes.post("/invite", async (c) => {
26
26
const result = inviteSchema . safeParse ( body ) ;
27
27
28
28
if ( ! result . success ) {
29
- return c . json ( { message : result . error . errors [ 0 ] ?. message || "Invalid input" } , 400 ) ;
29
+ return c . json (
30
+ { message : result . error . errors [ 0 ] ?. message || "Invalid input" } ,
31
+ 400
32
+ ) ;
30
33
}
31
34
const { receiverEmail } = result . data ;
32
35
33
36
if ( receiverEmail === user . email ) {
34
- return c . json ( { message : "You cannot send a contact request to yourself." } , 400 ) ;
37
+ return c . json (
38
+ { message : "You cannot send a contact request to yourself." } ,
39
+ 400
40
+ ) ;
35
41
}
36
42
37
- const [ receiver ] = await db . select ( { id : userTable . id } ) . from ( userTable ) . where ( eq ( userTable . email , receiverEmail ) ) ;
43
+ const [ receiver ] = await db
44
+ . select ( { id : userTable . id } )
45
+ . from ( userTable )
46
+ . where ( eq ( userTable . email , receiverEmail ) ) ;
38
47
39
48
if ( ! receiver ) {
40
49
await sendMail ( {
41
50
to : receiverEmail ,
42
51
subject : "Invitation to join Call" ,
43
52
text : `You are invited to join Call by ${ user . name } here is the link ${ process . env . FRONTEND_URL } /login` ,
44
-
45
53
} ) ;
46
- return c . json ( { message : "Email sent to receiver to invite them to the app." } , 200 ) ;
54
+ return c . json (
55
+ { message : "Email sent to receiver to invite them to the app." } ,
56
+ 200
57
+ ) ;
47
58
}
48
59
49
60
const receiverId = receiver . id ;
50
61
51
- const [ existingRelation ] = await db
52
- . select ( { check : contacts . userId } )
53
- . from ( contacts )
54
- . where ( and ( eq ( contacts . userId , senderId ) , eq ( contacts . contactId , receiverId ) ) )
55
- . limit ( 1 ) ;
56
-
57
- if ( existingRelation ) {
58
- return c . json ( { message : "You are already contacts with this user." } , 409 ) ;
59
- }
60
-
62
+ const [ existingRelation ] = await db
63
+ . select ( { check : contacts . userId } )
64
+ . from ( contacts )
65
+ . where (
66
+ and ( eq ( contacts . userId , senderId ) , eq ( contacts . contactId , receiverId ) )
67
+ )
68
+ . limit ( 1 ) ;
69
+
70
+ if ( existingRelation ) {
71
+ return c . json (
72
+ { message : "You are already contacts with this user." } ,
73
+ 409
74
+ ) ;
75
+ }
76
+
61
77
const [ existingRequest ] = await db
62
78
. select ( { id : contactRequests . id } )
63
79
. from ( contactRequests )
@@ -71,7 +87,10 @@ contactsRoutes.post("/invite", async (c) => {
71
87
. limit ( 1 ) ;
72
88
73
89
if ( existingRequest ) {
74
- return c . json ( { message : "A pending request to this user already exists." } , 409 ) ;
90
+ return c . json (
91
+ { message : "A pending request to this user already exists." } ,
92
+ 409
93
+ ) ;
75
94
}
76
95
77
96
await db . insert ( contactRequests ) . values ( {
@@ -105,10 +124,15 @@ contactsRoutes.get("/requests", async (c) => {
105
124
} )
106
125
. from ( contactRequests )
107
126
. leftJoin ( userTable , eq ( contactRequests . senderId , userTable . id ) )
108
- . where ( and ( eq ( contactRequests . receiverId , userId ) , eq ( contactRequests . status , "pending" ) ) ) ;
127
+ . where (
128
+ and (
129
+ eq ( contactRequests . receiverId , userId ) ,
130
+ eq ( contactRequests . status , "pending" )
131
+ )
132
+ ) ;
109
133
110
134
// Ensure senderName and senderEmail are always present (fallback to empty string if null)
111
- const requests = pendingRequests . map ( r => ( {
135
+ const requests = pendingRequests . map ( ( r ) => ( {
112
136
...r ,
113
137
senderName : r . senderName || "" ,
114
138
senderEmail : r . senderEmail || "" ,
@@ -131,15 +155,30 @@ contactsRoutes.patch("/requests/:id/accept", async (c) => {
131
155
const [ request ] = await tx
132
156
. select ( )
133
157
. from ( contactRequests )
134
- . where ( and ( eq ( contactRequests . id , requestId ) , eq ( contactRequests . receiverId , userId ) , eq ( contactRequests . status , "pending" ) ) ) ;
158
+ . where (
159
+ and (
160
+ eq ( contactRequests . id , requestId ) ,
161
+ eq ( contactRequests . receiverId , userId ) ,
162
+ eq ( contactRequests . status , "pending" )
163
+ )
164
+ ) ;
135
165
136
166
if ( ! request ) {
137
- return c . json ( { message : "Request not found, already handled, or you are not the recipient." } , 404 ) ;
167
+ return c . json (
168
+ {
169
+ message :
170
+ "Request not found, already handled, or you are not the recipient." ,
171
+ } ,
172
+ 404
173
+ ) ;
138
174
}
139
-
175
+
140
176
const senderId = request . senderId ;
141
177
142
- await tx . update ( contactRequests ) . set ( { status : "accepted" } ) . where ( eq ( contactRequests . id , requestId ) ) ;
178
+ await tx
179
+ . update ( contactRequests )
180
+ . set ( { status : "accepted" } )
181
+ . where ( eq ( contactRequests . id , requestId ) ) ;
143
182
144
183
await tx . insert ( contacts ) . values ( [
145
184
{ userId : userId , contactId : senderId , createdAt : new Date ( ) } ,
@@ -163,10 +202,22 @@ contactsRoutes.patch("/requests/:id/reject", async (c) => {
163
202
const { rowCount } = await db
164
203
. update ( contactRequests )
165
204
. set ( { status : "rejected" } )
166
- . where ( and ( eq ( contactRequests . id , requestId ) , eq ( contactRequests . receiverId , userId ) , eq ( contactRequests . status , "pending" ) ) ) ;
205
+ . where (
206
+ and (
207
+ eq ( contactRequests . id , requestId ) ,
208
+ eq ( contactRequests . receiverId , userId ) ,
209
+ eq ( contactRequests . status , "pending" )
210
+ )
211
+ ) ;
167
212
168
213
if ( rowCount === 0 ) {
169
- return c . json ( { message : "Request not found, already handled, or you are not the recipient." } , 404 ) ;
214
+ return c . json (
215
+ {
216
+ message :
217
+ "Request not found, already handled, or you are not the recipient." ,
218
+ } ,
219
+ 404
220
+ ) ;
170
221
}
171
222
172
223
return c . json ( { message : "Contact request rejected." } ) ;
@@ -207,12 +258,14 @@ contactsRoutes.delete("/:id", async (c) => {
207
258
try {
208
259
await db . transaction ( async ( tx ) => {
209
260
// Delete both sides of the contact relationship
210
- await tx . delete ( contacts ) . where (
211
- or (
212
- and ( eq ( contacts . userId , userId ) , eq ( contacts . contactId , contactId ) ) ,
213
- and ( eq ( contacts . userId , contactId ) , eq ( contacts . contactId , userId ) )
214
- )
215
- ) ;
261
+ await tx
262
+ . delete ( contacts )
263
+ . where (
264
+ or (
265
+ and ( eq ( contacts . userId , userId ) , eq ( contacts . contactId , contactId ) ) ,
266
+ and ( eq ( contacts . userId , contactId ) , eq ( contacts . contactId , userId ) )
267
+ )
268
+ ) ;
216
269
} ) ;
217
270
218
271
return c . json ( { message : "Contact deleted successfully." } ) ;
@@ -222,4 +275,4 @@ contactsRoutes.delete("/:id", async (c) => {
222
275
}
223
276
} ) ;
224
277
225
- export default contactsRoutes ;
278
+ export default contactsRoutes ;
0 commit comments