bam/lib/actions.ts

25 lines
730 B
TypeScript
Raw Permalink Normal View History

2024-10-18 13:12:19 +02:00
"use server";
import { z } from "zod";
import { oldestDate, signupFormSchema, youngestDate } from "@/app/sign-up/sign-up-form";
2024-10-18 13:36:15 +02:00
import listmonk, { listmonkData } from "./listmonk";
2024-10-18 13:12:19 +02:00
2024-10-18 14:20:58 +02:00
export async function signupFormSubmit(data: z.infer<typeof signupFormSchema>): Promise<string> {
2024-10-18 13:12:19 +02:00
if (data.dob > youngestDate || data.dob < oldestDate) {
2024-10-18 14:20:58 +02:00
return "Invalid date of birth";
2024-10-18 13:12:19 +02:00
}
2024-10-18 14:20:58 +02:00
const offset = data.dob.getTimezoneOffset();
data.dob = new Date(data.dob.getTime() - offset * 60 * 1000);
2024-10-18 13:36:15 +02:00
const listmonkData: listmonkData = {
2024-10-18 13:12:19 +02:00
email: data.email,
name: data.name,
2024-10-18 13:36:15 +02:00
status: "enabled",
lists: [6],
2024-10-18 13:12:19 +02:00
attribs: {
2024-10-18 14:20:58 +02:00
dob: data.dob.toISOString().split("T")[0],
2024-10-18 13:12:19 +02:00
},
};
2024-10-18 14:20:58 +02:00
return await listmonk(listmonkData);
2024-10-18 13:12:19 +02:00
}