134 lines
4.0 KiB
TypeScript
134 lines
4.0 KiB
TypeScript
"use server";
|
|
|
|
import { getCurrentUser } from "@/lib/user";
|
|
import { redirect } from "next/navigation";
|
|
import { revalidatePath } from "next/cache";
|
|
import { citizenshipApplicationSchema } from "./validation";
|
|
import { createApplication } from "./repository";
|
|
import { prisma } from "../prisma";
|
|
|
|
export type ApplicationState = {
|
|
success: boolean;
|
|
errors: string[];
|
|
};
|
|
|
|
function generateDocumentNumber(): string {
|
|
const letter = () => String.fromCharCode(65 + Math.floor(Math.random() * 26));
|
|
const digits = Math.floor(Math.random() * 1_000_000).toString().padStart(6, "0");
|
|
return `${letter()}${digits}${letter()}`;
|
|
}
|
|
|
|
export async function submitCitizenshipApplication(
|
|
_prevState: ApplicationState,
|
|
formData: FormData
|
|
): Promise<ApplicationState> {
|
|
const user = await getCurrentUser();
|
|
if (!user) redirect("/");
|
|
|
|
const applicationType = formData.get("applicationType") as string;
|
|
const isEuResident = formData.get("isEuResident") as string;
|
|
|
|
const raw: Record<string, string> = {
|
|
applicationType,
|
|
height: (formData.get("height") as string) ?? "",
|
|
eyeColor: (formData.get("eyeColor") as string) ?? "",
|
|
isEuResident,
|
|
regionId: (formData.get("regionId") as string) ?? "",
|
|
city: (formData.get("city") as string) ?? "",
|
|
street: (formData.get("street") as string) ?? "",
|
|
customAddress: (formData.get("customAddress") as string) ?? "",
|
|
};
|
|
|
|
if (applicationType === "PREVIOUS_CITIZENSHIP") {
|
|
raw.previousCountry = (formData.get("previousCountry") as string) ?? "";
|
|
raw.previousRegion = (formData.get("previousRegion") as string) ?? "";
|
|
} else if (applicationType === "NATURALIZATION") {
|
|
raw.naturalizationReason = (formData.get("naturalizationReason") as string) ?? "";
|
|
}
|
|
|
|
const parsed = citizenshipApplicationSchema.safeParse(raw);
|
|
if (!parsed.success) {
|
|
const fieldErrors = parsed.error.flatten().fieldErrors ?? {};
|
|
const messages = Object.values(fieldErrors).flat().filter(Boolean);
|
|
return { success: false, errors: messages };
|
|
}
|
|
|
|
const { isEuResident: _, regionId, city, street, customAddress, ...applicationData } = parsed.data;
|
|
|
|
const address = _ === "yes"
|
|
? `${street}, ${city}, ${regionId}, EU`
|
|
: customAddress!;
|
|
|
|
await createApplication(user.id, {
|
|
surname: user.secondName,
|
|
givenNames: user.firstName,
|
|
sex: user.sex,
|
|
birthDate: user.birthDate ?? new Date(),
|
|
...applicationData,
|
|
address,
|
|
regionId: _ === "yes" ? regionId : null,
|
|
city: _ === "yes" ? city : null,
|
|
street: _ === "yes" ? street : null,
|
|
customAddress: _ === "yes" ? null : customAddress,
|
|
});
|
|
|
|
revalidatePath("/account/citizenship");
|
|
return { success: true, errors: [] };
|
|
}
|
|
|
|
export async function approveApplication(id: string) {
|
|
const user = await getCurrentUser();
|
|
if (!user) redirect("/");
|
|
|
|
const application = await prisma.citizenshipApplication.update({
|
|
where: { id },
|
|
data: {
|
|
status: "APPROVED",
|
|
reviewedAt: new Date(),
|
|
reviewedBy: user.id,
|
|
},
|
|
});
|
|
|
|
const citizenship = await prisma.citizenship.create({
|
|
data: {
|
|
userId: application.userId,
|
|
applicationId: application.id,
|
|
},
|
|
});
|
|
|
|
const now = new Date();
|
|
const expiresAt = new Date(now);
|
|
expiresAt.setFullYear(expiresAt.getFullYear() + 10);
|
|
|
|
await prisma.identityDocument.create({
|
|
data: {
|
|
citizenshipId: citizenship.id,
|
|
documentNumber: generateDocumentNumber(),
|
|
regionId: application.regionId,
|
|
city: application.city,
|
|
street: application.street,
|
|
customAddress: application.customAddress,
|
|
issuedAt: now,
|
|
expiresAt,
|
|
},
|
|
});
|
|
|
|
revalidatePath("/account/citizenship/review");
|
|
}
|
|
|
|
export async function rejectApplication(id: string, reason: string) {
|
|
const user = await getCurrentUser();
|
|
if (!user) redirect("/");
|
|
|
|
await prisma.citizenshipApplication.update({
|
|
where: { id },
|
|
data: {
|
|
status: "REJECTED",
|
|
rejectionReason: reason,
|
|
reviewedAt: new Date(),
|
|
reviewedBy: user.id,
|
|
},
|
|
});
|
|
|
|
revalidatePath("/account/citizenship/review");
|
|
} |