Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: migrate fetch form submission counts flow to Typescript #592

Merged
merged 17 commits into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 0 additions & 53 deletions src/app/controllers/submissions.server.controller.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
'use strict'

const axios = require('axios')

const mongoose = require('mongoose')
const errorHandler = require('../utils/handle-mongo-error')
const getSubmissionModel = require('../models/submission.server.model').default
const Submission = getSubmissionModel(mongoose)

const { StatusCodes } = require('http-status-codes')

const { createReqMeta } = require('../utils/request')
const { isMalformedDate, createQueryWithDateParam } = require('../utils/date')
const logger = require('../../config/logger').createLoggerWithLabel(module)
const MailService = require('../services/mail/mail.service').default

Expand Down Expand Up @@ -190,52 +183,6 @@ const sendEmailAutoReplies = async function (req) {
}
}

/**
* Count number of form submissions for Results tab
* @param {Object} req - Express request object
* @param {String} [req.query.startDate] Optional date query parameter in YYYY-MM-DD format. Assumes GMT+8.
* @param {String} [req.query.endDate] Optional date query parameter in YYYY-MM-DD format. Assumes GMT+8.
* @param {Object} res - Express response object
*/
exports.count = function (req, res) {
let query = { form: req.form._id }

if (
isMalformedDate(req.query.startDate) ||
isMalformedDate(req.query.endDate)
) {
return res.status(StatusCodes.BAD_REQUEST).json({
message: 'Malformed date parameter',
})
}

const augmentedQuery = createQueryWithDateParam(
req.query.startDate,
req.query.endDate,
)

query = { ...query, ...augmentedQuery }

Submission.countDocuments(query, function (err, count) {
if (err) {
logger.error({
message: 'Error counting submission documents from database',
meta: {
action: 'count',
...createReqMeta(req),
},
error: err,
})

return res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({
message: errorHandler.getMongoErrorMessage(err),
})
} else {
return res.json(count)
}
})
}

exports.sendAutoReply = function (req, res) {
const { form, submission } = req
// Return the reply early to the submitter
Expand Down
7 changes: 7 additions & 0 deletions src/app/modules/auth/auth.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,10 @@ export const isUserInSession = (
): session is Express.AuthedSession => {
return !!session?.user?._id
}

// TODO(#212): Save userId instead of entire user collection in session.
export const getUserIdFromSession = (
session?: Express.Session,
): string | undefined => {
return session?.user?._id as string | undefined
}
5 changes: 5 additions & 0 deletions src/app/modules/core/core.errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ export class DatabaseError extends ApplicationError {
super(message)
}
}
export class MalformedParametersError extends ApplicationError {
constructor(message: string) {
super(message)
}
}
Loading