Don't look up user details in this api

This commit is contained in:
James Allen 2015-10-07 14:07:54 +01:00
parent ededf6f6b9
commit b6106697a0
2 changed files with 12 additions and 43 deletions

View file

@ -1,7 +1,5 @@
ContactManager = require "./ContactManager"
WebApiManager = require "./WebApiManager"
logger = require "logger-sharelatex"
async = require "async"
module.exports = HttpController =
addContact: (req, res, next) ->
@ -45,17 +43,11 @@ module.exports = HttpController =
HttpController._sortContacts contacts
contacts = contacts.slice(0, limit)
contact_ids = contacts.map (contact) -> contact.user_id
async.mapLimit contacts, 5,
(contact, cb) ->
WebApiManager.getUserDetails contact.user_id, (error, user) ->
return cb(error) if error?
cb null, HttpController._formatUser user
(error, users) ->
return next(error) if error?
res.status(200).send({
contacts: users
})
res.status(200).send({
contact_ids: contact_ids
})
_sortContacts: (contacts) ->
contacts.sort (a, b) ->
@ -72,11 +64,3 @@ module.exports = HttpController =
return 1
else
return 0
_formatUser: (user) ->
return {
id: user._id
email: user.email
first_name: user.first_name
last_name: user.last_name
}

View file

@ -9,7 +9,6 @@ describe "HttpController", ->
beforeEach ->
@HttpController = SandboxedModule.require modulePath, requires:
"./ContactManager": @ContactManager = {}
"./WebApiManager": @WebApiManager = {}
"logger-sharelatex": @logger = { log: sinon.stub() }
@user_id = "mock-user-id"
@contact_id = "mock-contact-id"
@ -67,15 +66,7 @@ describe "HttpController", ->
"user-id-2": { n: 4, ts: new Date(now) }
"user-id-3": { n: 2, ts: new Date(now - 1000) }
}
@user_details = {
"user-id-1": { _id: "user-id-1", email: "joe@example.com", first_name: "Joe", last_name: "Example", extra: "foo" }
"user-id-2": { _id: "user-id-2", email: "jane@example.com", first_name: "Sarah", last_name: "Example", extra: "foo" }
"user-id-3": { _id: "user-id-3", email: "sam@example.com", first_name: "Sam", last_name: "Example", extra: "foo" }
}
@ContactManager.getContacts = sinon.stub().callsArgWith(1, null, @contacts)
@WebApiManager.getUserDetails = (user_id, callback = (error, user) ->) =>
callback null, @user_details[user_id]
sinon.spy @WebApiManager, "getUserDetails"
describe "normally", ->
beforeEach ->
@ -86,19 +77,13 @@ describe "HttpController", ->
.calledWith(@user_id)
.should.equal true
it "should look up each contact in web for their details", ->
for user_id, data of @contacts
@WebApiManager.getUserDetails
.calledWith(user_id)
.should.equal true
it "should return a sorted list of contacts by count and timestamp", ->
@res.send
.calledWith({
contacts: [
{ id: "user-id-2", email: "jane@example.com", first_name: "Sarah", last_name: "Example" }
{ id: "user-id-1", email: "joe@example.com", first_name: "Joe", last_name: "Example" }
{ id: "user-id-3", email: "sam@example.com", first_name: "Sam", last_name: "Example" }
contact_ids: [
"user-id-2"
"user-id-1"
"user-id-3"
]
})
.should.equal true
@ -112,9 +97,9 @@ describe "HttpController", ->
it "should return the most commonly used contacts up to the limit", ->
@res.send
.calledWith({
contacts: [
{ id: "user-id-2", email: "jane@example.com", first_name: "Sarah", last_name: "Example" }
{ id: "user-id-1", email: "joe@example.com", first_name: "Joe", last_name: "Example" }
contact_ids: [
"user-id-2"
"user-id-1"
]
})
.should.equal true
@ -127,7 +112,7 @@ describe "HttpController", ->
it "should return an empty list", ->
@res.send
.calledWith({
contacts: []
contact_ids: []
})
.should.equal true