mirror of
https://github.com/documenso/documenso.git
synced 2025-11-14 08:42:12 +10:00
chore: refactor code
This commit is contained in:
@ -6,11 +6,10 @@ export const testCredentialsHandler = async (req: NextApiRequest, res: NextApiRe
|
|||||||
try {
|
try {
|
||||||
const { authorization } = req.headers;
|
const { authorization } = req.headers;
|
||||||
|
|
||||||
const user = await validateApiToken({ authorization });
|
const result = await validateApiToken({ authorization });
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
username: user.name,
|
name: result.userId ? result.user.name : result.team?.name,
|
||||||
email: user.email,
|
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return res.status(500).json({
|
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 { findDocuments } from '@documenso/lib/server-only/document/find-documents';
|
||||||
import { getRecipientsForDocument } from '@documenso/lib/server-only/recipient/get-recipients-for-document';
|
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 { getWebhooksByUserId } from '../get-webhooks-by-user-id';
|
||||||
import { validateApiToken } from './validateApiToken';
|
import { validateApiToken } from './validateApiToken';
|
||||||
|
|
||||||
export const listDocumentsHandler = async (req: NextApiRequest, res: NextApiResponse) => {
|
export const listDocumentsHandler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||||
try {
|
try {
|
||||||
const { authorization } = req.headers;
|
const { authorization } = req.headers;
|
||||||
const user = await validateApiToken({ authorization });
|
const { user, userId, teamId } = await validateApiToken({ authorization });
|
||||||
|
|
||||||
const documents = await findDocuments({ userId: user.id });
|
let allWebhooks: Webhook[] = [];
|
||||||
const allWebhooks = await getWebhooksByUserId(user.id);
|
let documents;
|
||||||
const recipients = await getRecipientsForDocument({
|
let recipients: Recipient[] = [];
|
||||||
|
|
||||||
|
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,
|
documentId: documents.data[0].id,
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
|
teamId,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (documents.data.length > 0 && allWebhooks.length > 0) {
|
if (documents && documents.data.length > 0 && allWebhooks.length > 0 && recipients.length > 0) {
|
||||||
const testWebhook = {
|
const testWebhook = {
|
||||||
event: allWebhooks[0].eventTriggers.toString(),
|
event: allWebhooks[0].eventTriggers.toString(),
|
||||||
createdAt: allWebhooks[0].createdAt,
|
createdAt: allWebhooks[0].createdAt,
|
||||||
@ -43,7 +58,6 @@ export const listDocumentsHandler = async (req: NextApiRequest, res: NextApiResp
|
|||||||
|
|
||||||
return res.status(200).json([]);
|
return res.status(200).json([]);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
|
||||||
return res.status(500).json({
|
return res.status(500).json({
|
||||||
message: 'Internal Server Error',
|
message: 'Internal Server Error',
|
||||||
});
|
});
|
||||||
|
|||||||
@ -10,7 +10,7 @@ export const subscribeHandler = async (req: NextApiRequest, res: NextApiResponse
|
|||||||
|
|
||||||
const { webhookUrl, eventTrigger } = req.body;
|
const { webhookUrl, eventTrigger } = req.body;
|
||||||
|
|
||||||
const user = await validateApiToken({ authorization });
|
const result = await validateApiToken({ authorization });
|
||||||
|
|
||||||
const createdWebhook = await prisma.webhook.create({
|
const createdWebhook = await prisma.webhook.create({
|
||||||
data: {
|
data: {
|
||||||
@ -18,7 +18,8 @@ export const subscribeHandler = async (req: NextApiRequest, res: NextApiResponse
|
|||||||
eventTriggers: [eventTrigger],
|
eventTriggers: [eventTrigger],
|
||||||
secret: null,
|
secret: null,
|
||||||
enabled: true,
|
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 { webhookId } = req.body;
|
||||||
|
|
||||||
const user = await validateApiToken({ authorization });
|
const result = await validateApiToken({ authorization });
|
||||||
|
|
||||||
const deletedWebhook = await prisma.webhook.delete({
|
const deletedWebhook = await prisma.webhook.delete({
|
||||||
where: {
|
where: {
|
||||||
id: webhookId,
|
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 = {
|
type ValidateApiTokenOptions = {
|
||||||
authorization: string | undefined;
|
authorization: string | undefined;
|
||||||
@ -9,7 +9,11 @@ export const validateApiToken = async ({ authorization }: ValidateApiTokenOption
|
|||||||
// Support for both "Authorization: Bearer api_xxx" and "Authorization: api_xxx"
|
// Support for both "Authorization: Bearer api_xxx" and "Authorization: api_xxx"
|
||||||
const [token] = (authorization || '').split('Bearer ').filter((s) => s.length > 0);
|
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) {
|
} catch (err) {
|
||||||
throw new Error(`Failed to validate API token`);
|
throw new Error(`Failed to validate API token`);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user