-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
186 lines (170 loc) · 6.21 KB
/
utils.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
const utils = {};
const fs = require('fs');
const request = require('request');
const unzip = require('unzip');
const XMLparser = require('xml2json');
const mongoose = require('mongoose');
const models = require('./models');
mongoose.connect('mongodb://localhost/dinesafe');
function isEmptyObject(obj) {
if(obj === null || obj === undefined) {
return true;
}
return Object.keys(obj).length === 0 && obj.constructor === Object
}
function restaurantExists(id) {
return new Promise((res,rej) => {
models.Restaurant.findOne({establishment_id:id},(err,doc) => {
if(err || doc === null) {
res([err,false]);
}
res([null,true]);
});
});
}
utils.downloadFile = (fileURL) => {
console.log(new Date(), 'Starting Download');
return new Promise((res,rej) => {
const splitPath = fileURL.split('/');
const fileName = splitPath[splitPath.length - 1];
//Check if tmp folder exists
if(fs.existsSync('./tmp') === false ){
//If not create it
fs.mkdirSync('./tmp');
}
const tempStream = fs.createWriteStream(`./tmp/${fileName}`)
request.get(fileURL)
.on('end', () => res(fileName))
.on('error', rej)
.pipe(tempStream);
});
};
utils.unzipFile = (fileName) => {
console.log(new Date(),'Unzipping File');
return new Promise((res,rej) => {
fs.createReadStream(`./tmp/${fileName}`)
.on('end', () => res(fileName))
.on('error', rej)
.pipe(unzip.Extract({
path: `./tmp/${fileName.replace('.zip','')}`
}));
});
};
utils.readXML = (fileName) => {
console.log(new Date(),"Reading XML");
return new Promise((res,rej) => {
const file = fileName.replace('.zip','');
const XMLdata = fs.readFileSync(`./tmp/${file}/${file}.xml`)
const json = JSON.parse(XMLparser.toJson(XMLdata));
res(json.ROWDATA.ROW);
});
};
utils.importRestaurants = (restaurants) => {
console.log(new Date(), 'Importing Restaurants');
return Object.keys(restaurants)
.map(key => restaurants[key])
.reduce((p,curr) => {
return p.then(() => {
return new Promise(async (res,rej) => {
const [err,exists] = await restaurantExists(curr.establishment_id);
if(err || exists) {
res();
return;
}
const restaurant = new models.Restaurant(curr);
restaurant.save((err) => {
if (err) rej(err);
res()
});
});
});
}, Promise.resolve())
};
utils.importInspections = (inspections) => {
console.log(new Date(),"Importing Inspections");
return Object
.keys(inspections)
.map(key => ({ id: key, inspections: inspections[key] }))
.reduce((p,curr) => {
return p.then(() => {
//curr.inspection is an array
return Promise.all(
curr.inspections
//Add update here and upsert
.map(inspection => new Promise((res) => {
models.Inspection.update({
row_id: inspection.row_id
},
inspection,
{ upsert: true },
(err,doc) => {
if(err) {
res(false)
return;
}
res(doc)
}
)
}))
.filter(inspection => inspection)
.map(inspection => new Promise((res,rej) => {
inspection.then((doc) => {
if(doc.upserted === undefined) {
res();
return;
}
models.Restaurant.findOneAndUpdate({
establishment_id: curr.id
},
{
$push: { inspections: doc.upserted[0]._id }
},(err) => {
if(err) rej(err);
res();
});
})
})));
});
}, Promise.resolve())
};
utils.importData = (dinesafeData) => {
const inspections = dinesafeData.reduce((acc,curr) => {
const ID = curr.ESTABLISHMENT_ID;
if (acc[ID] === undefined) {
acc[ID] = []
}
acc[ID].push({
inspection_id: curr.INSPECTION_ID,
infraction_details: isEmptyObject(curr.INFRACTION_DETAILS) ? '' : curr.INFRACTION_DETAILS,
inspection_date: curr.INSPECTION_DATE,
severity: isEmptyObject(curr.SEVERITY) ? '' : curr.SEVERITY,
action: isEmptyObject(curr.ACTION) ? '' : curr.ACTION,
court_outcome: isEmptyObject(curr.COURT_OUTCOME) ? '' : curr.COURT_OUTCOME,
amount_fined: isEmptyObject(curr.AMOUNT_FINED) ? '' : curr.AMOUNT_FINED,
row_id: curr.ROW_ID
});
return acc;
},{});
const restaurants = dinesafeData.reduce((acc,curr) => {
const ID = curr.ESTABLISHMENT_ID;
if(acc[ID]) {
return acc;
}
acc[ID] = {
establishment_id: ID,
establishment_name: curr.ESTABLISHMENT_NAME,
establishment_type: curr.ESTABLISHMENTTYPE,
establishment_address: curr.ESTABLISHMENT_ADDRESS,
establishment_status: curr.ESTABLISHMENT_STATUS,
location: {
type: "Point",
coordinates: [curr.LONGITUDE, curr.LATITUDE]
},
minimum_inspections_per_year: curr.MINIMUM_INSPECTIONS_PERYEAR,
}
return acc;
},{});
return utils.importRestaurants(restaurants)
.then(() => utils.importInspections(inspections));
};
module.exports = utils;