UNPKG

@wbg-mde/r-factory

Version:

Metadata editor R integration module

65 lines (64 loc) 2.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const test_config_1 = require("./test-config"); const fs = require("fs"); const path = require("path"); const import_test_1 = require("./import.test"); const api_helper_1 = require("../modules/shared/api-helper"); class BaseKeyValidation { constructor() { this.fileIndex = 0; this.tstImport = new import_test_1.ImportAPITest(); } execute(files, listOfVariables) { try { this.fileIndex = 0; files.reduce((p, x) => p.then(_ => this.executeFile(x, listOfVariables)), Promise.resolve()); } catch (e) { console.log('execute error >> ' + e); } } executeFile(filepath, listOfVariables) { this.fileIndex += 1; const inputDatasetPath = path.join(test_config_1.TestConfig.inputPath.dataset, filepath); const inputCSVPath = path.join(test_config_1.TestConfig.inputPath.csv, 'F' + this.fileIndex + '.csv'); const inputJSONPath = path.join(test_config_1.TestConfig.inputPath.json, 'F' + this.fileIndex + '.json'); const outputJSONPath = path.join(test_config_1.TestConfig.outputPath.json, 'F' + this.fileIndex + '.json'); return Promise.all([ this.tstImport.readDataFIle(inputDatasetPath, this.fileIndex, inputJSONPath), this.tstImport.writeCSV(inputDatasetPath, this.fileIndex, inputCSVPath) ]).then(resp => this.validateBaseKey(resp, outputJSONPath, listOfVariables)) .then((resp) => { if ((resp && resp.result === 'ok')) { console.log(`file ${filepath} completed successfully!!`); } else { console.log(`file ${filepath} completed with errors!!`); } }).catch((e) => { console.log(`error while executing the file ${filepath}`); console.log(e); }); } validateBaseKey(resp, outPath, listOfVariables) { if (resp.length === 2 && (resp[0].data && resp[0].data.result === 'ok') && (resp[1].data && resp[1].data.result === 'ok')) { const csvFilepath = resp[1].data.file; return api_helper_1.APIHelper.call('validateKeys', 'post', { "keyVariables": listOfVariables, "datafile": csvFilepath }).then((resp) => { if (resp.data && resp.data.result === 'ok') { fs.writeFileSync(outPath, JSON.stringify(resp.data.data, null, 4)); return resp.data; } else { return resp.data; } }); } } } exports.BaseKeyValidation = BaseKeyValidation;