Skip to content

Commit

Permalink
Merge pull request #18 from Turistforeningen/feat/list-log
Browse files Browse the repository at this point in the history
Implement /lister/{id}/logg API endpoint
  • Loading branch information
Starefossen authored Aug 29, 2016
2 parents f61f26c + fff5c52 commit 808a35b
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 10 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,28 @@ Content-Type: application/json
"data": {...}
}
```

### GET /v2/lister/{liste}/logg

**Status codes:**

Returns `200 Ok` on successfull request.

**Example:**

```http
GET /v2/lister/57974036b565590001a98884/logg HTTP/1.1
Accept: application/json
HTTP/1.1 Ok
Content-Type: application/json
{
"data": [
{...},
{...}
]
}
```

## [MIT lisenced](https://github.com/Turistforeningen/Verdandi/blob/master/LICENSE)
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ router.get('/', (req, res) => {
profile_view: `${req.fullUrl}/brukere/{bruker}`,
list_join: `${req.fullUrl}/lister/{liste}/blimed`,
list_leave: `${req.fullUrl}/lister/{liste}/meldav`,
list_log: `${req.fullUrl}/lister/{liste}/logg`,
});
});

Expand Down Expand Up @@ -153,7 +154,14 @@ router.get('/steder/:sted/besok/:checkin', (req, res, next) => {
});

router.get('/lister/:liste/stats', notImplementedYet);
router.get('/lister/:liste/logg', notImplementedYet);

router.get('/lister/:liste/logg', (req, res) => {
Checkin.getCheckinsForList(req.params.liste)
.then(checkins => {
res.json({ data: checkins });
});
});

router.post('/lister/:liste/blimed', requireAuth, (req, res) => {
const user = req.user;
user.lister.push(req.params.liste);
Expand Down
26 changes: 25 additions & 1 deletion models/Checkin.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict';

const { Schema, Types: { ObjectId: objectId } } = require('../lib/db');
const mongoose = require('../lib/db');

const Schema = mongoose.Schema;
const fetch = require('node-fetch');
const HttpError = require('@starefossen/http-error');

const checkinSchema = new Schema({
timestamp: Date,
Expand All @@ -18,6 +20,28 @@ const checkinSchema = new Schema({
dnt_user_id: { type: Number, ref: 'User' },
});

checkinSchema.statics.getCheckinsForList = function getCheckinsForList(list) {
const env = process.env.NTB_API_ENV || 'api';
const key = process.env.NTB_API_KEY;

const headers = {
Authorization: `Token ${key}`,
};

return fetch(`https://${env}.nasjonalturbase.no/lister/${list}`, { headers })
.then(res => {
if (res.status !== 200) {
throw new HttpError(`Status Code ${res.status}`, res.status);
} else {
return res;
}
})
.then(res => res.json())
.then(liste => liste.steder || [])
.then(steder => steder.map(id => objectId(id)))
.then(steder => this.find().where('ntb_steder_id').in(steder));
};

checkinSchema.index({ location: '2dsphere' });

// Fix for https://github.com/Automattic/mongoose/issues/1251
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"greenkeeper-postpublish": "^1.0.0",
"istanbul": "^0.4.3",
"mocha": "^3.0.0",
"mockery": "^1.7.0",
"nsp": "^2.4.0",
"semantic-release": "^4.3.5",
"supertest": "^2.0.0",
Expand Down
10 changes: 5 additions & 5 deletions test/acceptance/checkin.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ describe('GET /steder/:sted/besok/:id', () => {
coordinates: [-117.220406, 32.719464],
type: 'Point',
},
ntb_steder_id: '300000000000000000000000',
ntb_steder_id: '400000000000000000000000',
timestamp: '2016-07-07T23:32:49.923Z',
},
})
));
});

