Skip to content

Commit

Permalink
Merge pull request #12 from Turistforeningen/fix/user-checkin-refs
Browse files Browse the repository at this point in the history
Save checkin ref to user profile
  • Loading branch information
Starefossen authored Aug 25, 2016
2 parents 8ad01fd + b040c2a commit b71b6e4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
13 changes: 8 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ router.get('/steder/:sted/logg', (req, res, next) => {
});

router.post('/steder/:sted/besok', requireAuth, (req, res, next) => {
const checkin = new Checkin({
const promise = Checkin.create({
timestamp: new Date(),
location: {
type: 'Point',
Expand All @@ -98,11 +98,14 @@ router.post('/steder/:sted/besok', requireAuth, (req, res, next) => {
dnt_user_id: req.user.id,
});

const promise = checkin.save();

// @TODO add checkin to user profile
// Save new checkin to user profile
promise.then(checkin => {
req.user.innsjekkinger.push(checkin);
req.user.save();
});

promise.then(() => {
// Return new checkin object
promise.then(checkin => {
res.set('Location', `${req.fullUrl}${req.url}/${checkin._id}`);
res.json({
message: 'Ok',
Expand Down
28 changes: 27 additions & 1 deletion test/acceptance/checkin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ const request = require('supertest');
const app = request(require('../../index'));
const auth = require('../../lib/auth');

const User = require('../../models/User');
const users = require('../fixtures/dnt-users');

const getUserData = auth.getUserData;

describe('POST /steder/:sted/besok', () => {
before(() => {
auth.getUserData = () => Promise.resolve({ sherpa_id: 1234 });
auth.getUserData = () => Promise.resolve(users[1]);
});

after(() => {
Expand Down Expand Up @@ -64,6 +67,29 @@ describe('POST /steder/:sted/besok', () => {
});
})
));

it('saves reference to new checkin to user profile', done => {
app.post(url)
.set('X-User-Id', '1234')
.set('X-User-Token', 'abc123')
.send({ lon: -117.220406, lat: 32.719464 })
.end((err, res) => {
assert.ifError(err);

User.findOne({ _id: 1234 }).then(user => process.nextTick(() => {
// Convert checkin ObjectIDs to Strings
const innsjekkinger = user.innsjekkinger.map(i => i.toString());

assert.deepEqual(innsjekkinger, [
'200000000000000000000000',
'200000000000000000000001',
res.body.data._id,
]);

done();
}));
});
});
});

describe('GET /steder/:sted/besok/:id', () => {
Expand Down

0 comments on commit b71b6e4

Please sign in to comment.