Fixing a Paperless bug
This commit is contained in:
@ -10,10 +10,8 @@ export default createOperation.query({
|
||||
headers: {
|
||||
Authorization: process.env.PAPERLESS_TOKEN,
|
||||
},
|
||||
|
||||
});
|
||||
|
||||
|
||||
// Add download link, thumbnail link, preview link, PDF data, and metadata to each document
|
||||
const documentsWithLinksDataAndMetadata = await Promise.all(data.results.map(async doc => {
|
||||
const response = await axios.get(`https://paperless.andert.me/api/documents/${doc.id}/preview/`, {
|
||||
@ -25,26 +23,35 @@ export default createOperation.query({
|
||||
|
||||
const pdfData = Buffer.from(response.data, 'binary').toString('base64');
|
||||
|
||||
const correspondentResponse = await axios.get(`https://paperless.andert.me/api/correspondents/${doc.correspondent}/`, {
|
||||
headers: {
|
||||
Authorization: process.env.PAPERLESS_TOKEN,
|
||||
},
|
||||
});
|
||||
const correspondent = correspondentResponse.data;
|
||||
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;
|
||||
}
|
||||
|
||||
const tagsResponse = await Promise.all(doc.tags.map(tag => axios.get(`https://paperless.andert.me/api/tags/${tag}/`, {
|
||||
headers: {
|
||||
Authorization: process.env.PAPERLESS_TOKEN,
|
||||
},
|
||||
})));
|
||||
const tags = tagsResponse.map(response => response.data);
|
||||
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);
|
||||
}
|
||||
|
||||
const documentTypeResponse = await axios.get(`https://paperless.andert.me/api/document_types/${doc.document_type}/`, {
|
||||
headers: {
|
||||
Authorization: process.env.PAPERLESS_TOKEN,
|
||||
},
|
||||
});
|
||||
const documentType = documentTypeResponse.data;
|
||||
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;
|
||||
}
|
||||
|
||||
return {
|
||||
...doc,
|
||||
@ -54,7 +61,7 @@ export default createOperation.query({
|
||||
pdfData,
|
||||
correspondent,
|
||||
tags,
|
||||
document_type: documentType,
|
||||
documentType,
|
||||
};
|
||||
}));
|
||||
|
||||
|
57
.wundergraph/operations/paper.ts
Normal file
57
.wundergraph/operations/paper.ts
Normal file
@ -0,0 +1,57 @@
|
||||
// .wundergraph/operations/paper.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,
|
||||
},
|
||||
});
|
||||
|
||||
// Fetch additional metadata for each document
|
||||
const documents = await Promise.all(data.results.map(async doc => {
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
return {
|
||||
...doc,
|
||||
correspondent,
|
||||
tags,
|
||||
documentType,
|
||||
};
|
||||
}));
|
||||
|
||||
return documents;
|
||||
},
|
||||
});
|
Reference in New Issue
Block a user