-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariants.js
More file actions
759 lines (670 loc) · 25.8 KB
/
Copy pathvariants.js
File metadata and controls
759 lines (670 loc) · 25.8 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
const HTTP_STATUS = require('http-status-codes');
// const { HTTP_STATUS } = require('http-status-codes');
const express = require('express');
const router = express.Router({mergeParams: true});
const {literal} = require('sequelize');
const db = require('../../models');
const logger = require('../../log');
const {KB_PIVOT_MAPPING} = require('../../constants');
const KBMATCHEXCLUDE = ['id', 'reportId', 'variantId', 'deletedAt', 'updatedBy'];
const OBSVARANNOTEXCLUDE = KBMATCHEXCLUDE;
const STATEMENTEXCLUDE = ['id', 'reportId', 'deletedAt', 'updatedBy'];
const MUTATION_REGEX = '^([^\\s]+)(\\s)(mutation[s]?)?(missense)?$';
const MSI_CUTOFF = 20;
const getVariants = async (tableName, variantType, reportId) => {
return db.models[tableName].scope('extended').findAll({
order: [['id', 'ASC']],
attributes: {
include: [[literal(`'${variantType}'`), 'variantType']],
},
where: {
reportId,
},
include: [
{
model: db.models.kbMatches,
attributes: {exclude: KBMATCHEXCLUDE},
},
{
model: db.models.observedVariantAnnotations,
as: 'observedVariantAnnotation',
attributes: {exclude: OBSVARANNOTEXCLUDE},
},
],
});
};
const unknownSignificanceIncludes = ['mut'];
const cancerRelevanceOmits = ['exp'];
const geneLinkedVariantTypes = ['mut', 'cnv', 'exp'];
const getRapidReportVariants = async (tableName, variantType, reportId, rapidTable) => {
let allKbMatches;
const kbmatchJoin = {
model: db.models.kbMatches,
attributes: {exclude: KBMATCHEXCLUDE},
include: [
{
model: db.models.kbMatchedStatements,
as: 'kbMatchedStatements',
attributes: {
exclude: STATEMENTEXCLUDE,
},
through: {attributes: ['flags']},
},
],
};
// get variants that don't have kbmatches in the unknownSignificance table (if gene check passes)
if (rapidTable === 'unknownSignificance') {
kbmatchJoin.required = false;
}
const query = {
order: [['id', 'ASC']],
attributes: {
include: [[literal(`'${variantType}'`), 'variantType']],
},
where: {
reportId,
},
include: [
kbmatchJoin,
{
model: db.models.observedVariantAnnotations,
attributes: {exclude: OBSVARANNOTEXCLUDE},
as: 'observedVariantAnnotation',
},
],
};
if (geneLinkedVariantTypes.includes(variantType)) {
const geneSubquery = {
model: db.models.genes.scope('minimal'),
as: 'gene',
};
query.include.push(geneSubquery);
}
// if the table is unknownSignificance, only get subset of variant types
if (rapidTable === 'unknownSignificance') {
if (unknownSignificanceIncludes.includes(variantType)) {
allKbMatches = await db.models[tableName].scope('extended').findAll(query);
}
} else if (rapidTable === 'cancerRelevance') {
// exclude expression variants from cancer relevance table
if (!cancerRelevanceOmits.includes(variantType)) {
allKbMatches = await db.models[tableName].scope('extended').findAll(query);
}
} else {
allKbMatches = await db.models[tableName].scope('extended').findAll(query);
}
if (!allKbMatches) {
return [];
}
// do initial filtering based on contents of annotations - these should override defaults
const therapeuticResultsFromAnnotation = [];
const cancerRelevanceResultsFromAnnotation = [];
const unknownSignificanceFromAnnotation = [];
const unknownSignificanceFromGeneProperty = [];
const doNotReport = [];
const doNotReportIdents = new Set();
const tumourSuppressorLofVariants = {};
// separate out variants that qualify based on gene properties alone, if filtering for table 3
if (rapidTable === 'unknownSignificance') {
for (const variant of allKbMatches) {
if (variant?.observedVariantAnnotation?.annotations?.rapidReportTableTag === 'noTable') {
doNotReport.push(variant);
doNotReportIdents.add(variant.ident);
continue;
}
if (variant.gene.cancerGeneListMatch
|| variant.gene.oncogene
|| variant.gene.tumourSuppressor) {
unknownSignificanceFromGeneProperty.push(variant);
}
}
}
// remove the kbmatch-less variants from further sorting
allKbMatches = allKbMatches.filter((variant) => {
return (variant.kbMatches.length > 0);
});
for (const variant of allKbMatches) {
// check whether the variant has LOF effect on tumour suppressor gene
const tumourSuppressorLof = variant.kbMatches.some((kbmatch) => {
return kbmatch.kbMatchedStatements.some((stmt) => {
if (variant.gene?.tumourSuppressor) {
return (stmt.relevance?.includes('loss of function')
&& !(stmt.relevance?.includes('no loss of function')));
}
return false;
});
});
tumourSuppressorLofVariants[variant.ident] = tumourSuppressorLof;
if (variant?.observedVariantAnnotation?.annotations?.rapidReportTableTag) {
const tableTag = variant.observedVariantAnnotation.annotations.rapidReportTableTag;
// prune out statements where context is not therapeutic, if filtering for table 1
let variantKbMatches = variant.kbMatches.map((kbmatch) => {
const kbmatchKbMatchedStatements = kbmatch.kbMatchedStatements
.filter((stmt) => {
if (rapidTable === 'therapeuticAssociation') {
if (stmt.category === 'therapeutic') {
return true;
}
return false;
}
return true;
});
kbmatch.set('kbMatchedStatements', kbmatchKbMatchedStatements);
return kbmatch;
});
// remove kbmatches where there are no remaining kbmatched statements
variantKbMatches = variantKbMatches.filter((kbmatch) => {
return kbmatch.kbMatchedStatements.length > 0;
});
variant.set('kbMatches', variantKbMatches);
// don't use this variant if it has no tagged kb statements
if (variant.kbMatches.length > 0) {
if (tableTag === 'therapeuticAssociation') {
therapeuticResultsFromAnnotation.push(variant);
} else if (tableTag === 'cancerRelevance') {
cancerRelevanceResultsFromAnnotation.push(variant);
} else if (tableTag === 'unknownSignificance') {
unknownSignificanceFromAnnotation.push(variant);
} else if (tableTag === 'noTable') {
console.log(`Variant ${variant.ident} is tagged as noTable, will not be reported`);
doNotReport.push(variant);
doNotReportIdents.add(variant.ident);
}
}
}
}
// remove the tagged variants from further sorting
allKbMatches = allKbMatches.filter((variant) => {
return (!(variant?.observedVariantAnnotation?.annotations?.rapidReportTableTag));
});
let therapeuticAssociationResults = [];
if (!(variantType === 'exp')) { // omit expression variants from this table
therapeuticAssociationResults = JSON.parse(JSON.stringify(allKbMatches));
// remove nonmatching kbmatches
therapeuticAssociationResults = therapeuticAssociationResults.map((variant) => {
// if the variant has a loss of function effect on a tumour suppressor gene,
// retain matches where the mutation is of type 'GENENAME mutation';
// otherwise, remove these
if (!tumourSuppressorLofVariants[variant.ident]) {
variant.kbMatches = variant.kbMatches.filter((item) => {
const variantRegexMatch = item.kbVariant.match(MUTATION_REGEX);
return !(variantRegexMatch);
});
}
return variant;
});
// remove nonmatching statements
therapeuticAssociationResults = therapeuticAssociationResults.map((variant) => {
variant.kbMatches = variant.kbMatches.map((kbmatch) => {
const statements = kbmatch.kbMatchedStatements.filter((stmt) => {
if ((stmt.category === 'therapeutic')
&& stmt.matchedCancer
&& ['IPR-A', 'IPR-B'].includes(stmt.iprEvidenceLevel)
&& (
stmt.relevance === 'sensitivity'
|| (stmt.relevance === 'resistance' && stmt.iprEvidenceLevel === 'IPR-A')
)
) {
return true;
}
return false;
});
kbmatch.kbMatchedStatements = statements;
return kbmatch;
});
return variant;
});
// remove matches which have no matching statements
therapeuticAssociationResults = therapeuticAssociationResults.map((variant) => {
variant.kbMatches = variant.kbMatches.filter((kbmatch) => {
kbmatch.kbMatchedStatements = kbmatch.kbMatchedStatements
.filter((item) => {return item !== null;});
return kbmatch.kbMatchedStatements.length > 0;
});
return variant;
});
// remove variants which have no matches
therapeuticAssociationResults = therapeuticAssociationResults.filter((variant) => {
const kbmatches = variant.kbMatches.filter((item) => {return item !== null;});
return kbmatches.length > 0;
});
}
therapeuticAssociationResults.push(...therapeuticResultsFromAnnotation);
if (rapidTable === 'therapeuticAssociation') {
return therapeuticAssociationResults;
}
let cancerRelevanceResults = [];
const cancerRelevanceResultsFiltered = [];
cancerRelevanceResults = JSON.parse(JSON.stringify(allKbMatches));
// remove msi variants below cutoff
cancerRelevanceResults = cancerRelevanceResults.filter((variant) => {
if (variantType === 'msi') {
return (variant.score >= MSI_CUTOFF);
}
return true;
});
// remove kbmatches that fail the regex
cancerRelevanceResults = cancerRelevanceResults.map((variant) => {
const kbmatches = variant.kbMatches.filter((item) => {
const variantRegexMatch = item.kbVariant.match(MUTATION_REGEX);
return (!variantRegexMatch);
});
variant.kbMatches = kbmatches;
return variant;
});
// remove variants which now have no matches
cancerRelevanceResults = cancerRelevanceResults.filter((variant) => {
const kbmatches = variant.kbMatches.filter((item) => {return item !== null;});
return kbmatches.length > 0;
});
for (const row of cancerRelevanceResults) {
if (!(therapeuticAssociationResults.find(
(e) => {return e.ident === row.ident;},
))) {
cancerRelevanceResultsFiltered.push(row);
}
}
cancerRelevanceResultsFiltered.push(...cancerRelevanceResultsFromAnnotation);
if (rapidTable === 'cancerRelevance') {
return cancerRelevanceResultsFiltered;
}
const unknownSignificanceResultsFiltered = [];
let unknownSignificanceResults = [];
if (unknownSignificanceIncludes.includes(variantType)) {
unknownSignificanceResults = JSON.parse(JSON.stringify(allKbMatches));
}
// refine unknownSignificance results to only include variants that EITHER:
// a - have a linked gene that is oncogene, cancerGeneList or tumourSuppressor true, OR
// b - therapeuticAssoc-qualifying matches
// (unless they are tagged - add the tagged results back after filtering)
unknownSignificanceResults = unknownSignificanceResults.map((variant) => {
if (
// this is safe to check because variantType is smallMutation which always has a gene
variant.gene.oncogene || variant.gene.cancerGeneListMatch || variant.gene.tumourSuppressor
) {
return variant;
}
variant.kbMatches = variant.kbMatches.map((kbmatch) => {
const statements = kbmatch.kbMatchedStatements.filter((stmt) => {
if ((stmt.category === 'therapeutic')
&& stmt.matchedCancer
&& ['IPR-A', 'IPR-B'].includes(stmt.iprEvidenceLevel)
&& (
stmt.relevance === 'sensitivity'
|| (stmt.relevance === 'resistance' && stmt.iprEvidenceLevel === 'IPR-A')
)) {
return true;
}
return false;
});
kbmatch.kbMatchedStatements = statements;
return kbmatch;
});
return variant;
});
// remove matches which have no matching statements...
unknownSignificanceResults = unknownSignificanceResults.map((variant) => {
variant.kbMatches = variant.kbMatches.filter((kbmatch) => {
kbmatch.kbMatchedStatements = kbmatch.kbMatchedStatements
.filter((item) => {return item !== null;});
return kbmatch.kbMatchedStatements.length > 0;
});
return variant;
});
// remove variants which have no matches
unknownSignificanceResults = unknownSignificanceResults.filter((variant) => {
return variant.kbMatches.length > 0;
});
// add variants back to unknownSig list, that are tagged for this table based on gene properties alone
// so that they can be filtered out of results if they are in TA or CR
const existingVariants = new Set(unknownSignificanceResults.map((variant) => {return variant.ident;}));
unknownSignificanceFromGeneProperty.forEach((variant) => {
if (!existingVariants.has(variant.ident) && !doNotReportIdents.has(variant.ident)) {
unknownSignificanceResults.push(variant);
existingVariants.add(variant.ident);
}
});
// remove variants already included in a different section
for (const row of unknownSignificanceResults) {
if (!(therapeuticAssociationResults.find(
(e) => {return e.ident === row.ident;},
)) && !(cancerRelevanceResultsFiltered.find(
(e) => {return e.ident === row.ident;},
))) {
unknownSignificanceResultsFiltered.push(row);
}
}
// add variants that are tagged for this table regardless of whatever other
// reason there may be to exclude them
unknownSignificanceResultsFiltered.push(...unknownSignificanceFromAnnotation);
return unknownSignificanceResultsFiltered.filter((variant) => {
return !doNotReportIdents.has(variant.ident);
});
};
const updateKbDataSummaryTableTag = (kbData, rapidTable, variantType, variantIdent) => {
// Ensure `rapidReportTableTag` is initialized
kbData = kbData || {};
kbData.rapidReportTableTag = kbData.rapidReportTableTag || {};
// Remove `variantIdent` from all entries of rapidReportTableTag
for (const tableKey of Object.keys(kbData.rapidReportTableTag)) {
const typeMap = kbData.rapidReportTableTag[tableKey];
if (Array.isArray(typeMap?.[variantType])) {
typeMap[variantType] = typeMap[variantType].filter((id) => {return id !== variantIdent;});
}
}
// Add tag to the specified rapid table and variant type
if (!kbData.rapidReportTableTag[rapidTable]) {
kbData.rapidReportTableTag[rapidTable] = {[variantType]: [variantIdent]};
} else {
const tableEntry = kbData.rapidReportTableTag[rapidTable];
tableEntry[variantType] = tableEntry[variantType] || [];
if (!tableEntry[variantType].includes(variantIdent)) {
tableEntry[variantType].push(variantIdent);
}
}
return kbData;
};
const checkKbDataSummaryTableTag = (kbData, variantType, variantIdent) => {
let tag;
const thiskbData = kbData || {};
const rapidReportTableTags = thiskbData.rapidReportTableTag || {};
for (const tableKey of Object.keys(rapidReportTableTags)) {
const typeMap = rapidReportTableTags[tableKey];
if (Array.isArray(typeMap?.[variantType])) {
if (typeMap[variantType].includes(variantIdent)) {
tag = tableKey;
}
}
}
return tag;
};
// utils/variantUtils.js
async function findVariantOrRespond({req, res, variantTable}) {
try {
const variant = await db.models[variantTable].findOne({
where: {
ident: req.body.variantIdent,
reportId: req.report.id,
},
include: [
{
model: db.models.kbMatches,
include: [
{
model: db.models.kbMatchedStatements,
as: 'kbMatchedStatements',
},
],
},
],
});
if (!variant) {
const message = 'Variant not found';
logger.error(message);
res.status(400).json({error: {message}});
return null;
}
return variant;
} catch (error) {
const message = `Error while checking that linked variant exists ${error}`;
logger.error(message);
res.status(400).json({error: {message}});
return null;
}
}
router.route('/set-summary-table/')
.post(async (req, res) => {
// updates variant record and statement records
let rapidTable;
try {
rapidTable = req.body.rapidReportTableTag;
req.body.rapidTable = rapidTable;
} catch (error) {
const message = `Error checking rapid report table tag ${error}`;
logger.error(message);
return res.status(HTTP_STATUS.BAD_REQUEST).json({error: {message}});
}
// below is copied from observedVariantAnnotations.js, should probably DRY it
// validate variant - check type is real and the variant exists
let variantType;
let variantTable;
try {
variantTable = KB_PIVOT_MAPPING[req.body.variantType];
variantType = req.body.variantType;
} catch (error) {
const message = `Error checking variant type ${error}`;
logger.error(message);
return res.status(HTTP_STATUS.BAD_REQUEST).json({error: {message}});
}
if (typeof req.body.kbStatementIds === 'undefined') {
const message = 'No statement ids found';
logger.error(message);
return res.status(HTTP_STATUS.BAD_REQUEST).json({error: {message}});
}
let variant;
try {
variant = await findVariantOrRespond({req, res, variantTable});
} catch (error) {
const message = `Error checking variant ${error}`;
logger.error(message);
return res.status(HTTP_STATUS.BAD_REQUEST).json({error: {message}});
}
if (!variant) {
const message = 'Variant not found';
logger.error(message);
return res.status(HTTP_STATUS.BAD_REQUEST).json({error: {message}});
}
req.body.variantId = variant.id;
// check if all statement idents are valid
const statements = variant.kbMatches.flatMap((kb) => {return kb.kbMatchedStatements;});
const statementIdSet = new Set(statements.map((stmt) => {return stmt.ident;}));
const invalidIds = req.body.kbStatementIds.filter((id) => {return !statementIdSet.has(id);});
if (invalidIds.length > 0) {
const msg = `Idents invalid for some kb statements: ${invalidIds.join(', ')}`;
logger.error(msg);
return res.status(HTTP_STATUS.BAD_REQUEST).json({error: {message: msg}});
}
// check whether there is already an annotation record for this variant
let annotation;
try {
annotation = await db.models.observedVariantAnnotations.findOne({
where: {
variantId: req.body.variantId,
variantType: req.body.variantType,
reportId: req.report.id,
},
});
} catch (error) {
const message = `Error while checking for preexisting annotation record ${error}`;
logger.error(message);
return res.status(HTTP_STATUS.BAD_REQUEST).json({error: {message}});
}
if (annotation) {
// check if the annotation for this tag exists and is the same
const reportTableTag = annotation.annotations?.rapidReportTableTag;
let annotationMatch = false;
if (reportTableTag) {
if (reportTableTag === req.body.rapidTable) {
annotationMatch = true;
}
}
// update if not:
if (!annotationMatch) {
const newAnnotation = {...(annotation.annotations || {})};
newAnnotation.rapidReportTableTag = req.body.rapidTable;
const newFlags = req.body.flags ?? null;
try {
await db.models.observedVariantAnnotations.update({
annotations: newAnnotation,
flags: newFlags,
}, {where: {id: annotation.id}});
} catch (error) {
logger.error(`Unable to create update observed variant annotation ${error}`);
return res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).json({
error: {message: 'Unable to create observed variant annotation'},
});
}
}
} else {
// otherwise, create the annotation record
try {
await db.models.observedVariantAnnotations.create({
variantId: req.body.variantId,
variantType: req.body.variantType,
annotations: {rapidReportTableTag: req.body.rapidTable},
flags: req.body.flags ?? null,
reportId: req.report.id,
});
} catch (error) {
logger.error(`Unable to create mutation burden ${error}`);
return res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).json({
error: {message: 'Unable to create mutation burden'},
});
}
}
// now manage the variant's kb statement tags
const updatedStatements = [];
for (const match of variant.kbMatches) {
// (match);
for (const stmt of match.kbMatchedStatements) {
const kbData = {...(stmt.kbData || {})};
const {variantIdent} = req.body;
if (!req.body.kbStatementIds.includes(stmt.ident)) {
// if statement is not in the statement list:
// set it to noTable unless it is already tagged with the current rapidTable or noTable
const currentTag = checkKbDataSummaryTableTag(kbData, variantType, variantIdent);
if (currentTag === null || !(['noTable', rapidTable].includes(currentTag))) {
const newKbData = updateKbDataSummaryTableTag(kbData, 'noTable', variantType, variantIdent);
updatedStatements.push({
id: stmt.id, // primary key
kbData: newKbData,
});
}
} else {
// if statement is in the list, update (or add) the tag
const newKbData = updateKbDataSummaryTableTag(kbData, rapidTable, variantType, variantIdent);
updatedStatements.push({
id: stmt.id, // primary key
kbData: newKbData,
});
}
}
}
for (const stmt of updatedStatements) {
await db.models.kbMatchedStatements.update(
{kbData: stmt.kbData},
{where: {id: stmt.id}},
);
}
return res.status(204).end();
});
router.route('/set-statement-summary-table/')
.post(async (req, res) => {
// updates statement records
let rapidTable;
try {
rapidTable = req.body.rapidReportTableTag;
req.body.rapidTable = rapidTable;
} catch (error) {
const message = `Error checking rapid report table tag ${error}`;
logger.error(message);
return res.status(HTTP_STATUS.BAD_REQUEST).json({error: {message}});
}
// validate variant - check type is real and make sure the record exists
let variantType;
let variantTable;
try {
variantTable = KB_PIVOT_MAPPING[req.body.variantType];
variantType = req.body.variantType;
} catch (error) {
const message = `Error checking variant type ${error}`;
logger.error(message);
return res.status(HTTP_STATUS.BAD_REQUEST).json({error: {message}});
}
if (typeof req.body.kbStatementIds === 'undefined') {
const message = 'No statement ids found';
logger.error(message);
return res.status(HTTP_STATUS.BAD_REQUEST).json({error: {message}});
}
let variant;
try {
variant = await findVariantOrRespond({req, res, variantTable});
} catch (error) {
const message = `Error checking variant ${error}`;
logger.error(message);
return res.status(HTTP_STATUS.BAD_REQUEST).json({error: {message}});
}
if (!(variant)) {
const message = 'Variant not found';
logger.error(message);
return res.status(HTTP_STATUS.BAD_REQUEST).json({error: {message}});
}
// check if all statement idents are valid
const statements = variant.kbMatches.flatMap((kb) => {return kb.kbMatchedStatements;});
const statementIdSet = new Set(statements.map((stmt) => {return stmt.ident;}));
const invalidIds = req.body.kbStatementIds.filter((id) => {return !statementIdSet.has(id);});
if (invalidIds.length > 0) {
const msg = `Idents invalid for some kb statements: ${invalidIds.join(', ')}`;
logger.error(msg);
return res.status(HTTP_STATUS.BAD_REQUEST).json({error: {message: msg}});
}
// all we need to do is edit the rapidReportTableTags field in the statement kbData
const updatedStatements = [];
for (const match of variant.kbMatches) {
// (match);
for (const stmt of match.kbMatchedStatements) {
if (!req.body.kbStatementIds.includes(stmt.ident)) {
continue; // Skip this statement if it's not in the list
}
const kbData = {...(stmt.kbData || {})};
const {variantIdent} = req.body;
const newKbData = updateKbDataSummaryTableTag(kbData, rapidTable, variantType, variantIdent);
// Queue for bulk update
updatedStatements.push({
id: stmt.id, // primary key
kbData: newKbData,
});
}
}
for (const stmt of updatedStatements) {
await db.models.kbMatchedStatements.update(
{kbData: stmt.kbData},
{where: {id: stmt.id}},
);
}
return res.status(204).end();
});
router.route('/')
.get(async (req, res) => {
// Get all variants for this report
// Cache was removed from this endpoint due to requiring multiple tables,
// increasing the chance of retrieving outdated data
const {query: {rapidTable}} = req;
try {
const variantTypes = Object.keys(KB_PIVOT_MAPPING);
const variantsArray = [];
for (const variantType of variantTypes) {
const tableName = KB_PIVOT_MAPPING[variantType];
if (rapidTable) {
variantsArray.push(
await getRapidReportVariants(tableName, variantType, req.report.id, rapidTable),
);
} else {
variantsArray.push(await getVariants(tableName, variantType, req.report.id));
}
}
const results = variantsArray.flat(1);
return res.json(results);
} catch (error) {
logger.error(`Unable to retrieve variants ${error}`);
return res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).json({
error: {message: 'Unable to retrieve variants'},
});
}
});
// Attach utils for testing
router._testUtils = {
checkKbDataSummaryTableTag,
updateKbDataSummaryTableTag,
};
module.exports = router;