diff --git a/packages/lib/server-only/public-api/test-credentials.ts b/packages/lib/server-only/public-api/test-credentials.ts index 4fa8bd16c..debbf9836 100644 --- a/packages/lib/server-only/public-api/test-credentials.ts +++ b/packages/lib/server-only/public-api/test-credentials.ts @@ -6,11 +6,10 @@ export const testCredentialsHandler = async (req: NextApiRequest, res: NextApiRe try { const { authorization } = req.headers; - const user = await validateApiToken({ authorization }); + const result = await validateApiToken({ authorization }); return res.status(200).json({ - username: user.name, - email: user.email, + name: result.userId ? result.user.name : result.team?.name, }); } catch (err) { return res.status(500).json({ diff --git a/packages/lib/server-only/webhooks/zapier/list-documents.ts b/packages/lib/server-only/webhooks/zapier/list-documents.ts index c66a671a2..433b58d5b 100644 --- a/packages/lib/server-only/webhooks/zapier/list-documents.ts +++ b/packages/lib/server-only/webhooks/zapier/list-documents.ts @@ -2,23 +2,38 @@ import type { NextApiRequest, NextApiResponse } from 'next'; import { findDocuments } from '@documenso/lib/server-only/document/find-documents'; import { getRecipientsForDocument } from '@documenso/lib/server-only/recipient/get-recipients-for-document'; +import type { Recipient, Webhook } from '@documenso/prisma/client'; +import { getWebhooksByTeamId } from '../get-webhooks-by-team-id'; import { getWebhooksByUserId } from '../get-webhooks-by-user-id'; import { validateApiToken } from './validateApiToken'; export const listDocumentsHandler = async (req: NextApiRequest, res: NextApiResponse) => { try { const { authorization } = req.headers; - const user = await validateApiToken({ authorization }); + const { user, userId, teamId } = await validateApiToken({ authorization }); - const documents = await findDocuments({ userId: user.id }); - const allWebhooks = await getWebhooksByUserId(user.id); - const recipients = await getRecipientsForDocument({ - documentId: documents.data[0].id, - userId: user.id, - }); + let allWebhooks: Webhook[] = []; + let documents; + let recipients: Recipient[] = []; - if (documents.data.length > 0 && allWebhooks.length > 0) { + if (userId) { + documents = await findDocuments({ userId }); + allWebhooks = await getWebhooksByUserId(userId); + recipients = await getRecipientsForDocument({ documentId: documents.data[0].id, userId }); + } + + if (teamId) { + documents = await findDocuments({ userId: user.id, teamId }); + allWebhooks = await getWebhooksByTeamId(teamId, user.id); + recipients = await getRecipientsForDocument({ + documentId: documents.data[0].id, + userId: user.id, + teamId, + }); + } + + if (documents && documents.data.length > 0 && allWebhooks.length > 0 && recipients.length > 0) { const testWebhook = { event: allWebhooks[0].eventTriggers.toString(), createdAt: allWebhooks[0].createdAt, @@ -43,7 +58,6 @@ export const listDocumentsHandler = async (req: NextApiRequest, res: NextApiResp return res.status(200).json([]); } catch (err) { - console.error(err); return res.status(500).json({ message: 'Internal Server Error', }); diff --git a/packages/lib/server-only/webhooks/zapier/subscribe.ts b/packages/lib/server-only/webhooks/zapier/subscribe.ts index af12c1daa..0a5863ba7 100644 --- a/packages/lib/server-only/webhooks/zapier/subscribe.ts +++ b/packages/lib/server-only/webhooks/zapier/subscribe.ts @@ -10,7 +10,7 @@ export const subscribeHandler = async (req: NextApiRequest, res: NextApiResponse const { webhookUrl, eventTrigger } = req.body; - const user = await validateApiToken({ authorization }); + const result = await validateApiToken({ authorization }); const createdWebhook = await prisma.webhook.create({ data: { @@ -18,7 +18,8 @@ export const subscribeHandler = async (req: NextApiRequest, res: NextApiResponse eventTriggers: [eventTrigger], secret: null, enabled: true, - userId: user.id, + userId: result.userId ? result.userId : result.user.id, + teamId: result.userId ? undefined : result.teamId, }, }); diff --git a/packages/lib/server-only/webhooks/zapier/unsubscribe.ts b/packages/lib/server-only/webhooks/zapier/unsubscribe.ts index 7da0e8110..9de419005 100644 --- a/packages/lib/server-only/webhooks/zapier/unsubscribe.ts +++ b/packages/lib/server-only/webhooks/zapier/unsubscribe.ts @@ -10,12 +10,13 @@ export const unsubscribeHandler = async (req: NextApiRequest, res: NextApiRespon const { webhookId } = req.body; - const user = await validateApiToken({ authorization }); + const result = await validateApiToken({ authorization }); const deletedWebhook = await prisma.webhook.delete({ where: { id: webhookId, - userId: user.id, + userId: result.userId ? result.userId : result.user.id, + teamId: result.userId ? undefined : result.teamId, }, }); diff --git a/packages/lib/server-only/webhooks/zapier/validateApiToken.ts b/packages/lib/server-only/webhooks/zapier/validateApiToken.ts index 2a8a44777..45e2b7522 100644 --- a/packages/lib/server-only/webhooks/zapier/validateApiToken.ts +++ b/packages/lib/server-only/webhooks/zapier/validateApiToken.ts @@ -1,4 +1,4 @@ -import { getUserByApiToken } from '../../public-api/get-user-by-token'; +import { getApiTokenByToken } from '../../public-api/get-api-token-by-token'; type ValidateApiTokenOptions = { authorization: string | undefined; @@ -9,7 +9,11 @@ export const validateApiToken = async ({ authorization }: ValidateApiTokenOption // Support for both "Authorization: Bearer api_xxx" and "Authorization: api_xxx" const [token] = (authorization || '').split('Bearer ').filter((s) => s.length > 0); - return await getUserByApiToken({ token }); + if (!token) { + throw new Error('Missing API token'); + } + + return await getApiTokenByToken({ token }); } catch (err) { throw new Error(`Failed to validate API token`); }