sendou.ink/utils/plus.ts
Kalle (Sendou) e1ee688c88 Closes #531
2021-05-31 23:16:17 +03:00

67 lines
1.7 KiB
TypeScript

import { PlusRegion } from "@prisma/client";
export const getPercentageFromCounts = (
countsNA: number[],
countsEU: number[],
votedUserRegion: PlusRegion
) => {
const sameRegionArr = votedUserRegion === "NA" ? countsNA : countsEU;
const otherRegionArr = votedUserRegion === "NA" ? countsEU : countsNA;
const sameSum =
sameRegionArr[0] * -2 +
sameRegionArr[1] * -1 +
sameRegionArr[2] * 1 +
sameRegionArr[3] * 2;
const sameVoterCount = sameRegionArr.reduce((acc, cur) => acc + cur, 0);
const otherSum = otherRegionArr[1] * -1 + otherRegionArr[2] * 1;
const otherVoterCount = otherRegionArr.reduce((acc, cur) => acc + cur, 0);
return parseFloat(
(
((sameSum / sameVoterCount + otherSum / otherVoterCount + 3) / 6) *
100
).toFixed(1)
);
};
const getThirdFridayDate = (nextMonth?: boolean) => {
const result = new Date();
if (nextMonth) result.setMonth(result.getMonth() + 1, 1);
result.setUTCHours(10, 0, 0, 0);
result.setDate(1);
// Get the first Friday in the month
while (result.getDay() !== 5) {
result.setDate(result.getDate() + 1);
}
// Get the third Friday
result.setDate(result.getDate() + 14);
return result;
};
export const getVotingRange = () => {
const startDate = getThirdFridayDate();
// Get the ending time on Monday
const endDate = new Date(startDate);
endDate.setDate(endDate.getDate() + 3);
const isHappening =
new Date().getTime() > startDate.getTime() &&
new Date().getTime() < endDate.getTime();
const nextVotingDate =
startDate.getTime() > new Date().getTime()
? startDate
: getThirdFridayDate(true);
return { startDate, endDate, isHappening, nextVotingDate };
};