-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): add relation between certification and formacode sub domains
- Loading branch information
Showing
9 changed files
with
203 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 6 additions & 1 deletion
7
packages/reva-api/modules/referential/features/getFormacodes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
...s/reva-api/modules/referential/features/updateCertificationWithRncpFiledsAndSubDomains.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { prismaClient } from "../../../prisma/client"; | ||
import { RNCPCertification, RNCPReferential } from "../rncp"; | ||
import { getFormacodes, Formacode } from "./getFormacodes"; | ||
|
||
export const updateCertificationWithRncpFiledsAndSubDomains = async (params: { | ||
rncpId: string; | ||
}) => { | ||
const { rncpId } = params; | ||
|
||
const certification = await prismaClient.certification.findFirst({ | ||
where: { rncpId }, | ||
}); | ||
if (!certification) { | ||
throw new Error( | ||
`La certification avec le code rncp ${rncpId} n'existe pas`, | ||
); | ||
} | ||
|
||
const rncpCertification = | ||
await RNCPReferential.getInstance().findOneByRncp(rncpId); | ||
if (!rncpCertification) { | ||
throw new Error( | ||
`La certification avec le code rncp ${rncpId} n'existe pas dans le référentiel RNCP`, | ||
); | ||
} | ||
|
||
// Update certification from based on RNCP | ||
await prismaClient.certification.update({ | ||
where: { id: certification.id }, | ||
data: { | ||
rncpLabel: rncpCertification.INTITULE, | ||
rncpLevel: getLevelFromRNCPCertification(rncpCertification), | ||
rncpTypeDiplome: rncpCertification.ABREGE?.LIBELLE, | ||
rncpExpiresAt: new Date(rncpCertification.DATE_FIN_ENREGISTREMENT), | ||
rncpDeliveryDeadline: rncpCertification.DATE_LIMITE_DELIVRANCE | ||
? new Date(rncpCertification.DATE_LIMITE_DELIVRANCE) | ||
: null, | ||
}, | ||
}); | ||
|
||
// Link certification to sub domains | ||
const referential = await getFormacodes(); | ||
|
||
const subDomains: Formacode[] = []; | ||
|
||
for (const rncpFormacode of rncpCertification.FORMACODES) { | ||
const formacode = getFormacodeByCode(rncpFormacode.CODE, referential); | ||
|
||
if (!formacode) { | ||
throw new Error( | ||
`Le formacode avec le code ${rncpFormacode.CODE} n'existe pas dans le référentiel RNCP`, | ||
); | ||
} | ||
|
||
const parents = getParents(formacode, referential); | ||
const subDomain = parents.find( | ||
(formacode) => formacode.type == "SUB_DOMAIN", | ||
); | ||
if ( | ||
subDomain && | ||
subDomains.findIndex((domain) => domain.code == subDomain.code) == -1 | ||
) { | ||
subDomains.push(subDomain); | ||
} | ||
} | ||
|
||
// Remove all links by certificationId | ||
await prismaClient.certificationOnFormacode.deleteMany({ | ||
where: { certificationId: certification.id }, | ||
}); | ||
|
||
// Create all links | ||
await prismaClient.certificationOnFormacode.createMany({ | ||
data: subDomains.map((subDomain) => ({ | ||
formacodeId: subDomain.id, | ||
certificationId: certification.id, | ||
})), | ||
}); | ||
}; | ||
|
||
const getLevelFromRNCPCertification = ( | ||
certification: RNCPCertification, | ||
): number => { | ||
try { | ||
const strLevel = | ||
certification.NOMENCLATURE_EUROPE.INTITULE.split(" ").reverse()[0]; | ||
const level = parseInt(strLevel, 10); | ||
return level; | ||
} catch (error) { | ||
throw new Error( | ||
`Le niveau de la certification pour le code RNCP ${certification.ID_FICHE} n'a pas pu être formatté`, | ||
); | ||
} | ||
}; | ||
|
||
const getFormacodeByCode = ( | ||
code: string, | ||
referential: Formacode[], | ||
): Formacode | undefined => { | ||
return referential.find((formacode) => formacode.code == code); | ||
}; | ||
|
||
const getParent = (child: Formacode, referential: Formacode[]) => { | ||
return referential.find((formacode) => formacode.code == child.parentCode); | ||
}; | ||
|
||
const getParents = ( | ||
formacode: Formacode, | ||
referential: Formacode[], | ||
): Formacode[] => { | ||
const parent = getParent(formacode, referential); | ||
|
||
if (parent) { | ||
return [...getParents(parent, referential), formacode]; | ||
} | ||
|
||
return [formacode]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...pi/prisma/migrations/20240909134035_add_rncp_fields_to_certification_entity/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
-- AlterTable | ||
ALTER TABLE "certification" ADD COLUMN "rncp_delivery_deadline" TIMESTAMPTZ(6), | ||
ADD COLUMN "rncp_expires_at" TIMESTAMPTZ(6), | ||
ADD COLUMN "rncp_label" VARCHAR(255), | ||
ADD COLUMN "rncp_level" INTEGER, | ||
ADD COLUMN "rncp_type_diplome" VARCHAR(255); |
19 changes: 19 additions & 0 deletions
19
...ations/20240909140249_add_relation_between_formacode_certification_entities/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
-- CreateTable | ||
CREATE TABLE "certification_on_formacode" ( | ||
"id" UUID NOT NULL DEFAULT uuid_generate_v4(), | ||
"formacode_id" UUID NOT NULL, | ||
"certification_id" UUID NOT NULL, | ||
"created_at" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updated_at" TIMESTAMPTZ(6), | ||
|
||
CONSTRAINT "certification_on_formacode_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "certification_on_formacode_formacode_id_certification_id_key" ON "certification_on_formacode"("formacode_id", "certification_id"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "certification_on_formacode" ADD CONSTRAINT "certification_on_formacode_formacode_id_fkey" FOREIGN KEY ("formacode_id") REFERENCES "formacode"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "certification_on_formacode" ADD CONSTRAINT "certification_on_formacode_certification_id_fkey" FOREIGN KEY ("certification_id") REFERENCES "certification"("id") ON DELETE CASCADE ON UPDATE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
packages/reva-api/scripts/updateAvailableCertificationsWithRncpFiledsAndSubDomains.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { prismaClient } from "../prisma/client"; | ||
|
||
import { updateCertificationWithRncpFiledsAndSubDomains } from "../modules/referential/features/updateCertificationWithRncpFiledsAndSubDomains"; | ||
|
||
const updateAvailableCertificationsWithRncpFiledsAndSubDomains = async () => { | ||
const certifications = await prismaClient.certification.findMany({ | ||
where: { status: "AVAILABLE" }, | ||
select: { id: true, rncpId: true }, | ||
}); | ||
|
||
for (const { rncpId } of certifications) { | ||
await updateCertificationWithRncpFiledsAndSubDomains({ rncpId }); | ||
} | ||
}; | ||
|
||
const main = async () => { | ||
await updateAvailableCertificationsWithRncpFiledsAndSubDomains(); | ||
}; | ||
|
||
main(); |