Skip to content
This repository was archived by the owner on Oct 4, 2024. It is now read-only.
Merged

Dev #32

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions routes/v1/ratings.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ const {
const { validateAuraPlayer } = require('../../src/middlewear/aurahandler')
const { persistToLog } = require('../../src/controllers/activityLogController')
const { decrypt } = require('../../src/middlewear/decryption')
const {
resetRatingForConnectionPostRating,
} = require('../../src/controllers/energyAllocationController')

var router = express.Router()

Expand All @@ -34,12 +31,11 @@ router.post('/:fromBrightId/:toBrightId', validateAuraPlayer, async function (
} catch (exception) {
return res.status(500).send(exception.toString())
}
await resetRatingForConnectionPostRating(fromBrightId, toBrightId)
await rateConnection(fromBrightId, toBrightId, rating)
await persistToLog(fromBrightId, toBrightId, {
action: 'RATED_CONNECTION',
amount: rating,
})
// await persistToLog(fromBrightId, toBrightId, {
// action: 'RATED_CONNECTION',
// amount: rating,
// })
res.send()
})

Expand Down
12 changes: 6 additions & 6 deletions src/controllers/energyAllocationController.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ async function clearEnergyForBrightId(brightId) {

async function allocateEnergy(to, from, amount, scale) {
if (amount > 0) {
const userFrom = 'energy/' + from;
const userTo = 'energy/' + to;
const energyFrom = 'energy/' + from;
const energyTo = 'energy/' + to;
await arango.query(aql`
upsert { _to: ${userTo}, _from: ${userFrom} }
insert { _to: ${userTo}, _from: ${userFrom}, allocation: ${amount}, modified: DATE_NOW() }
upsert { _to: ${energyTo}, _from: ${energyFrom} }
insert { _to: ${energyTo}, _from: ${energyFrom}, allocation: ${amount}, modified: DATE_NOW() }
update { modified: DATE_NOW(), allocation: ${amount} }
in ${energyAllocation}
`);
Expand Down Expand Up @@ -58,7 +58,7 @@ async function getSpecificEnergy(fromBrightId, toBrightId) {
)
}

async function resetRatingForConnectionPostRating(fromBrightId, toBrightId) {
async function deleteEnergyAllocation(fromBrightId, toBrightId) {
return messagesModel.pool.query(
'DELETE from "energyTransfer" where "fromBrightId" = $1 AND "toBrightId" = $2',
[fromBrightId, toBrightId],
Expand All @@ -70,6 +70,6 @@ module.exports = {
allocateEnergy,
getEnergy,
getSpecificEnergy,
resetRatingForConnectionPostRating,
deleteEnergyAllocation,
getInboundEnergy,
}
20 changes: 17 additions & 3 deletions src/controllers/ratingController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ const { aql } = require("arangojs");
const { arango } = require("../models/pool.js");
const Model = require('../models/model')
const ratings = new Model('ratings')
const { deleteEnergyAllocation } = require("./energyAllocationController");

const honesty = arango.collection("honesty");
const energyAllocation = arango.collection("energyAllocation");

async function getConnectionsRated(brightId) {
return ratings.pool.query(
Expand All @@ -13,14 +15,26 @@ async function getConnectionsRated(brightId) {
}

async function rateConnection(from, to, honestyRating) {
const userFrom = 'energy/' + from;
const energyFrom = 'energy/' + from;
const energyTo = 'energy/' + to;
const userTo = 'users/' + to;
await arango.query(aql`
upsert { _to: ${userTo}, _from: ${userFrom} }
insert { _to: ${userTo}, _from: ${userFrom}, modified: DATE_NOW(), honesty: ${honestyRating} }
let removeEnergy = (
for e in ${energyAllocation}
filter ${honestyRating} < 1
filter e._from == ${energyFrom}
filter e._to == ${energyTo}
update e with { modified: DATE_NOW(), allocation: 0 } in ${energyAllocation}
)

upsert { _to: ${userTo}, _from: ${energyFrom} }
insert { _to: ${userTo}, _from: ${energyFrom}, modified: DATE_NOW(), honesty: ${honestyRating} }
update { modified: DATE_NOW(), honesty: ${honestyRating} }
in ${honesty}
`);
if (honestyRating < 1){
await deleteEnergyAllocation(from, to);
}
return ratings.pool.query(
'Insert into "ratings"("fromBrightId", "toBrightId", "rating") values ($1, $2, $3) ON CONFLICT ("fromBrightId", "toBrightId") DO UPDATE SET "rating" = $3, "updatedAt" = current_timestamp',
[from, to, honestyRating],
Expand Down