diff --git a/f2/server.js b/f2/server.js index 24dc595e8..9bc73cf6d 100644 --- a/f2/server.js +++ b/f2/server.js @@ -6,6 +6,7 @@ const { getAllCerts, encryptAndSend } = require('./src/utils/encryptedEmail') const { isAvailable } = require('./src/utils/checkIfAvailable') const { getData } = require('./src/utils/getData') const { saveRecord } = require('./src/utils/saveRecord') +const { getReportCount } = require('./src/utils/saveRecord') const { saveBlob } = require('./src/utils/saveBlob') const { scanFiles, contentModeratorFiles } = require('./src/utils/scanFiles') const { @@ -47,11 +48,15 @@ const allowedOrigins = [ 'https://centreantifraude.ca', ] -const availableData = { - numberOfSubmissions: 0, - numberOfRequests: 0, - lastRequested: undefined, +let availableData +async function initializeAvailableData() { + availableData = { + numberOfSubmissions: await getReportCount(), + numberOfRequests: 0, + lastRequested: undefined, + } } +initializeAvailableData() // These can all be done async to avoid holding up the nodejs process? async function save(data, res) { @@ -78,7 +83,8 @@ const uploadData = async (req, res, fields, files) => { contentModeratorFiles(data, () => save(data, res)) } -app.get('/', function(req, res, next) { +app.get('/', async function(req, res, next) { + availableData.numberOfSubmissions = await getReportCount() if (availableData.numberOfSubmissions >= process.env.SUBMISSIONS_PER_DAY) { console.warn('Warning: redirecting request to CAFC') res.redirect( @@ -128,7 +134,6 @@ app }) .post('/submit', (req, res) => { - availableData.numberOfSubmissions += 1 var form = new formidable.IncomingForm() form.parse(req) let files = [] diff --git a/f2/src/utils/checkIfAvailable.js b/f2/src/utils/checkIfAvailable.js index 401dbb993..7d19207a0 100644 --- a/f2/src/utils/checkIfAvailable.js +++ b/f2/src/utils/checkIfAvailable.js @@ -11,13 +11,6 @@ else console.info('Availability configured') const isAvailable = availableData => { try { const currentTime = new Date() - if ( - availableData.lastRequested && - availableData.lastRequested.toDateString() !== currentTime.toDateString() - ) { - availableData.numberOfSubmissions = 0 // a new day has started - return true - } if (!submissionsPerDay || !secondsBetweenRequests) return false if (availableData.numberOfSubmissions >= submissionsPerDay) return false if ( diff --git a/f2/src/utils/getData.js b/f2/src/utils/getData.js index 6edbd51e2..53a22bd1c 100644 --- a/f2/src/utils/getData.js +++ b/f2/src/utils/getData.js @@ -68,6 +68,7 @@ async function getData(fields, files) { const timeString = padNumber(now.getHours()) + ':' + padNumber(now.getMinutes()) data.submissionTime = `${dateString} ${timeString} UTC-${timeZoneOffset}` + data.submissionDate = `${dateString}` return data } diff --git a/f2/src/utils/saveRecord.js b/f2/src/utils/saveRecord.js index 96215e06b..4c6b861c2 100644 --- a/f2/src/utils/saveRecord.js +++ b/f2/src/utils/saveRecord.js @@ -1,5 +1,13 @@ const MongoClient = require('mongodb').MongoClient +const date = new Date() +const currentDate = + (date.getDate() > 9 ? date.getDate() : '0' + date.getDate()) + + '/' + + (date.getMonth() > 8 ? date.getMonth() + 1 : '0' + (date.getMonth() + 1)) + + '/' + + date.getFullYear() +let numberofReports = 0 const dbName = process.env.COSMOSDB_NAME const dbKey = process.env.COSMOSDB_KEY @@ -43,5 +51,32 @@ async function saveRecord(data, res) { res.send('CosmosDB not configured') } } +async function getReportCount() { + if (cosmosDbConfigured) { + MongoClient.connect(url, function(err, db) { + if (err) { + console.warn(`ERROR in MongoClient.connect: ${err}`) + } else { + var dbo = db.db('cybercrime') + dbo + .collection('reports') + .find({ + submissionDate: { + $eq: currentDate, + }, + }) + .toArray(function(err, result) { + if (err) { + console.warn(`ERROR in find: ${err}`) + } else { + db.close() + numberofReports = result.length + } + }) + } + }) + } + return numberofReports +} -module.exports = { saveRecord } +module.exports = { saveRecord, getReportCount }