Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Reports a day counter #1664

Merged
merged 13 commits into from
Mar 17, 2020
17 changes: 11 additions & 6 deletions f2/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -47,11 +48,15 @@ const allowedOrigins = [
'http://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) {
Expand All @@ -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.log('Warning: redirecting request to CAFC')
res.redirect(
Expand Down Expand Up @@ -129,7 +135,6 @@ app
})

.post('/submit', (req, res) => {
availableData.numberOfSubmissions += 1
var form = new formidable.IncomingForm()
form.parse(req)
let files = []
Expand Down
7 changes: 0 additions & 7 deletions f2/src/utils/checkIfAvailable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
1 change: 1 addition & 0 deletions f2/src/utils/getData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
37 changes: 36 additions & 1 deletion f2/src/utils/saveRecord.js
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -44,5 +52,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 }