2020-12-18 02:34:58 -08:00
|
|
|
import { Router } from 'express';
|
|
|
|
import SQL from 'sql-template-strings';
|
|
|
|
import sha1 from 'sha1';
|
|
|
|
import {ulid} from "ulid";
|
|
|
|
|
2021-02-01 02:17:26 -08:00
|
|
|
const getIp = req => {
|
|
|
|
try {
|
|
|
|
return req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.ips.join(',') || req.ip;
|
|
|
|
} catch {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 02:34:58 -08:00
|
|
|
const buildFingerprint = req => sha1(`
|
2021-02-01 02:17:26 -08:00
|
|
|
${getIp(req)}
|
2020-12-18 02:34:58 -08:00
|
|
|
${req.headers['user-agent']}
|
|
|
|
${req.headers['accept-language']}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const hasFinished = async req => {
|
|
|
|
if (req.user) {
|
|
|
|
const byUser = await req.db.get(SQL`
|
|
|
|
SELECT * FROM census
|
|
|
|
WHERE locale = ${req.config.locale}
|
|
|
|
AND edition = ${req.config.census.edition}
|
|
|
|
AND userId = ${req.user.id}
|
|
|
|
`);
|
2021-02-01 01:35:06 -08:00
|
|
|
return !!byUser;
|
2020-12-18 02:34:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
const fingerprint = buildFingerprint(req);
|
|
|
|
const byFingerprint = await req.db.get(SQL`
|
|
|
|
SELECT * FROM census
|
|
|
|
WHERE locale = ${req.config.locale}
|
|
|
|
AND edition = ${req.config.census.edition}
|
|
|
|
AND fingerprint = ${fingerprint}
|
2021-02-01 01:41:13 -08:00
|
|
|
AND userId IS NULL
|
2020-12-18 02:34:58 -08:00
|
|
|
`);
|
2021-02-01 01:41:13 -08:00
|
|
|
return !!byFingerprint;
|
2020-12-18 02:34:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
const router = Router();
|
|
|
|
|
|
|
|
router.get('/census/finished', async (req, res) => {
|
|
|
|
return res.json(await hasFinished(req));
|
|
|
|
});
|
|
|
|
|
|
|
|
router.post('/census/submit', async (req, res) => {
|
2021-02-01 02:02:57 -08:00
|
|
|
const suspicious = await hasFinished(req);
|
2020-12-18 02:34:58 -08:00
|
|
|
|
|
|
|
const id = ulid();
|
2021-02-01 02:02:57 -08:00
|
|
|
await req.db.get(SQL`INSERT INTO census (id, locale, edition, userId, fingerprint, answers, writins, ip, userAgent, acceptLanguage, suspicious) VALUES (
|
2020-12-18 02:34:58 -08:00
|
|
|
${id},
|
|
|
|
${req.config.locale},
|
|
|
|
${req.config.census.edition},
|
|
|
|
${req.user ? req.user.id : null},
|
|
|
|
${buildFingerprint(req)},
|
|
|
|
${req.body.answers},
|
2021-02-01 02:02:57 -08:00
|
|
|
${req.body.writins},
|
2021-02-01 02:17:26 -08:00
|
|
|
${getIp(req)},
|
2021-02-01 02:02:57 -08:00
|
|
|
${req.headers['user-agent']},
|
|
|
|
${req.headers['accept-language']},
|
|
|
|
${suspicious}
|
2020-12-18 02:34:58 -08:00
|
|
|
)`);
|
|
|
|
|
|
|
|
return res.json(id);
|
|
|
|
});
|
|
|
|
|
2020-12-18 08:41:01 -08:00
|
|
|
router.get('/census/count', async (req, res) => {
|
|
|
|
return res.json((await req.db.get(SQL`
|
|
|
|
SELECT COUNT(*) as c FROM census
|
|
|
|
WHERE locale = ${req.config.locale}
|
|
|
|
AND edition = ${req.config.census.edition}
|
|
|
|
`)).c);
|
|
|
|
});
|
|
|
|
|
2020-12-18 02:34:58 -08:00
|
|
|
export default router;
|