-
Notifications
You must be signed in to change notification settings - Fork 1
/
funding-mechanism.ts
50 lines (45 loc) · 1.63 KB
/
funding-mechanism.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { eq } from 'drizzle-orm';
import * as schema from '../db/schema';
import { allocateFunding } from '../modules/funding-mechanism';
import { NodePgDatabase } from 'drizzle-orm/node-postgres';
/**
* calculates the funding amount for each option.
*
* @param { NodePgDatabase<typeof db>} dbPool - The PostgreSQL database pool instance.
* @param {string} forumQuestionId - The ID of the forum question for which statistics are to be retrieved.
* @returns {Promise<{ allocated_funding: { [key: string]: number }; remaining_funding: number }>}
* - A promise resolving to an object containing the allocated funding for each project and the remaining funding.
*/
export async function calculateFunding(
dbPool: NodePgDatabase<typeof schema>,
forumQuestionId: string,
): Promise<{
allocatedFunding: { [key: string]: number } | null;
remainingFunding: number | null;
error: string | null;
}> {
const getOptionData = await dbPool
.select({
id: schema.options.id,
voteScore: schema.options.voteScore,
fundingRequest: schema.options.fundingRequest,
})
.from(schema.options)
.where(eq(schema.options.questionId, forumQuestionId));
if (getOptionData.length === 0) {
return {
allocatedFunding: null,
remainingFunding: null,
error: 'Error in query getOptionData',
};
}
const funding = allocateFunding(100000, 10000, getOptionData);
if (!funding) {
return { allocatedFunding: null, remainingFunding: null, error: 'Error in allocating funding' };
}
return {
allocatedFunding: funding.allocated_funding,
remainingFunding: funding.remaining_funding,
error: null,
};
}