Skip to content

Commit

Permalink
Merge branch 'staging'
Browse files Browse the repository at this point in the history
  • Loading branch information
xavier506 committed Oct 19, 2021
2 parents e76886b + 126cd52 commit f156ffe
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 44 deletions.
13 changes: 13 additions & 0 deletions hapi/.prettierrc
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 0 additions & 11 deletions hapi/prettier.config.js

This file was deleted.

14 changes: 8 additions & 6 deletions hapi/src/index.js
Original file line number Diff line number Diff line change
@@ -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')

Expand Down Expand Up @@ -35,16 +37,16 @@ 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' }
])

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,
Expand Down
44 changes: 17 additions & 27 deletions hapi/src/libs/sync-ratings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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'
Expand All @@ -64,12 +59,7 @@ const updateUserRatingsAux = async (scope) => {
} catch (err) {
console.log(`Error: ${err}`)
}
})
}

const updateUserRatings = () => {
updateUserRatingsAux(generalContractScope)
updateUserRatingsAux(edenContractScope)
}
}

updateUserRatings()
updateUserRatings()
5 changes: 5 additions & 0 deletions hapi/src/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
updateBpStatsUtil: require('./update-bp-stats.util'),
updateUserRatingUtil: require('./update-user-rating.util'),
validateAccountNameUtil: require('./valid-account-name')
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit f156ffe

Please sign in to comment.