Skip to content

Commit

Permalink
Support manual charity adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
no1mann committed Dec 8, 2024
1 parent c910ed6 commit a180b1d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion website/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
current: [],
previous: [],
refreshTime: 10000, //How often to wait for an API refresh
waitTime: 4000, //How long to wait for the data on the backend to be updated
waitTime: 5000, //How long to wait for the data on the backend to be updated
minRefreshTime: 5000, //Minimum refresh time for the API
graphTime: 1000 * 60 * 10, //Graph should update every 10 minutes
pageIsVisible: true,
Expand Down
2 changes: 1 addition & 1 deletion website/scriptTotal.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
current: [],
previous: [],
refreshTime: 10000, //How often to wait for an API refresh
waitTime: 4000, //How long to wait for the data on the backend to be updated
waitTime: 5000, //How long to wait for the data on the backend to be updated
minRefreshTime: 5000, //Minimum refresh time for the API
graphTime: 1000 * 60 * 10, //Graph should update every 10 minutes
pageIsVisible: true,
Expand Down
27 changes: 22 additions & 5 deletions workers/tiltify-cache/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ const allCharitiesRegionId = 566;
// Pre-2023 Jingle Jam dollar amount = 3371741.16
// End of 2023 yogscast dollar amount = 5747814.82
async function getSummaryData(env: Env): Promise<ApiResponse> {
let apiResponse = await getDefaultResponse(env);
let campaignsComputed: Campaign[] = [];

let causes: Cause[] = JSON.parse(await env.JINGLE_JAM_DATA.get('causes') || "") || [];

let apiResponse = await getDefaultResponse(env, new Date(), causes);

try {
// Perform all Tiltify and Yogscast lookups in parallel
Expand Down Expand Up @@ -163,6 +166,19 @@ async function getSummaryData(env: Env): Promise<ApiResponse> {
}
}

// Any manual adjustments to causes? This is if manual donations are made to the entire fundraiser, but not to a specific cause
for(const cause of causes){
if(cause.override){
for(const apiCause of apiResponse.causes){
if(cause.id === apiCause.id){
apiCause.raised.yogscast += cause.override;
}

apiCause.raised.yogscast -= cause.override/causes.length;
}
}
}

// Round the raised amounts for each cause
for (const cause of apiResponse.causes) {
cause.raised.fundraisers = roundAmount(cause.raised.fundraisers);
Expand Down Expand Up @@ -216,12 +232,13 @@ async function getSummaryData(env: Env): Promise<ApiResponse> {
}

// Get the default response data before any Tiltify or Yogscast API calls
async function getDefaultResponse(env: Env, date = new Date()): Promise<ApiResponse> {
let causes: Cause[] = [];
async function getDefaultResponse(env: Env, date = new Date(), causes: Cause[] | null = null): Promise<ApiResponse> {
let donationHistory: DonationHistory[] = [];

try {
causes = JSON.parse(await env.JINGLE_JAM_DATA.get('causes') || "") || [];
if(!causes){
causes = JSON.parse(await env.JINGLE_JAM_DATA.get('causes') || "") || [];
}
} catch { }

try {
Expand All @@ -236,7 +253,7 @@ async function getDefaultResponse(env: Env, date = new Date()): Promise<ApiRespo
url: cause.url,
donateUrl: cause.donateUrl,
raised: { yogscast: 0, fundraisers: 0 },
}));
})) || [];

return {
date: date,
Expand Down

0 comments on commit a180b1d

Please sign in to comment.