diff --git a/app/core/components/ApplicantComments/index.tsx b/app/core/components/ApplicantComments/index.tsx index 3ad0a1c..e60c34e 100644 --- a/app/core/components/ApplicantComments/index.tsx +++ b/app/core/components/ApplicantComments/index.tsx @@ -82,7 +82,7 @@ function CommentItem({ profileId, }: { comment: CommentItemType; - applicantId: string; + applicantId: number; profileId: string; }) { const [openDeleteModal, setOpenDeleteModal] = useState(false); diff --git a/app/models/applicant.server.ts b/app/models/applicant.server.ts index 3cd729d..f22f246 100644 --- a/app/models/applicant.server.ts +++ b/app/models/applicant.server.ts @@ -137,6 +137,14 @@ export async function searchApplicants() { .execute(); } +export async function getApplicantIdByEmail(email: string) { + return await db + .selectFrom("Applicant as a") + .select("id") + .where("a.email", "=", email) + .executeTakeFirstOrThrow(); +} + export async function getApplicantByEmail(email: string) { const applicant = await db .selectFrom("Applicant as a") @@ -158,11 +166,7 @@ export async function getApplicantByEmail(email: string) { "m.lastName as mentorLastName", ]) .where("a.email", "=", email) - .executeTakeFirst(); - - if (!applicant) { - return null; - } + .executeTakeFirstOrThrow(); const projectMembers = await db .selectFrom("ProjectMembers") diff --git a/app/routes/applicants.$applicantId._index.tsx b/app/routes/applicants.$applicantId._index.tsx index 55eed8b..b1badce 100644 --- a/app/routes/applicants.$applicantId._index.tsx +++ b/app/routes/applicants.$applicantId._index.tsx @@ -38,7 +38,7 @@ import ModalBox from "~/core/components/ModalBox"; import RegularSelect from "~/core/components/RegularSelect"; import WhatsAppLink from "~/core/components/WhatsAppLink"; import Header from "~/core/layouts/Header"; -import { getApplicantById } from "~/models/applicant.server"; +import { getApplicantByEmail } from "~/models/applicant.server"; import { getCommentsApplicant } from "~/models/applicantComment.server"; import { checkPermission } from "~/models/authorization.server"; import type { Roles } from "~/models/authorization.server"; @@ -88,12 +88,11 @@ export const validator = withZod( ); export const loader = async ({ params, request }: LoaderFunctionArgs) => { - invariant(params.applicantId, "projectId not found"); + invariant(params.applicantId, "applicant not found"); const projects = await getProjectsList(); - const applicantId = params.applicantId; - const comments = await getCommentsApplicant(parseInt(applicantId as string)); - const applicant = await getApplicantById(params.applicantId); + const applicant = await getApplicantByEmail(params.applicantId); + const comments = await getCommentsApplicant(applicant.id); if (!applicant) { throw new Response("Not Found", { status: 404 }); } @@ -114,7 +113,7 @@ export const loader = async ({ params, request }: LoaderFunctionArgs) => { applicant, projects, canEditProject, - applicantId, + applicantId: applicant.id, profileId, comments, }); @@ -191,11 +190,11 @@ export default function Applicant() { }; const editApplicant = () => { - submit(null, { + submit(null, { method: "get", - action: `/applicationForm/${applicantId}`, + action: `/applicationForm/${applicant.email}`, }); - } + }; return ( <> diff --git a/app/routes/applicants.$applicantId.comment.$id.tsx b/app/routes/applicants.$applicantId.comment.$id.tsx index 9b3d456..de4c3f4 100644 --- a/app/routes/applicants.$applicantId.comment.$id.tsx +++ b/app/routes/applicants.$applicantId.comment.$id.tsx @@ -6,9 +6,8 @@ import { deleteComment, updateComment } from "~/models/applicantComment.server"; import { getSession } from "~/session.server"; export const action: ActionFunction = async ({ request, params }) => { - invariant(params.applicantId, "projectId could not be found"); + invariant(params.applicantId, "applicant could not be found"); invariant(params.id, "comment id could not be found"); - const applicantId = params.applicantId; const id = params.id; try { @@ -27,5 +26,5 @@ export const action: ActionFunction = async ({ request, params }) => { const session = await getSession(request); session.flash("warning", "Error while updating comment"); } - return redirect(`/applicants/${applicantId}`); + return redirect(`/applicants/${params.applicantId}`); }; diff --git a/app/routes/applicants.$applicantId.comment.create.tsx b/app/routes/applicants.$applicantId.comment.create.tsx index a09889c..bd8bca5 100644 --- a/app/routes/applicants.$applicantId.comment.create.tsx +++ b/app/routes/applicants.$applicantId.comment.create.tsx @@ -3,20 +3,21 @@ import { redirect } from "@remix-run/node"; import { validationError } from "remix-validated-form"; import invariant from "tiny-invariant"; import { validator } from "~/core/components/Comments"; +import { getApplicantIdByEmail } from "~/models/applicant.server"; import { createComment } from "~/models/applicantComment.server"; import { getSession, requireProfile } from "~/session.server"; export const action: ActionFunction = async ({ request, params }) => { invariant(params.applicantId, "applicantId could not be found"); - const applicantId = params.applicantId; const profile = await requireProfile(request); const result = await validator.validate(await request.formData()); + const applicant = await getApplicantIdByEmail(params.applicantId); if (result.error != undefined) return validationError(result.error); try { await createComment( - parseInt(applicantId as string), + applicant.id, profile.id, result.data.body, result.data.parentId @@ -25,5 +26,5 @@ export const action: ActionFunction = async ({ request, params }) => { const session = await getSession(request); session.flash("warning", "Error while saving comment"); } - return redirect(`/applicants/${applicantId}`); + return redirect(`/applicants/${params.applicantId}`); }; diff --git a/app/routes/applicants.$applicantId.status.tsx b/app/routes/applicants.$applicantId.status.tsx index f33cd51..cc08b20 100644 --- a/app/routes/applicants.$applicantId.status.tsx +++ b/app/routes/applicants.$applicantId.status.tsx @@ -1,18 +1,18 @@ /* eslint-disable no-console */ import type { ActionFunction } from "@remix-run/server-runtime"; import { redirect } from "remix-typedjson"; -import { editApplicant, getApplicantById } from "~/models/applicant.server"; -import { validator } from "~/routes/applicants.$applicantId._index"; -import { requireProfile, requireUser } from "~/session.server"; import invariant from "tiny-invariant"; +import { editApplicant, getApplicantByEmail } from "~/models/applicant.server"; import { checkPermission } from "~/models/authorization.server"; import type { Roles } from "~/models/authorization.server"; +import { validator } from "~/routes/applicants.$applicantId._index"; +import { requireProfile, requireUser } from "~/session.server"; export const action: ActionFunction = async ({ params, request }) => { const profile = await requireProfile(request); const user = await requireUser(request); invariant(params.applicantId, "applicantId could not be found"); - const applicant = await getApplicantById(params.applicantId); + const applicant = await getApplicantByEmail(params.applicantId); if (!applicant) { throw new Response("Not Found", { status: 404 }); } @@ -27,8 +27,6 @@ export const action: ActionFunction = async ({ params, request }) => { throw new Response("Unauthorized", { status: 401 }); } - - const result = await validator.validate(await request.formData()); const applicantId = parseInt(result.data?.applicantId as string); const status = result.data?.status; @@ -41,5 +39,7 @@ export const action: ActionFunction = async ({ params, request }) => { }, applicantId ); - return redirect(`/applicants/${response.id}?status=${response.status}`); + return redirect( + `/applicants/${params.applicantId}?status=${response.status}` + ); }; diff --git a/app/routes/applicationForm.$applicantId._index.tsx b/app/routes/applicationForm.$applicantId._index.tsx index fbbe642..f3cfb6e 100644 --- a/app/routes/applicationForm.$applicantId._index.tsx +++ b/app/routes/applicationForm.$applicantId._index.tsx @@ -130,17 +130,6 @@ function getCurrentDate(): string { return `${year}-${month}-${day}`; } -interface UserProfile { - email: string; - name: string; -} - -interface LoaderData { - universities: Awaited>; - profile: UserProfile; - applicantByEmail: Awaited>; -} - interface UniversityValue { id: string; name: string; @@ -156,7 +145,7 @@ export const loader: LoaderFunction = async ({ request }) => { const profile = await requireProfile(request); const applicantByEmail = await getApplicantByEmail(profile.email); - return json({ + return json({ universities, profile, applicantByEmail, @@ -165,7 +154,7 @@ export const loader: LoaderFunction = async ({ request }) => { export default function FormPage() { const { universities, profile, applicantByEmail } = - useLoaderData() as LoaderData; + useLoaderData(); const [selectedUniversity, setSelectedUniversity] = useState({ diff --git a/app/routes/auth.auth0.callback.tsx b/app/routes/auth.auth0.callback.tsx index 69dc6f5..b00eac9 100644 --- a/app/routes/auth.auth0.callback.tsx +++ b/app/routes/auth.auth0.callback.tsx @@ -4,7 +4,6 @@ import { existApplicant } from "~/models/applicant.server"; import { createUserSession, getUserRole, - requireProfile, returnToCookie, } from "~/session.server"; @@ -23,14 +22,12 @@ export const loader: LoaderFunction = async ({ request, params }) => { if (userRole === "ADMIN" || userRole === "USER") { roleRedirect = "/projects"; } else if (userRole === "APPLICANT") { - const profile = await requireProfile(request); - const checkExistApplicant = await existApplicant(profile.email); - + const checkExistApplicant = await existApplicant(user.email); //Check if the user has already answered the "aplicationform". if (checkExistApplicant) { roleRedirect = "/internshipProjects"; } else { - roleRedirect = `/applicationForm/${profile.id}`; + roleRedirect = `/applicationForm/${user.email}`; } } else { roleRedirect = `/login/${params.connection}`;