From 88dbedb05e5c8a8bfde62c8a0a5ee451d6bb346d Mon Sep 17 00:00:00 2001 From: Kai Hendry Date: Fri, 19 Oct 2018 16:44:39 +0800 Subject: [PATCH] Preview what would be in the digest email https://github.com/unee-t/frontend/issues/282 --- imports/api/rest/digest.js | 27 +++++++++++++++++++++++++++ imports/api/rest/rest-routes.js | 2 ++ 2 files changed, 29 insertions(+) create mode 100644 imports/api/rest/digest.js diff --git a/imports/api/rest/digest.js b/imports/api/rest/digest.js new file mode 100644 index 00000000..bdc8af7e --- /dev/null +++ b/imports/api/rest/digest.js @@ -0,0 +1,27 @@ +import CaseNotifications from '../case-notifications' + +// https://github.com/unee-t/frontend/issues/282 +export default (req, res) => { + if (req.query.accessToken !== process.env.API_ACCESS_TOKEN) { + res.send(401) + } + + var oneWeekAgo = new Date() + oneWeekAgo.setDate(oneWeekAgo.getDate() - 7) + + const query = { createdAt: {} } + query.createdAt['$gte'] = oneWeekAgo + + if (!req.query.userid) { + res.send(400, 'userid missing') + } + + query.userId = req.query.userid + + if (req.query.from) { query.createdAt['$gte'] = new Date(req.query.from) } + if (req.query.to) { query.createdAt['$lte'] = new Date(req.query.to) } + + console.log('digest', query) + + res.send(CaseNotifications.find(query).fetch()) +} diff --git a/imports/api/rest/rest-routes.js b/imports/api/rest/rest-routes.js index bff46047..6277fe2d 100644 --- a/imports/api/rest/rest-routes.js +++ b/imports/api/rest/rest-routes.js @@ -3,6 +3,7 @@ import { json } from 'body-parser' import getPendingInvitations from './get-pending-invitations' import putPendingInvitations from './put-pending-invitations' import getInvitations from './get-invitations' +import digest from './digest' import getConvertedInvitations from './get-converted-invitations' import postProcessDbChangeMessage from './post-process-db-change-message' import postSesNotification from './post-ses-notification' @@ -28,6 +29,7 @@ const createRoute = (method, url, handler) => { createRoute('get', '/pending-invitations', getPendingInvitations) createRoute('get', '/invitations', getInvitations) +createRoute('get', '/digest', digest) createRoute('get', '/converted-invitations', getConvertedInvitations) createRoute('put', '/pending-invitations/done', putPendingInvitations) createRoute('post', '/db-change-message/process', postProcessDbChangeMessage)