-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathdocumentsService.js
49 lines (42 loc) · 1.31 KB
/
documentsService.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// services/documentsService.js
const paperlessService = require('./paperlessService');
class DocumentsService {
constructor() {
this.tagCache = new Map();
this.correspondentCache = new Map();
}
async getTagNames() {
if (this.tagCache.size === 0) {
const tags = await paperlessService.getTags();
tags.forEach(tag => {
this.tagCache.set(tag.id, tag.name);
});
}
return Object.fromEntries(this.tagCache);
}
async getCorrespondentNames() {
if (this.correspondentCache.size === 0) {
const correspondents = await paperlessService.listCorrespondentsNames();
correspondents.forEach(corr => {
this.correspondentCache.set(corr.id, corr.name);
});
}
return Object.fromEntries(this.correspondentCache);
}
async getDocumentsWithMetadata() {
const [documents, tagNames, correspondentNames] = await Promise.all([
paperlessService.getDocuments(),
this.getTagNames(),
this.getCorrespondentNames()
]);
// Sort documents by created date (newest first)
documents.sort((a, b) => new Date(b.created) - new Date(a.created));
return {
documents,
tagNames,
correspondentNames,
paperlessUrl: process.env.PAPERLESS_API_URL.replace('/api', '')
};
}
}
module.exports = new DocumentsService();