github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-supply-chain-master/fish_client/src/utils/records.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 _getProp = (record, propName) => { 20 return record.properties.find((prop) => prop.name === propName) 21 } 22 23 const getPropertyValue = (record, propName, defaultValue = null) => { 24 let prop = _getProp(record, propName) 25 if (prop && prop.value) { 26 return prop.value 27 } else { 28 return defaultValue 29 } 30 } 31 32 const isReporter = (record, propName, publicKey) => { 33 let prop = _getProp(record, propName) 34 if (prop) { 35 return prop.reporters.indexOf(publicKey) > -1 36 } else { 37 return false 38 } 39 } 40 41 const _getPropTimeByComparison = (compare) => (record) => { 42 if (!record.updates.properties) { 43 return null 44 } 45 46 return Object.values(record.updates.properties) 47 .reduce((acc, updates) => acc.concat(updates), []) 48 .reduce((selected, update) => 49 compare(selected.timestamp, update.timestamp) ? update : selected) 50 .timestamp 51 } 52 53 const getLatestPropertyUpdateTime = 54 _getPropTimeByComparison((selected, timestamp) => selected < timestamp) 55 56 const getOldestPropertyUpdateTime = 57 _getPropTimeByComparison((selected, timestamp) => selected > timestamp) 58 59 const countPropertyUpdates = (record) => { 60 if (!record.updates.properties) { 61 return 0 62 } 63 64 return Object.values(record.updates.properties).reduce( 65 (sum, updates) => sum + updates.length, 0) 66 } 67 68 module.exports = { 69 getPropertyValue, 70 isReporter, 71 getLatestPropertyUpdateTime, 72 getOldestPropertyUpdateTime, 73 countPropertyUpdates 74 }