remove_email.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. const { ObjectId, waitForDb } = require('../app/src/infrastructure/mongodb')
  2. const UserUpdater = require('../app/src/Features/User/UserUpdater')
  3. const UserGetter = require('../app/src/Features/User/UserGetter')
  4. waitForDb()
  5. .then(removeEmail)
  6. .catch(error => {
  7. console.error(error)
  8. process.exit(1)
  9. })
  10. .then(() => {
  11. console.log('Done.')
  12. process.exit()
  13. })
  14. async function removeEmail() {
  15. const userId = process.argv[2]
  16. let email = process.argv[3]
  17. if (!ObjectId.isValid(userId)) {
  18. throw new Error(`user ID ${userId} is not valid`)
  19. }
  20. if (!email) {
  21. throw new Error('no email provided')
  22. }
  23. // email arg can be within double quotes for arg so that we can handle
  24. // malformed emails with spaces
  25. email = email.replace(/"/g, '')
  26. console.log(
  27. `\nBegin request to remove email "${email}" from user "${userId}"\n`
  28. )
  29. const userWithEmail = await UserGetter.promises.getUserByAnyEmail(email, {
  30. _id: 1,
  31. })
  32. if (!userWithEmail) {
  33. throw new Error(`no user found with email "${email}"`)
  34. }
  35. if (userWithEmail._id.toString() !== userId) {
  36. throw new Error(
  37. `email does not belong to user. Belongs to ${userWithEmail._id}`
  38. )
  39. }
  40. const auditLog = {
  41. initiatorId: undefined,
  42. ipAddress: '0.0.0.0',
  43. extraInfo: {
  44. script: true,
  45. },
  46. }
  47. const skipParseEmail = true
  48. await UserUpdater.promises.removeEmailAddress(
  49. userId,
  50. email,
  51. auditLog,
  52. skipParseEmail
  53. )
  54. }