-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuildConnector.ts
244 lines (226 loc) · 7.41 KB
/
buildConnector.ts
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
import {
annotationModule,
annotationType,
documentType,
idModule,
settingsType,
} from '@label/core';
import {
buildDocumentRepository,
documentService,
} from '../../modules/document';
import { logger } from '../../utils';
import { connectorConfigType } from './connectorConfigType';
import { treatmentService } from '../../modules/treatment';
import { buildPreAssignator } from '../preAssignator';
import { Sources } from 'dbsder-api-types';
export { buildConnector };
function buildConnector(connectorConfig: connectorConfigType) {
return {
importSpecificDocument,
importNewDocuments,
};
async function importSpecificDocument({
documentNumber,
source,
lowPriority,
keepLabelTreatments,
settings,
}: {
documentNumber: number;
source: string;
lowPriority: boolean;
keepLabelTreatments: boolean;
settings: settingsType;
}) {
logger.log({
operationName: 'importSpecificDocument',
msg: `START: ${documentNumber} - ${source}, lowPriority: ${lowPriority}, keepLabelTreatments: ${keepLabelTreatments}`,
});
try {
const courtDecision = await connectorConfig.fetchCourtDecisionBySourceIdAndSourceName(
documentNumber,
source,
);
if (!courtDecision) {
logger.log({
operationName: 'importSpecificDocument',
msg:
'No court decision found for specified documentNumber and source',
});
return;
}
logger.log({
operationName: 'importSpecificDocument',
msg: `Court decision found. labelStatus: ${
courtDecision.labelStatus
}, ${!!courtDecision.pseudoText ? 'already' : 'never'} pseudonymised`,
});
const document = await connectorConfig.mapCourtDecisionToDocument(
courtDecision,
'manual',
);
logger.log({
operationName: 'importSpecificDocument',
msg: 'Court decision converted. Inserting document into database...',
});
if (lowPriority) {
await insertDocument({ ...document, route: 'exhaustive' });
} else {
await insertDocument({ ...document, route: 'request', priority: 4 });
}
logger.log({
operationName: 'importSpecificDocument',
msg: 'Insertion done',
});
if (keepLabelTreatments) {
if (courtDecision.labelTreatments?.length == 0) {
logger.error({
operationName: 'importSpecificDocument',
msg:
'LabelTreatments not found in court decision, skiping labelTreatments reimport.',
});
} else {
logger.log({
operationName: 'importSpecificDocument',
msg: 'LabelTreatments found in court decision, importing.',
});
const annotations: annotationType[] =
courtDecision.labelTreatments == undefined
? []
: courtDecision.labelTreatments[
courtDecision.labelTreatments.length - 1
].annotations.map((annotation) => {
return annotationModule.lib.buildAnnotation({
category: annotation.category,
start: annotation.start,
text: annotation.text,
certaintyScore: 1,
entityId: annotation.entityId,
});
});
await treatmentService.createTreatment(
{
documentId: document._id,
previousAnnotations: [],
nextAnnotations: annotations,
source: 'reimportedTreatment',
},
settings,
);
// on récupère la checklist du treatment NLP le plus récent
const reimportedChecklist = courtDecision.labelTreatments
?.filter((treatment) => treatment.source === 'NLP')
.sort((a, b) => b.order - a.order)[0].checklist;
if (reimportedChecklist) {
await documentService.updateDocumentChecklist(
document._id,
reimportedChecklist,
);
logger.log({
operationName: 'importSpecificDocument',
msg: 'Checklist reimported',
});
}
logger.log({
operationName: 'importSpecificDocument',
msg: 'LabelTreatments reimported, checking for pre-assignation.',
});
const preAssignator = buildPreAssignator();
const isPreassignated = await preAssignator.preAssignDocument(
document,
);
if (!isPreassignated) {
logger.log({
operationName: 'importSpecificDocument',
msg:
'No preAssignation found, setting documentStatus to next status.',
});
if (lowPriority) {
await documentService.updateDocumentStatus(
idModule.lib.buildId(document._id),
'free',
);
} else {
await documentService.updateDocumentStatus(
idModule.lib.buildId(document._id),
'toBeConfirmed',
);
}
}
}
}
logger.log({
operationName: 'importSpecificDocument',
msg: 'Selected document has been inserted in label database.',
});
await connectorConfig.updateDocumentLabelStatusToLoaded(
document.externalId,
);
logger.log({ operationName: 'importSpecificDocument', msg: 'DONE' });
} catch (error) {
logger.error({
operationName: 'importSpecificDocument',
msg: `${error}`,
data: error as Record<string, unknown>,
});
}
}
async function importNewDocuments() {
logger.log({
operationName: 'importNewDocuments',
msg: `Starting importNewDocuments...`,
});
for (const source of Object.values(Sources)) {
logger.log({
operationName: 'importNewDocuments',
msg: `Fetching ${source} decisions...`,
});
const newDecisionForSource = await connectorConfig.fetchDecisionsToPseudonymise(
source,
);
logger.log({
operationName: 'importNewDocuments',
msg: `${newDecisionForSource.length} ${source} decisions to pseudonymise found.`,
});
for (const decision of newDecisionForSource) {
const converted = await connectorConfig.mapCourtDecisionToDocument(
decision,
'recent',
);
insertDocument(converted);
await connectorConfig.updateDocumentLabelStatusToLoaded(
converted.externalId,
);
}
}
}
}
function insertDocument(document: documentType) {
const documentRepository = buildDocumentRepository();
try {
const insertedDocument = documentRepository.insert(document);
logger.log({
operationName: 'documentInsertion',
msg: `Document ${document.source}:${document.documentNumber} has been inserted in database imported by ${document.importer}`,
data: {
decision: {
sourceId: document.documentNumber,
sourceName: document.source,
},
},
});
return insertedDocument;
} catch (error) {
logger.error({
operationName: 'documentInsertion',
msg: `Failed to import ${document.source}:${document.documentNumber} document. ${error}`,
data: {
decision: {
sourceId: document.documentNumber,
sourceName: document.source,
},
},
});
}
}