-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
455 lines (422 loc) · 10.5 KB
/
index.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
const express = require("express");
const multer = require("multer");
const XLSX = require("xlsx");
const csv = require("csv-parser");
const fs = require("fs");
const mongoose = require("mongoose");
// Set up Mongoose connection
mongoose.connect("mongodb:https://localhost:27017/mydatabase", {
useNewUrlParser: true,
//useUnifiedTopology: true,
});
// Schema for all the data
const DataSchema = new mongoose.Schema({
agent: {
type: String,
},
userType: {
type: String,
},
policy_mode: {
type: String,
},
producer: {
type: String,
},
policy_number: {
type: String,
},
premium_amount_written: String,
premium_amount: {
type: Number,
},
policy_type: {
type: String,
},
company_name: {
type: String,
},
category_name: {
type: String,
},
policy_start_date: {
type: Date,
},
policy_end_date: {
type: Date,
},
csr: {
type: String,
},
account_name: {
type: String,
},
email: {
type: String,
},
gender: String,
firstname: {
type: String,
},
city: {
type: String,
},
account_type: {
type: String,
},
phone: {
type: String,
},
address: {
type: String,
},
state: {
type: String,
},
zip: {
type: String,
},
dob: {
type: Date,
},
primary: String,
Applicant_ID: String,
agency_id: String,
haveactive_client_policy: String,
});
const UserSchema = new mongoose.Schema({
userType: { type: String },
firstname: { type: String},
email: { type: String},
accountNumber: { type: String},
account_name: { type: String },
gender: { type: String },
city: { type: String },
account_type: { type: String },
phone: { type: String },
address: { type: String },
state: { type: String },
zip: { type: String },
dob: { type: String },
});
const AccountSchema = new mongoose.Schema({
accountNumber: { type: String},
account_name: { type: String },
account_type: { type: String },
producer: { type: String },
premium_amount_written: { type: String },
policy_number: { type: String },
company_name: { type: String },
category_name: { type: String },
});
const PolicySchema = new mongoose.Schema({
policy_number: { type: String},
policy_start_date: { type: Date},
policy_end_date: { type: Date},
agent: { type: String },
policy_mode: { type: String },
producer: { type: String },
premium_amount_written: { type: String },
policy_number: { type: String },
premium_amount: { type: String },
policy_type: { type: String },
company_name: { type: String },
category_name: { type: String },
});
// Create models for each collection
const Data = mongoose.model("Data", DataSchema);
const User = mongoose.model("User", UserSchema);
const Account = mongoose.model("Account", AccountSchema);
const Policy = mongoose.model("Policy", PolicySchema);
// Set up Multer storage for file uploads
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "uploads/");
},
filename: (req, file, cb) => {
cb(null, `${file.fieldname}-${Date.now()}`);
},
});
const upload = multer({ storage });
const app = express();
app.post("/upload", upload.single("file"), async (req, res) => {
const file = req.file;
if (!file) {
return res.status(400).send("No file uploaded");
}
let data = [];
const parseCsv = () => {
return new Promise((resolve, reject) => {
fs.createReadStream(file.path)
.pipe(csv())
.on("data", (row) => {
data.push(row);
})
.on("end", () => {
resolve();
})
.on("error", (error) => {
reject(error);
});
});
};
const dataCsv = await parseCsv()
.then(() => {
return data;
})
.catch((error) => {
console.error(error);
res.status(500).send("Error parsing CSV");
});
console.log(dataCsv);
res.send(dataCsv);
// Save the data to the appropriate collections
dataCsv.forEach((row) => {
// storing all the data
const data = new Data({
agent: row.agent,
userType: row.userType,
policy_mode: row.policy_mode,
producer: row.producer,
policy_number: row.policy_number,
premium_amount_written: row.premium_amount_written,
premium_amount: row.premium_amount,
policy_type: row.policy_type,
company_name: row.company_name,
category_name: row.category_name,
policy_start_date: row.policy_start_date,
policy_end_date: row.policy_end_date,
csr: row.csr,
account_name: row.account_name,
email: row.email,
gender: row.gender,
firstname: row.firstname,
city: row.city,
account_type: row.account_type,
phone: row.phone,
address: row.address,
state: row.state,
zip: row.zip,
dob: row.dob,
primary: row.primary,
agency_id: row.agency_id,
});
data.save();
// Create a new user
const user = new User({
userType: row.userType,
firstname: row.firstname,
email: row.email,
accountNumber: row.accountNumber,
account_name: row.account_name,
gender: row.gender,
city: row.city,
account_type: row.account_type,
phone: row.phone,
address: row.address,
state: row.state,
zip: row.zip,
dob: row.dob,
});
user.save();
// Create a new account
const account = new Account({
accountNumber: row.accountNumber,
account_name: row.account_name,
account_type: row.account_type,
producer: row.producer,
premium_amount_written: row.premium_amount,
policy_number: row.policy_number,
company_name: row.company_name,
category_name: row.category_name,
});
account.save();
// Create a new policy
const policy = new Policy({
policy_number: row.policy_number,
policy_start_date: row.policy_start_date,
policy_end_date: row.policy_end_date,
agent: row.agent,
policy_mode: row.policy_mode,
producer: row.producer,
premium_amount_written: row.premium_amount_written,
policy_number: row.policy_number,
premium_amount: row.premium_amount,
policy_type: row.policy_type,
company_name: row.company_name,
category_name: row.category_name,
});
policy.save();
});
});
// CRUD operation for user
// Create a new user
app.post('/users', async (req, res) => {
try {
const user = new User(req.body);
//console.log(user)
await user.save();
res.status(200).json({success:"user added successfully"});
} catch (error) {
res.status(500).send(error);
}
});
// Read all users
app.get('/users', async (req, res) => {
try {
const users = await User.find();
res.send(users);
} catch (error) {
res.status(500).send(error);
}
});
// Read a user by ID
app.get('/users/:id', async (req, res) => {
try {
const user = await User.findById(req.params.id);
if (!user) {
return res.status(404).send();
}
res.send(user);
} catch (error) {
res.status(500).send(error);
}
});
// Update a user by ID
app.patch('/users/:id', async (req, res) => {
try {
const user = await User.findByIdAndUpdate(req.params.id, req.body, { new: true });
if (!user) {
return res.status(404).send();
}
res.status(200).json({success:"user updated successfully"});
} catch (error) {
res.status(500).send(error);
}
});
// Delete a user by ID
app.delete('/users/:id', async (req, res) => {
try {
const user = await User.findByIdAndDelete(req.params.id);
if (!user) {
return res.status(404).send();
}
res.status(200).json({success:"user deleted successfully"});
} catch (error) {
res.status(500).send(error);
}
});
// Create Account
app.post('/accounts', async (req, res) => {
try {
const account = new Account(req.body);
await account.save();
res.status.json({success:"added account successfully"});
} catch (error) {
res.status(500).send(error);
}
});
// Read all Accounts
app.get('/accounts', async (req, res) => {
try {
const accounts = await Account.find();
res.send(accounts);
} catch (error) {
res.status(500).send(error);
}
});
// Read a account by ID
app.get('/accounts/:id', async (req, res) => {
try {
const account = await Account.findById(req.params.id);
if (!account) {
return res.status(404).send();
}
res.send(account);
} catch (error) {
res.status(500).send(error);
}
});
// Update a account by ID
app.patch('/account/:id', async (req, res) => {
try {
const account = await Account.findByIdAndUpdate(req.params.id, req.body, { new: true });
if (!account) {
return res.status(404).send();
}
res.status(200).json({success:"account updated successfully"});
} catch (error) {
res.status(500).send(error);
}
});
// Delete a account by ID
app.delete('/account/:id', async (req, res) => {
try {
const account = await Account.findByIdAndDelete(req.params.id);
if (!account) {
return res.status(404).send();
}
res.status.json({success:"account deleted successfully"});
} catch (error) {
res.status(500).send(error);
}
});
//create Policy
app.post('/policy', async (req, res) => {
try {
const account = new Policy(req.body);
await account.save();
res.status(200).json({success:"policy created successfully"});
} catch (error) {
res.status(500).send(error);
}
});
// Read all policies
app.get('/policies', async (req, res) => {
try {
const account = await Policy.find();
res.send(account);
} catch (error) {
res.status(500).send(error);
}
});
// Read a policy by ID
app.get('/policy/:id', async (req, res) => {
try {
const account = await Policy.findById(req.params.id);
if (!account) {
return res.status(404).send();
}
res.send(account);
} catch (error) {
res.status(500).send(error);
}
});
// Update a policy by ID
app.patch('/policy/:id', async (req, res) => {
try {
const user = await Policy.findByIdAndUpdate(req.params.id, req.body, { new: true });
if (!user) {
return res.status(404).send();
}
res.status(200).json({success: "policy updated successfully"});
} catch (error) {
res.status(500).send(error);
}
});
// Delete a policy by ID
app.delete('/policy/:id', async (req, res) => {
try {
const user = await Policy.findByIdAndDelete(req.params.id);
if (!user) {
return res.status(404).send();
}
res.status(200).json({success:"deleted policy successfully"});
} catch (error) {
res.status(500).send(error);
}
});
// Start the server
app.listen(3000, () => {
console.log("Server listening on port 3000");
});