forked from vladwulf/claims_vault
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
171 lines (123 loc) · 3.66 KB
/
app.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
var express = require('express');
var app = express();
var fs = require('fs');
var cors = require('cors')
var Datastore = require('nedb')
, db = new Datastore();
function init_db(){
const insured = {
username: 'insured',
password: '0000', // don't laugh
policies: [],
}
const insurer = {
username: 'insurer',
password: '1111', // don't laugh
policies: [],
claims: []
}
const expert = {
username: 'expert',
password: '2222', // don't laugh
claims: []
}
db.insert(insured)
db.insert(insurer)
db.insert(expert)
}
init_db()
const chainApi = require('./lib/core')('http://127.0.0.1:8545');
// console.log(chainApi.get_accounts());
const path = require('path')
const file = fs.readFileSync(path.resolve(__dirname, 'contracts/contract.json'))
const byte = JSON.parse(file).byte
const abi = JSON.parse(file).abi
const address = JSON.parse(file).address
app.use(cors())
const PORT = 5000;
const HOST = 'localhost';
app.get('/insured/submit_policy', (req, res) => {
// insured submits a policy
const id = req.query.id;
const start = req.query.start;
const end = req.query.end;
const type = req.query.type;
chainApi.contract_setter(abi, address, 'proposePolicy', [id, start, end, type] )
.then((result)=>{
db.update({username: 'insured'}, {$push: {policies: id}})
res.send({msg: 'success'});
}).catch((error)=>{
console.log(error);
res.send({msg: 'error'});
})
})
app.get('/insured/accept_policy', (req, res) => {
const id = req.query.id;
chainApi.contract_setter(abi, address, 'acceptPolicy', [id] )
.then((result)=>{
res.send({msg: 'success'});
}).catch((error)=>{
console.log(error);
res.send({msg: 'error'});
})
})
app.get('/insurer/submit_price', (req, res) => {
const id = req.query.id;
const premium = req.query.premium;
const max = req.query.max;
console.log(id, premium, max)
chainApi.contract_setter(abi, address, 'proposePrice', [id, max, premium] )
.then((result)=>{
res.send({msg: 'success'});
}).catch((error)=>{
console.log(error);
res.send({msg: 'error'});
})
})
app.get('/insurer/action', (req, res) => {
// insurer accepts, denies or sumbits to review a given claim
})
app.get('/expert/action', (req, res) => {
// expert begins investigation or declines
})
app.get('/claimant/make_claim', (req, res) => {
// claimant makes a claim
})
app.get('/add',(req,res)=>{
const id = req.query.id;
db.update({username: 'insured'}, {$push: {policies: id }},(error, doc)=>{
if(error) res.send({msg:'error'});
if(doc) res.send({msg: 'success'})
});
})
app.get('/getPolicies', (req, res)=>{
const username = req.query.user;
let policies = [];
db.findOne({username: username}, (err, user) =>{
user.policies.forEach((elem, i)=>{
policies.push(chainApi.contract_getter(abi, address, 'getPolicy', [elem]))
})
res.send(policies)
})
})
app.get ('/login', (req, res) =>{
const username = req.query.username;
const password = req.query.password;
db.findOne({ username: username }, function (err, user) {
if(err) console.log(err);
if(user.password === password){
res.send({
msg: 'success',
username: username
})
}
else {
res.send({
msg: 'error'
})
}
});
})
app.listen(PORT, HOST, (req, res) => {
console.log(`listening from ${HOST}:${PORT}`)
})