Skip to content

Commit

Permalink
Merge pull request #5 from locator-kn/refactoring
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
timoweiss committed Feb 19, 2016
2 parents 1585a3a + e0d59d9 commit c215d11
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 31 deletions.
4 changes: 2 additions & 2 deletions lib/conversations.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ fns.getConversationById = (message, next) => {

db.findById(data.conversation_id, 'conversations')
.then(queryResult => {
if (!queryResult.length) {
if (!queryResult) {
throw new Error('not found');
}
next(null, queryResult[0]);
next(null, queryResult);
})
.catch(next);
});
Expand Down
52 changes: 25 additions & 27 deletions lib/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fns.findById = (id, collectionId) => {
return collection
.find({'_id': oId})
.limit(-1)
.toArray();
.next();
});
};

Expand Down Expand Up @@ -71,33 +71,31 @@ fns.findMessagesByConversationId = (conversationId, query) => {
};

fns.findLatestMessagesByDistinctConversation = (userId, query) => {
return new Promise((resolve, reject) => {

let collectionMessages = database.collection(c.MESSAGES);
let collectionConversations = database.collection(c.CONVERSATIONS);
collectionConversations
.find({'participants.user_id': userId})
.toArray((err, conversations) => {
if (err) {
return reject(err);
}
let promises = conversations.map(con => {
return collectionMessages.find({
'conversation_id': con._id.toString()
})
.limit(-1)
.sort({timestamp: -1})
.toArray();
});
Promise.all(promises).then(results => {
let merged = [].concat.apply([], results);
merged.sort((a, b) => {
return b.timestamp - a.timestamp;
});
resolve(merged.slice(0, query.count || 3));
}).catch(reject);

let collectionMessages = database.collection(c.MESSAGES);
let collectionConversations = database.collection(c.CONVERSATIONS);
return collectionConversations
.find({'participants.user_id': userId})
.toArray()
.then(conversations => {
return conversations.map(con => {
return collectionMessages.find({
'conversation_id': con._id.toString()
})
.limit(-1)
.sort({timestamp: -1})
.toArray();
});
});
})
.then(promises => Promise.all(promises))
.then(messages => messages.filter(message => message.length))
.then(results => {
let merged = [].concat.apply([], results);
merged.sort((a, b) => {
return b.timestamp - a.timestamp;
});
return merged.slice(0, query.count || 3);
});
};

fns.acknowledgeConversation = (conversationId, userId, lastRead) => {
Expand Down
4 changes: 2 additions & 2 deletions test/stubs/database.stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ fns.findConversationsByUser = (query) => {
fns.findById = (id) => {
if(conversationFixtures.getConversationByIdResponse._id === id) {

return Promise.resolve([conversationFixtures.getConversationByIdResponse]);
return Promise.resolve(conversationFixtures.getConversationByIdResponse);
} else {
return Promise.resolve([]);
return Promise.resolve(null);
}
};

Expand Down

0 comments on commit c215d11

Please sign in to comment.