Skip to content
Snippets Groups Projects
Commit 867e5746 authored by Louis's avatar Louis :fire:
Browse files

Extract device info and attach it to recorded metrics

parent d7df7358
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,7 @@ const session = require('koa-session')
const routers = require('http/routes')
const handlebars = require('./vendor/koa-handlebars')
const { fs, config } = require('bootstrap')
const { extractDevice } = require('./http/middleware/deviceProperties')
const app = new Koa()
......@@ -17,6 +18,7 @@ app.use(session({
}, app))
app.use(require('./http/middleware/injection'))
app.use(handlebars(fs.path('views')))
app.use(extractDevice)
Object.values(routers).forEach(router => {
app.use(router.routes())
......
......@@ -73,6 +73,10 @@ module.exports = (sequelize, DataTypes) => {
return Object.keys(metricConversionMap)
}
Model.findUnassociatedForDevice = function findUnassociatedMetrics(deviceId) {
return Model.findAll({ where: { author_id: null, meta: { device: { id: deviceId } } } })
}
Model.prototype.toJSON = function userToJSON() {
const author = this.author ? { author: this.author } : {}
......
exports.register = async ctx => {
const { email, name, password, date_of_birth, age_bracket } = ctx.request.body
}
\ No newline at end of file
......@@ -21,7 +21,11 @@ exports.postMetric = async ctx => {
if (allowedTypes.has(type)) {
const point = { type: 'Point', coordinates: [location.longitude, location.latitude] }
const metric = await Metric.create({ value, type, location: point, author_id: user ? user.id : null })
const payload = { value, type, location: point, author_id: user ? user.id : null }
if (ctx.request.device) {
payload.meta = { device: ctx.request.device }
}
const metric = await Metric.create(payload)
ctx.body = { metric: metric.toJSON() }
} else {
throw new HttpError({ status: 400, code: 'MTR-002', title: 'Invalid Metric', description: `${ type } is not a supported type`})
......
exports.extractDevice = async (ctx, next) => {
const deviceId = ctx.get('X-Request-Device')
const platform = ctx.get('X-Request-Platform')
const rawSlug = ctx.get('X-Request-Slug')
const slug = Buffer.from(rawSlug, 'base64').toString('utf-8')
ctx.request.device = {
id: deviceId,
platform,
info: JSON.parse(slug || 'null'),
}
return await next()
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment