From c2211e3c81e87acebbdf646535cdfccd58b30c45 Mon Sep 17 00:00:00 2001 From: CDP21 <132059856+c2d13p@users.noreply.github.com> Date: Fri, 6 Dec 2024 10:46:04 +0000 Subject: [PATCH] add option to filter visits by dates --- server/controllers/public/visits.js | 79 ++++++++++++++++++++++------- 1 file changed, 61 insertions(+), 18 deletions(-) diff --git a/server/controllers/public/visits.js b/server/controllers/public/visits.js index d954936c..b1ea4188 100644 --- a/server/controllers/public/visits.js +++ b/server/controllers/public/visits.js @@ -1,34 +1,76 @@ const Visit = require('../../models/Visit') const { personRoomAssignmentPipeline } = require('../../models/RoomAssignment') +const { createSortAndLimitFilters } = require('./common-filters') + async function visitsQuery(req) { - // restituisce le visite correnti + // restituisce le visite correnti se from e to non specificati // si può filtrare sull'email + var from = undefined; + switch (req.query.from) { + case 'now': + from = new Date(); + break; + case undefined: + from = undefined; + break; + default: + from = new Date(req.query.from); + } + + var to = undefined; + switch (req.query.to) { + case 'now': + to = new Date(); + break; + case undefined: + to = undefined; + break; + default: + to = new Date(req.query.to); + } + + var match = {}; + + if (from !== undefined || to !== undefined) { + if (from !== undefined) { + match["endDate"] = { "$gte": from }; + } + if (to !== undefined) { + match["startDate"] = { "$lte": to }; + } + } else { + match = { + $expr: { + $and: [ + { + $or: [ + { $eq: ["$endDate", null] }, + { $gte: ["$endDate", { $dateAdd: { startDate: "$$NOW", unit: "day", amount: -1 } }] } + ] + }, + { + $or: [ + { $eq: ["$startDate", null] }, + { $lte: ["$startDate", "$$NOW"] } + ] + } + ] + } + }; + } + const matches = [] if (req.query.email) { matches.push({ 'person.email': req.query.email }, { 'person.alternativeEmails': req.query.email }) } + const sort_and_limit = createSortAndLimitFilters(req) + const pipeline = [ - {$match: { - $expr: { - $and: [ - { $or: [ - { $eq: ["$endDate", null] }, - { $gte: ["$endDate", { - $dateAdd: { - startDate: "$$NOW", - unit: "day", - amount: -1 - }}] } ]}, - { $or: [ - { $eq: ["$startDate", null] }, - { $lte: ["$startDate", "$$NOW"]} - ]}, - ]}, - }}, + {$match: match }, { $lookup: { from: 'people', localField: 'person', @@ -60,6 +102,7 @@ async function visitsQuery(req) { }} ] }}, + ...sort_and_limit, { $project: { _id: 0, startDate: 1,