overleaf/services/contacts/app/coffee/ContactManager.coffee
2015-10-06 17:22:11 +01:00

30 lines
No EOL
725 B
CoffeeScript

{db, ObjectId} = require "./mongojs"
module.exports = ContactManager =
touchContact: (user_id, contact_id, callback = (error) ->) ->
try
user_id = ObjectId(user_id.toString())
catch error
return callback error
update = { $set: {}, $inc: {} }
update.$inc["contacts.#{contact_id}.n"] = 1
update.$set["contacts.#{contact_id}.ts"] = new Date()
db.contacts.update({
user_id: user_id
}, update, {
upsert: true
}, callback)
getContacts: (user_id, callback = (error) ->) ->
try
user_id = ObjectId(user_id.toString())
catch error
return callback error
db.contacts.findOne {
user_id: user_id
}, (error, user) ->
return callback(error) if error?
callback null, user?.contacts