Skip to content

Commit

Permalink
chore(api): reworked candidate login in order to remove getCandidateW…
Browse files Browse the repository at this point in the history
…ithCandidacyFromKeycloakId function and replace it with the candidate->candidacy resolver
  • Loading branch information
agarbe committed Aug 15, 2024
1 parent 37858bb commit 0f9cf4a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 92 deletions.
67 changes: 0 additions & 67 deletions packages/reva-api/modules/candidate/database/candidates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,73 +100,6 @@ export const createCandidateWithCandidacy = async (candidate: any) => {
};
};

export const getCandidateWithCandidacyFromKeycloakId = async (
keycloakId: string,
) => {
if (!keycloakId) {
throw new Error("Identifiant utilisateur invalide");
}
let candidate = await prismaClient.candidate.findFirst({
where: {
keycloakId: keycloakId,
candidacies: {
some: ongoingCandidacyFilter,
},
},
include: {
candidacies: {
where: ongoingCandidacyFilter,
include: { certification: true },
},
highestDegree: true,
vulnerabilityIndicator: true,
},
});

if (!candidate) {
candidate = await prismaClient.candidate.update({
where: {
keycloakId: keycloakId,
},
data: {
candidacies: {
create: {
candidacyStatuses: {
create: {
status: CandidacyStatusStep.PROJET,
isActive: true,
},
},
admissibility: { create: {} },
examInfo: { create: {} },
},
},
},
include: {
candidacies: {
where: ongoingCandidacyFilter,
include: { certification: true },
},
highestDegree: true,
vulnerabilityIndicator: true,
},
});
}

return candidate
? {
...candidate,
candidacies: candidate?.candidacies.map((candidacy) => ({
...candidacy,
certification: candidacy?.certification && {
...candidacy?.certification,
codeRncp: candidacy?.certification?.rncpId,
},
})),
}
: null;
};

export const getCandidateByCandidacyId = async (id: string) =>
prismaClient.candidate.findFirst({
where: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ import {
CandidateAuthenticationInput,
CandidateRegistrationInput,
} from "../candidate.types";
import {
createCandidateWithCandidacy,
getCandidateWithCandidacyFromKeycloakId,
} from "../database/candidates";
import { createCandidateWithCandidacy } from "../database/candidates";
import { getCandidateByKeycloakId } from "./getCandidateByKeycloakId";

export const candidateAuthentication = async ({ token }: { token: string }) => {
const candidateAuthenticationInput = (await getJWTContent(
Expand Down Expand Up @@ -97,23 +95,12 @@ const confirmRegistration = async ({
departmentId: candidateRegistrationInput.departmentId,
feasibilityFormat: certification.feasibilityFormat,
});

//reload candidate and candidacy after certification update
candidateWithCandidacy =
await getCandidateWithCandidacyFromKeycloakId(candidateKeycloakId);

if (!candidateWithCandidacy) {
throw new Error("Candidat non trouvé");
}
}

const tokens = await generateIAMToken(candidateKeycloakId);
const iamToken = {
tokens,
candidate: {
...candidateWithCandidacy,
candidacy: candidateWithCandidacy.candidacies[0],
},
candidate,
};

await logCandidacyAuditEvent({
Expand All @@ -136,21 +123,18 @@ const loginCandidate = async ({ email }: { email: string }) => {
`Candidat non trouvé`,
);
}
const candidateWithCandidacy = await getCandidateWithCandidacyFromKeycloakId(
account?.id || "",
);
const candidate = await getCandidateByKeycloakId({
keycloakId: account?.id || "",
});

if (!candidateWithCandidacy) {
if (!candidate) {
throw new Error("Candidat non trouvé");
}

const tokens = await generateIAMToken(candidateWithCandidacy.keycloakId);
const tokens = await generateIAMToken(candidate.keycloakId);
const iamToken = {
tokens,
candidate: {
...candidateWithCandidacy,
candidacy: candidateWithCandidacy.candidacies[0],
},
candidate,
};

return iamToken;
Expand Down

0 comments on commit 0f9cf4a

Please sign in to comment.