forked from 0386861680/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevents.js
More file actions
89 lines (81 loc) · 2.87 KB
/
events.js
File metadata and controls
89 lines (81 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const express = require('express')
const Airtable = require('airtable')
const { omit } = require('lodash')
const Ajv = require('ajv')
const schema = require('../lib/schema-event')
const schemaHydro = require('../lib/schema-event-2')
const TABLE_NAMES = {
HELPFULNESS: 'Helpfulness Survey',
EXPERIMENT: 'Experiment Events'
}
const OMIT_FIELDS = ['type', 'token']
const ajv = new Ajv()
const router = express.Router()
async function airtablePost (req, res, next) {
const { AIRTABLE_API_KEY, AIRTABLE_BASE_KEY } = process.env
if (!AIRTABLE_API_KEY || !AIRTABLE_BASE_KEY) {
return res.status(501).send({})
}
if (!ajv.validate(schema, req.body)) {
if (process.env.NODE_ENV !== 'production') console.log(ajv.errorsText())
return res.status(400).send({})
}
const fields = omit(req.body, OMIT_FIELDS)
try {
const base = new Airtable({ apiKey: AIRTABLE_API_KEY })
.base(AIRTABLE_BASE_KEY)
// TODO stop the test at T+C > N or T-C > 2*sqrt(N)
const records = await base(TABLE_NAMES[req.body.type])
.create([{ fields }])
return res.status(201).send({ id: records[0].getId() })
} catch (err) {
console.error('unable to POST event', err)
return res.status(502).send({})
}
}
router.post('/', async (req, res, next) => {
// All-caps type is an "Airtable" event
if (req.body.type === 'HELPFULNESS' || req.body.type === 'EXPERIMENT') {
return airtablePost(req, res, next)
}
// Remove the condition above when we are no longer sending to Airtable
if (!ajv.validate(schemaHydro, req.body)) {
if (process.env.NODE_ENV === 'development') console.log(ajv.errorsText())
return res.status(400).json({})
}
const fields = omit(req.body, OMIT_FIELDS)
try {
const hydroRes = await req.hydro.publish(req.hydro.schemas[req.body.type], fields)
if (!hydroRes.ok) return res.status(502).json({})
return res.status(201).json(fields)
} catch (err) {
if (process.env.NODE_ENV === 'development') console.log(err)
return res.status(502).json({})
}
})
router.put('/:id', async (req, res, next) => {
const { AIRTABLE_API_KEY, AIRTABLE_BASE_KEY } = process.env
if (!AIRTABLE_API_KEY || !AIRTABLE_BASE_KEY) {
return res.status(501).send({})
}
// Most event types will not be update eligible
if (!['HELPFULNESS'].includes(req.body.type)) {
return res.status(400).send({})
}
if (!ajv.validate(schema, req.body)) {
if (process.env.NODE_ENV !== 'production') console.log(ajv.errorsText())
return res.status(400).send({})
}
const fields = omit(req.body, OMIT_FIELDS)
try {
const base = new Airtable({ apiKey: AIRTABLE_API_KEY })
.base(AIRTABLE_BASE_KEY)
await base(TABLE_NAMES[req.body.type])
.update([{ id: req.params.id, fields }])
return res.status(200).send({})
} catch (err) {
console.error('unable to PUT event', err)
return res.status(502).send({})
}
})
module.exports = router