can't vouch or suggest if voting has started

This commit is contained in:
Kalle 2021-02-28 15:06:55 +02:00
parent 85408d30cd
commit d38f707d17
2 changed files with 33 additions and 3 deletions

View File

@ -22,3 +22,29 @@ export const getPercentageFromCounts = (
((sameSum / sameVoterCount + otherSum / otherVoterCount + 3) / 6) * 100
);
};
export const getVotingRange = () => {
const startDate = new Date();
startDate.setUTCHours(10, 0, 0, 0);
startDate.setDate(1);
// Get the first Friday in the month
while (startDate.getDay() !== 5) {
startDate.setDate(startDate.getDate() + 1);
}
// Get the second Friday
startDate.setDate(startDate.getDate() + 7);
// 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();
return { startDate, endDate, isHappening };
};

View File

@ -1,6 +1,6 @@
import { Prisma } from "@prisma/client";
import { UserError } from "lib/errors";
import { getPercentageFromCounts } from "lib/plus";
import { getPercentageFromCounts, getVotingRange } from "lib/plus";
import { userBasicSelection } from "lib/prisma";
import { suggestionFullSchema } from "lib/validators/suggestion";
import { vouchSchema } from "lib/validators/vouch";
@ -211,7 +211,9 @@ const addSuggestion = async ({
throw new UserError("suggested user already has access");
}
// TODO voting has started
if (getVotingRange().isHappening) {
throw new UserError("voting has already started");
}
return prisma.$transaction([
prisma.plusSuggestion.create({
@ -260,7 +262,9 @@ const addVouch = async ({
throw new UserError("vouched user already has access");
}
// TODO voting has started
if (getVotingRange().isHappening) {
throw new UserError("voting has already started");
}
return prisma.$transaction([
prisma.plusStatus.upsert({