DBLabs/abitur_list_client/savePostgresToJSON.js

66 lines
2.6 KiB
JavaScript

const axios = require('axios');
const fs = require('fs');
const {MongoClient, ObjectId} = require('mongodb')
const saveToJson = async () => {
var mongoData = []
var abiturs = (await axios.get('http://127.0.0.1:8080/api/abitur')).data
mongoData = mongoData.concat(abiturs)
for(let abitur of mongoData) {
// saving exam results for every abitur
var exam_results = (await axios.get('http://127.0.0.1:8080/api/examresult/abitur/' + abitur['id'])).data
for (let exam_res of exam_results) {
abitur['exam_results'] = abitur['exam_results'] ?? []
abitur['exam_results'].push({
'title': exam_res['title'],
'result': exam_res['result']
})
}
//saving requests for every abitur
var requests = (await axios.get('http://127.0.0.1:8080/api/request/abitur/' + abitur['id'])).data
//console.log(requests)
for (let req of requests) {
abitur['requests'] = abitur['requests'] ?? []
let education_form = (await axios.get('http://127.0.0.1:8080/api/educationform/' + req['education_form_id'])).data
let specialization = (await axios.get('http://127.0.0.1:8080/api/specialization/' + req['specialization_id'])).data
//получаем закрепленные за заявкой результаты
let results = []
var request_result_pairs = (await axios.get('http://127.0.0.1:8080/api/request_examresult/' + req['id'])).data
for(const pair of request_result_pairs) {
results.push((await axios.get('http://127.0.0.1:8080/api/examresult/' + pair['exam_result_id'])).data)
}
let formattedResults = []
for(let res of results) {
formattedResults.push({
'title': res['title'],
'result': res['result']
})
}
abitur['requests'].push({
'date': req['date'],
'education_form': education_form['title'],
'specialization': specialization['title'],
'exam_results': formattedResults
})
}
}
console.log(mongoData)
let data = JSON.stringify(mongoData)
fs.writeFileSync('mongoDataSave.json', data)
console.log('saved!')
}
saveToJson()
module.exports = saveToJson