From b040c2af0fbed446dc341d55c164b7a9135434d3 Mon Sep 17 00:00:00 2001 From: Hans Kristian Flaatten Date: Thu, 25 Aug 2016 11:43:04 +0200 Subject: [PATCH] fix(checkin): save checkin ref to user profile --- index.js | 13 ++++++++----- test/acceptance/checkin.js | 28 +++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 820639f..fdd6424 100644 --- a/index.js +++ b/index.js @@ -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', @@ -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', diff --git a/test/acceptance/checkin.js b/test/acceptance/checkin.js index 0711884..062abe8 100644 --- a/test/acceptance/checkin.js +++ b/test/acceptance/checkin.js @@ -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(() => { @@ -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', () => {