Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

2.5 KB
Raw Blame
"use server";
import { AuthError } from "next-auth";
import { signIn } from "../auth";
import { delay } from "@/utils/delay";
import prisma from "@/lib/db";
import bcrypt from "bcryptjs";
import { SignupFormSchema } from "@/models/signupForm.schema";
import {
  CONTACT_ROLES,
  JOB_SOURCES,
  JOB_STAGES,
  JOB_STATUSES,
} from "@/lib/constants";

export async function signup(formData: {
  name: string;
  email: string;
  password: string;
}) {
  const parsed = SignupFormSchema.safeParse(formData);
  if (!parsed.success) {
    return { error: "Invalid form data." };
  }

  const { name, email, password } = parsed.data;

  const existingUser = await prisma.user.findUnique({
    where: { email },
  });

  if (existingUser) {
    return { error: "An account with this email already exists." };
  }

  const hashedPassword = await bcrypt.hash(password, 10);

  const newUser = await prisma.user.create({
    data: { name, email, password: hashedPassword },
  });

  await prisma.jobSource.createMany({
    data: JOB_SOURCES.map((source) => ({
      label: source.label,
      value: source.value,
      createdBy: newUser.id,
    })),
  });

  await prisma.contactRole.createMany({
    data: CONTACT_ROLES.map((role) => ({
      label: role.label,
      value: role.value,
      createdBy: newUser.id,
    })),
  });

  for (const status of JOB_STATUSES) {
    await prisma.jobStatus.upsert({
      where: { value: status.value },
      update: {},
      create: status,
    });
  }

  // Stage types point at JobStatus, so this must follow the upsert loop.
  const statusRows = await prisma.jobStatus.findMany({
    select: { id: true, value: true },
  });
  const statusIdByValue = new Map(statusRows.map((s) => [s.value, s.id]));
  await prisma.jobStageType.createMany({
    data: JOB_STAGES.map((stage) => ({
      label: stage.label,
      value: stage.value,
      statusId: statusIdByValue.get(stage.status)!,
      sortOrder: stage.sortOrder,
      createdBy: newUser.id,
    })),
  });

  return { success: true };
}

export async function authenticate(
  prevState: string | undefined,
  formData: FormData
) {
  try {
    await delay(1000);
    await signIn("credentials", {
      email: formData.get("email") as string,
      password: formData.get("password") as string,
      redirect: false,
    });
    return null;
  } catch (error) {
    if (error instanceof AuthError) {
      switch (error.type) {
        case "CredentialsSignin":
          return "Invalid credentials.";
        default:
          return "Something went wrong.";
      }
    }
    throw error;
  }
}