2023-11-22 15:44:49 +02:00
|
|
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
|
|
|
|
2023-12-06 15:27:30 +00:00
|
|
|
import { createDocumentData } from '@documenso/lib/server-only/document-data/create-document-data';
|
|
|
|
|
import { createDocument } from '@documenso/lib/server-only/document/create-document';
|
|
|
|
|
import { deleteDocument } from '@documenso/lib/server-only/document/delete-document';
|
2023-11-23 10:02:22 +02:00
|
|
|
import { getDocumentById } from '@documenso/lib/server-only/document/get-document-by-id';
|
2023-11-22 15:44:49 +02:00
|
|
|
import { getDocuments } from '@documenso/lib/server-only/public-api/get-documents';
|
2023-11-30 14:39:31 +02:00
|
|
|
import { checkUserFromToken } from '@documenso/lib/server-only/public-api/get-user-by-token';
|
2023-12-06 15:27:30 +00:00
|
|
|
import { putFile } from '@documenso/lib/universal/upload/put-file';
|
2023-11-22 15:44:49 +02:00
|
|
|
import { contract } from '@documenso/trpc/api-contract/contract';
|
|
|
|
|
import { createNextRoute, createNextRouter } from '@documenso/trpc/server/public-api/ts-rest';
|
|
|
|
|
|
2023-11-30 14:39:31 +02:00
|
|
|
const validateUserToken = async (token: string) => {
|
|
|
|
|
try {
|
|
|
|
|
return await checkUserFromToken({ token });
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-11-22 15:44:49 +02:00
|
|
|
const router = createNextRoute(contract, {
|
|
|
|
|
getDocuments: async (args) => {
|
|
|
|
|
const page = Number(args.query.page) || 1;
|
|
|
|
|
const perPage = Number(args.query.perPage) || 10;
|
2023-11-30 14:39:31 +02:00
|
|
|
const { authorization } = args.headers;
|
|
|
|
|
|
|
|
|
|
const user = await validateUserToken(authorization);
|
2023-11-22 15:44:49 +02:00
|
|
|
|
2023-11-30 14:39:31 +02:00
|
|
|
if (!user) {
|
|
|
|
|
return {
|
|
|
|
|
status: 401,
|
|
|
|
|
body: {
|
|
|
|
|
message: 'Unauthorized',
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { documents, totalPages } = await getDocuments({ page, perPage, userId: user.id });
|
2023-11-22 15:44:49 +02:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
status: 200,
|
|
|
|
|
body: {
|
|
|
|
|
documents,
|
|
|
|
|
totalPages,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
},
|
2023-11-23 10:02:22 +02:00
|
|
|
getDocument: async (args) => {
|
2023-11-30 14:39:31 +02:00
|
|
|
const { id: documentId } = args.params;
|
|
|
|
|
const { authorization } = args.headers;
|
2023-11-23 10:02:22 +02:00
|
|
|
|
2023-11-30 14:39:31 +02:00
|
|
|
const user = await validateUserToken(authorization);
|
|
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
|
return {
|
|
|
|
|
status: 401,
|
|
|
|
|
body: {
|
|
|
|
|
message: 'Unauthorized',
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const document = await getDocumentById({ id: Number(documentId), userId: user.id });
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
status: 200,
|
|
|
|
|
body: document,
|
|
|
|
|
};
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return {
|
|
|
|
|
status: 404,
|
|
|
|
|
body: {
|
|
|
|
|
message: 'Document not found',
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
deleteDocument: async (args) => {
|
|
|
|
|
const { id: documentId } = args.params;
|
|
|
|
|
const { authorization } = args.headers;
|
|
|
|
|
|
|
|
|
|
const user = await validateUserToken(authorization);
|
|
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
|
return {
|
|
|
|
|
status: 401,
|
|
|
|
|
body: {
|
|
|
|
|
message: 'Unauthorized',
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2023-12-06 15:27:30 +00:00
|
|
|
const document = await getDocumentById({ id: Number(documentId), userId: user.id });
|
|
|
|
|
|
|
|
|
|
const deletedDocument = await deleteDocument({
|
|
|
|
|
id: Number(documentId),
|
|
|
|
|
userId: user.id,
|
|
|
|
|
status: document.status,
|
|
|
|
|
});
|
2023-11-30 14:39:31 +02:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
status: 200,
|
2023-12-06 15:27:30 +00:00
|
|
|
body: deletedDocument,
|
2023-11-30 14:39:31 +02:00
|
|
|
};
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return {
|
|
|
|
|
status: 404,
|
|
|
|
|
body: {
|
|
|
|
|
message: 'Document not found',
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-11-23 10:02:22 +02:00
|
|
|
},
|
2023-12-06 15:27:30 +00:00
|
|
|
createDocument: async (args) => {
|
|
|
|
|
const { authorization } = args.headers;
|
|
|
|
|
const { body } = args;
|
|
|
|
|
|
|
|
|
|
const user = await validateUserToken(authorization);
|
|
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
|
return {
|
|
|
|
|
status: 401,
|
|
|
|
|
body: {
|
|
|
|
|
message: 'Unauthorized',
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const regexPattern = /filename="(.+?)"/;
|
|
|
|
|
const match = body.toString().match(regexPattern);
|
|
|
|
|
const documentTitle = match?.[1] ?? 'Untitled document';
|
|
|
|
|
|
2023-12-06 16:53:34 +00:00
|
|
|
const file = new Blob([body], {
|
2023-12-06 15:27:30 +00:00
|
|
|
type: 'application/pdf',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const { type, data } = await putFile(file);
|
|
|
|
|
|
|
|
|
|
const { id: documentDataId } = await createDocumentData({
|
|
|
|
|
type,
|
|
|
|
|
data,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const { id } = await createDocument({
|
|
|
|
|
title: documentTitle,
|
|
|
|
|
documentDataId,
|
|
|
|
|
userId: user.id,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
status: 200,
|
|
|
|
|
body: {
|
|
|
|
|
uploadedFile: {
|
|
|
|
|
id,
|
|
|
|
|
message: 'Document uploaded successfuly',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
return {
|
|
|
|
|
status: 500,
|
|
|
|
|
body: {
|
|
|
|
|
message: 'An error occurred while uploading your document.',
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-11-22 15:44:49 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const nextRouter = createNextRouter(contract, router);
|
|
|
|
|
|
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
|
await nextRouter(req, res);
|
|
|
|
|
}
|