Skip to content

Commit

Permalink
add option to filter visits by dates
Browse files Browse the repository at this point in the history
  • Loading branch information
c2d13p committed Dec 6, 2024
1 parent f451fcd commit c2211e3
Showing 1 changed file with 61 additions and 18 deletions.
79 changes: 61 additions & 18 deletions server/controllers/public/visits.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -60,6 +102,7 @@ async function visitsQuery(req) {
}}
]
}},
...sort_and_limit,
{ $project: {
_id: 0,
startDate: 1,
Expand Down

0 comments on commit c2211e3

Please sign in to comment.