-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Paul Gaucher <[email protected]>
- Loading branch information
Showing
8 changed files
with
170 additions
and
2 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
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(); | ||
}; |
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
66 changes: 66 additions & 0 deletions
66
server/src/db/migrations/20240812151917-ajout-erp-en-db.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,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]); | ||
} | ||
}; |
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,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); | ||
}; |
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,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(); | ||
}; |
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
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 }; |
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