-
Notifications
You must be signed in to change notification settings - Fork 1
/
createCorrections.js
879 lines (798 loc) · 35.2 KB
/
createCorrections.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
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
module.exports = function () {
// Load NPM packages.
var fs = require('fs');
var inquirer = require('inquirer');
// Import constant values and path values (if provided).
var cons = require('./constants.js');
try {
var paths = require('./paths.js');
} catch (e) {
}
// Initialize filename and data variables in global scope.
var currentFileName;
var currentData;
var currentNodeIDs;
var currentEdgeIDs;
var objectID;
var currentCorrectionType;
// Run initial prompt.
getIdentifyingData();
// Initial prompt that takes ID and visit number.
function getIdentifyingData() {
// Initialize/empty these global variables.
currentFileName = '';
currentData = {};
currentEdgeIDs = [];
currentNodeIDs = [];
inquirer.prompt(cons.fileIdentifyingQuestions).then(findFiles);
}
// Searches for files that parameters given.
function findFiles(answers) {
var idPattern;
var visitPattern = new RegExp('(V' + answers.visitNumber + ')');
// Initialize to 1, because there will always be an underscore at the end of the pattern.
var idLength = 1;
var possibleFiles = [];
var folder;
if (typeof(paths) !== 'undefined') {
folder = paths.data + answers.visitNumber + '/';
} else {
folder = 'data/';
}
if (answers.radarID !== '0000') {
idPattern = new RegExp('(' + answers.radarID + '_)');
idLength += 4;
} else {
idPattern = new RegExp('(' + answers.nonstandardRADARID + '_)');
idLength += answers.nonstandardRADARID.length;
}
fs.readdir(folder, function (err, data) {
if (err) {
console.log('File not found. Let\'s try again.');
return getIdentifyingData();
}
data.forEach(function (fileName) {
if (idPattern.test(fileName.substring(0, idLength))) {
if (visitPattern.test(fileName.substring())) {
possibleFiles.push(fileName);
}
}
});
confirmFile(possibleFiles, folder);
});
}
// Ask the user to select between matching files.
function confirmFile(files, path) {
if (files.length === 0) {
console.log('No files found. Let\'s start over.');
getIdentifyingData();
} else {
var confirmQuestions = [
{
name: 'singleConfirm',
type: 'confirm',
message: files[0] + '\nIs this the file you would like to correct?',
when: files.length === 1
},
{
name: 'selectBetween',
type: 'list',
message: 'Multiple files were found with that RADAR ID/Visit Number combination. Please select the file you would like to correct.',
choices: files,
when: files.length > 1
}
];
inquirer.prompt(confirmQuestions).then(function (answers) {
loadFile(answers, files, path)
});
}
}
// Read in selected file.
function loadFile(answers, files, path) {
if (answers.selectBetween) {
currentFileName = path + answers.selectBetween;
fs.readFile(currentFileName, 'utf8', function (error, data) {
if (error) {
console.log(error);
return getIdentifyingData();
}
// Parse data and save to currentData.
currentData = JSON.parse(data);
// Iterate through edge IDs to build currentEdgeIDs.
currentData.edges.forEach(function (edge) {
currentEdgeIDs.push(edge.id.toString());
});
// Iterate through node IDs to build currentNodeIDs.
currentData.nodes.forEach(function (node) {
currentNodeIDs.push(node.id.toString());
});
chooseCorrection();
})
} else if (answers.singleConfirm) {
currentFileName = path + files[0];
fs.readFile(currentFileName, 'utf8', function (error, data) {
if (error) {
console.log(error);
return getIdentifyingData();
}
// Parse data and save to currentData.
currentData = JSON.parse(data);
// Iterate through edge IDs to build currentEdgeIDs.
currentData.edges.forEach(function (edge) {
currentEdgeIDs.push(edge.id.toString());
});
// Iterate through node IDs to build currentNodeIDs
currentData.nodes.forEach(function (node) {
currentNodeIDs.push(node.id.toString());
});
chooseCorrection();
})
} else {
console.log('No? Let\'s try again.');
getIdentifyingData();
}
}
// Ask the user which kind of correction they want to make + other basic info.
function chooseCorrection() {
var intID;
if (typeof(currentData.sessionParameters) !== 'undefined') {
intID = currentData.sessionParameters.interviewerID;
}
objectID = null;
var inspectionQuestions = [
{
name: 'correctionType',
type: 'list',
message: 'What is the kind of correction you would like to make to this file?',
pageSize: 9,
choices: cons.correctionChoices
},
{
name: 'edgeNumber',
type: 'list',
message: 'What is the ID on the edge would you like to correct?',
choices: currentEdgeIDs,
when: function (answers) {
return answers.correctionType === 'Edge update';
}
},
{
name: 'nodeNumber',
type: 'list',
message: 'What is the ID on the node you would like to correct?',
choices: currentNodeIDs,
when: function (answers) {
return answers.correctionType === 'Node update';
}
},
{
name: 'newNodeType',
type: 'list',
message: 'What kind of node would you like to add?',
choices: cons.nodeTypes,
when: function (answers) {
return answers.correctionType === 'Node creation';
}
},
{
name: 'newEdgeType',
type: 'list',
message: 'What kind of edge would you like to add?',
choices: cons.edgeTypes,
when: function (answers) {
return answers.correctionType === 'Edge creation';
},
filter: function (response) {
return response.split('/')[0];
}
},
{
name: 'newInterviewerID',
type: 'input',
message: 'The current interviewer ID is ' + intID + '.\n' + 'What should it be changed to?',
when: function (answers) {
return answers.correctionType === 'Interviewer ID update';
},
validate: function (answer) {
var pattern = /\d\d/;
if (pattern.test(answer)) {
return true;
} else {
return 'Please enter a valid interviewer ID. It should be a two-digit number.';
}
}
},
{
name: 'confirmRemoval',
type: 'confirm',
message: (
'Are you sure that you would like to remove this interview from the analysis set?\n' +
'It will no longer be processed and copied to Analysis Ready.'
),
when: function (answers) {
return answers.correctionType === 'Remove interview from analysis';
}
}
];
inquirer.prompt(inspectionQuestions).then(function (answers) {
currentCorrectionType = answers.correctionType;
if (answers.edgeNumber) {
confirmID(answers.edgeNumber);
} else if (answers.nodeNumber) {
confirmID(answers.nodeNumber);
} else if (answers.newNodeType) {
createNode(answers.newNodeType);
} else if (answers.newEdgeType) {
createEdge(answers.newEdgeType);
} else if (currentCorrectionType === 'Node deletion' || currentCorrectionType === 'Edge deletion') {
deleteObjects();
} else if (answers.newInterviewerID) {
writeCorrectionToFile({interviewerID: answers.newInterviewerID});
} else if (answers.confirmRemoval) {
writeCorrectionToFile();
} else if (answers.correctionType === 'Renumber nodes/edges') {
renumberIDs();
} else {
getIdentifyingData();
}
});
}
// If an edge or node ID was given, show the user the object they selected and confirm that it is the object they want to correct.
function confirmID(ID) {
var idConfirmQuestion;
var currentObject;
if (currentCorrectionType === 'Edge update' || currentCorrectionType === 'Edge deletion') {
currentObject = currentData.edges[currentEdgeIDs.indexOf(ID)];
idConfirmQuestion = {
name: 'idConfirm',
type: 'confirm',
message: JSON.stringify(currentObject, null, 2) + '\nIs this the edge you want to change or delete?'
}
} else {
currentObject = currentData.nodes[currentNodeIDs.indexOf(ID)];
idConfirmQuestion = {
name: 'idConfirm',
type: 'confirm',
message: JSON.stringify(currentObject, null, 2) + '\nIs this the node you want to change or delete?'
}
}
inquirer.prompt(idConfirmQuestion).then(function (answers) {
if (answers.idConfirm) {
objectID = ID;
if (currentCorrectionType.split(' ')[0] === 'Edge') {
updateEdge(currentObject);
} else {
updateNode(currentObject);
}
} else {
chooseCorrection();
}
});
}
/*
If previous data is loaded incorrectly, duplicate node IDs for different alters may occur. Thus, we increment the
IDs of alters elicited during the problematic interview, to make sure there is no overlap.
*/
function renumberIDs() {
inquirer.prompt([
{
name: 'start',
type: 'list',
choices: currentNodeIDs.filter(function (id) {
return id !== '0'
}),
message: 'Which is the first node that needs to be renumbered?'
},
{
name: 'offset',
type: 'input',
message: 'What number needs to be added to each affected node ID?',
validate: function (answer) {
var pattern = /\d+/;
if (pattern.test(answer) && parseInt(answer, 10) > 0) {
return true;
} else {
return 'Please enter a positive whole number.';
}
}
}
]).then(function (answers) {
confirmCorrection({
start: parseInt(answers.start, 10),
offset: parseInt(answers.offset, 10)
});
});
}
// Creates base edge and determines which other variables will be added. Begin recursion through these variables.
function createEdge(edgeType) {
var newEdge = {
type: edgeType
};
inquirer.prompt(
[
{
name: 'from',
type: 'list',
message: 'Who is this edge `from`?',
choices: currentNodeIDs.concat('Other node id')
},
{
name: 'otherFrom',
type: 'input',
message: 'Who is this edge `from`?',
when: function (answers) {
return answers.from === 'Other node id';
},
validate: function (answer) {
var pattern = /\d+/;
if (pattern.test(answer) && currentNodeIDs.indexOf(answer) === -1) {
return true;
} else {
return 'Please enter a valid node ID that was not in the list provided.';
}
}
},
{
name: 'to',
type: 'list',
message: 'Who is this edge `to`?',
choices: function (answers) {
// 'to' cannot be '0' or the same as 'from'
function isValidToValue(id) {
return id !== answers.from && id !== '0';
}
// Give the option to manually enter `to`, since a new node may have been created.
return currentNodeIDs.filter(isValidToValue).concat('Other node id');
}
},
{
name: 'otherTo',
type: 'input',
message: 'Who is this edge `to`?',
when: function (answers) {
return answers.to === 'Other node id';
},
validate: function (answer, answers) {
var pattern = /\d+/;
// test that the answer is a positive integer, not 0, not already in the list given, and
// not the same as the `otherFrom` value. `from` value test not needed because that would be in the list.
if (pattern.test(answer) &&
currentNodeIDs.indexOf(answer) === -1 &&
answer !== '0' &&
answer !== answers.otherFrom) {
return true;
} else {
return 'Please enter a valid node ID that was not in the list provided.';
}
}
}
]
).then(function (answers) {
// Add `to` to the edge.
if (answers.otherTo) {
newEdge.to = parseInt(answers.otherTo, 10);
} else {
newEdge.to = parseInt(answers.to, 10);
}
// Add `from` to the edge.
if (answers.otherFrom) {
newEdge.from = parseInt(answers.otherFrom, 10);
} else {
newEdge.from = parseInt(answers.from, 10);
}
// If this edge has variables other than id/type/to/from, grab those.
if (Object.keys(cons.edgeVariables).indexOf(edgeType) !== -1) {
var requiredVars = cons.edgeVariables[edgeType].required;
var optionalVars = cons.edgeVariables[edgeType].optional;
}
inquirer.prompt(
{
name: 'optionalVariables',
type: 'checkbox',
message: 'Which, if any, of the following optional variables you would like to add to this this edge?',
when: typeof(optionalVars) !== 'undefined',
choices: optionalVars
}).then(function (answers) {
var optionalVars = answers.optionalVariables;
var variablesToAdd;
var variablesProcessed = 0;
if (optionalVars && requiredVars) {
variablesToAdd = requiredVars.concat(answers.optionalVariables);
} else if (optionalVars) {
variablesToAdd = optionalVars;
} else if (requiredVars) {
variablesToAdd = requiredVars;
} else {
confirmCorrection(newEdge);
}
getNewVariableValue(variablesProcessed, variablesToAdd, newEdge);
});
});
}
// Creates node edge and determines which other variables will be added. Begin recursion through these variables.
function createNode(nodeType) {
objectID = currentData.nodes[0].reserved_ids[currentData.nodes[0].reserved_ids.length - 1] + 1;
var newNode = {
type_t0: nodeType
};
if (nodeType === 'HIVService') {
inquirer.prompt({
name: 'name',
type: 'input',
message: 'What is the name of this service provider?'
}).then(function (answers) {
newNode.name = answers.name;
confirmCorrection(newNode);
})
} else {
confirmCorrection(newNode);
}
}
// Determine which nodes or edges to delete.
function deleteObjects() {
var deleteChoices;
var deleteMessage;
if (currentCorrectionType === 'Edge deletion') {
deleteChoices = currentEdgeIDs;
deleteMessage = 'Which edges would you like to delete? Select all that apply.'
} else if (currentCorrectionType === 'Node deletion') {
deleteChoices = currentNodeIDs;
deleteMessage = 'Which nodes would you like to delete? Select all that apply.'
}
var deletePrompt = {
name: 'toDelete',
type: 'checkbox',
message: deleteMessage,
choices: deleteChoices,
validate: function (response) {
if (response.length > 0) {
return true;
} else {
return 'Please select at least one item to delete.'
}
}
};
inquirer.prompt(deletePrompt).then(function (answers) {
var correction = {
ids: answers.toDelete
};
confirmCorrection(correction);
});
}
// Determine which variables will be updated/added/removed. Begin recursion through these variables.
function updateEdge(edge) {
inquirer.prompt([
{
name: 'updateVariables',
type: 'checkbox',
message: 'Which variables would you like to update or remove?',
choices: Object.keys(edge)
},
{
name: 'addVariablesConfirm',
type: 'confirm',
message: 'Are there any variables you would like to add to this this edge?',
when: typeof(cons.edgeVariables[edge.type].optional) !== 'undefined'
},
{
name: 'addVariables',
type: 'checkbox',
message: 'Which variables would you like to add to this edge?',
choices: function () {
var choices = [];
cons.edgeVariables[edge.type].optional.forEach(function (variable) {
if (Object.keys(edge).indexOf(variable) === -1) {
choices.push(variable);
}
});
return choices;
},
when: function (answers) {
return answers.addVariablesConfirm;
}
}]).then(function (answers) {
var variablesToModify;
var variablesProcessed = 0;
var corrections = {};
if (answers.addVariables) {
variablesToModify = answers.updateVariables.concat(answers.addVariables);
} else {
variablesToModify = answers.updateVariables;
}
getNewVariableValue(variablesProcessed, variablesToModify, corrections, edge);
});
}
// Determine which variables will be updated/added/removed. Begin recursion through these variables.
function updateNode(node) {
inquirer.prompt([
{
name: 'updateVariables',
type: 'checkbox',
message: 'Which variables would you like to update or remove?',
choices: Object.keys(node)
},
{
name: 'addVariablesConfirm',
type: 'confirm',
message: 'Are there any variables you would like to add to this this node?',
when: node.type_t0 === 'Ego'
},
{
name: 'addVariables',
type: 'checkbox',
message: 'Which variables would you like to add to this node?',
choices: function () {
var choices = [];
cons.egoVariables.forEach(function (variable) {
if (Object.keys(node).indexOf(variable) === -1) {
choices.push(variable);
}
});
return choices;
},
when: function (answers) {
return answers.addVariablesConfirm;
}
}]).then(function (answers) {
var variablesToModify;
var variablesProcessed = 0;
var corrections = {};
if (answers.addVariables) {
variablesToModify = answers.updateVariables.concat(answers.addVariables);
} else {
variablesToModify = answers.updateVariables;
}
getNewVariableValue(variablesProcessed, variablesToModify, corrections, node);
});
}
// Iterates through the variable types and returns the type that the variable belongs to.
function getVariableType(variableName) {
var type = '';
Object.keys(cons.variableTypes).forEach(function(variableType) {
if (cons.variableTypes[variableType].variables.indexOf(variableName) !== -1) {
type = variableType;
}
});
return cons.variableTypes[type];
}
/*
Recursively accumulate new variable values for all needed variables. When all variables have been assigned
new values, pass the accumulated data to confirmCorrections.
*/
function getNewVariableValue(loopCount, variables, correctionsSoFar, dataObject) {
if (loopCount < variables.length) {
var variableName = variables[loopCount];
if (cons.unchangeableVariables.indexOf(variableName) === -1) {
if (dataObject) {
var currentVariableValue = dataObject[variableName];
}
var newValue = '';
var variableChangePrompt;
// Initialize an array that will hold 0 or 1 item.
var deleteChoice = [];
// If the variable is in the 'deletableVariables' array in constants.js,
// push the option '[Delete]' which will be displayed as the last choice in the `choices` array.
if (cons.deletableVariables.indexOf(variableName) !== -1) {
deleteChoice = ['[Delete]'];
}
if (cons.tractVariables.indexOf(variableName) !== -1) {
variableChangePrompt = [
{
name: 'newValue',
type: 'list',
choices: cons.variableLists[variableName].concat(deleteChoice),
message: 'The current value for ' + variableName + ' is ' + currentVariableValue + '.\nWhich of the following should it be set to?'
},
{
name: 'newTract',
type: 'input',
message: 'What census tract should ' + variableName + ' be set to? \ngeo_5jrd-6zik-',
when: function (answers) {
return answers.newValue === 'Census Tract';
},
validate: function (answer) {
if (cons.chicagoCensusTracts.indexOf('geo_5jrd-6zik-' + answer) !== -1) {
return true;
} else {
return 'Please enter only the portion of the census tract (as found in Network Canvas) that comes after "geo_5jrd-6zik-".';
}
}
}
]
} else if (variableName === 'details') {
variableChangePrompt =
{
name: 'newValue',
type: 'checkbox',
choices: cons.variableCheckboxes[dataObject.type],
message: 'The current value for ' + variableName + ' is ' + currentVariableValue + '.\nWhich of the following should it be set to? Select ALL that apply.'
}
} else if (variableName === 'radar_id') {
variableChangePrompt =
{
name: 'newValue',
type: 'input',
message: 'The current value for ' + variableName + ' is ' + currentVariableValue + '.\nWhat should it be set to?',
validate: cons.radarIDvalidate
}
} else if (Object.keys(cons.variableLists).indexOf(variableName) !== -1) {
variableChangePrompt =
{
name: 'newValue',
type: 'list',
choices: cons.variableLists[variableName].concat(deleteChoice),
message: 'The current value for ' + variableName + ' is ' + currentVariableValue + '.\nWhich of the following should it be set to?'
}
} else if (cons.dateVariables.indexOf(variableName) !== -1) {
variableChangePrompt =
[
{
name: 'newValue',
type: 'list',
choices: ['null', 'Date'].concat(deleteChoice),
message: 'The current value for ' + variableName + ' is ' + currentVariableValue + '.\nWhich of the following should it be set to?',
when: variableName === 'sex_first_t0'
},
{
name: 'month',
type: 'input',
message: 'The current value for ' + variableName + ' is ' + currentVariableValue + '.\nWhat should the MONTH be set to? (MM)',
validate: function (answers) {
var pattern = /\d\d/;
if (pattern.test(answers) && parseInt(answers, 10) <= 12 && parseInt(answers, 10) >= 1) {
return true;
} else {
return 'Please enter a valid month in the format "MM".';
}
},
when: function (answers) {
return variableName === 'sex_last_t0' || answers.newValue === 'Date';
}
},
{
name: 'day',
type: 'input',
message: 'The current value for ' + variableName + ' is ' + currentVariableValue + '.\nWhat should the DAY be set to? (DD)',
validate: function (answers) {
var pattern = /\d\d/;
if (pattern.test(answers) && parseInt(answers, 10) <= 31 && parseInt(answers, 10) >= 1) {
return true;
} else {
return 'Please enter a valid day in the format "DD".';
}
},
when: function (answers) {
return variableName === 'sex_last_t0' || answers.newValue === 'Date';
}
},
{
name: 'year',
type: 'input',
message: 'The current value for ' + variableName + ' is ' + currentVariableValue + '.\nWhat should the YEAR be set to? (YYYY)',
validate: function (answers) {
var pattern = /\d\d\d\d/;
var maxYear = new Date().getFullYear();
if (pattern.test(answers) && parseInt(answers, 10) <= maxYear && parseInt(answers, 10) >= 1900) {
return true;
} else {
return 'Please enter a valid year in the format "YYYY".';
}
},
when: function (answers) {
return variableName === 'sex_last_t0' || answers.newValues === 'Date';
}
}
];
} else {
variableChangePrompt = {};
variableChangePrompt.name = 'newValue';
variableChangePrompt.message = 'The current value for ' + variableName + ' is ' + currentVariableValue + '.\nWhat should it be set to?';
var variableType = getVariableType(variableName);
if (variableType.choices) {
variableChangePrompt.type = 'list';
variableChangePrompt.choices = variableType.choices.concat(deleteChoice);
} else {
variableChangePrompt.type = 'input';
}
if (variableType.validate) {
variableChangePrompt.validate = variableType.validate;
}
}
inquirer.prompt(variableChangePrompt).then(
function (answers) {
if (answers.year) {
newValue = answers.month + '/' + answers.day + '/' + answers.year
} else if (answers.newValue === 'Census Tract') {
newValue = 'geo_5jrd-6zik-' + answers.newTract;
} else {
newValue = answers.newValue;
}
// Test equality *with* type coercion. Leave RADAR IDs as strings.
if (newValue == parseInt(newValue, 10) && variableName !== 'radar_id') {
newValue = parseInt(newValue, 10);
}
correctionsSoFar[variableName] = newValue;
loopCount += 1;
getNewVariableValue(loopCount, variables, correctionsSoFar, dataObject);
}
);
} else {
loopCount += 1;
getNewVariableValue(loopCount, variables, correctionsSoFar, dataObject);
}
} else {
confirmCorrection(correctionsSoFar);
}
}
// Display the correction to the user and confirm whether it is correct.
function confirmCorrection(correction) {
inquirer.prompt({
name: 'changeApproval',
type: 'confirm',
message: currentCorrectionType + '\n' + JSON.stringify(correction, null, 2) + '\nIs this change correct?'
}).then(function (answers) {
if (answers.changeApproval) {
writeCorrectionToFile(correction);
} else {
// TODO: Decide where to pipe back here. For now, to chooseCorrection.
chooseCorrection();
}
});
}
// If the correction was confirmed, add it to corrections.json.
function writeCorrectionToFile(correction) {
var newCorrection = {
type: currentCorrectionType,
correctData: correction,
timestamp: Date.now()
};
if (objectID) {
newCorrection.id = parseInt(objectID, 10);
}
var currentCorrections = {};
var correctionFile;
if (typeof(paths) !== 'undefined') {
correctionFile = paths.corrections;
} else {
correctionFile = './corrections.json';
}
fs.readFile(correctionFile, 'utf8', function (error, data) {
if (error) {
return console.log(error);
}
currentCorrections = JSON.parse(data);
if (Object.keys(currentCorrections).indexOf(currentFileName) === -1) {
currentCorrections[currentFileName] = [newCorrection];
} else {
currentCorrections[currentFileName].push(newCorrection);
}
fs.writeFile(correctionFile, JSON.stringify(currentCorrections, null, 2));
});
checkForMoreCorrections();
}
// After a correction is saved, ask the user for more corrections to this or other files. Pipe back as necessary.
function checkForMoreCorrections() {
inquirer.prompt([
{
name: 'thisFileCorrections',
type: 'confirm',
message: 'Correction saved! Would you like to make more corrections to this file?'
},
{
name: 'anyCorrections',
type: 'confirm',
message: 'Would you like to make corrections to another file?',
when: function (answers) {
return !(answers.thisFileCorrections)
}
}]).then(function (answers) {
if (answers.thisFileCorrections) {
chooseCorrection();
} else if (answers.anyCorrections) {
getIdentifyingData();
} else {
console.log('Thank you, goodbye!');
}
})
}
};