feat: citizenship built
This commit is contained in:
@@ -5,12 +5,19 @@ 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
|
||||
@@ -19,19 +26,22 @@ export async function submitCitizenshipApplication(
|
||||
if (!user) redirect("/");
|
||||
|
||||
const applicationType = formData.get("applicationType") as string;
|
||||
const isEuResident = formData.get("isEuResident") as string;
|
||||
|
||||
const raw: Record<string, string> = {
|
||||
applicationType,
|
||||
address: (formData.get("address") as string) ?? "",
|
||||
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 === "BORN_IN_TERRITORY") {
|
||||
raw.birthRegion = (formData.get("birthRegion") as string) ?? "";
|
||||
} else if (applicationType === "NATURALIZATION") {
|
||||
raw.naturalizationReason = (formData.get("naturalizationReason") as string) ?? "";
|
||||
}
|
||||
@@ -43,14 +53,82 @@ export async function submitCitizenshipApplication(
|
||||
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(),
|
||||
...parsed.data,
|
||||
...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");
|
||||
}
|
||||
Reference in New Issue
Block a user