Skip to content

Commit 0e77cd4

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

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/scores.ts

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

100+
async function updateProposalScoresValue(proposalId: string) {
101+
const [proposal] = await db.queryAsync(
102+
'SELECT vp_value_by_strategy, scores_by_strategy FROM proposals WHERE id = ? LIMIT 1;',
103+
[proposalId]
104+
);
105+
const scoresByStrategy: number[][] = JSON.parse(proposal.scores_by_strategy);
106+
const vpValueByStrategy: number[] = JSON.parse(proposal.vp_value_by_strategy);
107+
108+
const score_total_value =
109+
scoresByStrategy[0]
110+
?.map((_, index) => scoresByStrategy.reduce((sum, array) => sum + array[index], 0))
111+
?.map((value, index) => value * vpValueByStrategy[index])
112+
?.reduce((sum, value) => sum + value, 0) || 0;
113+
114+
const query = 'UPDATE proposals SET scores_total_value = ? WHERE id = ? LIMIT 1;';
115+
await db.queryAsync(query, [score_total_value, proposalId]);
116+
}
117+
100118
const pendingRequests = {};
101119

102120
export async function updateProposalAndVotes(proposalId: string, force = false) {
@@ -186,6 +204,9 @@ export async function updateProposalAndVotes(proposalId: string, force = false)
186204
);
187205

188206
delete pendingRequests[proposalId];
207+
208+
await updateProposalScoresValue(proposalId);
209+
189210
return true;
190211
} catch (e) {
191212
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)