prod
This commit is contained in:
+40
-19
@@ -11,7 +11,7 @@ import { createApplication, getActiveCitizenship, getApplicationEligibility } fr
|
||||
import { prisma } from "../prisma";
|
||||
import { formatAddress } from "@/lib/address";
|
||||
import { Prisma } from "@/lib/generated/prisma/client";
|
||||
import { uploadImage } from "../image";
|
||||
import { removeImage, uploadImage } from "../image";
|
||||
|
||||
function generateDocumentNumber(): string {
|
||||
const letter = () => String.fromCharCode(65 + Math.floor(Math.random() * 26));
|
||||
@@ -70,23 +70,37 @@ export const submitCitizenshipApplication = formAction(citizenshipApplicationSch
|
||||
customAddress: data.customAddress,
|
||||
});
|
||||
|
||||
const profileKey = await uploadImage(profile, "citizenship/profiles");
|
||||
const signatureKey = await uploadImage(signature, "citizenship/signatures");
|
||||
let profileKey: string | undefined;
|
||||
let signatureKey: string | undefined;
|
||||
|
||||
await createApplication(user.id, {
|
||||
surname: user.secondName,
|
||||
givenNames: user.firstName,
|
||||
sex: user.sex,
|
||||
birthDate: user.birthDate!,
|
||||
...applicationData,
|
||||
address,
|
||||
regionId: isEuResidentYes ? data.regionId! : null,
|
||||
city: isEuResidentYes ? data.city! : null,
|
||||
street: isEuResidentYes ? data.street! : null,
|
||||
customAddress: isEuResidentYes ? null : data.customAddress!,
|
||||
profileKey,
|
||||
signatureKey
|
||||
});
|
||||
try {
|
||||
const profileKey = await uploadImage(profile, "citizenship/profiles", {
|
||||
maxWidth: 800, maxHeight: 1000, fit: "cover",
|
||||
});
|
||||
|
||||
const signatureKey = await uploadImage(signature, "citizenship/signatures", {
|
||||
maxWidth: 1000, maxHeight: 400, fit: "inside",
|
||||
});
|
||||
|
||||
await createApplication(user.id, {
|
||||
surname: user.secondName,
|
||||
givenNames: user.firstName,
|
||||
sex: user.sex,
|
||||
birthDate: user.birthDate!,
|
||||
...applicationData,
|
||||
address,
|
||||
regionId: isEuResidentYes ? data.regionId! : null,
|
||||
city: isEuResidentYes ? data.city! : null,
|
||||
street: isEuResidentYes ? data.street! : null,
|
||||
customAddress: isEuResidentYes ? null : data.customAddress!,
|
||||
profileKey,
|
||||
signatureKey
|
||||
});
|
||||
} catch (e) {
|
||||
if (profileKey) await removeImage(profileKey);
|
||||
if (signatureKey) await removeImage(signatureKey);
|
||||
throw e;
|
||||
}
|
||||
|
||||
revalidatePath("/account/citizenship");
|
||||
redirect("/account/citizenship");
|
||||
@@ -102,8 +116,10 @@ export async function approveApplication(id: string): Promise<ActionResult> {
|
||||
if (application.status !== "PENDING") throw new AppError("This application is no longer pending review.");
|
||||
if (application.userId === user.id) throw new AppError("You cannot approve your own application.");
|
||||
|
||||
const existingCitizenship = await prisma.citizenship.findUnique({ where: { userId: application.userId } });
|
||||
if (existingCitizenship && !existingCitizenship.revokedAt) {
|
||||
const existingCitizenship = await prisma.citizenship.findFirst({
|
||||
where: { userId: application.userId, revokedAt: null },
|
||||
});
|
||||
if (existingCitizenship) {
|
||||
throw new AppError("This applicant is already a citizen.");
|
||||
}
|
||||
|
||||
@@ -154,6 +170,11 @@ export async function rejectApplication(id: string, reason: string): Promise<Act
|
||||
data: { status: "REJECTED", rejectionReason: trimmedReason, reviewedAt: new Date(), reviewedBy: user.id },
|
||||
});
|
||||
|
||||
await Promise.allSettled([
|
||||
removeImage(application.profileKey),
|
||||
removeImage(application.signatureKey),
|
||||
]);
|
||||
|
||||
revalidatePath("/account/citizenship/review");
|
||||
});
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ export async function getApplicationEligibility(
|
||||
};
|
||||
}
|
||||
|
||||
const citizenship = await prisma.citizenship.findUnique({ where: { userId } });
|
||||
const citizenship = await prisma.citizenship.findFirst({ where: { userId, revokedAt: null } });
|
||||
if (citizenship && !citizenship.revokedAt) {
|
||||
return {
|
||||
eligible: false,
|
||||
@@ -100,7 +100,7 @@ export async function getPendingApplicationsCount(): Promise<number> {
|
||||
}
|
||||
|
||||
export async function getActiveCitizenship(userId: string) {
|
||||
const citizenship = await prisma.citizenship.findUnique({ where: { userId } });
|
||||
if (!citizenship || citizenship.revokedAt) return null;
|
||||
return citizenship;
|
||||
}
|
||||
return prisma.citizenship.findFirst({
|
||||
where: { userId, revokedAt: null },
|
||||
});
|
||||
}
|
||||
@@ -36,8 +36,6 @@ export type CitizenshipApplication = {
|
||||
updatedAt: Date;
|
||||
profileKey: string;
|
||||
signatureKey: string;
|
||||
profile: File;
|
||||
signature: File;
|
||||
};
|
||||
|
||||
export type CitizenshipApplicationWithWarning = CitizenshipApplication & {
|
||||
|
||||
Reference in New Issue
Block a user