diff --git a/hapi/.prettierrc b/hapi/.prettierrc new file mode 100644 index 00000000..1719afbd --- /dev/null +++ b/hapi/.prettierrc @@ -0,0 +1,13 @@ +{ + "trailingComma": "none", + "endOfLine": "auto", + "tabWidth": 2, + "singleQuote": true, + "semi": false, + "arrowParens": "avoid", + "printWidth": 80, + "useTabs": false, + "bracketSpacing": true, + "jsxBracketSameLine": false, + "jsxSingleQuote": true +} diff --git a/hapi/prettier.config.js b/hapi/prettier.config.js deleted file mode 100644 index a63208a7..00000000 --- a/hapi/prettier.config.js +++ /dev/null @@ -1,11 +0,0 @@ -module.exports = { - printWidth: 80, - tabWidth: 2, - useTabs: false, - semi: false, - singleQuote: true, - trailingComma: 'none', - bracketSpacing: true, - jsxBracketSameLine: false, - jsxSingleQuote: true -} diff --git a/hapi/src/index.js b/hapi/src/index.js index 1a705087..1ae53b44 100644 --- a/hapi/src/index.js +++ b/hapi/src/index.js @@ -1,9 +1,11 @@ 'use strict' const { HAPI_SERVER_PORT, HAPI_SERVER_ADDRESS } = process.env -const updateBpStats = require('./libs/sync-bp-stats') -const updateUserRatings = require('./libs/sync-user-rating') -const accountValidation = require('./libs/valid-account-name') +const { + updateBpStatsUtil, + updateUserRatingUtil, + validateAccountNameUtil +} = require('./utils/') const Hapi = require('@hapi/hapi') @@ -35,7 +37,7 @@ const init = async () => { const { ratingInput: { user, producer, transaction, isEden } } = input - const isValidAccountName = accountValidation([ + const isValidAccountName = validateAccountNameUtil([ { name: user, type: 'user account' }, { name: producer, type: 'block producer' } ]) @@ -43,8 +45,8 @@ const init = async () => { if (!isValidAccountName.isValidAccountName) throw new Error(isValidAccountName.message) - const resultEden = await updateBpStats(producer) - const result = await updateUserRatings( + const resultEden = await updateBpStatsUtil(producer) + const result = await updateUserRatingUtil( user, producer, transaction, diff --git a/hapi/src/libs/sync-ratings.js b/hapi/src/libs/sync-ratings.js index f8d0b9ac..9167f036 100644 --- a/hapi/src/libs/sync-ratings.js +++ b/hapi/src/libs/sync-ratings.js @@ -11,7 +11,7 @@ const HAPI_EOS_API_ENDPOINT = process.env.HAPI_EOS_API_ENDPOINT || 'https://jungle.eosio.cr' const HAPI_RATING_CONTRACT = process.env.HAPI_RATING_CONTRACT || 'rateproducer' -const getUserRatings = async (scope) => { +const getUserRatings = async scope => { const eos = new JsonRpc(HAPI_EOS_API_ENDPOINT, { fetch }) try { @@ -32,30 +32,25 @@ const getUserRatings = async (scope) => { } } -const updateUserRatingsAux = async (scope) => { - console.log(`==== Updating ratings for ${scope} ====`) - const userRatings = await getUserRatings(scope) +const updateUserRatings = async () => { + console.log(`==== Updating ratings ====`) - userRatings.rows.forEach(async (rating) => { - const ratingsCore = { - user: rating.user, - bp: rating.bp, - ratings: { - transparency: rating.transparency || 0, - infrastructure: rating.infrastructure || 0, - trustiness: rating.trustiness || 0, - development: rating.development || 0, - community: rating.community || 0 - } - } + const db = await massiveDB + if (!db) throw new Error('Missing massive instance') + + const generalRatings = await getUserRatings(generalContractScope) + const edenRatings = await getUserRatings(edenContractScope) + const allRatings = [...generalRatings.rows, ...edenRatings.rows] + + for (const singleRating of allRatings) { + const { user, bp, ...ratings } = singleRating + const ratingsCore = { user, bp, ratings } try { - const resultRatingsSave = await ( - await massiveDB - ).user_ratings.save(ratingsCore) + const resultRatingsSave = await db.user_ratings.save(ratingsCore) const dbResult = resultRatingsSave ? resultRatingsSave - : await (await massiveDB).user_ratings.insert(ratingsCore) + : await db.user_ratings.insert(ratingsCore) console.log( `Save or insert of ${ratingsCore.user}-${ratingsCore.bp} was ${ dbResult ? 'SUCCESSFULL' : 'UNSUCCESSFULL' @@ -64,12 +59,7 @@ const updateUserRatingsAux = async (scope) => { } catch (err) { console.log(`Error: ${err}`) } - }) -} - -const updateUserRatings = () => { - updateUserRatingsAux(generalContractScope) - updateUserRatingsAux(edenContractScope) + } } -updateUserRatings() \ No newline at end of file +updateUserRatings() diff --git a/hapi/src/utils/index.js b/hapi/src/utils/index.js new file mode 100644 index 00000000..09210780 --- /dev/null +++ b/hapi/src/utils/index.js @@ -0,0 +1,5 @@ +module.exports = { + updateBpStatsUtil: require('./update-bp-stats.util'), + updateUserRatingUtil: require('./update-user-rating.util'), + validateAccountNameUtil: require('./valid-account-name') +} diff --git a/hapi/src/libs/sync-bp-stats.js b/hapi/src/utils/update-bp-stats.util.js similarity index 100% rename from hapi/src/libs/sync-bp-stats.js rename to hapi/src/utils/update-bp-stats.util.js diff --git a/hapi/src/libs/sync-user-rating.js b/hapi/src/utils/update-user-rating.util.js similarity index 100% rename from hapi/src/libs/sync-user-rating.js rename to hapi/src/utils/update-user-rating.util.js diff --git a/hapi/src/libs/valid-account-name.js b/hapi/src/utils/valid-account-name.js similarity index 100% rename from hapi/src/libs/valid-account-name.js rename to hapi/src/utils/valid-account-name.js