github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-supply-chain-master/server/db/agents.js (about) 1 /** 2 * Copyright 2017 Intel Corporation 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * ---------------------------------------------------------------------------- 16 */ 17 'use strict' 18 19 const r = require('rethinkdb') 20 21 const db = require('./') 22 23 const hasCurrentBlock = currentBlock => obj => { 24 return r.and( 25 obj('startBlockNum').le(currentBlock), 26 obj('endBlockNum').gt(currentBlock) 27 ) 28 } 29 30 const getAttribute = attr => obj => obj(attr) 31 const getRecordId = getAttribute('recordId') 32 const getPublicKey = getAttribute('publicKey') 33 const getName = getAttribute('name') 34 const getReporters = getAttribute('reporters') 35 const getAuthorized = getAttribute('authorized') 36 37 const hasPublicKey = key => obj => { 38 return r.eq( 39 key, 40 getPublicKey(obj) 41 ) 42 } 43 44 const getAssociatedAgentId = role => record => record(role).nth(-1)('agentId') 45 const getOwnerId = getAssociatedAgentId('owners') 46 const getCustodianId = getAssociatedAgentId('custodians') 47 48 const isAssociatedWithRecord = association => agent => record => { 49 return r.eq( 50 association(record), 51 getPublicKey(agent) 52 ) 53 } 54 55 const isRecordOwner = isAssociatedWithRecord(getOwnerId) 56 const isRecordCustodian = isAssociatedWithRecord(getCustodianId) 57 58 const isReporter = agent => property => { 59 return getReporters(property) 60 .filter(hasPublicKey(getPublicKey(agent))) 61 .do(seq => r.branch( 62 seq.isEmpty(), 63 false, 64 getAuthorized(seq.nth(0)) 65 )) 66 } 67 68 const getTable = (tableName, block) => 69 r.table(tableName).filter(hasCurrentBlock(block)) 70 71 const listQuery = filterQuery => block => { 72 return getTable('agents', block) 73 .filter(filterQuery) 74 .map(agent => r.expr({ 75 'name': getName(agent), 76 'key': getPublicKey(agent), 77 'owns': getTable('records', block) 78 .filter(isRecordOwner(agent)) 79 .map(getRecordId) 80 .distinct(), 81 'custodian': getTable('records', block) 82 .filter(isRecordCustodian(agent)) 83 .map(getRecordId) 84 .distinct(), 85 'reports': getTable('properties', block) 86 .filter(isReporter(agent)) 87 .map(getRecordId) 88 .distinct() 89 })).coerceTo('array') 90 } 91 92 const fetchQuery = (publicKey, auth) => block => { 93 return getTable('agents', block) 94 .filter(hasPublicKey(publicKey)) 95 .pluck('name', 'publicKey') 96 .nth(0) 97 .do( 98 agent => { 99 return r.branch( 100 auth, 101 agent.merge( 102 fetchUser(publicKey)), 103 agent) 104 }) 105 } 106 107 const fetchUser = publicKey => { 108 return r.table('users') 109 .filter(hasPublicKey(publicKey)) 110 .pluck('username', 'email', 'encryptedKey') 111 .nth(0) 112 } 113 114 const list = filterQuery => db.queryWithCurrentBlock(listQuery(filterQuery)) 115 116 const fetch = (publicKey, auth) => 117 db.queryWithCurrentBlock(fetchQuery(publicKey, auth)) 118 119 module.exports = { 120 list, 121 fetch 122 }