-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
1159 lines (1047 loc) · 43.6 KB
/
Copy pathserver.ts
File metadata and controls
1159 lines (1047 loc) · 43.6 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @license
* SPDX-License-Identifier: Apache-2.0
*/
import express from 'express';
import path from 'path';
import fs from 'fs';
import { createServer as createViteServer } from 'vite';
import { GoogleGenAI, Type } from '@google/genai';
import dotenv from 'dotenv';
import { DEFAULT_DB } from './src/utils/demoData';
dotenv.config();
const app = express();
const PORT = 3000;
// Resolve clean workspace data directory on disk
let DATA_DIR = path.join(process.cwd(), 'db-data');
const OLD_DATA_DIR = path.join(process.cwd(), 'data');
const configPath = path.join(process.cwd(), 'storage_config.json');
// Self-healing migration from legacy 'data' directory to 'db-data'
if (fs.existsSync(OLD_DATA_DIR)) {
try {
if (!fs.existsSync(DATA_DIR)) {
fs.renameSync(OLD_DATA_DIR, DATA_DIR);
console.log(`Successfully migrated legacy data folder to 'db-data' on disk.`);
} else {
// If both exist, copy contents of OLD_DATA_DIR into DATA_DIR to ensure DATA_DIR is fully up-to-date
const copyRecursive = (src: string, dest: string) => {
const stats = fs.statSync(src);
if (stats.isDirectory()) {
if (!fs.existsSync(dest)) fs.mkdirSync(dest, { recursive: true });
fs.readdirSync(src).forEach((child) => {
copyRecursive(path.join(src, child), path.join(dest, child));
});
} else {
// Overwrite always to make sure we get the latest active data
fs.copyFileSync(src, dest);
}
};
copyRecursive(OLD_DATA_DIR, DATA_DIR);
console.log(`Successfully copied/synchronized latest active data from 'data' to 'db-data'.`);
}
} catch (err) {
console.error('Migration from legacy data folder failed:', err);
}
}
if (fs.existsSync(configPath)) {
try {
const cfg = JSON.parse(fs.readFileSync(configPath, 'utf8'));
if (cfg.customDataDir) {
let resolved = path.resolve(cfg.customDataDir.trim());
// If storage_config.json refers to the old 'data' directory, migrate it to 'db-data'
if (resolved === path.resolve(OLD_DATA_DIR)) {
resolved = path.resolve(DATA_DIR);
fs.writeFileSync(configPath, JSON.stringify({ customDataDir: resolved }, null, 2), 'utf8');
console.log(`Successfully updated storage_config.json path to ${resolved}`);
}
DATA_DIR = resolved;
}
} catch (e) {
console.error('Failed to parse storage_config.json:', e);
}
}
if (!fs.existsSync(DATA_DIR)) {
fs.mkdirSync(DATA_DIR, { recursive: true });
}
/**
* Ensures that every company folder listed in the folders.json has an actual
* physical directory created for it on disk, containing its database file (db.json).
* It also migrates old flat db_*.json files into their respective subdirectories automatically.
*/
function ensureDirectoriesExist() {
const foldersPath = path.join(DATA_DIR, 'folders.json');
let foldersList: any[] = [];
if (fs.existsSync(foldersPath)) {
try {
foldersList = JSON.parse(fs.readFileSync(foldersPath, 'utf8'));
} catch (e) {
console.error('Failed reading folders.json during alignment:', e);
}
}
// If empty or invalid, fall back to defaults
if (!foldersList || !Array.isArray(foldersList) || foldersList.length === 0) {
foldersList = [
{
id: 'folder-demo-advertising',
name: 'Avantgarde Creative Agency e.K.',
color: 'indigo',
isDemo: true,
createdAt: new Date().toISOString()
},
{
id: 'folder-my-company',
name: 'Eigene Unternehmung',
color: 'emerald',
isDemo: false,
createdAt: new Date().toISOString()
}
];
try {
fs.writeFileSync(foldersPath, JSON.stringify(foldersList, null, 2), 'utf8');
} catch (e) {
console.error('Failed initialization of default folders.json:', e);
}
}
// Double check that folders.json includes its physical directory and db file
for (const f of foldersList) {
if (!f.id) continue;
const folderDir = path.join(DATA_DIR, f.id);
if (!fs.existsSync(folderDir)) {
try {
fs.mkdirSync(folderDir, { recursive: true });
console.log(`Created physical directory for firm: ${folderDir}`);
} catch (err) {
console.error(`Failed creating company directory ${folderDir}:`, err);
}
}
const nestedDbPath = path.join(folderDir, 'db.json');
const legacyFlatDbPath = path.join(DATA_DIR, `db_${f.id}.json`);
// We check if it is the demo folder and needs seeding or re-seeding
let needsDemoSeeding = false;
if (f.id === 'folder-demo-advertising' || f.isDemo) {
if (!fs.existsSync(nestedDbPath)) {
needsDemoSeeding = true;
} else {
try {
const content = JSON.parse(fs.readFileSync(nestedDbPath, 'utf8'));
if (!content.invoices || content.invoices.length === 0) {
needsDemoSeeding = true; // Seed/restore if empty/partial/wiped
}
} catch (e) {
needsDemoSeeding = true;
}
}
}
if (needsDemoSeeding) {
try {
// Extract and physically save any demo receipt files first
saveReceiptFiles(f.id, DEFAULT_DB);
// Keep database JSON on disk light by removing base64 values
const dbToSave = JSON.parse(JSON.stringify(DEFAULT_DB));
if (dbToSave && Array.isArray(dbToSave.expenses)) {
for (const exp of dbToSave.expenses) {
if (exp.receiptAttached && exp.receiptFileName) {
delete exp.receiptBase64;
}
}
}
fs.writeFileSync(nestedDbPath, JSON.stringify(dbToSave, null, 2), 'utf8');
console.log(`Successfully seeded the default demo database at: ${nestedDbPath}`);
} catch (err) {
console.error(`Could not write default seeded db file for ${f.id}:`, err);
}
} else if (!fs.existsSync(nestedDbPath)) {
if (fs.existsSync(legacyFlatDbPath)) {
try {
fs.copyFileSync(legacyFlatDbPath, nestedDbPath);
fs.unlinkSync(legacyFlatDbPath);
console.log(`Successfully migrated legacy flat database to nested workspace: ${nestedDbPath}`);
} catch (err) {
console.error(`Failed migrating flat database for ${f.id}:`, err);
}
} else {
// Create an empty, initial database shell so that a physical file exists immediately
const initialDB = {
settings: {
companyName: f.name,
ownerName: 'Admin',
street: 'Hauptstraße 1',
zipCode: '10115',
city: 'Berlin',
taxPeriod: 'monthly',
kontenrahmen: 'SKR03',
isOnboarded: false
},
contacts: [],
invoices: [],
expenses: [],
cashbook: [],
transactions: [],
articles: [],
deadlines: []
};
try {
fs.writeFileSync(nestedDbPath, JSON.stringify(initialDB, null, 2), 'utf8');
console.log(`Established fresh physical database file at: ${nestedDbPath}`);
} catch (err) {
console.error(`Could not write default db file for ${f.id}:`, err);
}
}
}
}
}
// Perform active alignment immediately on launch
ensureDirectoriesExist();
// Set request payload limits high enough to handle base64 receipt uploads
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ limit: '50mb', extended: true }));
// --- SERVER-SIDE FILE PERSISTENCE API ENDPOINTS ---
/**
* 1. GET /api/folders
* Returns folders list from disk folders.json, or sets up defaults. Also aligns physical subfolders if needed.
*/
app.get('/api/folders', (req, res) => {
ensureDirectoriesExist();
const foldersPath = path.join(DATA_DIR, 'folders.json');
if (fs.existsSync(foldersPath)) {
try {
const data = fs.readFileSync(foldersPath, 'utf8');
return res.json(JSON.parse(data));
} catch (e) {
console.error('Failed reading folders.json:', e);
}
}
res.json([]);
});
/**
* 2. POST /api/folders
* Saves folders configuration to disk folders.json
*/
app.post('/api/folders', (req, res) => {
const { folders } = req.body;
if (!folders || !Array.isArray(folders)) {
return res.status(400).json({ error: 'INVALID_FOLDERS_STRUCT', message: 'Ungültige Ordner-Konfiguration.' });
}
const foldersPath = path.join(DATA_DIR, 'folders.json');
try {
fs.writeFileSync(foldersPath, JSON.stringify(folders, null, 2), 'utf8');
// Instantly reflect actual folders on drive
ensureDirectoriesExist();
res.json({ success: true });
} catch (e: any) {
console.error('Error writing folders.json to disk:', e);
res.status(500).json({ error: 'DISK_WRITE_ERROR', message: e.message });
}
});
/**
* 3. GET /api/active-folder
* Returns current active folder ID saved on disk active_folder_id.txt
*/
app.get('/api/active-folder', (req, res) => {
const activePath = path.join(DATA_DIR, 'active_folder_id.txt');
if (fs.existsSync(activePath)) {
try {
const activeId = fs.readFileSync(activePath, 'utf8').trim();
return res.json({ activeFolderId: activeId });
} catch (e) {
console.error('Error reading active_folder_id.txt:', e);
}
}
res.json({ activeFolderId: 'folder-demo-advertising' });
});
/**
* 4. POST /api/active-folder
* Saves current active folder ID to disk active_folder_id.txt
*/
app.post('/api/active-folder', (req, res) => {
const { activeFolderId } = req.body;
if (!activeFolderId) {
return res.status(400).json({ error: 'INVALID_ID_VALUE', message: 'activeFolderId muss angegeben werden.' });
}
const activePath = path.join(DATA_DIR, 'active_folder_id.txt');
try {
fs.writeFileSync(activePath, activeFolderId, 'utf8');
res.json({ success: true });
} catch (e: any) {
console.error('Error saving active_folder_id.txt to disk:', e);
res.status(500).json({ error: 'DISK_WRITE_ERROR', message: e.message });
}
});
/**
* Saves base64 receipt attachments as individual physical files in the folder's 'belege' subdirectory
*/
function saveReceiptFiles(folderId: string, db: any): void {
if (!db || !Array.isArray(db.expenses)) return;
const belegeDir = path.join(DATA_DIR, folderId, 'belege');
if (!fs.existsSync(belegeDir)) {
fs.mkdirSync(belegeDir, { recursive: true });
}
const referencedFiles = new Set<string>();
for (const exp of db.expenses) {
if (exp.receiptAttached && exp.receiptFileName) {
const sanitizedFileName = path.basename(exp.receiptFileName);
referencedFiles.add(sanitizedFileName);
if (exp.receiptBase64 && typeof exp.receiptBase64 === 'string') {
const matches = exp.receiptBase64.match(/^data:([^;]+);base64,(.+)$/);
try {
let buffer: Buffer;
if (matches) {
buffer = Buffer.from(matches[2], 'base64');
} else {
// Raw base64 or other format
buffer = Buffer.from(exp.receiptBase64, 'base64');
}
const filePath = path.join(belegeDir, sanitizedFileName);
fs.writeFileSync(filePath, buffer);
console.log(`Saved physical receipt file: ${filePath}`);
} catch (e) {
console.error(`Error saving physical receipt file ${sanitizedFileName}:`, e);
}
}
}
}
// Delete orphaned physical files in the folder's belegeDir that are no longer referenced in db
if (fs.existsSync(belegeDir)) {
try {
const files = fs.readdirSync(belegeDir);
for (const file of files) {
if (!referencedFiles.has(file)) {
const orphanPath = path.join(belegeDir, file);
fs.unlinkSync(orphanPath);
console.log(`Physically deleted orphaned receipt: ${orphanPath}`);
}
}
} catch (e) {
console.error(`Error cleaning up orphaned files in ${belegeDir}:`, e);
}
}
}
/**
* Loads physical receipt files from the 'belege' subdirectory and populates receiptBase64 for the client
*/
function loadReceiptFiles(folderId: string, db: any): void {
if (!db || !Array.isArray(db.expenses)) return;
const belegeDir = path.join(DATA_DIR, folderId, 'belege');
if (!fs.existsSync(belegeDir)) return;
for (const exp of db.expenses) {
if (exp.receiptAttached && exp.receiptFileName) {
const sanitizedFileName = path.basename(exp.receiptFileName);
const filePath = path.join(belegeDir, sanitizedFileName);
if (fs.existsSync(filePath)) {
try {
const buffer = fs.readFileSync(filePath);
const base64Data = buffer.toString('base64');
const ext = path.extname(sanitizedFileName).toLowerCase();
let mimeType = 'image/jpeg';
if (ext === '.pdf') {
mimeType = 'application/pdf';
} else if (ext === '.png') {
mimeType = 'image/png';
} else if (ext === '.gif') {
mimeType = 'image/gif';
} else if (ext === '.webp') {
mimeType = 'image/webp';
}
exp.receiptBase64 = `data:${mimeType};base64,${base64Data}`;
} catch (e) {
console.error(`Error loading physical receipt file ${sanitizedFileName}:`, e);
}
}
}
}
}
/**
* 5. GET /api/db/load
* Loads a folder specific database JSON from its corresponding subdirectory (e.g. data/folderId/db.json)
*/
app.get('/api/db/load', (req, res) => {
const { folderId } = req.query;
if (!folderId || typeof folderId !== 'string') {
return res.status(400).json({ error: 'INVALID_FOLDER_ID', message: 'folderId erforderlich.' });
}
// Prevent path traversal
if (folderId.includes('..') || folderId.includes('/') || folderId.includes('\\')) {
return res.status(400).json({ error: 'PATH_TRAVERSAL_PREVENTED', message: 'Ungültige folderId.' });
}
const nestedDbPath = path.join(DATA_DIR, folderId, 'db.json');
const legacyFlatPath = path.join(DATA_DIR, `db_${folderId}.json`);
// Try nested database path
if (fs.existsSync(nestedDbPath)) {
try {
const data = fs.readFileSync(nestedDbPath, 'utf8');
const parsed = JSON.parse(data);
// Load physical receipt file attachments from the subfolder
loadReceiptFiles(folderId, parsed);
return res.json(parsed);
} catch (e) {
console.error(`Error loading database file ${nestedDbPath} from disk:`, e);
}
}
// Fallback / Auto-migration during load
if (fs.existsSync(legacyFlatPath)) {
try {
const data = fs.readFileSync(legacyFlatPath, 'utf8');
const parsed = JSON.parse(data);
const folderDir = path.join(DATA_DIR, folderId);
if (!fs.existsSync(folderDir)) {
fs.mkdirSync(folderDir, { recursive: true });
}
fs.writeFileSync(nestedDbPath, JSON.stringify(parsed, null, 2), 'utf8');
fs.unlinkSync(legacyFlatPath);
// Save physical receipt files if any legacy ones are present as base64 in the loaded state
saveReceiptFiles(folderId, parsed);
return res.json(parsed);
} catch (e) {
console.error(`Error migrating legacy database for ${folderId} on-the-fly:`, e);
}
}
res.json({ notFound: true });
});
/**
* 6. POST /api/db/save
* Writes standard pretty-printed database JSON content to the nested subdirectory (data/folderId/db.json)
*/
app.post('/api/db/save', (req, res) => {
const { folderId, db } = req.body;
if (!folderId || !db) {
return res.status(400).json({ error: 'INVALID_PAYLOAD_STRUCT', message: 'folderId und db-Objekt fehlen.' });
}
// Prevent path traversal
if (folderId.includes('..') || folderId.includes('/') || folderId.includes('\\')) {
return res.status(400).json({ error: 'PATH_TRAVERSAL_PREVENTED', message: 'Ungültige folderId.' });
}
const folderDir = path.join(DATA_DIR, folderId);
if (!fs.existsSync(folderDir)) {
fs.mkdirSync(folderDir, { recursive: true });
}
// Extract and save receipt files to the physical subfolder
saveReceiptFiles(folderId, db);
// Strip large base64 strings from db.json to keep filesystem clean and fast
const dbToSave = JSON.parse(JSON.stringify(db));
if (dbToSave && Array.isArray(dbToSave.expenses)) {
for (const exp of dbToSave.expenses) {
if (exp.receiptAttached && exp.receiptFileName) {
delete exp.receiptBase64;
}
}
}
const dbPath = path.join(folderDir, 'db.json');
try {
fs.writeFileSync(dbPath, JSON.stringify(dbToSave, null, 2), 'utf8');
res.json({ success: true });
} catch (e: any) {
console.error(`Error saving database file ${dbPath} to disk:`, e);
res.status(500).json({ error: 'DISK_WRITE_ERROR', message: e.message });
}
});
/**
* 6c. POST /api/db/save-document
* Saves a document file physically to a dynamic subdirectory (belege, rechnungen, or dokumente) inside the active folder.
*/
app.post('/api/db/save-document', (req, res) => {
const { folderId, fileName, base64, subfolder } = req.body;
if (!folderId || !fileName || !base64 || !subfolder) {
return res.status(400).json({ error: 'INVALID_PAYLOAD', message: 'Pfad, Dateiname, Inhalt und Unterordner erforderlich.' });
}
// Prevent path traversal
if (folderId.includes('..') || folderId.includes('/') || folderId.includes('\\') ||
fileName.includes('..') || fileName.includes('/') || fileName.includes('\\') ||
subfolder.includes('..') || subfolder.includes('/') || subfolder.includes('\\')) {
return res.status(400).json({ error: 'PATH_TRAVERSAL_PREVENTED', message: 'Ungültige Pfadangaben.' });
}
const targetDir = path.join(DATA_DIR, folderId, subfolder);
if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir, { recursive: true });
}
const cleanBase64 = base64.replace(/^data:[^;]+;base64,/, '');
try {
const buffer = Buffer.from(cleanBase64, 'base64');
const filePath = path.join(targetDir, fileName);
fs.writeFileSync(filePath, buffer);
console.log(`Physically saved document under relevant folder: ${filePath}`);
res.json({ success: true, path: filePath });
} catch (e: any) {
console.error(`Error saving document ${fileName} in ${subfolder}:`, e);
res.status(500).json({ error: 'DISK_WRITE_ERROR', message: e.message });
}
});
/**
* 6d. GET /api/db/get-document
* Serves or downloads a physical file from the active folder's subdirectories.
*/
app.get('/api/db/get-document', (req, res) => {
const { folderId, subfolder, fileName } = req.query;
if (!folderId || !subfolder || !fileName || typeof folderId !== 'string' || typeof subfolder !== 'string' || typeof fileName !== 'string') {
return res.status(400).json({ error: 'INVALID_PARAMS', message: 'Parameter fehlen.' });
}
// Prevent path traversal
if (folderId.includes('..') || folderId.includes('/') || folderId.includes('\\') ||
subfolder.includes('..') || subfolder.includes('/') || subfolder.includes('\\') ||
fileName.includes('..') || fileName.includes('/') || fileName.includes('\\')) {
return res.status(400).json({ error: 'PATH_TRAVERSAL_PREVENTED', message: 'Ungültige Pfadangaben.' });
}
const filePath = path.join(DATA_DIR, folderId, subfolder, fileName);
if (!fs.existsSync(filePath)) {
return res.status(404).json({ error: 'FILE_NOT_FOUND', message: 'Datei nicht gefunden.' });
}
try {
const stat = fs.statSync(filePath);
const fileContent = fs.readFileSync(filePath);
const ext = path.extname(fileName).toLowerCase();
let mimeType = 'application/octet-stream';
if (ext === '.pdf') {
mimeType = 'application/pdf';
} else if (ext === '.png') {
mimeType = 'image/png';
} else if (ext === '.jpg' || ext === '.jpeg') {
mimeType = 'image/jpeg';
} else if (ext === '.gif') {
mimeType = 'image/gif';
} else if (ext === '.webp') {
mimeType = 'image/webp';
} else if (ext === '.txt') {
mimeType = 'text/plain';
} else if (ext === '.json') {
mimeType = 'application/json';
}
res.setHeader('Content-Type', mimeType);
res.setHeader('Content-Length', stat.size);
res.setHeader('Content-Disposition', `inline; filename="${encodeURIComponent(fileName)}"`);
res.send(fileContent);
} catch (e: any) {
console.error(`Error serving document ${fileName}:`, e);
res.status(500).json({ error: 'SERVE_ERROR', message: e.message });
}
});
/**
* 6e. POST /api/db/delete-document
* Deletes a physical file from the active folder's subdirectories.
*/
app.post('/api/db/delete-document', (req, res) => {
const { folderId, subfolder, fileName } = req.body;
if (!folderId || !subfolder || !fileName) {
return res.status(400).json({ error: 'INVALID_PARAMS', message: 'Parameter fehlen.' });
}
// Prevent path traversal
if (folderId.includes('..') || folderId.includes('/') || folderId.includes('\\') ||
subfolder.includes('..') || subfolder.includes('/') || subfolder.includes('\\') ||
fileName.includes('..') || fileName.includes('/') || fileName.includes('\\')) {
return res.status(400).json({ error: 'PATH_TRAVERSAL_PREVENTED', message: 'Ungültige Pfadangaben.' });
}
const filePath = path.join(DATA_DIR, folderId, subfolder, fileName);
if (!fs.existsSync(filePath)) {
return res.status(404).json({ error: 'FILE_NOT_FOUND', message: 'Datei nicht gefunden.' });
}
try {
fs.unlinkSync(filePath);
console.log(`Physically deleted document: ${filePath}`);
res.json({ success: true });
} catch (e: any) {
console.error(`Error deleting document ${fileName}:`, e);
res.status(500).json({ error: 'DELETE_ERROR', message: e.message });
}
});
/**
* 6b. POST /api/db/delete-folder
* Physically deletes a specific folder directory from disk (recursive)
*/
app.post('/api/db/delete-folder', (req, res) => {
const { folderId } = req.body;
if (!folderId || typeof folderId !== 'string') {
return res.status(400).json({ error: 'INVALID_FOLDER_ID', message: 'folderId ist erforderlich.' });
}
// Prevent path traversal
if (folderId.includes('..') || folderId.includes('/') || folderId.includes('\\')) {
return res.status(400).json({ error: 'INVALID_FOLDER_ID', message: 'Ungültige folderId.' });
}
const folderDir = path.join(DATA_DIR, folderId);
try {
if (fs.existsSync(folderDir)) {
fs.rmSync(folderDir, { recursive: true, force: true });
console.log(`Physically deleted folder subdirectory from disk: ${folderDir}`);
}
res.json({ success: true });
} catch (e: any) {
console.error(`Error deleting folder ${folderId} from disk:`, e);
res.status(500).json({ error: 'DISK_DELETE_ERROR', message: e.message });
}
});
/**
* 7. POST /api/db/delete-all
* Deletes all database assets from disk (reset app)
*/
app.post('/api/db/delete-all', (req, res) => {
const foldersPath = path.join(DATA_DIR, 'folders.json');
const activePath = path.join(DATA_DIR, 'active_folder_id.txt');
try {
if (fs.existsSync(foldersPath)) fs.unlinkSync(foldersPath);
if (fs.existsSync(activePath)) fs.unlinkSync(activePath);
const files = fs.readdirSync(DATA_DIR);
for (const file of files) {
const fullPath = path.join(DATA_DIR, file);
if (fs.statSync(fullPath).isDirectory()) {
fs.rmSync(fullPath, { recursive: true, force: true });
} else if (file !== '.gitkeep' && file !== 'folders.json' && file !== 'active_folder_id.txt') {
fs.unlinkSync(fullPath);
}
}
res.json({ success: true });
} catch (e: any) {
console.error('Error clearing data folder:', e);
res.status(500).json({ error: 'DISK_DELETE_ERROR', message: e.message });
}
});
/**
* Helper to recursively crawl data folder and find directories + nested files
*/
function crawlDirectory(basePath: string, relativePath: string = ''): any[] {
let fileList: any[] = [];
const activePath = path.join(basePath, relativePath);
if (!fs.existsSync(activePath)) return [];
const items = fs.readdirSync(activePath);
for (const item of items) {
const relativeName = relativePath ? path.join(relativePath, item) : item;
const itemFullPath = path.join(basePath, relativeName);
const stat = fs.statSync(itemFullPath);
if (stat.isDirectory()) {
// Add directory node itself
fileList.push({
name: relativeName,
size: 0,
mtime: stat.mtime,
type: 'directory'
});
// Recurse down
fileList = fileList.concat(crawlDirectory(basePath, relativeName));
} else {
fileList.push({
name: relativeName,
size: stat.size,
mtime: stat.mtime,
type: relativeName.endsWith('.json') ? 'database' : relativeName.endsWith('.txt') ? 'text' : 'file'
});
}
}
return fileList;
}
/**
* 8. GET /api/data-tree
* Scans the data/ folder recursively and returns details about all subfolders and files
*/
app.get('/api/data-tree', (req, res) => {
try {
const crawledFiles = crawlDirectory(DATA_DIR);
res.json({ success: true, path: DATA_DIR, files: crawledFiles });
} catch (e: any) {
console.error('Error scanning nested data/ tree:', e);
res.status(500).json({ error: 'SCAN_ERROR', message: e.message });
}
});
/**
* 9. GET /api/storage-config
* Returns current dynamic files storage location metadata for offline admin control
*/
app.get('/api/storage-config', (req, res) => {
res.json({
success: true,
currentPath: DATA_DIR,
absolutePath: path.resolve(DATA_DIR),
isCustom: DATA_DIR !== path.join(process.cwd(), 'db-data'),
defaultPath: path.join(process.cwd(), 'db-data')
});
});
/**
* 10. POST /api/storage-config
* Changes the physical directory used for active database storage, optionally copying files
*/
app.post('/api/storage-config', (req, res) => {
const { customDataDir, copyExisting } = req.body;
if (!customDataDir) {
return res.status(400).json({ error: 'MISSING_PATH', message: 'Pfad-Angabe fehlt.' });
}
try {
const resolvedPath = path.resolve(customDataDir.trim());
// Attempt to verify/create directory
if (!fs.existsSync(resolvedPath)) {
fs.mkdirSync(resolvedPath, { recursive: true });
}
const oldDir = DATA_DIR;
// Transfer contents if requested
if (copyExisting && oldDir !== resolvedPath) {
if (fs.existsSync(oldDir)) {
const files = fs.readdirSync(oldDir);
for (const file of files) {
const src = path.join(oldDir, file);
const dest = path.join(resolvedPath, file);
if (fs.statSync(src).isFile()) {
fs.copyFileSync(src, dest);
}
}
}
}
// Save choice locally to survive restarts
fs.writeFileSync(configPath, JSON.stringify({ customDataDir: resolvedPath }, null, 2), 'utf8');
// Switch dynamic runtime pointer immediately
DATA_DIR = resolvedPath;
res.json({
success: true,
currentPath: DATA_DIR,
absolutePath: path.resolve(DATA_DIR),
isCustom: DATA_DIR !== path.join(process.cwd(), 'db-data')
});
} catch (e: any) {
console.error('Failed to update storage directory:', e);
res.status(500).json({ error: 'STORAGE_UPDATE_FAILED', message: e.message });
}
});
// Initialize Google GenAI client
// Initialize Google GenAI client
// User-Agent: 'aistudio-build' is set for telemetry
const ai = new GoogleGenAI({
apiKey: process.env.GEMINI_API_KEY,
httpOptions: {
headers: {
'User-Agent': 'aistudio-build',
},
},
});
// Helper: Get AI client, dynamically using header key if present
function getAiClient(req: express.Request): GoogleGenAI {
const customKey = req.headers['x-gemini-api-key'] as string;
if (customKey && customKey !== 'MY_GEMINI_API_KEY' && customKey.trim() !== '') {
return new GoogleGenAI({
apiKey: customKey,
httpOptions: {
headers: {
'User-Agent': 'aistudio-build',
},
},
});
}
return ai;
}
// Helper: Ensure Gemini API key is configured
function checkApiKey(req: express.Request, res: express.Response): boolean {
const customKey = req.headers['x-gemini-api-key'] as string;
if (customKey && customKey !== 'MY_GEMINI_API_KEY' && customKey.trim() !== '') {
return true;
}
if (!process.env.GEMINI_API_KEY || process.env.GEMINI_API_KEY === 'MY_GEMINI_API_KEY') {
res.status(500).json({
error: 'GEMINI_API_KEY_MISSING',
message: 'Der Gemini API Key fehlt. Bitte konfiguriere ihn in den Systemeinstellungen oder im AI Studio Secrets-Panel.',
});
return false;
}
return true;
}
/**
* Endpoint: /api/chat
* Handles conversational queries with Büro Susi, injecting full background company/business context
* and returning structured chat solutions + structured actionable payloads (proposals).
*/
app.post('/api/chat', async (req, res) => {
if (!checkApiKey(req, res)) return;
const { messages, userContext } = req.body;
if (!messages || !Array.isArray(messages)) {
res.status(400).json({ error: 'Falsche Eingabedaten', message: 'Nachrichten-Verlauf fehlt.' });
return;
}
// Build high-context system instructions incorporating user's parameters
const clientStatus = userContext || {};
const isKlein = clientStatus.isKleinunternehmer ? 'Ja (Umsatzsteuerbefreit nach §19 UStG. Umsatzsteuer darf auf Rechnungen NICHT erhoben werden)' : 'Nein (Regelbesteuert, 19% bzw. 7% MwSt. ist auszuweisen)';
const systemInstruction = `
Du bist Büro Susi – ein smarter, extrem benutzerfreundlicher Büro- und Buchhaltungs-Assistent für Selbstständige, Freiberufler und Kleinunternehmer in Deutschland.
Deine Zielgruppe hat keine tiefen Steuer- oder Buchhaltungskenntnisse. Antworte immer auf Deutsch, verständlich, unterstützend und lösungsorientiert. Unnötiges Steuer-Fachchinesisch ist zu vermeiden.
Aktueller Unternehmenskontext des Nutzers:
- Firmenname: ${clientStatus.companyName || 'Nicht angegeben'}
- Inhaber: ${clientStatus.ownerName || 'Nicht angegeben'}
- Kleinunternehmer-Status nach § 19 UStG: ${isKlein}
- Steuernummer: ${clientStatus.taxId || 'Nicht angegeben'}
- USt-IdNr: ${clientStatus.vatId || 'Nicht angegeben'}
- Zahl der vorhandenen Kunden/Lieferanten: ${clientStatus.contactsCount || 0}
- Zahl der erstellten Rechnungen/Angebote: ${clientStatus.invoicesCount || 0}
- Zahl der Ausgaben: ${clientStatus.expensesCount || 0}
Richtlinien für dein Verhalten:
1. Denke immer GoBD-konform. Belege müssen geordnet und lesbar sein.
2. Wenn der Nutzer Rechnungen erstellen will:
- Falls Kleinunternehmer, weise ihn freundlich darauf hin, dass keine Umsatzsteuer ausgewiesen wird, sondern der Steuerbefreiungshinweis (§ 19 UStG) Pflicht ist.
- Falls regelbesteuert, erinnere an 19% oder 7%.
3. Wenn der Nutzer nach Umsätzen oder Steuern fragt, gib präzise, übersichtliche Rückmeldungen. Nutze Markdown-Tabellen für Datenstrukturen.
4. Biete proaktiv Vorschläge an! Wenn der Nutzer sagt "Erstelle eine Rechnung an Sabine Becker über 5 Std Consulting zu je 100€", antworte hilfsbereit und fülle das "actionProposal" im Rückgabe-JSON aus, damit das UI das Formular direkt automatisch laden oder erstellen kann!
Schema für die Aktionstypen im "actionProposal" (falls zutreffend):
- "create_invoice": Erstellung einer Rechnung/eines Angebots. Datenstruktur:
{
"type": "invoice" | "quote",
"customerName": string,
"items": [{"name": string, "quantity": number, "unit": string, "unitPrice": number, "vatRate": number}]
}
- "add_expense": Buchen einer Ausgabe. Datenstruktur:
{
"supplierName": string,
"description": string,
"netAmount": number,
"vatRate": number,
"vatAmount": number,
"grossAmount": number,
"category": string
}
- "add_contact": Neuen Kontakt anlegen. Datenstruktur:
{
"type": "customer" | "supplier",
"name": string,
"contactPerson": string,
"email": string,
"phone": string
}
Antworte IMMER im spezifizierten JSON-Format mit den Feldern "reply" (Markdown Text) und optional "actionProposal".
`;
try {
// We map client messages to Google GenAI structure
// Translating role 'assistant' to 'model' and 'user' to 'user'
const contents = messages.map((m: any) => ({
role: m.sender === 'assistant' ? 'model' : 'user',
parts: [{ text: m.text }],
}));
const dynamicAi = getAiClient(req);
const response = await dynamicAi.models.generateContent({
model: 'gemini-3.5-flash',
contents: contents,
config: {
systemInstruction: systemInstruction,
responseMimeType: 'application/json',
responseSchema: {
type: Type.OBJECT,
properties: {
reply: {
type: Type.STRING,
description: 'Der eigentliche Antworttext auf Deutsch. Nutze Markdown zur Strukturierung (Überschriften, Tabellen, Aufzählungen).',
},
actionProposal: {
type: Type.OBJECT,
description: 'Wird befüllt, wenn der Benutzer im Chat explizit darum bittet eine Rechnung zu schreiben, Ausgaben zu buchen oder Kontakte anzulegen.',
properties: {
type: {
type: Type.STRING,
description: "Der Aktionstyp: 'create_invoice' | 'create_quote' | 'add_expense' | 'add_contact'",
},
data: {
type: Type.OBJECT,
description: 'Gepolsterte Werte für das Formular laut Spezifikation.',
},
},
},
},
required: ['reply'],
},
},
});
const resultText = response.text || '{}';
const parsedResult = JSON.parse(resultText);
res.json(parsedResult);
} catch (error: any) {
console.error('Büro Susi Chat Endpoint Error:', error);
res.status(500).json({
error: 'GEMINI_API_ERROR',
message: 'Fehler bei der Kommunikation mit Büro Susi. Bitte versuche es erneut.',
debug: error.message,
});
}
});
/**
* Endpoint: /api/analyze-receipt
* Uses Gemini-3.5-Flash to visually parse images of business receipts / bills
* and automatically proposes highly accurate tax booking values in German standards.
*/
app.post('/api/analyze-receipt', async (req, res) => {
if (!checkApiKey(req, res)) return;
const { imageBase64, mimeType } = req.body;
if (!imageBase64 || !mimeType) {
res.status(400).json({ error: 'Falsche Eingabedaten', message: 'Bitte lade ein gültiges Belegbild hoch.' });
return;
}
const prompt = `
Analysiere diesen Beleg / diese Quittung im Detail für die deutsche Buchhaltung (GoBD-konform).
Extrahiere alle relevanten Daten für eine Steuerausgabe.
Prüfe außerdem ausdrücklich, ob auf der Rechnung ein QR-Code vorhanden ist:
- Dies kann ein EPC-QR-Code (Girocode) für SEPA-Zahlungen sein (beginnt typischerweise mit dem Service-Tag "BCD", gefolgt von Version, BIC, Name des Empfängers, IBAN, Betrag, Verwendungszweck).
- Oder ein ZUGFeRD / Factur-X QR-Code, der Rechnungsdaten kodiert.
Falls ein solcher QR-Code zu sehen ist, lies dessen Werte aus (bzw. die danebenstehenden Bank-/Zahlungsdetails, falls sie direkt damit verknüpft sind) und befülle das Objekt "qrCodeData".
Regeln für die Extraktion:
1. "supplierName": Finde den Namen des Ausstellers (Supermarkt, Tankstelle, Softwarefirma, Dienstleister, etc.).
2. "date": Finde das Belegdatum und gib es im ISO-Format (YYYY-MM-DD) aus (z.B. "2026-06-15").
3. "grossAmount": Der vollständige Zahlbetrag inklusive Umsatzsteuer.
4. "vatRate": Finde den Steuersatz in Prozent (üblicherweise 19 oder 7, in manchen Fällen 0 oder 5). Wenn verschiedene Sätze vorhanden sind, wähle den Hauptsatz oder berechne den Durchschnitt, bzw. nimm den höchsten Anteil.
5. "netAmount" & "vatAmount": Berechne oder extrahiere die Nettosumme und Steuerbeträge. Falls nicht lesbar, berechne ausgehend vom Bruttobetrag und dem Steuersatz (z.B. netAmount = grossAmount / (1 + vatRate/100)).
6. "category": Ordne diesen Beleg einer dieser deutschen Standard-Betriebsausgabenkategorien zu:
- "Bürobedarf" (für Papier, Schreibwaren, Möbel, kleinere Elektronik)
- "Software & Lizenzen" (Cloud-Hosting, SaaS-Tools, Software)
- "Fremdleistungen" (Dienstleister, Freelancer-Rechnungen)
- "Reisekosten" (Hotel, Bahn, ÖPNV, Flug)
- "Bewirtungskosten" (Restaurantbelege, Geschäftsessen)
- "Telekommunikation" (Internet, Handyvertrag, Telefon)
- "Werbungs- und Marketingkosten" (Anzeigen, Visitenkarten)
- "Miete & Raumkosten" (Büromiete, Strom, Heizung)
- "Kfz-Kosten" (Tanken, Reparatur, Autowäsche)
- "Versicherungen" (Haftpflicht, Rechtschutz)
- "Sonstige Betriebsausgaben" (alles andere)
7. "description": Eine kurze, aussagekräftige Beschreibung der gekauften Produkte/Leistungen auf Deutsch.
Erstelle ein absolut valides JSON laut dem vorgegebenen Schema. Rechnen muss korrekt aufgehen!
`;
try {
const cleanBase64 = imageBase64.replace(/^data:[^;]+;base64,/, '');
const imagePart = {
inlineData: {
data: cleanBase64,
mimeType: mimeType,
},
};
const textPart = {
text: prompt,
};
const dynamicAi = getAiClient(req);
const response = await dynamicAi.models.generateContent({