Skip to content

Commit

Permalink
change metohd of entry calculate deadlien in sent email
Browse files Browse the repository at this point in the history
  • Loading branch information
qweliant committed Jan 26, 2024
1 parent c527129 commit 05c61b8
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 70 deletions.
105 changes: 39 additions & 66 deletions integrations/evaluations/app/configure/configure.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const schema: z.ZodType<InstanceConfig> = 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 => {
Expand All @@ -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<InstanceConfig, "deadlineLength" | "deadlineUnit">
): 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(
Expand All @@ -114,11 +90,6 @@ export function Configure(props: Props) {
defaultValues,
});
const onSubmit = async (values: z.infer<typeof schema>) => {
const deadline = calculateDeadline({
deadlineLength: values.deadlineLength,
deadlineUnit: values.deadlineUnit,
});
console.log(deadline);
const result = await configure(props.instanceId, values);
if ("error" in result) {
toast({
Expand Down Expand Up @@ -174,76 +145,78 @@ export function Configure(props: Props) {
/>
<FormField
control={form.control}
name="deadlineLength"
name="evaluatorFieldSlug"
render={({ field }) => (
<FormItem>
<FormLabel>Deadline length</FormLabel>
<FormLabel>Evaluator Field</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormDescription>
This field is used to determine thhe length of the deadline.
The name of the field used to store the evaluator's user id.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="deadlineUnit"
name="titleFieldSlug"
render={({ field }) => (
<FormItem>
<FormLabel>Deadline Format</FormLabel>
<Select
onValueChange={field.onChange}
defaultValue={field.value}
>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select a verified email to display" />
</SelectTrigger>
</FormControl>
<SelectContent>
<SelectItem value="days">days</SelectItem>
<SelectItem value="weeks">weeks</SelectItem>
<SelectItem value="months">month</SelectItem>
</SelectContent>
</Select>
<FormLabel>Title Field</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormDescription>
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.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<div className="text-xl font-medium">
<span>Deadline</span>
</div>
<FormField
control={form.control}
name="evaluatorFieldSlug"
name="deadlineLength"
render={({ field }) => (
<FormItem>
<FormLabel>Evaluator Field</FormLabel>
<FormLabel>Deadline length</FormLabel>
<FormControl>
<Input {...field} />
<Input {...field} />
</FormControl>
<FormDescription>
The name of the field used to store the evaluator's user id.
This field is used to determine thhe length of the deadline.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="titleFieldSlug"
name="deadlineUnit"
render={({ field }) => (
<FormItem>
<FormLabel>Title Field</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormLabel>Deadline Format</FormLabel>
<Select
onValueChange={field.onChange}
defaultValue={field.value}
>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select a verified email to display" />
</SelectTrigger>
</FormControl>
<SelectContent>
<SelectItem value="days">days</SelectItem>
<SelectItem value="months">months</SelectItem>
</SelectContent>
</Select>
<FormDescription>
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.
</FormDescription>
<FormMessage />
</FormItem>
Expand Down
26 changes: 23 additions & 3 deletions integrations/evaluations/lib/emails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<InstanceConfig, "deadlineLength" | "deadlineUnit">,
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 =
'<p><em>This is an automated email sent from Unjournal. Please contact <a href="mailto:[email protected]">[email protected]</a> with any questions.</em></p>';

Expand Down Expand Up @@ -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,
Expand All @@ -216,7 +236,7 @@ export const sendAcceptedEmail = async (
},
extra: {
evaluate_link: `<a href="{{instance.actions.evaluate}}?instanceId={{instance.id}}&pubId={{pubs.submission.id}}&token={{user.token}}">this evaluation form</a>`,
due_at: dueAt.toLocaleDateString(),
due_at: deadline.toLocaleDateString(),
},
});
};
Expand Down
3 changes: 2 additions & 1 deletion integrations/evaluations/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export type EvaluatorWithInvite = z.infer<typeof EvaluatorWithInvite>;
export const EvaluatorWhoAccepted = EvaluatorWithInvite.merge(
z.object({
acceptedAt: z.string(),
deadline: z.date(),
})
);
export type EvaluatorWhoAccepted = z.infer<typeof EvaluatorWhoAccepted>;
Expand Down Expand Up @@ -96,7 +97,7 @@ export type InstanceConfig = {
titleFieldSlug: string;
emailTemplate: EmailTemplate;
deadlineLength: number;
deadlineUnit: "weeks" | "days" | "months";
deadlineUnit: "days" | "months";
};

export type InstanceState = {
Expand Down

0 comments on commit 05c61b8

Please sign in to comment.