Skip to content

Commit

Permalink
Use directly the history id to find the machine image. (#430)
Browse files Browse the repository at this point in the history
If the machine image have change du to image creation, passing from machine
directly avoid imageId problem.
  • Loading branch information
corentindrouet authored and dynamiccast committed Oct 31, 2016
1 parent 891e19c commit 325e39b
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions api/controllers/HistoryController.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers
*/

/* global App, User, MachineService, Image, JsonApiService */
/* global App, User, MachineService, Image, JsonApiService, History, Machine */

const _= require('lodash');
const Promise = require('bluebird');
Expand Down Expand Up @@ -84,6 +84,7 @@ module.exports = {
update(req, res) {

req.body = JsonApiService.deserialize(req.body);
var historyId = req.allParams().id;

let userId = _.get(req.body, 'data.attributes.userId');
let connectionId = _.get(req.body, 'data.attributes.connectionId');
Expand All @@ -99,25 +100,24 @@ module.exports = {
throw new Error('Connection not found');
}

return History.findOne({
id: historyId
});
})
.then((history) => {

return Promise.props({
image: Image.findOne(app.image),
machine: Machine.findOne({ id: history.machineId }),
user: User.findOne(userId)
});
})
.then(({image, user}) => {
return MachineService.sessionEnded(user, image)
.then(({machine, user}) => {
return MachineService.sessionEnded(user, { id: machine.image })
.then(() => {
return MachineService.getMachineForUser({
id: userId
}, {
id: image.id
});
_.set(req.body, 'data.attributes.machineId', machine.id);
return JsonApiService.updateOneRecord(req, res);
});
})
.then((machine) => {
_.set(req.body, 'data.attributes.machineId', machine.id);
return JsonApiService.updateOneRecord(req, res);
})
.catch((err) => {
if (err.message === 'Connection not found') {
return res.notFound(err);
Expand Down

0 comments on commit 325e39b

Please # to comment.