-
Notifications
You must be signed in to change notification settings - Fork 0
/
insuranceClaim.js
260 lines (223 loc) · 10.6 KB
/
insuranceClaim.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
'use strict';
const shim = require('fabric-shim');
const util = require('util');
var log4js = require('log4js');
var logger = log4js.getLogger('ChaincodeLogger');
// ===============================================
// Chaincode name:[insuranceClaim.js]
// ===============================================
let Chaincode = class {
async Init(stub) {
let ret = stub.getFunctionAndParameters();
logger.info(ret);
logger.info('=========== Instantiated Chaincode ===========');
return shim.success();
}
async Invoke(stub) {
logger.info('Transaction ID: ' + stub.getTxID());
logger.info(util.format('Args: %j', stub.getArgs()));
let ret = stub.getFunctionAndParameters();
logger.info(ret);
let method = this[ret.fcn];
if (!method) {
logger.error('no function of name:' + ret.fcn + ' found');
throw new Error('Received unknown function ' + ret.fcn + ' invocation');
}
try {
let payload = await method(stub, ret.params, this);
return shim.success(payload);
} catch (err) {
logger.error(err);
return shim.error(err);
}
}
// ============================================================================================================================
// Create the Insuree
// ============================================================================================================================
async function createInsuree(application) {
logger.info("============= START: createInsuree =============");
let jsonResp = {}
if (args.length != 5) {
jsonResp.success = false
jsonResp.errorCode = 'C-10000';
jsonResp.errorMsg = 'Incorrect number of arguments. Expecting 5 arguments';
throw new Error(JSON.stringify(jsonResp));
}
if (!args[0]) {
jsonResp.success = false
jsonResp.errorCode = 'C-10001';
jsonResp.errorMsg = 'mandatory argument Id is missing';
throw new Error(JSON.stringify(jsonResp));
}
if (!args[1]) {
jsonResp.success = false
jsonResp.errorCode = 'C-10002';
jsonResp.errorMsg = 'mandatory argument FirstName is missing';
throw new Error(JSON.stringify(jsonResp));
}
if (!args[2]) {
jsonResp.success = false
jsonResp.errorCode = 'C-10003';
jsonResp.errorMsg = 'mandatory argument LastName is missing';
throw new Error(JSON.stringify(jsonResp));
}
if (!args[3]) {
jsonResp.success = false
jsonResp.errorCode = 'C-10004';
jsonResp.errorMsg = 'mandatory argument SSN is missing';
throw new Error(JSON.stringify(jsonResp));
}
if (!args[4]) {
jsonResp.success = false
jsonResp.errorCode = 'C-10005';
jsonResp.errorMsg = 'mandatory argument PolicyNumber is missing';
throw new Error(JSON.stringify(jsonResp));
}
const factory = getFactory();
const namespace = 'insurance.claim';
const insuree = factory.newResource(namespace, 'Insuree', application.insureeId);
insuree.firstName = application.firstName;;
insuree.lastName = application.lastName;
insuree.ssn = application.ssn;;
insuree.policyNumber = application.policyNumber;;
const participantRegistry = await getParticipantRegistry(insuree.getFullyQualifiedType());
await participantRegistry.add(insuree);
// emit event
const initEventEvent = factory.newEvent(namespace, 'InitEvent');
initEventEvent.insuree = insuree;
emit(initEventEvent);
}
// ============================================================================================================================
// Insuree report the lost
// ============================================================================================================================
async function ReportLost(request) {
logger.info("============= START: ReportLost =============");
let jsonResp = {}
if (args.length != 4) {
jsonResp.success = false
jsonResp.errorCode = 'C-10000';
jsonResp.errorMsg = 'Incorrect number of arguments. Expecting 4 arguments';
throw new Error(JSON.stringify(jsonResp));
}
if (!args[0]) {
jsonResp.success = false
jsonResp.errorCode = 'C-10001';
jsonResp.errorMsg = 'mandatory argument claimId is missing';
throw new Error(JSON.stringify(jsonResp));
}
if (!args[1]) {
jsonResp.success = false
jsonResp.errorCode = 'C-10002';
jsonResp.errorMsg = 'mandatory argument desc is missing';
throw new Error(JSON.stringify(jsonResp));
}
if (!args[2]) {
jsonResp.success = false
jsonResp.errorCode = 'C-10003';
jsonResp.errorMsg = 'mandatory argument insureeId is missing';
throw new Error(JSON.stringify(jsonResp));
}
if (!args[3]) {
jsonResp.success = false
jsonResp.errorCode = 'C-10004';
jsonResp.errorMsg = 'mandatory argument policeId is missing';
throw new Error(JSON.stringify(jsonResp));
}
const factory = getFactory();
const namespace = 'insurance.claim';
let claimId = request.claimId;
let desc = request.desc;
let insureeId = request.insureeId;
let policeId = request.policeId;
const claim = factory.newResource(namespace, 'Claim', claimId);
claim.desc = desc;
claim.status = "ReportLost";
claim.insureeId = insureeId;
claim.policeId = policeId;
claim.insurerId = "";
claim.comment = "";
claim.processAt = (new Date()).toString();
const claimRegistry = await getAssetRegistry(claim.getFullyQualifiedType());
await claimRegistry.add(claim);
// emit event
const reportLostEvent = factory.newEvent(namespace, 'ReportLostEvent');
reportLostEvent.claim = claim;
emit(reportLostEvent);
}
// ============================================================================================================================
// police return requested Information
// ============================================================================================================================
async function RequestedInfo(request) {
const factory = getFactory();
const namespace = 'insurance.claim';
let claim = request.claim;
if (claim.status !== 'ReportLost') {
throw new Error ('This claim should be in ReportLost status');
}
claim.status = 'RequestedInfo';
claim.processAt = (new Date()).toString();
const assetRegistry = await getAssetRegistry(request.claim.getFullyQualifiedType());
await assetRegistry.update(claim);
// emit event
const requestedInfoEventEvent = factory.newEvent(namespace, 'RequestedInfoEvent');
requestedInfoEventEvent.claim = claim;
emit(requestedInfoEventEvent);
}
// ============================================================================================================================
// police submit a claim to insurer
// ============================================================================================================================
async function SubmitClaim(request) {
const factory = getFactory();
const namespace = 'insurance.claim';
let claim = request.claim;
if (claim.status !== 'RequestedInfo') {
throw new Error ('This claim should be in RequestedInfo status');
}
claim.status = 'SubmitClaim';
claim.processAt = (new Date()).toString();
const assetRegistry = await getAssetRegistry(request.claim.getFullyQualifiedType());
await assetRegistry.update(claim);
// emit event
const submitClaimEvent = factory.newEvent(namespace, 'SubmitClaimEvent');
submitClaimEvent.claim = claim;
emit(submitClaimEvent);
}
// ============================================================================================================================
// insurer confirm get police submission
// ============================================================================================================================
async function ConfirmClaimSubmission(request) {
const factory = getFactory();
const namespace = 'insurance.claim';
let claim = request.claim;
if (claim.status !== 'SubmitClaim') {
throw new Error ('This claim should be in SubmitClaim status');
}
claim.status = 'ConfirmClaimSubmission';
claim.processAt = (new Date()).toString();
const assetRegistry = await getAssetRegistry(request.claim.getFullyQualifiedType());
await assetRegistry.update(claim);
// emit event
const confirmClaimSubmissionEvent = factory.newEvent(namespace, 'ConfirmClaimSubmissionEvent');
confirmClaimSubmissionEvent.claim = claim;
emit(confirmClaimSubmissionEvent);
}
// ============================================================================================================================
// insurer approval claim
// ============================================================================================================================
async function ApproveClaim(request) {
const factory = getFactory();
const namespace = 'insurance.claim';
let claim = request.claim;
if (claim.status !== 'ConfirmClaimSubmission') {
throw new Error ('This claim should be in ConfirmClaimSubmission status');
}
claim.status = 'ApproveClaim';
claim.processAt = (new Date()).toString();
const assetRegistry = await getAssetRegistry(request.claim.getFullyQualifiedType());
await assetRegistry.update(claim);
// emit event
const approveClaimEvent = factory.newEvent(namespace, 'ApproveClaimEvent');
approveClaimEvent.claim = claim;
emit(approveClaimEvent);
}
}