Skip to content

Commit af4325d

Browse files
committed
feat: update proposal's value
1 parent 85782d9 commit af4325d

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/scores.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,26 @@ async function updateProposalScores(proposalId: string, scores: any, votes: numb
9797
]);
9898
}
9999

100+
async function updateProposalScoresValue(
101+
proposalId: string,
102+
score_by_strategy_by_choice: number[][]
103+
) {
104+
const scores_by_strategy =
105+
score_by_strategy_by_choice?.[0]
106+
?.map((_, index) => score_by_strategy_by_choice.reduce((sum, array) => sum + array[index], 0))
107+
?.map(score => parseFloat(score.toFixed(9))) || [];
108+
109+
const score_total_value = scores_by_strategy.reduce((sum, score) => sum + score, 0);
110+
111+
const query = `
112+
UPDATE proposals
113+
SET scores_total_value = ?,
114+
scores_by_strategy = ?
115+
WHERE id = ? LIMIT 1;
116+
`;
117+
await db.queryAsync(query, [score_total_value, JSON.stringify(scores_by_strategy), proposalId]);
118+
}
119+
100120
const pendingRequests = {};
101121

102122
export async function updateProposalAndVotes(proposalId: string, force = false) {
@@ -186,6 +206,9 @@ export async function updateProposalAndVotes(proposalId: string, force = false)
186206
);
187207

188208
delete pendingRequests[proposalId];
209+
210+
await updateProposalScoresValue(proposalId, results.scores_by_strategy);
211+
189212
return true;
190213
} catch (e) {
191214
delete pendingRequests[proposalId];

test/schema.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ CREATE TABLE proposals (
6464
scores_state VARCHAR(24) NOT NULL DEFAULT '',
6565
scores_total DECIMAL(64,30) NOT NULL,
6666
scores_updated INT(11) NOT NULL,
67-
scores_total_value DECIMAL(64,30) NOT NULL DEFAULT '0.000000000000000000000000000000',
67+
scores_total_value DECIMAL(13,3) NOT NULL DEFAULT 0.000,
6868
vp_value_by_strategy json NOT NULL,
6969
votes INT(12) NOT NULL,
7070
flagged INT NOT NULL DEFAULT 0,

0 commit comments

Comments
 (0)