2023-12-02 09:38:24 +11:00
import { completeDocumentWithToken } from '@documenso/lib/server-only/document/complete-document-with-token' ;
2024-11-14 21:37:42 +11:00
import { rejectDocumentWithToken } from '@documenso/lib/server-only/document/reject-document-with-token' ;
2024-12-26 17:25:14 +11:00
import {
ZGetRecipientByIdResponseSchema ,
getRecipientById ,
} from '@documenso/lib/server-only/recipient/get-recipient-by-id' ;
2024-12-14 01:23:35 +09:00
import {
ZSetRecipientsForDocumentResponseSchema ,
setRecipientsForDocument ,
} from '@documenso/lib/server-only/recipient/set-recipients-for-document' ;
import {
ZSetRecipientsForTemplateResponseSchema ,
setRecipientsForTemplate ,
} from '@documenso/lib/server-only/recipient/set-recipients-for-template' ;
2024-02-12 12:04:53 +11:00
import { extractNextApiRequestMetadata } from '@documenso/lib/universal/extract-request-metadata' ;
2023-12-02 09:38:24 +11:00
import { authenticatedProcedure , procedure , router } from '../trpc' ;
2023-12-21 17:01:12 +11:00
import {
ZAddSignersMutationSchema ,
ZAddTemplateSignersMutationSchema ,
ZCompleteDocumentWithTokenMutationSchema ,
2024-12-26 17:25:14 +11:00
ZGetRecipientQuerySchema ,
2024-11-14 21:37:42 +11:00
ZRejectDocumentWithTokenMutationSchema ,
2023-12-21 17:01:12 +11:00
} from './schema' ;
2023-12-02 09:38:24 +11:00
export const recipientRouter = router ( {
2024-12-14 01:23:35 +09:00
/ * *
2024-12-26 17:25:14 +11:00
* @public
* /
getRecipient : authenticatedProcedure
. meta ( {
openapi : {
method : 'GET' ,
path : '/recipient/{recipientId}' ,
summary : 'Get recipient' ,
description :
'Returns a single recipient. If you want to retrieve all the recipients for a document or template, use the "Get Document" or "Get Template" request.' ,
tags : [ 'Recipients' ] ,
} ,
} )
. input ( ZGetRecipientQuerySchema )
. output ( ZGetRecipientByIdResponseSchema )
. query ( async ( { input , ctx } ) = > {
const { recipientId , teamId } = input ;
return await getRecipientById ( {
userId : ctx.user.id ,
teamId ,
recipientId ,
} ) ;
} ) ,
/ * *
* @public
2024-12-14 01:23:35 +09:00
* /
2023-12-02 09:38:24 +11:00
addSigners : authenticatedProcedure
2024-12-10 16:11:20 +09:00
. meta ( {
openapi : {
method : 'POST' ,
path : '/document/{documentId}/recipient/set' ,
summary : 'Set document recipients' ,
tags : [ 'Recipients' ] ,
} ,
} )
2023-12-02 09:38:24 +11:00
. input ( ZAddSignersMutationSchema )
2024-12-14 01:23:35 +09:00
. output ( ZSetRecipientsForDocumentResponseSchema )
2023-12-02 09:38:24 +11:00
. mutation ( async ( { input , ctx } ) = > {
2024-12-06 16:01:24 +09:00
const { documentId , teamId , signers } = input ;
2023-12-02 09:38:24 +11:00
2024-12-06 16:01:24 +09:00
return await setRecipientsForDocument ( {
userId : ctx.user.id ,
documentId ,
teamId ,
recipients : signers.map ( ( signer ) = > ( {
id : signer.nativeId ,
email : signer.email ,
name : signer.name ,
role : signer.role ,
signingOrder : signer.signingOrder ,
actionAuth : signer.actionAuth ,
} ) ) ,
requestMetadata : extractNextApiRequestMetadata ( ctx . req ) ,
} ) ;
2023-12-02 09:38:24 +11:00
} ) ,
2024-12-14 01:23:35 +09:00
/ * *
2024-12-26 17:25:14 +11:00
* @public
2024-12-14 01:23:35 +09:00
* /
2023-12-21 17:01:12 +11:00
addTemplateSigners : authenticatedProcedure
2024-12-10 16:11:20 +09:00
. meta ( {
openapi : {
method : 'POST' ,
path : '/template/{templateId}/recipient/set' ,
summary : 'Set template recipients' ,
tags : [ 'Recipients' ] ,
} ,
} )
2023-12-21 17:01:12 +11:00
. input ( ZAddTemplateSignersMutationSchema )
2024-12-14 01:23:35 +09:00
. output ( ZSetRecipientsForTemplateResponseSchema )
2023-12-21 17:01:12 +11:00
. mutation ( async ( { input , ctx } ) = > {
2024-12-06 16:01:24 +09:00
const { templateId , signers , teamId } = input ;
2023-12-21 17:01:12 +11:00
2024-12-06 16:01:24 +09:00
return await setRecipientsForTemplate ( {
userId : ctx.user.id ,
teamId ,
templateId ,
recipients : signers.map ( ( signer ) = > ( {
id : signer.nativeId ,
email : signer.email ,
name : signer.name ,
role : signer.role ,
signingOrder : signer.signingOrder ,
actionAuth : signer.actionAuth ,
} ) ) ,
} ) ;
2023-12-21 17:01:12 +11:00
} ) ,
2024-12-14 01:23:35 +09:00
/ * *
2024-12-26 17:25:14 +11:00
* @private
2024-12-14 01:23:35 +09:00
* /
2023-12-02 09:38:24 +11:00
completeDocumentWithToken : procedure
. input ( ZCompleteDocumentWithTokenMutationSchema )
2024-02-12 12:04:53 +11:00
. mutation ( async ( { input , ctx } ) = > {
2024-12-06 16:01:24 +09:00
const { token , documentId , authOptions } = input ;
2023-12-02 09:38:24 +11:00
2024-12-06 16:01:24 +09:00
return await completeDocumentWithToken ( {
token ,
documentId ,
authOptions ,
userId : ctx.user?.id ,
requestMetadata : extractNextApiRequestMetadata ( ctx . req ) ,
} ) ;
2023-12-02 09:38:24 +11:00
} ) ,
2024-11-14 21:37:42 +11:00
2024-12-14 01:23:35 +09:00
/ * *
2024-12-26 17:25:14 +11:00
* @private
2024-12-14 01:23:35 +09:00
* /
2024-11-14 21:37:42 +11:00
rejectDocumentWithToken : procedure
. input ( ZRejectDocumentWithTokenMutationSchema )
. mutation ( async ( { input , ctx } ) = > {
2024-12-06 16:01:24 +09:00
const { token , documentId , reason } = input ;
2024-11-14 21:37:42 +11:00
2024-12-06 16:01:24 +09:00
return await rejectDocumentWithToken ( {
token ,
documentId ,
reason ,
requestMetadata : extractNextApiRequestMetadata ( ctx . req ) ,
} ) ;
2024-11-14 21:37:42 +11:00
} ) ,
2023-12-02 09:38:24 +11:00
} ) ;