Skip to content

Commit

Permalink
Properly handle call to home
Browse files Browse the repository at this point in the history
  • Loading branch information
Renaud8469 committed Aug 7, 2017
1 parent 27f9d98 commit 359a5b7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/client/http.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const requestAPI = function (route, method, reqBody, token) {
const reqConfig = {
uri: api.url + route,
method: method,
json: true,
json: true,
headers: {
Accept: 'application/vnd.hal+json'
}
Expand Down
34 changes: 17 additions & 17 deletions lib/dao/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@ const _ = require('lodash')
let actions = require('./actions.json')

function getAction (actionName) {
return _.find(actions, (o) => {return o.name === actionName})
return _.find(actions, (o) => { return o.name === actionName })
}

function updateActionUrls (body) {
for (let rel in body._links) {
let action = _.find(actions, (o) => {return o['api-transition'] === rel})
let action = _.find(actions, (o) => { return o['api-transition'] === rel })
if (action) {
let link = body._links[rel].href
action.url = link.substring(link.indexOf('api/'))
}
}
}

exports.pingApplication = function () {
const action = getAction("home")
exports.callHome = function () {
const action = getAction('home')
return client.requestAPI(action.url, action.method)
.then((body) => {
updateActionUrls(body)
})
}

exports.getPepiteFromSchool = function (schoolName) {
const action = getAction("search-school")
const action = getAction('search-school')
return client.requestAPI(action.url, action.method)
.then((body) => {
updateActionUrls(body)
Expand All @@ -44,7 +44,7 @@ exports.getPepiteFromSchool = function (schoolName) {
}

exports.getPepiteFromId = function (id) {
const action = getAction("get-pepite")
const action = getAction('get-pepite')
return client.requestAPI(action.url + id, action.method)
.then((body) => {
updateActionUrls(body)
Expand All @@ -56,34 +56,34 @@ exports.getPepiteFromId = function (id) {
}

exports.createApplication = function (application) {
const action = getAction("create-application")
const action = getAction('create-application')
return client.requestAPI(action.url, action.method, application)
.then((body) => {
updateActionUrls(body)
return body._id
})
}

exports.updateApplication = function (id, application) {
const action = getAction("update-application")
exports.updateApplication = function (application) {
const action = getAction('update-application')
return client.requestAPI(action.url, action.method, application)
.then((body) => {
updateActionUrls(body)
return true
})
}

exports.sendApplication = function (id) {
const action = getAction("send-application")
exports.sendApplication = function () {
const action = getAction('send-application')
return client.requestAPI(action.url, action.method)
.then((body) => {
updateActionUrls(body)
return body.status
})
}

exports.getApplicationStatus = function (id) {
const action = getAction("get-application-status")
exports.getApplicationStatus = function () {
const action = getAction('get-application-status')
return client.requestAPI(action.url, action.method)
.then((body) => {
updateActionUrls(body)
Expand All @@ -92,17 +92,17 @@ exports.getApplicationStatus = function (id) {
}

exports.getToken = function (email, pass) {
const action = getAction("get-token")
const action = getAction('get-token')
return client.requestAPI(action.url, action.method, { email: email, password: pass })
.then((body) => {
updateActionUrls(body)
return body.token
})
}

exports.answerApplication = function (id, answer, token) {
const action = getAction("answer-application")
return client.requestAPI(action.url + id, action.method, answer, token)
exports.answerApplication = function (answer, token) {
const action = getAction('answer-application')
return client.requestAPI(action.url, action.method, answer, token)
.then((body) => {
updateActionUrls(body)
return body.status
Expand Down
10 changes: 5 additions & 5 deletions lib/services/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ const homeActions = function (req, res) {
result = 'Hey ! Here are the results you want. <br>'

// Ping the application resource
result += '<br> Ping application resource : '
dao.pingApplication()
result += '<br> Call API root to check if it is working : '
dao.callHome()
.then((body) => {
result += body + '<br>'
result += 'Success ! <br>'
//
// Call the establishment resource to search for corresponding pepite
result += '<br> Get PEPITE for CentraleSupélec : '
Expand All @@ -35,7 +35,7 @@ const homeActions = function (req, res) {
//
// Complete the application
result += '<br> Complete the application : '
return dao.updateApplication(applicationId, fullApplication)
return dao.updateApplication(fullApplication)
}).then((success) => {
if (success) {
result += 'Success ! Application completed. Next step : send it.<br>'
Expand All @@ -45,7 +45,7 @@ const homeActions = function (req, res) {
//
// Send the application
result += '<br> Send the application to the PEPITE : '
return dao.sendApplication(applicationId)
return dao.sendApplication()
}).then((applicationStatus) => {
result += 'response received ! Your application status is now : ' + applicationStatus + ' .'
res.send(result)
Expand Down

0 comments on commit 359a5b7

Please sign in to comment.