style, backend
This commit is contained in:
@ -2,18 +2,20 @@
|
||||
|
||||
import { z } from "zod";
|
||||
import { oldestDate, signupFormSchema, youngestDate } from "@/app/sign-up/sign-up-form";
|
||||
import listmonk from "./listmonk";
|
||||
import listmonk, { listmonkData } from "./listmonk";
|
||||
|
||||
export async function signupFormSubmit(data: z.infer<typeof signupFormSchema>) {
|
||||
if (data.dob > youngestDate || data.dob < oldestDate) {
|
||||
return { error: "Invalid date of birth" };
|
||||
}
|
||||
const listmonkData = {
|
||||
const listmonkData: listmonkData = {
|
||||
email: data.email,
|
||||
name: data.name,
|
||||
status: "enabled",
|
||||
lists: [6],
|
||||
attribs: {
|
||||
dob: data.dob.toISOString(),
|
||||
},
|
||||
};
|
||||
console.log(listmonkData);
|
||||
listmonk(listmonkData);
|
||||
}
|
||||
|
@ -1,5 +1,13 @@
|
||||
import "server-only";
|
||||
|
||||
export type listmonkData = {
|
||||
email: string;
|
||||
name: string;
|
||||
status: "enabled" | "blocklisted";
|
||||
lists: number[];
|
||||
attribs: {};
|
||||
};
|
||||
|
||||
async function makeApiCall(url: string, options?: RequestInit) {
|
||||
try {
|
||||
const response = await fetch(url, options);
|
||||
@ -13,9 +21,31 @@ async function makeApiCall(url: string, options?: RequestInit) {
|
||||
}
|
||||
}
|
||||
|
||||
async function listmonk() {
|
||||
const listmonkUrl = process.env.LISTMONK_URL ?? "";
|
||||
makeApiCall(listmonkUrl);
|
||||
async function listmonk(data: listmonkData) {
|
||||
const listmonkUrl = process.env.LISTMONK_URL ?? "http://localhost:9000/api/";
|
||||
const listmonkUser = process.env.LISTMONK_USER ?? "nouser";
|
||||
const listmonkPass = process.env.LISTMONK_PASS ?? "nopass";
|
||||
// Encode the username and password in base64
|
||||
const credentials = Buffer.from(`${listmonkUser}:${listmonkPass}`).toString("base64");
|
||||
|
||||
const options: RequestInit = {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Basic ${credentials}`,
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
};
|
||||
try {
|
||||
const response = await fetch(`${listmonkUrl}subscribers`, options);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
}
|
||||
const responseData = await response.json();
|
||||
console.log("Subscriber created successfully:", responseData);
|
||||
} catch (error) {
|
||||
console.error("Failed to create subscriber:", error);
|
||||
}
|
||||
}
|
||||
|
||||
export default listmonk;
|
||||
|
Reference in New Issue
Block a user