-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
144 lines (125 loc) · 4.63 KB
/
app.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
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
import { app, errorHandler } from 'mu';
import VRDocumentName from './lib/vr-name';
import CONSTANTS from './constants';
import { getOrderedAgendaitems } from './lib/ordered-agendaitems';
import { getSubcasePieces } from './lib/subcase-pieces';
import { addAgendaSubcaseId } from './lib/add-agenda-subcase-id';
import { addPiecePosition } from './lib/add-piece-position';
import { addPieceOriginalName } from './lib/add-piece-original-name';
app.get('/', async function (req, res) {
res.sendStatus(202);
const results = await getOrderedAgendaitems(2024);
const knownSubcases = new Set();
const errors = [];
const addError = (thing) => errors.push(thing);
const counters = {
regular: {
doc: 0,
med: 0,
dec: 0,
},
pvv: {
doc: 0,
med: 0,
dec: 0,
},
}
for (const result of results) {
const {
subcase,
agendaitemType,
subcaseType,
meetingType,
} = result;
if (knownSubcases.has(subcase)) {
console.debug(`Skipping subcase <${subcase}>`);
continue;
}
knownSubcases.add(subcase);
console.debug(result);
const piecesResults = await getSubcasePieces(subcase);
let hasDecreetPiece = false;
for (const { pieceType } of piecesResults) {
if (pieceType === CONSTANTS.PIECE_TYPES.DECREET) {
hasDecreetPiece = true;
}
}
const isMed = agendaitemType === CONSTANTS.AGENDA_ITEM_TYPES.MEDEDELING;
const isDec = agendaitemType === CONSTANTS.AGENDA_ITEM_TYPES.NOTA && subcaseType === CONSTANTS.SUBCASE_TYPES.BEKRACHTIGING && hasDecreetPiece;
const isDoc = !isDec && agendaitemType === CONSTANTS.AGENDA_ITEM_TYPES.NOTA;
console.debug('isDoc:', isDoc);
console.debug('isMed:', isMed);
console.debug('isDec:', isDec);
// Counters
let counter;
if (meetingType === CONSTANTS.MEETING_TYPES.PVV) {
counter = 1 + (isDec ? counters.pvv.dec : isDoc ? counters.pvv.doc : counters.pvv.med);
} else {
counter = 1 + (isDec ? counters.regular.dec : isDoc ? counters.regular.doc : counters.regular.med);
}
const pieces = [];
let counterMatched = false;
for (const { piece, pieceName, documentContainer } of piecesResults) {
let vrDoc = null;
try {
vrDoc = new VRDocumentName(pieceName);
} catch {
console.debug(`Could not parse piece name ${pieceName}. Probably a piece from testing, skipping`);
continue;
}
pieces.push({ piece, pieceName, vrDoc, documentContainer });
// Validation
if (isDoc && isMed) {
addError({ subcase, counter, vrDoc, message: 'Marked as a NOTA and a MEDEDELING' });
}
if (isDec && isMed) {
addError({ subcase, counter, vrDoc, message: 'Marked as a DECREET and a MEDEDELING' });
}
if (vrDoc.caseNr !== counter) {
console.debug(JSON.stringify({ subcase, counter, vrDoc, message: 'Counter on piece name and calculated counter do not match' }));
addError({ subcase, counter, vrDoc, message: 'Counter on piece name and calculated counter do not match' });
} else {
counterMatched = true;
}
}
if (counterMatched || process.env.ALLOW_MISMATCHING_DOCUMENT_NAMES) {
if (meetingType === CONSTANTS.MEETING_TYPES.PVV) {
isDec
? counters.pvv.dec++
: isDoc
? counters.pvv.doc++
: counters.pvv.med++;
} else {
isDec
? counters.regular.dec++
: isDoc
? counters.regular.doc++
: counters.regular.med++;
}
}
const sortedPieces = pieces.sort((a, b) => {
return a.vrDoc.meta.index - b.vrDoc.meta.index;
});
for (const [index, { vrDoc }] of sortedPieces.entries()) {
console.debug('Agenderingsactiviteitnummer:', vrDoc.meta.caseNrRaw, 'Index:', vrDoc.meta.index, vrDoc.name);
if (pieces.length > 1 && vrDoc.meta.index !== index + 1) {
addError({ subcase, counter, vrDoc, index: index + 1, indexFromPiece: vrDoc.meta.index, message: `Index in piece name does not match calculated index: ${index + 1}` });
}
}
if (errors.length && !process.env.ALLOW_MISMATCHING_DOCUMENT_NAMES) {
break;
}
// Add new data
await addAgendaSubcaseId(subcase, counter);
for (const [index, { piece, pieceName, documentContainer }] of sortedPieces.entries()) {
await addPiecePosition(documentContainer, index + 1);
await addPieceOriginalName(piece, pieceName);
}
}
if (errors.length) {
console.debug('Encountered following errors:', JSON.stringify(errors, null, 2));
} else {
console.debug('Finished processing files without any errors');
}
});
app.use(errorHandler);