106 lines
2.6 KiB
TypeScript
106 lines
2.6 KiB
TypeScript
import { prisma } from "@/lib/prisma";
|
|
import type { ApplicationType, EyeColor } from "./types";
|
|
|
|
type CreateData = {
|
|
surname: string;
|
|
givenNames: string;
|
|
sex: "MALE" | "FEMALE";
|
|
birthDate: Date;
|
|
applicationType: ApplicationType;
|
|
previousCountry?: string | null;
|
|
previousRegion?: string | null;
|
|
naturalizationReason?: string | null;
|
|
address: string;
|
|
regionId?: string | null;
|
|
city?: string | null;
|
|
street?: string | null;
|
|
customAddress?: string | null;
|
|
height: number;
|
|
eyeColor: EyeColor;
|
|
profileKey: string;
|
|
signatureKey: string;
|
|
};
|
|
|
|
export type ApplicationEligibility = {
|
|
eligible: boolean;
|
|
reason: string | null;
|
|
};
|
|
|
|
export async function getApplicationEligibility(
|
|
userId: string,
|
|
birthDate: Date | null | undefined
|
|
): Promise<ApplicationEligibility> {
|
|
if (!birthDate) {
|
|
return {
|
|
eligible: false,
|
|
reason:
|
|
"Date of birth is required. Update your profile in Kotosoft ID and synchronise your account.",
|
|
};
|
|
}
|
|
|
|
const citizenship = await prisma.citizenship.findFirst({ where: { userId, revokedAt: null } });
|
|
if (citizenship && !citizenship.revokedAt) {
|
|
return {
|
|
eligible: false,
|
|
reason: "You are already a citizen of the European Federation.",
|
|
};
|
|
}
|
|
|
|
const applications = await prisma.citizenshipApplication.findMany({
|
|
where: { userId },
|
|
select: { status: true },
|
|
});
|
|
|
|
if (applications.some((application) => application.status === "PENDING")) {
|
|
return {
|
|
eligible: false,
|
|
reason: "You already have a pending application.",
|
|
};
|
|
}
|
|
|
|
return { eligible: true, reason: null };
|
|
}
|
|
|
|
export async function createApplication(userId: string, data: CreateData) {
|
|
return prisma.citizenshipApplication.create({
|
|
data: {
|
|
userId,
|
|
...data,
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function getUserApplications(userId: string) {
|
|
return prisma.citizenshipApplication.findMany({
|
|
where: { userId },
|
|
orderBy: { createdAt: "asc" },
|
|
});
|
|
}
|
|
|
|
export async function getPendingApplications() {
|
|
return prisma.citizenshipApplication.findMany({
|
|
where: { status: "PENDING" },
|
|
orderBy: { createdAt: "desc" },
|
|
include: {
|
|
user: {
|
|
include: {
|
|
_count: {
|
|
select: {
|
|
citizenships: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
});
|
|
}
|
|
|
|
export async function getPendingApplicationsCount(): Promise<number> {
|
|
return prisma.citizenshipApplication.count({ where: { status: "PENDING" } });
|
|
}
|
|
|
|
export async function getActiveCitizenship(userId: string) {
|
|
return prisma.citizenship.findFirst({
|
|
where: { userId, revokedAt: null },
|
|
});
|
|
} |