Skip to content

Commit

Permalink
Merge pull request #5 from fabrix-app/v1.1
Browse files Browse the repository at this point in the history
[feat] analytics route starter
  • Loading branch information
scott-wyatt authored Aug 29, 2018
2 parents c57f0ad + 9839a34 commit 64cd3db
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 9 deletions.
48 changes: 44 additions & 4 deletions lib/api/controllers/AnalyticsController.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,57 @@
import { FabrixController as Controller } from '@fabrix/fabrix/dist/common'

import * as moment from 'moment'
/**
* @module AnalyticsController
* @description Fabrix Controller.
*/
export class AnalyticsController extends Controller {
create(req, res) {
//
// TODO
res.json({})
}

findOne(req, res) {
//
res.json({})
const Analytics = this.app.models['Analytic']
const name = req.params.name

Analytics.findOne({
where: {
name: name
}
})
.then(analytic => {
return res.json(analytic)
})
.catch(err => {
return res.serverError(err)
})
}

// TODO
findGroup(req, res) {
const Analytics = this.app.models['Analytic']
const start = moment(req.query.start)
const end = moment(req.query.end || Date.now())
const name = req.params.name

Analytics.findAll({
where: {
name: name,
start: {
$gte: start.format('YYYY-MM-DD HH:mm:ss')
},
end: {
$gte: end.format('YYYY-MM-DD HH:mm:ss')
}
}
})
.then(analytic => {
return res.json(analytic)
})
.catch(err => {
return res.serverError(err)
})

}
findAll(req, res) {
const Analytics = this.app.models['Analytic']
Expand Down
9 changes: 9 additions & 0 deletions lib/api/services/AnalyticsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,14 @@ export class AnalyticsService extends Service {
return
})
}

findOne(params) {

}

findGroup(params) {

}

}

27 changes: 24 additions & 3 deletions lib/config/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,39 @@ export const routes = {
}
}
},
'/analytic/:id': {
'/analytic/:name': {
'GET': 'AnalyticsController.findOne',
config: {
prefix: 'analytics.prefix',
validate: {
params: {
id: joi.string().required()
name: joi.string().required()
}
},
app: {
permissions: {
resource_name: 'apiGetAnalyticIdRoute',
resource_name: 'apiGetAnalyticNameRoute',
roles: ['admin']
}
}
}
},
'/analytic/:name/group': {
'GET': 'AnalyticsController.findGroup',
config: {
prefix: 'analytics.prefix',
validate: {
params: {
name: joi.string().required()
},
query: {
start: joi.number(),
end: joi.number()
}
},
app: {
permissions: {
resource_name: 'apiGetAnalyticNameRoute',
roles: ['admin']
}
}
Expand Down
89 changes: 88 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fabrix/spool-analytics",
"version": "1.1.3",
"version": "1.1.4",
"description": "Spool: Analytics for Fabrix",
"homepage": "https://fabrix.app",
"author": {
Expand Down Expand Up @@ -60,6 +60,7 @@
"nyc": "^12.0.2",
"pg": "^6.4.2",
"smokesignals": "^3.0.4",
"supertest": "^3.1.0",
"tslib": "~1.9.0",
"tslint": "~5.11.0",
"tslint-microsoft-contrib": "~5.1.0",
Expand Down
44 changes: 44 additions & 0 deletions test/intergration/controllers/admin/AnalyticsController.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict'
/* global describe, it */
const assert = require('assert')
const supertest = require('supertest')

describe('AnalyticsController', () => {
let adminUser
let prefix

before((done) => {
adminUser = supertest.agent(global.app.spools.express.server)
prefix = global.app.config.get('analytics.prefix')
done()
})

it('should exist', () => {
assert(global.app.api.controllers['AnalyticsController'])
assert(global.app.controllers['AnalyticsController'])
})
it('should find all analytics', (done) => {
adminUser
.get(prefix + '/analytics')
.expect(200)
.end((err, res) => {
done(err)
})
})
it('should find an analytic', (done) => {
adminUser
.get(prefix + '/analytic/testName')
.expect(200)
.end((err, res) => {
done(err)
})
})
it('should find an analytic group', (done) => {
adminUser
.get(prefix + '/analytic/testName/group')
.expect(200)
.end((err, res) => {
done(err)
})
})
})

0 comments on commit 64cd3db

Please sign in to comment.