From a60d2faee5c8ce08a86e4c6d115954289b5e829d Mon Sep 17 00:00:00 2001 From: "U-THOMASB\\thoma" Date: Fri, 16 Aug 2024 15:58:04 +0200 Subject: [PATCH] fix: diploma creation for existing programs --- .../[location]/diplomas/_actions/fetch.ts | 29 ++++++++++++- .../_components/create-dialog-client.tsx | 42 ++++++++++++++++++- apps/web/src/lib/nwd.ts | 24 ++++++++--- .../core/src/models/student/curriculum.ts | 27 ++++++++++++ 4 files changed, 114 insertions(+), 8 deletions(-) diff --git a/apps/web/src/app/(dashboard)/(management)/locatie/[location]/diplomas/_actions/fetch.ts b/apps/web/src/app/(dashboard)/(management)/locatie/[location]/diplomas/_actions/fetch.ts index bb1daa25..110d86ea 100644 --- a/apps/web/src/app/(dashboard)/(management)/locatie/[location]/diplomas/_actions/fetch.ts +++ b/apps/web/src/app/(dashboard)/(management)/locatie/[location]/diplomas/_actions/fetch.ts @@ -1,6 +1,11 @@ "use server"; -import { listCurriculaByProgram, listGearTypesByCurriculum } from "~/lib/nwd"; +import { + listCompletedCompetenciesByStudentCurriculumId, + listCurriculaByProgram, + listGearTypesByCurriculum, + retrieveStudentCurriculumByPersonIdAndCurriculumId, +} from "~/lib/nwd"; export async function getCurriculaByProgram(programId: string) { return await listCurriculaByProgram(programId, true); @@ -9,3 +14,25 @@ export async function getCurriculaByProgram(programId: string) { export async function getGearTypesByCurriculum(curriculumId: string) { return await listGearTypesByCurriculum(curriculumId); } + +export async function getCompletedCompetencies( + personId: string, + curriculumId: string, + gearTypeId: string, +) { + const studentCurriculum = + await retrieveStudentCurriculumByPersonIdAndCurriculumId( + personId, + curriculumId, + gearTypeId, + ); + + if (!studentCurriculum) { + return null; + } + + const completedCompetencies = + await listCompletedCompetenciesByStudentCurriculumId(studentCurriculum.id); + + return completedCompetencies.map((cc) => cc.competencyId); +} diff --git a/apps/web/src/app/(dashboard)/(management)/locatie/[location]/diplomas/_components/create-dialog-client.tsx b/apps/web/src/app/(dashboard)/(management)/locatie/[location]/diplomas/_components/create-dialog-client.tsx index fa64b5c8..0d04e0b3 100644 --- a/apps/web/src/app/(dashboard)/(management)/locatie/[location]/diplomas/_components/create-dialog-client.tsx +++ b/apps/web/src/app/(dashboard)/(management)/locatie/[location]/diplomas/_components/create-dialog-client.tsx @@ -46,6 +46,7 @@ import type { } from "~/lib/nwd"; import { createCertificate } from "../_actions/create"; import { + getCompletedCompetencies, getCurriculaByProgram, getGearTypesByCurriculum, } from "../_actions/fetch"; @@ -93,16 +94,26 @@ function CreateDialogClient({ setIsOpen(false); } + if (result.message === "Error" && Object.keys(result.errors).length < 1) { + toast.error("Er is iets misgegaan"); + } + return result; }; const [state, formAction] = useActionState(submit, undefined); + const [selectedPerson, setSelectedPerson] = useState< + (typeof persons)[number] | null + >(null); const [selectedProgram, setSelectedProgram] = useState(null); const [selectedGearType, setSelectedGearType] = useState(null); const [selectedCurriculum, setSelectedCurriculum] = useState< Awaited>[number] | null >(null); + const [completedCompetencies, setCompletedCompetencies] = useState + > | null>(null); const [programQuery, setProgramQuery] = useState(""); const [personQuery, setPersonQuery] = useState(""); @@ -131,6 +142,7 @@ function CreateDialogClient({ const [curriculum] = await getCurriculaByProgram(selectedProgram); if (!curriculum) { + setCompletedCompetencies(null); setSelectedCurriculum(null); setGearTypes([]); return; @@ -144,6 +156,24 @@ function CreateDialogClient({ void fetchCurricula(); }, [selectedProgram]); + useEffect(() => { + async function fetchCompletedCompetencies() { + if (!selectedPerson || !selectedCurriculum || !selectedGearType) { + return; + } + + const completedCompetencies = await getCompletedCompetencies( + selectedPerson.id, + selectedCurriculum.id, + selectedGearType, + ); + + setCompletedCompetencies(completedCompetencies ?? []); + } + + void fetchCompletedCompetencies(); + }, [selectedPerson, selectedCurriculum, selectedGearType]); + return ( <>