1
0
mirror of synced 2025-12-23 03:44:00 -05:00
Files
docs/middleware/events.js
Kevin Heis 26a8e61d3a Update survey to use React/TS (#19728)
* Update survey to use React/TS

* Working survey form in react land

* A little cleanup

* Update Search.tsx

* Update Survey.tsx

* Remove token field from schema entirely, just let it fail validation

* Update events.js

* Update survey.html

* use Link component

* Use enum for state

* Update old to match new
2021-06-08 17:34:45 +00:00

38 lines
939 B
JavaScript

const express = require('express')
const { omit } = require('lodash')
const Ajv = require('ajv')
const addFormats = require('ajv-formats')
const schema = require('../lib/schema-event')
const OMIT_FIELDS = ['type']
const ajv = new Ajv()
addFormats(ajv)
const router = express.Router()
router.post('/', async function postEvents (req, res, next) {
const isDev = process.env.NODE_ENV === 'development'
const fields = omit(req.body, '_csrf')
if (!ajv.validate(schema, fields)) {
if (isDev) console.log(ajv.errorsText())
return res.status(400).json({})
}
if (req.hydro.maySend()) {
// intentionally don't await this async request
// so that the http response afterwards is sent immediately
req.hydro.publish(
req.hydro.schemas[fields.type],
omit(fields, OMIT_FIELDS)
).catch((e) => {
if (isDev) console.error(e)
})
}
return res.status(200).json({})
})
module.exports = router