ContactManager.js 516 B

123456789101112131415161718192021222324
  1. import { db, ObjectId } from './mongodb.js'
  2. export async function touchContact(userId, contactId) {
  3. await db.contacts.updateOne(
  4. { user_id: ObjectId(userId.toString()) },
  5. {
  6. $inc: {
  7. [`contacts.${contactId}.n`]: 1,
  8. },
  9. $set: {
  10. [`contacts.${contactId}.ts`]: new Date(),
  11. },
  12. },
  13. { upsert: true }
  14. )
  15. }
  16. export async function getContacts(userId) {
  17. const user = await db.contacts.findOne({
  18. user_id: ObjectId(userId.toString()),
  19. })
  20. return user?.contacts
  21. }