202407131109055_admin_reversed_hostname.mjs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. const tags = ['server-ce', 'server-pro']
  2. const migrate = async client => {
  3. const { db } = client
  4. const adminsWithEmails = await db.users
  5. .find(
  6. {
  7. isAdmin: true,
  8. emails: { $exists: true },
  9. },
  10. { _id: 1, emails: 1 }
  11. )
  12. .toArray()
  13. for (const { _id, emails } of adminsWithEmails) {
  14. let shouldUpdateEmails = false
  15. for (const emailObj of emails) {
  16. if (!emailObj.reversedHostname) {
  17. shouldUpdateEmails = true
  18. emailObj.reversedHostname = emailObj.email
  19. .split('@')[1]
  20. .split('')
  21. .reverse()
  22. .join('')
  23. }
  24. }
  25. if (shouldUpdateEmails) {
  26. await db.users.updateOne({ _id }, { $set: { emails } })
  27. }
  28. }
  29. const adminsNoEmails = await db.users
  30. .find(
  31. {
  32. isAdmin: true,
  33. emails: { $exists: false },
  34. },
  35. { _id: 1, email: 1 }
  36. )
  37. .toArray()
  38. for (const { _id, email } of adminsNoEmails) {
  39. const reversedHostname = email.split('@')[1].split('').reverse().join('')
  40. await db.users.updateOne(
  41. { _id },
  42. {
  43. emails: [
  44. {
  45. email,
  46. reversedHostname,
  47. createdAt: new Date(),
  48. },
  49. ],
  50. }
  51. )
  52. }
  53. }
  54. const rollback = async () => {}
  55. export default {
  56. tags,
  57. migrate,
  58. rollback,
  59. }