-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatientrecordcontract.js
201 lines (165 loc) · 5.9 KB
/
patientrecordcontract.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
/* eslint-disable quote-props */
/* eslint-disable quotes */
/* eslint-disable linebreak-style */
/*
SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const { Contract, Context } = require('fabric-contract-api');
const PatientRecord = require('./patientrecord.js');
const PatientRecordList = require('./patientrecordlist.js');
class PatientRecordContext extends Context {
constructor() {
super();
this.patientRecordList = new PatientRecordList(this);
}
}
/**
* Define patient record smart contract by extending Fabric Contract class
*
*/
class PatientRecordContract extends Contract {
constructor() {
super('edu.asu.patientrecordcontract');
}
/**
* Define a custom context for commercial paper
*/
createContext() {
return new PatientRecordContext();
}
/**
* Instantiate to perform any setup of the ledger that might be required.
* @param {Context} ctx the transaction context
*/
async init(ctx) {
console.log('Instantiated the patient record smart contract.');
}
async unknownTransaction(ctx){
throw new Error('Function name missing')
}
async afterTransaction(ctx){
console.log('---------------------INSIDE afterTransaction-----------------------')
let func_and_params = ctx.stub.getFunctionAndParameters()
console.log('---------------------func_and_params-----------------------')
console.log(func_and_params)
console.log(func_and_params['fcn'] === 'createPatientRecord' && func_and_params['params'][4]==='AB-')
if (func_and_params['fcn'] === 'createPatientRecord' && func_and_params['params'][4]==='AB-') {
ctx.stub.setEvent('rare-blood-type', JSON.stringify({'username': func_and_params.params[0]}))
console.log('Chaincode event is being created!')
}
}
/**
* patient record
* @param {Context} ctx the transaction context
* @param {String} username username
* @param {String} name name
* @param {String} dob date of birth
* @param {String} gender gender
* @param {String} blood_type blood type
*/
async createPatientRecord(ctx,username,name,dob,gender,blood_type){
let precord = PatientRecord.createInstance(username,name,dob,gender,blood_type);
await ctx.patientRecordList.addPRecord(precord);
return precord.toBuffer();
}
async getPatientByKey(ctx, username, name){
let precordKey = PatientRecord.makeKey([username,name]);
//TASK-1: Use a method from patientRecordList to read a record by key
let precord = await ctx.patientRecordList.getPRecord(precordKey);
return JSON.stringify(precord)
}
/**
* Update lastCheckupDate to an existing record
* @param {Context} ctx the transaction context
* @param {String} username username
* @param {String} name name
* @param {String} lastCheckupDate date string
*/
async updateCheckupDate(ctx,username,name,lastCheckupDate){
let precordKey = PatientRecord.makeKey([username,name]);
let precord = await ctx.patientRecordList.getPRecord(precordKey);
precord.setlastCheckupDate(lastCheckupDate);
await ctx.patientRecordList.updatePRecord(precord);
return precord.toBuffer();
}
/**
* Evaluate a queryString
* This is the helper function for making queries using a query string
*
* @param {Context} ctx the transaction context
* @param {String} queryString the query string to be evaluated
*/
async queryWithQueryString(ctx, queryString) {
console.log("query String");
console.log(JSON.stringify(queryString));
let resultsIterator = await ctx.stub.getQueryResult(queryString);
let allResults = [];
while (true) {
let res = await resultsIterator.next();
if (res.value && res.value.value.toString()) {
let jsonRes = {};
console.log(res.value.value.toString('utf8'));
jsonRes.Key = res.value.key;
try {
jsonRes.Record = JSON.parse(res.value.value.toString('utf8'));
} catch (err) {
console.log(err);
jsonRes.Record = res.value.value.toString('utf8');
}
allResults.push(jsonRes);
}
if (res.done) {
console.log('end of data');
await resultsIterator.close();
console.info(allResults);
console.log(JSON.stringify(allResults));
return JSON.stringify(allResults);
}
}
}
/**
* Query by Gender
*
* @param {Context} ctx the transaction context
* @param {String} gender gender to be queried
*/
async queryByGender(ctx, gender) {
const queryString = {
selector : {gender : gender},
use_index: "genderIndex"
}
const selectorString = JSON.stringify(queryString);
return this.queryWithQueryString(ctx,selectorString);
}
/**
* Query by Blood_Type
*
* @param {Context} ctx the transaction context
* @param {String} blood_type blood_type to queried
*/
// Graded Function
async queryByBlood_Type(ctx, blood_type) {
const queryString = {
selector : {blood_type : blood_type},
use_index: "blood_typeIndex"
}
const selectorString = JSON.stringify(queryString);
return this.queryWithQueryString(ctx,selectorString);
}
/**
* Query by Blood_Type Dual Query
*
* @param {Context} ctx the transaction context
* @param {String} blood_type blood_type to queried
*/
async queryByBlood_Type_Dual(ctx, blood_type1, blood_type2) {
const queryString = {
selector : {blood_type : {$in:[blood_type1,blood_type2]}},
use_index: "blood_typeIndex"
}
const selectorString = JSON.stringify(queryString);
return this.queryWithQueryString(ctx,selectorString);
}
}
module.exports = PatientRecordContract;