From 905031e154a1226f4c85702801fe5fb0d2f5ca65 Mon Sep 17 00:00:00 2001 From: Guillaume Fay Date: Wed, 7 Aug 2024 17:39:22 +0200 Subject: [PATCH] feat: add find many paginated for partenaires --- .../lib/partenaire-de-la-charte/controller.js | 15 ++++ server/lib/partenaire-de-la-charte/service.js | 71 +++++++++++++++++++ server/utils/mongo-client.js | 11 +++ 3 files changed, 97 insertions(+) diff --git a/server/lib/partenaire-de-la-charte/controller.js b/server/lib/partenaire-de-la-charte/controller.js index 08a1511..3897411 100644 --- a/server/lib/partenaire-de-la-charte/controller.js +++ b/server/lib/partenaire-de-la-charte/controller.js @@ -21,6 +21,21 @@ partenaireDeLaCharteRoutes.get("/", async (req, res) => { } }); +partenaireDeLaCharteRoutes.get("/paginated", async (req, res) => { + try { + const {page, limit, ...query} = req.query; + const partenairesDeLaCharte = await PartenaireDeLaCharteService.findManyPaginated( + query, + parseInt(page), + parseInt(limit) + ); + res.json(partenairesDeLaCharte); + } catch (err) { + console.error(err); + res.status(500).json({ error: err.message }); + } +}); + partenaireDeLaCharteRoutes.get("/random", async (req, res) => { try { const { limit } = req.query; diff --git a/server/lib/partenaire-de-la-charte/service.js b/server/lib/partenaire-de-la-charte/service.js index 4bd8c03..48c06dc 100644 --- a/server/lib/partenaire-de-la-charte/service.js +++ b/server/lib/partenaire-de-la-charte/service.js @@ -62,6 +62,76 @@ async function findMany(query = {}) { return records; } +async function findManyPaginated(query = {}, page = 1, limit = 10) { + const { + search, + codeDepartement, + services, + type, + withCandidates, + withoutPictures, + dataGouvOrganizationId, + apiDepotClientId, + } = query; + + const mongoQuery = withCandidates + ? {} + : { + signatureDate: { $exists: true }, + }; + + if (search) { + mongoQuery.name = { $regex: search, $options: "i" } + } + + if (type) { + mongoQuery.type = type; + } + + if (dataGouvOrganizationId) { + mongoQuery.dataGouvOrganizationId = dataGouvOrganizationId; + } + + if (apiDepotClientId) { + mongoQuery.apiDepotClientId = apiDepotClientId; + } + + if (codeDepartement) { + mongoQuery.$or = [ + { codeDepartement: { $in: [codeDepartement] } }, + { isPerimeterFrance: true }, + ]; + } + + if (services) { + mongoQuery.services = { $in: services.split(",") }; + } + + const total = await mongoClient.count(collectionName, mongoQuery); + const totalCommunes = await mongoClient.count(collectionName, { + ...mongoQuery, + type: "commune", + }); + const totalOrganismes = await mongoClient.count(collectionName, { + ...mongoQuery, + type: "organisme", + }); + const totalEntreprises = await mongoClient.count(collectionName, { + ...mongoQuery, + type: "entreprise", + }); + const records = await mongoClient.findManyPaginated(collectionName, mongoQuery, page, limit); + + if (withoutPictures) { + return records.map((record) => { + const { picture, ...rest } = record; + return rest; + }); + } + + return {total, totalCommunes, totalOrganismes, totalEntreprises, data: records}; +} + async function findOneOrFail(id) { const record = await mongoClient.findOneById(collectionName, id); @@ -164,6 +234,7 @@ async function deleteOne(id) { module.exports = { findMany, + findManyPaginated, findOneOrFail, createOne, updateOne, diff --git a/server/utils/mongo-client.js b/server/utils/mongo-client.js index f60849e..2a64051 100644 --- a/server/utils/mongo-client.js +++ b/server/utils/mongo-client.js @@ -86,6 +86,17 @@ class Mongo { return this.db.collection(collectionName).find({_deleted: undefined, ...options}).toArray() } + findManyPaginated(collectionName, options = {}, page = 1, pageSize = 20) { + return this.db.collection(collectionName).find({_deleted: undefined, ...options}) + .limit(pageSize) + .skip((page - 1) * pageSize) + .toArray() + } + + count(collectionName, options = {}) { + return this.db.collection(collectionName).countDocuments({_deleted: undefined, ...options}) + } + findDistinct(collectionName, field) { return this.db.collection(collectionName).distinct(field) }