diff --git a/server/src/common/actions/erp.actions.ts b/server/src/common/actions/erp.actions.ts new file mode 100644 index 000000000..2217d7882 --- /dev/null +++ b/server/src/common/actions/erp.actions.ts @@ -0,0 +1,22 @@ +import Boom from "boom"; +import { ObjectId } from "mongodb"; + +import { erpDb } from "../model/collections"; + +export const createERP = async (name: string) => { + return erpDb().insertOne({ _id: new ObjectId(), name, created_at: new Date() }); +}; + +export const deleteERPById = async (id: string) => { + const erp = await erpDb().findOne({ _id: new ObjectId(id) }); + + if (!erp) { + throw Boom.notFound(`ERP with id ${id} not found`); + } + + return erpDb().deleteOne({ _id: new ObjectId(id) }); +}; + +export const findAllERP = async () => { + return await erpDb().find().toArray(); +}; diff --git a/server/src/common/model/collections.ts b/server/src/common/model/collections.ts index 633a2c836..1604ca422 100644 --- a/server/src/common/model/collections.ts +++ b/server/src/common/model/collections.ts @@ -7,6 +7,7 @@ import effectifsModelDescriptor, { IEffectif } from "shared/models/data/effectif import effectifsArchiveModelDescriptor, { IEffectifArchive } from "shared/models/data/effectifsArchive.model"; import effectifsDECAModelDescriptor, { IEffectifDECA } from "shared/models/data/effectifsDECA.model"; import effectifsQueueModelDescriptor, { IEffectifQueue } from "shared/models/data/effectifsQueue.model"; +import erpModelDescriptor, { IErp } from "shared/models/data/erp.model"; import fiabilisationUaiSiretModelDescriptor from "shared/models/data/fiabilisationUaiSiret.model"; import formationsModelDescriptor, { IFormation } from "shared/models/data/formations.model"; import formationsCatalogueModelDescriptor, { IFormationCatalogue } from "shared/models/data/formationsCatalogue.model"; @@ -104,7 +105,7 @@ export const decaRawDb = () => getDbCollection(decaRawModelDescriptor. export const voeuxAffelnetDb = () => getDbCollection(voeuxAffelnetDescriptor.collectionName); export const telechargementListesNominativesLogsDb = () => getDbCollection(telechargementListesNominativesLogsDescriptor.collectionName); - +export const erpDb = () => getDbCollection(erpModelDescriptor.collectionName); export const opcosDb = () => getDbCollection(opcosDescriptor.collectionName); export const opcosRncpDb = () => getDbCollection(opcosRncpDescriptor.collectionName); // v2 diff --git a/server/src/db/migrations/20240812151917-ajout-erp-en-db.ts b/server/src/db/migrations/20240812151917-ajout-erp-en-db.ts new file mode 100644 index 000000000..e9dc0318b --- /dev/null +++ b/server/src/db/migrations/20240812151917-ajout-erp-en-db.ts @@ -0,0 +1,66 @@ +import { Db } from "mongodb"; + +export const up = async (db: Db) => { + const erpToInsert = [ + { + name: "Gesti", + apiV3: true, + helpFilePath: "/Gestibase-2024.pdf", + }, + { + name: "Ypareo", + apiV3: true, + helpFilePath: "/Ypareo-2024.pdf", + }, + { + name: "SC Form", + apiV3: true, + helpFilePath: "/SC-form-2024.pdf", + }, + { + name: "Formasup", + }, + { + name: "FCA Manager", + helpFilePath: "https://files.tableau-de-bord.apprentissage.beta.gouv.fr/pas-a-pas/fcamanager.pdf", + helpFileSize: "288 ko", + }, + { + name: "Aimaira", + apiV3: true, + helpFilePath: "/Aimaira-2024.pdf", + }, + { + name: "Filiz", + apiV3: true, + }, + { + name: "Hyperplanning", + apiV3: true, + }, + { + name: "Gescicca (CNAM)", + apiV3: true, + }, + { + name: "Charlemagne", + apiV3: true, + }, + { + name: "Formasup HDF", + apiV3: true, + }, + { + name: "Ammon", + apiV3: true, + }, + { + name: "Formasup PACA", + apiV3: true, + }, + ]; + + for (let i = 0; i < erpToInsert.length; i++) { + await db.collection("erp").insertOne(erpToInsert[i]); + } +}; diff --git a/server/src/http/routes/admin.routes/erps.routes.ts b/server/src/http/routes/admin.routes/erps.routes.ts new file mode 100644 index 000000000..ba899b3fd --- /dev/null +++ b/server/src/http/routes/admin.routes/erps.routes.ts @@ -0,0 +1,39 @@ +import express from "express"; +import { z } from "zod"; + +import { createERP, deleteERPById } from "@/common/actions/erp.actions"; +import objectIdSchema from "@/common/validation/objectIdSchema"; +import { returnResult } from "@/http/middlewares/helpers"; +import validateRequestMiddleware from "@/http/middlewares/validateRequestMiddleware"; + +export default () => { + const router = express.Router(); + + router.post( + "/", + validateRequestMiddleware({ + body: z.object({ name: z.string() }), + }), + returnResult(addERP) + ); + + router.delete( + "/:id", + validateRequestMiddleware({ + params: objectIdSchema("id"), + }), + returnResult(deleteERP) + ); + + return router; +}; + +const addERP = ({ body }) => { + const { name } = body; + return createERP(name); +}; + +const deleteERP = ({ params }) => { + const { id } = params; + return deleteERPById(id); +}; diff --git a/server/src/http/routes/specific.routes/erps.routes.ts b/server/src/http/routes/specific.routes/erps.routes.ts new file mode 100644 index 000000000..4c352c569 --- /dev/null +++ b/server/src/http/routes/specific.routes/erps.routes.ts @@ -0,0 +1,16 @@ +import express from "express"; + +import { findAllERP } from "@/common/actions/erp.actions"; +import { returnResult } from "@/http/middlewares/helpers"; + +export default () => { + const router = express.Router(); + + router.get("/", returnResult(getErp)); + + return router; +}; + +const getErp = async () => { + return await findAllERP(); +}; diff --git a/server/src/http/server.ts b/server/src/http/server.ts index df271ac0a..6b2be1d1d 100644 --- a/server/src/http/server.ts +++ b/server/src/http/server.ts @@ -134,6 +134,7 @@ import validateRequestMiddleware from "./middlewares/validateRequestMiddleware"; import { openApiFilePath } from "./open-api-path"; import affelnetRoutesAdmin from "./routes/admin.routes/affelnet.routes"; import effectifsAdmin from "./routes/admin.routes/effectifs.routes"; +import erpsRoutesAdmin from "./routes/admin.routes/erps.routes"; import maintenancesAdmin from "./routes/admin.routes/maintenances.routes"; import opcosRoutesAdmin from "./routes/admin.routes/opcos.routes"; import organismesAdmin from "./routes/admin.routes/organismes.routes"; @@ -142,6 +143,7 @@ import usersAdmin from "./routes/admin.routes/users.routes"; import emails from "./routes/emails.routes"; import affelnetRoutes from "./routes/specific.routes/affelnet.routes"; import dossierApprenantRouter from "./routes/specific.routes/dossiers-apprenants.routes"; +import erpRoutes from "./routes/specific.routes/erps.routes"; import { getOrganismeEffectifs, updateOrganismeEffectifs } from "./routes/specific.routes/organisme.routes"; import organismesRouter from "./routes/specific.routes/organismes.routes"; import transmissionRoutes from "./routes/specific.routes/transmission.routes"; @@ -448,7 +450,8 @@ function setupRoutes(app: Application) { has_accept_cgu_version: req.params.version, }); }) - ); + ) + .use("/api/v1/erps", erpRoutes()); /******************************** * API pour un organisme * @@ -899,6 +902,7 @@ function setupRoutes(app: Application) { .use("/transmissions", transmissionRoutesAdmin()) .use("/affelnet", affelnetRoutesAdmin()) .use("/opcos", opcosRoutesAdmin()) + .use("/erps", erpsRoutesAdmin()) .get( "/stats", returnResult(async () => { diff --git a/shared/models/data/erp.model.ts b/shared/models/data/erp.model.ts new file mode 100644 index 000000000..5786452f7 --- /dev/null +++ b/shared/models/data/erp.model.ts @@ -0,0 +1,19 @@ +import type { CreateIndexesOptions, IndexSpecification } from "mongodb"; +import { z } from "zod"; +import { zObjectId } from "zod-mongodb-schema"; + +const collectionName = "erp"; + +const indexes: [IndexSpecification, CreateIndexesOptions][] = [[{ nom: 1 }, { unique: true }]]; + +const zErp = z.object({ + _id: zObjectId, + name: z.string(), + created_at: z.date(), + apiV3: z.boolean().nullish(), + helpFilePath: z.string().nullish(), + helpFileSize: z.string().nullish(), +}); + +export type IErp = z.output; +export default { zod: zErp, indexes, collectionName }; diff --git a/shared/models/data/index.ts b/shared/models/data/index.ts index a1b99116b..152d6f83b 100644 --- a/shared/models/data/index.ts +++ b/shared/models/data/index.ts @@ -19,6 +19,7 @@ export * from "./rncp.model"; export * from "./rome.model"; export * from "./users.model"; export * from "./usersMigration.model"; +export * from "./erp.model"; export * from "./v2"; export * from "./opco"; export { zodToMongoSchema } from "zod-mongodb-schema";