Skip to content

Commit

Permalink
use email instead of applicant id on applicant pages
Browse files Browse the repository at this point in the history
  • Loading branch information
jackbravo committed Jun 22, 2024
1 parent 34cf8f7 commit 628aeee
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 46 deletions.
2 changes: 1 addition & 1 deletion app/core/components/ApplicantComments/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function CommentItem({
profileId,
}: {
comment: CommentItemType;
applicantId: string;
applicantId: number;
profileId: string;
}) {
const [openDeleteModal, setOpenDeleteModal] = useState<boolean>(false);
Expand Down
14 changes: 9 additions & 5 deletions app/models/applicant.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down
17 changes: 8 additions & 9 deletions app/routes/applicants.$applicantId._index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 });
}
Expand All @@ -114,7 +113,7 @@ export const loader = async ({ params, request }: LoaderFunctionArgs) => {
applicant,
projects,
canEditProject,
applicantId,
applicantId: applicant.id,
profileId,
comments,
});
Expand Down Expand Up @@ -191,11 +190,11 @@ export default function Applicant() {
};

const editApplicant = () => {
submit(null, {
submit(null, {
method: "get",
action: `/applicationForm/${applicantId}`,
action: `/applicationForm/${applicant.email}`,
});
}
};

return (
<>
Expand Down
5 changes: 2 additions & 3 deletions app/routes/applicants.$applicantId.comment.$id.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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}`);
};
7 changes: 4 additions & 3 deletions app/routes/applicants.$applicantId.comment.create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}`);
};
14 changes: 7 additions & 7 deletions app/routes/applicants.$applicantId.status.tsx
Original file line number Diff line number Diff line change
@@ -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 });
}
Expand All @@ -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;
Expand All @@ -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}`
);
};
15 changes: 2 additions & 13 deletions app/routes/applicationForm.$applicantId._index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,6 @@ function getCurrentDate(): string {
return `${year}-${month}-${day}`;
}

interface UserProfile {
email: string;
name: string;
}

interface LoaderData {
universities: Awaited<ReturnType<typeof getActiveUniversities>>;
profile: UserProfile;
applicantByEmail: Awaited<ReturnType<typeof getApplicantByEmail>>;
}

interface UniversityValue {
id: string;
name: string;
Expand All @@ -156,7 +145,7 @@ export const loader: LoaderFunction = async ({ request }) => {
const profile = await requireProfile(request);
const applicantByEmail = await getApplicantByEmail(profile.email);

return json<LoaderData>({
return json({
universities,
profile,
applicantByEmail,
Expand All @@ -165,7 +154,7 @@ export const loader: LoaderFunction = async ({ request }) => {

export default function FormPage() {
const { universities, profile, applicantByEmail } =
useLoaderData() as LoaderData;
useLoaderData<typeof loader>();

const [selectedUniversity, setSelectedUniversity] =
useState<UniversityValue | null>({
Expand Down
7 changes: 2 additions & 5 deletions app/routes/auth.auth0.callback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { existApplicant } from "~/models/applicant.server";
import {
createUserSession,
getUserRole,
requireProfile,
returnToCookie,
} from "~/session.server";

Expand All @@ -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}`;
Expand Down

0 comments on commit 628aeee

Please sign in to comment.