We learned how to implement a transaction function in the previous section by reviewing SampleTransaction. Following a similar approach, we will implement an insurance claim transaction function. Rename sample.js to logic.js.
Implement the Init function, as follows:
Init() function is used to register insuree person information.
/**
* Create the insuree
* @param {com.packt.quickstart.claim.Init} initalAppliation - the InitialApplication transaction
* @transaction
*/
async function Init(application) { // eslint-disable-line no-unused-vars
const factory = getFactory();
const namespace = 'com.packt.quickstart.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);
}
Implement ReportLost and, set up and create a claim, as follows:
/**
* insuree report lost item
* @param {com.packt.quickstart.claim.ReportLost} ReportLost - the ReportLost transaction
* @transaction
*/
async function ReportLost(request) {
const factory = getFactory();
const namespace = 'com.packt.quickstart.claim';
let claimId = request.claimId;
let desc = request.desc;
let insureeId = request.insureeId;
let brokerId = request.brokerId;
const claim = factory.newResource(namespace, 'Claim', claimId);
claim.desc = desc;
claim.status = "ReportLost";
claim.insureeId = insureeId;
claim.brokerId = brokerId;
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); }
Implement RequestedInfo to verify and update the claim status, as follows:
/**
* broker send Requested Info to insuree
* @param {com.packt.quickstart.claim.RequestedInfo} RequestedInfo - the RequestedInfo transaction
* @transaction
*/
async function RequestedInfo(request) { // eslint-disable-line no-unused-vars
const factory = getFactory();
const namespace = 'com.packt.quickstart.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); }
Implement SubmitClaim, ConfirmClaimSubmission, and ApproveClaim. These functions are similar to RequestedInfo.