2023-09-20 18:56:27 +00:00
|
|
|
// .wundergraph/operations/getPaperless.ts
|
|
|
|
import { createOperation, z } from '../generated/wundergraph.factory';
|
|
|
|
import axios from 'axios';
|
|
|
|
|
|
|
|
export default createOperation.query({
|
|
|
|
input: z.object({}),
|
|
|
|
handler: async () => {
|
|
|
|
console.log('Making request to Paperless API');
|
|
|
|
const { data } = await axios.get('https://paperless.andert.me/api/documents/', {
|
|
|
|
headers: {
|
|
|
|
Authorization: process.env.PAPERLESS_TOKEN,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-09-21 18:45:49 +00:00
|
|
|
// Add download link, thumbnail link, preview link, PDF data, and metadata to each document
|
|
|
|
const documentsWithLinksDataAndMetadata = await Promise.all(data.results.map(async doc => {
|
2023-09-20 18:56:27 +00:00
|
|
|
const response = await axios.get(`https://paperless.andert.me/api/documents/${doc.id}/preview/`, {
|
|
|
|
responseType: 'arraybuffer',
|
|
|
|
headers: {
|
|
|
|
Authorization: process.env.PAPERLESS_TOKEN,
|
|
|
|
},
|
|
|
|
});
|
2023-09-21 18:45:49 +00:00
|
|
|
|
2023-09-20 18:56:27 +00:00
|
|
|
const pdfData = Buffer.from(response.data, 'binary').toString('base64');
|
|
|
|
|
2023-10-17 07:42:09 +00:00
|
|
|
let correspondent = null;
|
|
|
|
if (doc.correspondent) {
|
|
|
|
const correspondentResponse = await axios.get(`https://paperless.andert.me/api/correspondents/${doc.correspondent}/`, {
|
|
|
|
headers: {
|
|
|
|
Authorization: process.env.PAPERLESS_TOKEN,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
correspondent = correspondentResponse.data;
|
|
|
|
}
|
2023-09-21 18:45:49 +00:00
|
|
|
|
2023-10-17 07:42:09 +00:00
|
|
|
let tags = [];
|
|
|
|
if (doc.tags) {
|
|
|
|
const tagsResponse = await Promise.all(doc.tags.map(tag => axios.get(`https://paperless.andert.me/api/tags/${tag}/`, {
|
|
|
|
headers: {
|
|
|
|
Authorization: process.env.PAPERLESS_TOKEN,
|
|
|
|
},
|
|
|
|
})));
|
|
|
|
tags = tagsResponse.map(response => response.data);
|
|
|
|
}
|
2023-09-21 18:45:49 +00:00
|
|
|
|
2023-10-17 07:42:09 +00:00
|
|
|
let documentType = null;
|
|
|
|
if (doc.document_type) {
|
|
|
|
const documentTypeResponse = await axios.get(`https://paperless.andert.me/api/document_types/${doc.document_type}/`, {
|
|
|
|
headers: {
|
|
|
|
Authorization: process.env.PAPERLESS_TOKEN,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
documentType = documentTypeResponse.data;
|
|
|
|
}
|
2023-09-21 18:45:49 +00:00
|
|
|
|
2023-09-20 18:56:27 +00:00
|
|
|
return {
|
|
|
|
...doc,
|
|
|
|
downloadLink: `https://paperless.andert.me/api/documents/${doc.id}/download/`,
|
|
|
|
thumbnailLink: `https://paperless.andert.me/api/documents/${doc.id}/thumb/`,
|
|
|
|
previewLink: `https://paperless.andert.me/api/documents/${doc.id}/preview/`,
|
|
|
|
pdfData,
|
2023-09-21 18:45:49 +00:00
|
|
|
correspondent,
|
|
|
|
tags,
|
2023-10-17 07:42:09 +00:00
|
|
|
documentType,
|
2023-09-20 18:56:27 +00:00
|
|
|
};
|
|
|
|
}));
|
|
|
|
|
2023-09-21 18:45:49 +00:00
|
|
|
return documentsWithLinksDataAndMetadata;
|
2023-09-20 18:56:27 +00:00
|
|
|
},
|
|
|
|
});
|