From d38f707d17dc13660b4e5f47e8abf7d4f44f2ed7 Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Sun, 28 Feb 2021 15:06:55 +0200 Subject: [PATCH] can't vouch or suggest if voting has started --- lib/plus.ts | 26 ++++++++++++++++++++++++++ services/plus.ts | 10 +++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/lib/plus.ts b/lib/plus.ts index 326688668..9cbc6ea8c 100644 --- a/lib/plus.ts +++ b/lib/plus.ts @@ -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 }; +}; diff --git a/services/plus.ts b/services/plus.ts index d6a37a5ea..8ff9ba32f 100644 --- a/services/plus.ts +++ b/services/plus.ts @@ -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({