Skip to content

Commit

Permalink
fix: ajout des erps en bdd (#3803)
Browse files Browse the repository at this point in the history
Co-authored-by: Paul Gaucher <[email protected]>
  • Loading branch information
Pomarom and Pomarom authored Aug 12, 2024
1 parent 92ad3f7 commit a33baa7
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 2 deletions.
22 changes: 22 additions & 0 deletions server/src/common/actions/erp.actions.ts
Original file line number Diff line number Diff line change
@@ -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();
};
3 changes: 2 additions & 1 deletion server/src/common/model/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -104,7 +105,7 @@ export const decaRawDb = () => getDbCollection<IDecaRaw>(decaRawModelDescriptor.
export const voeuxAffelnetDb = () => getDbCollection<IVoeuAffelnet>(voeuxAffelnetDescriptor.collectionName);
export const telechargementListesNominativesLogsDb = () =>
getDbCollection<ITelechargementListeNomLogs>(telechargementListesNominativesLogsDescriptor.collectionName);

export const erpDb = () => getDbCollection<IErp>(erpModelDescriptor.collectionName);
export const opcosDb = () => getDbCollection<IOpcos>(opcosDescriptor.collectionName);
export const opcosRncpDb = () => getDbCollection<IOpcoRncp>(opcosRncpDescriptor.collectionName);
// v2
Expand Down
66 changes: 66 additions & 0 deletions server/src/db/migrations/20240812151917-ajout-erp-en-db.ts
Original file line number Diff line number Diff line change
@@ -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]);
}
};
39 changes: 39 additions & 0 deletions server/src/http/routes/admin.routes/erps.routes.ts
Original file line number Diff line number Diff line change
@@ -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);
};
16 changes: 16 additions & 0 deletions server/src/http/routes/specific.routes/erps.routes.ts
Original file line number Diff line number Diff line change
@@ -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();
};
6 changes: 5 additions & 1 deletion server/src/http/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
Expand Down Expand Up @@ -448,7 +450,8 @@ function setupRoutes(app: Application) {
has_accept_cgu_version: req.params.version,
});
})
);
)
.use("/api/v1/erps", erpRoutes());

/********************************
* API pour un organisme *
Expand Down Expand Up @@ -899,6 +902,7 @@ function setupRoutes(app: Application) {
.use("/transmissions", transmissionRoutesAdmin())
.use("/affelnet", affelnetRoutesAdmin())
.use("/opcos", opcosRoutesAdmin())
.use("/erps", erpsRoutesAdmin())
.get(
"/stats",
returnResult(async () => {
Expand Down
19 changes: 19 additions & 0 deletions shared/models/data/erp.model.ts
Original file line number Diff line number Diff line change
@@ -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<typeof zErp>;
export default { zod: zErp, indexes, collectionName };
1 change: 1 addition & 0 deletions shared/models/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

0 comments on commit a33baa7

Please sign in to comment.