mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 08:13:56 +10:00
chore: refactor code
This commit is contained in:
@ -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({
|
||||
|
||||
@ -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',
|
||||
});
|
||||
|
||||
@ -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,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@ -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,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@ -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`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user