mirror of
https://github.com/documenso/documenso.git
synced 2025-11-10 04:22:32 +10:00
chore: update routes
trying to add the route for creating documents
This commit is contained in:
@ -1,9 +1,12 @@
|
||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||
|
||||
import { deleteDraftDocument } from '@documenso/lib/server-only/document/delete-draft-document';
|
||||
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';
|
||||
import { getDocumentById } from '@documenso/lib/server-only/document/get-document-by-id';
|
||||
import { getDocuments } from '@documenso/lib/server-only/public-api/get-documents';
|
||||
import { checkUserFromToken } from '@documenso/lib/server-only/public-api/get-user-by-token';
|
||||
import { putFile } from '@documenso/lib/universal/upload/put-file';
|
||||
import { contract } from '@documenso/trpc/api-contract/contract';
|
||||
import { createNextRoute, createNextRouter } from '@documenso/trpc/server/public-api/ts-rest';
|
||||
|
||||
@ -89,11 +92,17 @@ const router = createNextRoute(contract, {
|
||||
}
|
||||
|
||||
try {
|
||||
const document = await deleteDraftDocument({ id: Number(documentId), userId: user.id });
|
||||
const document = await getDocumentById({ id: Number(documentId), userId: user.id });
|
||||
|
||||
const deletedDocument = await deleteDocument({
|
||||
id: Number(documentId),
|
||||
userId: user.id,
|
||||
status: document.status,
|
||||
});
|
||||
|
||||
return {
|
||||
status: 200,
|
||||
body: document,
|
||||
body: deletedDocument,
|
||||
};
|
||||
} catch (e) {
|
||||
return {
|
||||
@ -104,6 +113,64 @@ const router = createNextRoute(contract, {
|
||||
};
|
||||
}
|
||||
},
|
||||
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';
|
||||
|
||||
console.log(body.toString());
|
||||
|
||||
const file = new Blob([body.toString()], {
|
||||
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.',
|
||||
},
|
||||
};
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
const nextRouter = createNextRouter(contract, router);
|
||||
|
||||
50
package-lock.json
generated
50
package-lock.json
generated
@ -167,6 +167,18 @@
|
||||
"url": "https://github.com/sponsors/sindresorhus"
|
||||
}
|
||||
},
|
||||
"node_modules/@anatine/zod-openapi": {
|
||||
"version": "1.14.2",
|
||||
"resolved": "https://registry.npmjs.org/@anatine/zod-openapi/-/zod-openapi-1.14.2.tgz",
|
||||
"integrity": "sha512-q0qHfnuNYVKu0Swrnnvfj9971AEyW7c8v9jCOZGCl5ZbyGMNG4RPyJkRcMi/JC8CRfdOe0IDfNm1nNsi2avprg==",
|
||||
"dependencies": {
|
||||
"ts-deepmerge": "^6.0.3"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"openapi3-ts": "^2.0.0 || ^3.0.0",
|
||||
"zod": "^3.20.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@aws-crypto/crc32": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@aws-crypto/crc32/-/crc32-3.0.0.tgz",
|
||||
@ -5989,6 +6001,19 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@ts-rest/open-api": {
|
||||
"version": "3.30.5",
|
||||
"resolved": "https://registry.npmjs.org/@ts-rest/open-api/-/open-api-3.30.5.tgz",
|
||||
"integrity": "sha512-FOq6afvj6VCLMSQEO8J0B2YuZ2BfvQrscMy9i5rinI4sJO2/q17fdUqOoT9AI6n4coHCOFpcRUOz2xks7Nn5fA==",
|
||||
"dependencies": {
|
||||
"@anatine/zod-openapi": "^1.12.0",
|
||||
"openapi3-ts": "^2.0.2"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@ts-rest/core": "3.30.5",
|
||||
"zod": "^3.22.3"
|
||||
}
|
||||
},
|
||||
"node_modules/@tsconfig/node10": {
|
||||
"version": "1.0.9",
|
||||
"resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.9.tgz",
|
||||
@ -14367,6 +14392,22 @@
|
||||
"node": ">= 14.17.0"
|
||||
}
|
||||
},
|
||||
"node_modules/openapi3-ts": {
|
||||
"version": "2.0.2",
|
||||
"resolved": "https://registry.npmjs.org/openapi3-ts/-/openapi3-ts-2.0.2.tgz",
|
||||
"integrity": "sha512-TxhYBMoqx9frXyOgnRHufjQfPXomTIHYKhSKJ6jHfj13kS8OEIhvmE8CTuQyKtjjWttAjX5DPxM1vmalEpo8Qw==",
|
||||
"dependencies": {
|
||||
"yaml": "^1.10.2"
|
||||
}
|
||||
},
|
||||
"node_modules/openapi3-ts/node_modules/yaml": {
|
||||
"version": "1.10.2",
|
||||
"resolved": "https://registry.npmjs.org/yaml/-/yaml-1.10.2.tgz",
|
||||
"integrity": "sha512-r3vXyErRCYJ7wg28yvBY5VSoAF8ZvlcW9/BwUzEtUsjvX/DKs24dIkuwjtuprwJJHsbyUbLApepYTR1BN4uHrg==",
|
||||
"engines": {
|
||||
"node": ">= 6"
|
||||
}
|
||||
},
|
||||
"node_modules/openid-client": {
|
||||
"version": "5.6.1",
|
||||
"resolved": "https://registry.npmjs.org/openid-client/-/openid-client-5.6.1.tgz",
|
||||
@ -17830,6 +17871,14 @@
|
||||
"typescript": ">=4.2.0"
|
||||
}
|
||||
},
|
||||
"node_modules/ts-deepmerge": {
|
||||
"version": "6.2.0",
|
||||
"resolved": "https://registry.npmjs.org/ts-deepmerge/-/ts-deepmerge-6.2.0.tgz",
|
||||
"integrity": "sha512-2qxI/FZVDPbzh63GwWIZYE7daWKtwXZYuyc8YNq0iTmMUwn4mL0jRLsp6hfFlgbdRSR4x2ppe+E86FnvEpN7Nw==",
|
||||
"engines": {
|
||||
"node": ">=14.13.1"
|
||||
}
|
||||
},
|
||||
"node_modules/ts-interface-checker": {
|
||||
"version": "0.1.13",
|
||||
"resolved": "https://registry.npmjs.org/ts-interface-checker/-/ts-interface-checker-0.1.13.tgz",
|
||||
@ -19490,6 +19539,7 @@
|
||||
"@trpc/server": "^10.36.0",
|
||||
"@ts-rest/core": "^3.30.5",
|
||||
"@ts-rest/next": "^3.30.5",
|
||||
"@ts-rest/open-api": "^3.30.5",
|
||||
"luxon": "^3.4.0",
|
||||
"superjson": "^1.13.1",
|
||||
"ts-pattern": "^5.0.5",
|
||||
|
||||
@ -47,7 +47,6 @@
|
||||
"apps/*",
|
||||
"packages/*"
|
||||
],
|
||||
"dependencies": {},
|
||||
"overrides": {
|
||||
"next-auth": {
|
||||
"next": "14.0.3"
|
||||
|
||||
@ -23,6 +23,18 @@ const DocumentSchema = z.object({
|
||||
completedAt: z.date().nullable(),
|
||||
});
|
||||
|
||||
const SendDocumentForSigningMutationSchema = z.object({
|
||||
signerEmail: z.string(),
|
||||
signerName: z.string().optional(),
|
||||
});
|
||||
|
||||
const UploadDocumentSuccessfulSchema = z.object({
|
||||
uploadedFile: z.object({
|
||||
id: z.number(),
|
||||
message: z.string(),
|
||||
}),
|
||||
});
|
||||
|
||||
const SuccessfulResponseSchema = z.object({
|
||||
documents: DocumentSchema.array(),
|
||||
totalPages: z.number(),
|
||||
@ -66,6 +78,18 @@ export const contract = c.router(
|
||||
},
|
||||
summary: 'Delete a document',
|
||||
},
|
||||
createDocument: {
|
||||
method: 'POST',
|
||||
path: '/documents',
|
||||
contentType: 'multipart/form-data',
|
||||
body: c.type<{ file: File }>(),
|
||||
responses: {
|
||||
200: UploadDocumentSuccessfulSchema,
|
||||
401: UnsuccessfulResponseSchema,
|
||||
500: UnsuccessfulResponseSchema,
|
||||
},
|
||||
summary: 'Upload a new document',
|
||||
},
|
||||
},
|
||||
{
|
||||
baseHeaders: z.object({
|
||||
|
||||
@ -20,6 +20,7 @@
|
||||
"@ts-rest/core": "^3.30.5",
|
||||
"@ts-rest/next": "^3.30.5",
|
||||
"luxon": "^3.4.0",
|
||||
"@ts-rest/open-api": "^3.30.5",
|
||||
"superjson": "^1.13.1",
|
||||
"ts-pattern": "^5.0.5",
|
||||
"zod": "^3.22.4"
|
||||
|
||||
Reference in New Issue
Block a user