describe('GET /steder/:sted/stats', () => {
const url = '/api/dev/steder/300000000000000000000001/stats';
const url = '/api/dev/steder/400000000000000000000001/stats';

it('returns checkin statistics for a given place', () => (
app.get(url)
Expand All @@ -135,7 +135,7 @@ describe('GET /steder/:sted/stats', () => {
});

describe('GET /steder/:sted/logg', () => {
const url = '/api/dev/steder/300000000000000000000001/logg';
const url = '/api/dev/steder/400000000000000000000001/logg';

it('returns the most recent checkins', () => (
app.get(url)
Expand All @@ -145,13 +145,13 @@ describe('GET /steder/:sted/logg', () => {
_id: '200000000000000000000001',
timestamp: '2016-07-07T23:32:50.923Z',
location: { type: 'Point', coordinates: [-117.220406, 32.719464] },
ntb_steder_id: '300000000000000000000001',
ntb_steder_id: '400000000000000000000001',
dnt_user_id: 1234,
}, {
_id: '200000000000000000000002',
timestamp: '2016-07-06T23:32:58.923Z',
location: { type: 'Point', coordinates: [-117.220406, 32.719464] },
ntb_steder_id: '300000000000000000000001',
ntb_steder_id: '400000000000000000000001',
dnt_user_id: 5678,
}] });
})
Expand Down
35 changes: 35 additions & 0 deletions test/acceptance/lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const assert = require('assert');
const request = require('supertest');
const mockery = require('mockery');
const app = request(require('../../index'));
const auth = require('../../lib/auth');

Expand Down Expand Up @@ -60,4 +61,38 @@ describe('POST /lister/:liste/*', () => {
});
});
});

describe.only('GET /lister/:liste/logg', () => {
before(() => mockery.enable({
useCleanCache: true,
warnOnReplace: false,
warnOnUnregistered: false,
}));

afterEach(() => mockery.deregisterMock('node-fetch'));

after(() => mockery.disable());

it('returns a log of checkins to a place in a list', done => {
// Mock node-fetch
mockery.registerMock('node-fetch', () => Promise.resolve({
status: 200,
json: () => ({
steder: ['400000000000000000000001'],
}),
}));

// Require new app to apply mocked data
const appMocked = request(require('../../index')); // eslint-disable-line global-require

appMocked.get(`${url}/logg`)
.set('X-User-Id', '1234')
.set('X-User-Token', 'abc123')
.expect(200)
.end((req, res) => {
assert.equal(res.body.data.length, 2);
done();
});
});
});
});
6 changes: 3 additions & 3 deletions test/fixtures/checkins.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ module.exports = [{
_id: objectId('200000000000000000000000'),
timestamp: new Date('2016-07-07T23:32:49.923Z'),
location: { type: 'Point', coordinates: [-117.220406, 32.719464] },
ntb_steder_id: objectId('300000000000000000000000'),
ntb_steder_id: objectId('400000000000000000000000'),
dnt_user_id: 1234,
}, {
_id: objectId('200000000000000000000001'),
timestamp: new Date('2016-07-07T23:32:50.923Z'),
location: { type: 'Point', coordinates: [-117.220406, 32.719464] },
ntb_steder_id: objectId('300000000000000000000001'),
ntb_steder_id: objectId('400000000000000000000001'),
dnt_user_id: 1234,
}, {
_id: objectId('200000000000000000000002'),
timestamp: new Date('2016-07-06T23:32:58.923Z'),
location: { type: 'Point', coordinates: [-117.220406, 32.719464] },
ntb_steder_id: objectId('300000000000000000000001'),
ntb_steder_id: objectId('400000000000000000000001'),
dnt_user_id: 5678,
}];
5 changes: 5 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ before(function before(done) {
}
});

after(() => {
mongo.models = {};
mongo.modelSchemas = {};
});

// mongodb clean
beforeEach(() => mongo.connection.db.dropDatabase());

Expand Down
37 changes: 37 additions & 0 deletions test/models/Checkin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* eslint import/no-extraneous-dependencies: ["error", {"devDependencies": true}] */
'use strict';

const assert = require('assert');
const mockery = require('mockery');

describe('Checkin', () => {
before(() => mockery.enable({
useCleanCache: true,
warnOnReplace: false,
warnOnUnregistered: false,
}));

afterEach(() => mockery.deregisterMock('node-fetch'));

after(() => mockery.disable());

describe('#getCheckinsForList()', () => {
it('it returns existing checkins for existing list', () => {
// Mock node-fetch
mockery.registerMock('node-fetch', () => Promise.resolve({
status: 200,
json: () => ({
steder: ['400000000000000000000000', '400000000000000000000001'],
}),
}));

// Require Checkin (it now uses the mock above)
const Checkin = require('../../models/Checkin'); // eslint-disable-line global-require

return Checkin.getCheckinsForList('300000000000000000000000')
.then(checkins => {
assert.equal(checkins.length, 3);
});
});
});
});
3 changes: 3 additions & 0 deletions test/support/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
process.env.NODE_ENV = 'test';
process.env.MONGO_DB = 'test';
process.env.VIRTUAL_PATH = '/api/dev';

process.env.NTB_API_ENV = 'dev';
process.env.NTB_API_KEY = 'fake';

0 comments on commit 808a35b

Please sign in to comment.