-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
300 lines (244 loc) · 8.53 KB
/
Copy pathindex.js
File metadata and controls
300 lines (244 loc) · 8.53 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
const express = require('express');
const fs = require('fs');
const path = require('path');
const json_parser = require('./json_parser');
const fileUpload = require('express-fileupload');
const file_upload_checks=require('./file_upload_checks');
const swaggerJsDOC = require('swagger-jsdoc');
const swaggerUI = require('swagger-ui-express');
const mongoose = require('mongoose');
const { resolve } = require('path');
const app = express();
app.use(express.json());
app.use(fileUpload());
const port = process.env.PORT || 4000;
const swaggerOptions = {
swaggerDefinition:{
info:{
title: 'File Transfer API',
description: 'API for file Upload',
contact:{
name:"Swapnil Satpathy"
}
},
servers:[`http://localhost:${port}`]
},
apis:["index.js"]
}
const swaggerDocs = swaggerJsDOC(swaggerOptions);
app.use('/api-docs',swaggerUI.serve,swaggerUI.setup(swaggerDocs));
mongoose.connect('mongodb://localhost:27017/exercise-1')
.then(() => console.log('Connected to MongoDB'))
.catch((err)=>console.error("Could not connect to MongoDB", err));
const resultSchema = new mongoose.Schema({
filename: String,
Description: String,
Qty: String,
UOM: String,
"Unit Price(Curr)":String,
"Discount%": String,
"Net Amt(Curr)":String,
"Net Amt(AED)":String,
"VAT Code":String,
"VAT%":String,
"VAT Amt(Curr)":String,
"VAT Amt(AED)":String,
"Gross Amt(Curr)":String,
"Gross Amt(AED)":String,
});
const result_from_db = mongoose.model('result',resultSchema);
// Global Objects
let shas=[];
let result_obj={};
// Utility function to convert string to float with logic to handle values with "," in them
function convert_to_float(a) {
let splits = a.split(',');
if(splits[1]){
a=splits[0]+splits[1];
}
let floatValue = +(a);
return floatValue;
}
function insert_into_db(file_name,result){
// Mongoose
async function createDBEntry(){
for(let res of result){
let res1 = new result_from_db({
filename:file_name,
...res});
const res2 = await res1.save();
}
}
createDBEntry();
}
async function get_from_db(file_name){
console.log(file_name);
const result_ = await result_from_db.find({filename:file_name});
return result_;
}
// The parser function to handle parsing of the uploaded json file from client
function parser(file_name){
console.log("Filename: " + file_name);
let rawdata = fs.readFileSync(path.resolve(__dirname, file_name));
try{
let data = JSON.parse(rawdata);
let result = [];
json_parser.extractLabelAndValue(data,result); //Calling the function to extract the label and value fields and get the array of objects in the required format
// Below is done since the file_name is file/input.json but from the client the GET request contains only the filename i.e input.json
key = file_name.split('/')[1];
result_obj[key]=result;
insert_into_db(key,result)
}catch{
throw new Error("Please provide a file of type json");
}
}
/**
* @swagger
* /:
* post:
* summary: Upload a file to server
* description: Upload a json file to server
* consumes:
* - multipart/form-data
* parameters:
* - name: input
* in: formData # <-----
* description: The uploaded file data
* required: true
* type: file # <-----
* responses:
* '200':
* description: File Transfer Success
* '404':
* description: File Transfer Unsuccess
*/
// POST Handler to handle the Upload of the file from client
app.post('/',(req,res)=>{
let dir = './files';
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
if(file_upload_checks.fileExists(`files/${req.files.input.name}`)){
res.status(404).send("File Already Exists");
throw new Error("The file already exists");
}
if(file_upload_checks.shaExists(shas,req.files.input.md5)){
res.status(404).send("File Already Exists");
throw new Error("The file already exists");
}
const name = req.files.input.name;
const arr = name.split('.');
if(arr.length > 2 || arr[1] !== "json"){
res.status(404).send("Please provide the input file in the appropriate json format");
throw new Error("Please provide the input file in the appropriate json format");
}
let disk = require('diskusage');
disk.check('/', function(err, info) {
if(req.files.input.size > info.free){
res.status(404).send("The file size is greater than the available memory");
throw new Error("The file size is greater than the available memory");
}
});
shas.push(req.files.input.md5);
file_name = `files/${req.files.input.name}`;
fs.writeFile(file_name,req.files.input.data,(err)=>{
if(err){
res.status(404).send("Upload Data Failed");
throw new Error("Failed in uploading data");
}else{
parser(file_name);
res.status(200).send("File Transfer Success");
}
})
});
/**
* @swagger
* /{filename}:
* get:
* summary: Use to Get the result for the given file-name in the required format
* description: Use to Get the result for the given file-name in the required format
* parameters:
* - in: path
* name: filename
* required: true
* description: name of the file
* schema:
* type: string
* responses:
* '200':
* description: A successful result in the format of array of objects is returned...
* '404':
* description: If the file is not present
*/
// Querying is based on value and field and field_value
let queries = ["field","value","field_value"];
app.get('/:filename',async (req,res)=>{
if(!(file_upload_checks.fileExists(`files/${req.params.filename}`))){
res.status(404).send("OOPS!!! The file requested for is not available");
throw new Error("OOPS!!! The file requested for is not available");
}
const result = await get_from_db(req.params.filename);
if(!result){
res.status(404).send("The file requested is not available");
throw new Error("The file requested is not available");
}
if(Object.keys(req.query).length === 0){
return res.send(result);
}
for(query in req.query){
if(queries.indexOf(query) === -1){
console.log(query);
res.status(404).send("The provided query parameters are not supported");
throw new Error("The provided query parameters are not supported");
}
}
if(req.query.value){
if(!req.query.field){
let out = 0;
let value = req.query.value;
// Calculating the output specific to the value query parameter, if there is no field_value and field given in query string
for(let i=0;i<result.length;i++){
try{
console.log(convert_to_float(result[i][value]));
const val = convert_to_float(result[i][value]);
if(isNaN(val) == false){
out+=val;
}
}catch{
}
}
if(out == 0){
throw new Error("No Required information is there");
}else{
return res.send(out.toString());
}
}else{
if(!("field_value" in req.query)){
res.status(404).send("Please provide a field_value associated with the field");
throw new Error("Please provide a field_value associated with the field");
}
let out = 0;
// Calculating the output specific to the field_value query parameter
for(let i=0;i<result.length;i++){
if(result[i][req.query.field] === req.query.field_value){
let value = req.query.value;
console.log(convert_to_float(result[i][value]));
const val = convert_to_float(result[i][value]);
if(isNaN(val) == false){
out+=val;
}
}
}
if(out == 0){
throw new Error("No Required information is there");
}else{
return res.send(out.toString());
}
}
}
else{
res.status(404).send("The provided query parameters are not supported");
throw new Error("The provided query parameters are not supported");
}
});
app.listen(port,()=> console.log(`Listening at port ${port}`));