resolve-comment.mjs 1010 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { ObjectId } from 'mongodb'
  2. import { db } from '../app/js/mongodb.js'
  3. const OPTS = parseArgs()
  4. function parseArgs() {
  5. const args = process.argv.slice(2)
  6. if (args.length !== 3) {
  7. usage()
  8. process.exit(1)
  9. }
  10. const [roomId, userId, timestamp] = args
  11. return { roomId, userId, timestamp: new Date(timestamp) }
  12. }
  13. function usage() {
  14. console.error('Usage: resolve-comment.mjs ROOM_ID USER_ID TIMESTAMP')
  15. }
  16. async function resolveComment(roomId, userId, timestamp) {
  17. const result = await db.rooms.updateOne(
  18. { _id: new ObjectId(roomId) },
  19. {
  20. $set: {
  21. resolved: {
  22. user_id: userId, // this is a string in Mongo
  23. ts: timestamp,
  24. },
  25. },
  26. }
  27. )
  28. if (result.matchedCount === 0) {
  29. console.log(`Room not found: ${roomId}`)
  30. } else {
  31. console.log(`Comment resolved: room ${roomId}`)
  32. }
  33. }
  34. try {
  35. await resolveComment(OPTS.roomId, OPTS.userId, OPTS.timestamp)
  36. } catch (err) {
  37. console.error(err)
  38. process.exit(1)
  39. }
  40. process.exit(0)