@@ -16,7 +16,7 @@ import {
16
16
RecipientEditorSchema ,
17
17
deleteRecipientActionIntent ,
18
18
sendVerificationActionIntent ,
19
- updateRecipientActionIntent ,
19
+ upsertRecipientActionIntent ,
20
20
} from './__editor.tsx'
21
21
22
22
export async function handleVerification ( { submission } : VerifyFunctionArgs ) {
@@ -60,25 +60,25 @@ export async function action({ request, params }: ActionFunctionArgs) {
60
60
const userId = await requireUserId ( request )
61
61
const { recipientId } = params
62
62
63
- invariantResponse ( recipientId , 'Invalid recipient id' )
64
-
65
- const recipient = await prisma . recipient . findUnique ( {
66
- where : { id : recipientId , userId } ,
67
- select : { id : true , phoneNumber : true , verified : true } ,
68
- } )
69
-
70
- invariantResponse ( recipient , 'Recipient not found' , { status : 404 } )
63
+ const recipient = recipientId
64
+ ? await prisma . recipient . findUnique ( {
65
+ where : { id : recipientId , userId } ,
66
+ select : { id : true , phoneNumber : true , verified : true } ,
67
+ } )
68
+ : null
71
69
72
70
const formData = await request . formData ( )
73
71
74
72
switch ( formData . get ( 'intent' ) ) {
75
73
case deleteRecipientActionIntent : {
74
+ invariantResponse ( recipient , 'Recipient not found' , { status : 404 } )
76
75
return deleteRecipientAction ( { formData, userId, request, recipient } )
77
76
}
78
- case updateRecipientActionIntent : {
79
- return updateRecipientAction ( { formData, userId, request, recipient } )
77
+ case upsertRecipientActionIntent : {
78
+ return usertRecipientAction ( { formData, userId, request, recipient } )
80
79
}
81
80
case sendVerificationActionIntent : {
81
+ invariantResponse ( recipient , 'Recipient not found' , { status : 404 } )
82
82
return sendVerificationAction ( { formData, userId, request, recipient } )
83
83
}
84
84
default : {
@@ -87,12 +87,14 @@ export async function action({ request, params }: ActionFunctionArgs) {
87
87
}
88
88
}
89
89
90
- export async function updateRecipientAction ( {
90
+ export async function usertRecipientAction ( {
91
91
formData,
92
92
userId,
93
93
request,
94
94
recipient,
95
- } : RecipientActionArgs ) {
95
+ } : Pick < RecipientActionArgs , 'formData' | 'request' | 'userId' > & {
96
+ recipient : RecipientActionArgs [ 'recipient' ] | null
97
+ } ) {
96
98
const submission = await parseWithZod ( formData , {
97
99
schema : RecipientEditorSchema ,
98
100
async : true ,
@@ -135,8 +137,8 @@ export async function updateRecipientAction({
135
137
return sendVerificationAction ( { formData, userId, request, recipient } )
136
138
}
137
139
} else {
138
- await prisma . recipient . create ( {
139
- select : { id : true } ,
140
+ recipient = await prisma . recipient . create ( {
141
+ select : { id : true , phoneNumber : true , verified : true } ,
140
142
data : {
141
143
name,
142
144
phoneNumber,
0 commit comments