-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
111 lines (92 loc) · 3.21 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
const express = require('express');
const bodyParser = require('body-parser');
const Joi = require('joi');
// Initialize the app
const app = express();
// Configure bodyparser to handle post requests
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
// ROUTES
app.get('/', (req, res) => {
res.send('This API is made with Node.js + Express.js');
});
app.get('/v1/quote/car-insurance', (req, res) => {
res.send('Welcome to the car insurance API');
});
app.post('/v1/quote/car-insurance', (req, res) => {
// shema to validate params send by the user
const shema = {
car_value: Joi.number().min(99).required(),
driver_birthdate: Joi.string().regex(/^([0-2][0-9]|(3)[0-1])(\/)(((0)[0-9])|((1)[0-2]))(\/)\d{4}$/).required()
}
const result = Joi.validate(req.body, shema);
if (result.error || !req.body.car_value || !req.body.driver_birthdate) {
res.status(400).send(
{
"success": false,
"message": "parameters missing or incorrect values"
}
)
} else {
// date and age formating
function formatDate(str) {
const splitedDate = str.split(/\//);
const formatedDate = [splitedDate[1], splitedDate[0], splitedDate[2]].join('/');
return formatedDate;
}
function msAge(date) {
const ageMs = new Date() - new Date(date);
return ageMs;
}
function msToYear(ms) {
const year = ms / 31557600000; // = (365.25 * 24 * 60 * 60 * 1000)
return year;
}
let userAge = msToYear(msAge(formatDate(req.body.driver_birthdate)));
if (userAge < 18) {
return res.status(200).send(
{
"success": true,
"message": "quote successfully computed",
"data": {
"eligible": false,
}
}
);
} else if (18 <= userAge && userAge < 26) {
return res.status(200).send(
{
"success": true,
"message": "quote successfully computed",
"data": {
"eligible": true,
"premiums": {
"civil_liability": 1000.00,
"omnium": `${(Number(req.body.car_value) / 100) * 3}`
}
}
}
);
} else if (userAge >= 26) {
return res.status(200).send(
{
"success": true,
"message": "quote successfully computed",
"data": {
"eligible": true,
"premiums": {
"civil_liability": 500.00,
"omnium": `${(Number(req.body.car_value) / 100) * 3}`
}
}
}
);
}
}
});
//PORT
const port = process.env.port || 8080;
app.listen(port, () => console.log(`Listening on port ${port}...`));
module.exports = app; //export modules for testing units