Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add find many paginated for partenaires #90

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions server/lib/partenaire-de-la-charte/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
71 changes: 71 additions & 0 deletions server/lib/partenaire-de-la-charte/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -164,6 +234,7 @@ async function deleteOne(id) {

module.exports = {
findMany,
findManyPaginated,
findOneOrFail,
createOne,
updateOne,
Expand Down
11 changes: 11 additions & 0 deletions server/utils/mongo-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Loading