From 05c61b8e3999f7d256d2029c4042606b56bbed2c Mon Sep 17 00:00:00 2001 From: qweliant Date: Fri, 26 Jan 2024 13:13:43 -0500 Subject: [PATCH] change metohd of entry calculate deadlien in sent email --- .../evaluations/app/configure/configure.tsx | 105 +++++++----------- integrations/evaluations/lib/emails.ts | 26 ++++- integrations/evaluations/lib/types.ts | 3 +- 3 files changed, 64 insertions(+), 70 deletions(-) diff --git a/integrations/evaluations/app/configure/configure.tsx b/integrations/evaluations/app/configure/configure.tsx index 22ed8a1bb..9f0cfed27 100644 --- a/integrations/evaluations/app/configure/configure.tsx +++ b/integrations/evaluations/app/configure/configure.tsx @@ -57,7 +57,7 @@ const schema: z.ZodType = z.object({ message: z.string(), }), deadlineLength: z.number().int().min(1), - deadlineUnit: z.enum(["weeks", "days", "months"]), + deadlineUnit: z.enum(["days", "months"]), }); const isActionRedirect = (props: Props): props is RedirectProps => { @@ -69,39 +69,15 @@ const defaultEmailTemplate = { message: "Please reach out if you have any questions.", }; -const defaultFormValues = { +const defaultFormValues: InstanceConfig = { pubTypeId: "", evaluatorFieldSlug: "", titleFieldSlug: "", - template: defaultEmailTemplate, - deadlineLength: 3, - deadlineUnit: "weeks" as const, + emailTemplate: defaultEmailTemplate, + deadlineLength: 21, + deadlineUnit: "days", }; -function calculateDeadline( - deadline: Pick -): number { - let n: number; - switch (deadline.deadlineUnit) { - case "days": - // set deadline offset to n days - n = deadline.deadlineLength * 24 * 60 * 60 * 1000; - break; - case "weeks": - // set deadline offset to n weeks. eg. whatever this is 3 * 7 * 24 * 60 * 60 * 1000 - n = deadline.deadlineLength * 7 * 24 * 60 * 60 * 1000; - break; - case "months": - // set deadline offset to n months - n = deadline.deadlineLength; - break; - default: - throw new Error('Invalid time unit. Use "days", "weeks", or "months".'); - } - - return n; -} - export function Configure(props: Props) { const { toast } = useToast(); const defaultValues = useMemo( @@ -114,11 +90,6 @@ export function Configure(props: Props) { defaultValues, }); const onSubmit = async (values: z.infer) => { - const deadline = calculateDeadline({ - deadlineLength: values.deadlineLength, - deadlineUnit: values.deadlineUnit, - }); - console.log(deadline); const result = await configure(props.instanceId, values); if ("error" in result) { toast({ @@ -174,15 +145,15 @@ export function Configure(props: Props) { /> ( - Deadline length + Evaluator Field - This field is used to determine thhe length of the deadline. + The name of the field used to store the evaluator's user id. @@ -190,44 +161,34 @@ export function Configure(props: Props) { /> ( - Deadline Format - + Title Field + + + - This field allows you to select whether the deadline is in - days, weeks, or months. + The name of the field used to store the evaluation title. )} /> +
+ Deadline +
( - Evaluator Field + Deadline length - + - The name of the field used to store the evaluator's user id. + This field is used to determine thhe length of the deadline. @@ -235,15 +196,27 @@ export function Configure(props: Props) { /> ( - Title Field - - - + Deadline Format + - The name of the field used to store the evaluation title. + This field allows you to select whether the deadline is in + days or months. diff --git a/integrations/evaluations/lib/emails.ts b/integrations/evaluations/lib/emails.ts index d5bd7b78d..6d0081c59 100644 --- a/integrations/evaluations/lib/emails.ts +++ b/integrations/evaluations/lib/emails.ts @@ -10,6 +10,20 @@ const DAYS_TO_ACCEPT_INVITE = 10; const DAYS_TO_REMIND_EVALUATOR = 5; const DAYS_TO_SUBMIT_EVALUATION = 21; +export function calculateDeadline( + deadline: Pick, + date: Date +): Date { + switch (deadline.deadlineUnit) { + case "days": + return new Date(date.setMinutes(date.getMinutes() + deadline.deadlineLength * 24 * 60)); + case "months": + return new Date(date.setMonth(date.getMonth() + deadline.deadlineLength)); + default: + throw new Error('Invalid time unit. Use "days", "weeks", or "months".'); + } +} + const notificationFooter = '

This is an automated email sent from Unjournal. Please contact contact@unjournal.org with any questions.

'; @@ -191,8 +205,14 @@ export const sendAcceptedEmail = async ( evaluator: EvaluatorWhoAccepted ) => { const dueAt = new Date(evaluator.acceptedAt); - dueAt.setMinutes(dueAt.getMinutes() + DAYS_TO_SUBMIT_EVALUATION * 24 * 60); - + // dueAt.setMinutes(dueAt.getMinutes() + DAYS_TO_SUBMIT_EVALUATION * 24 * 60); + const deadline = calculateDeadline( + { + deadlineLength: instanceConfig.deadlineLength, + deadlineUnit: instanceConfig.deadlineUnit, + }, + dueAt + ); await client.sendEmail(instanceId, { to: { userId: evaluator.userId, @@ -216,7 +236,7 @@ export const sendAcceptedEmail = async ( }, extra: { evaluate_link: `this evaluation form`, - due_at: dueAt.toLocaleDateString(), + due_at: deadline.toLocaleDateString(), }, }); }; diff --git a/integrations/evaluations/lib/types.ts b/integrations/evaluations/lib/types.ts index aef6a8c43..79827e7c1 100644 --- a/integrations/evaluations/lib/types.ts +++ b/integrations/evaluations/lib/types.ts @@ -60,6 +60,7 @@ export type EvaluatorWithInvite = z.infer; export const EvaluatorWhoAccepted = EvaluatorWithInvite.merge( z.object({ acceptedAt: z.string(), + deadline: z.date(), }) ); export type EvaluatorWhoAccepted = z.infer; @@ -96,7 +97,7 @@ export type InstanceConfig = { titleFieldSlug: string; emailTemplate: EmailTemplate; deadlineLength: number; - deadlineUnit: "weeks" | "days" | "months"; + deadlineUnit: "days" | "months"; }; export type InstanceState = {