unlink_third_party_id.mjs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { waitForDb } from '../app/src/infrastructure/mongodb.js'
  2. import minimist from 'minimist'
  3. import ThirdPartyIdentityManager from '../app/src/Features/User/ThirdPartyIdentityManager.js'
  4. import UserGetter from '../app/src/Features/User/UserGetter.js'
  5. /**
  6. * This script is used to remove a linked third party identity from a user account.
  7. *
  8. * Parameters:
  9. * --providerId: the third party identity provider (e.g. google, collabratec)
  10. * --userId: the id of the user
  11. * --commit: if present, the script will commit the changes to the database.
  12. *
  13. * Usage:
  14. *
  15. * - dry run:
  16. * node scripts/unlink_third_party_id.mjs --providerId=google --userId=${SOME_USER_ID}
  17. * - commit:
  18. * node scripts/unlink_third_party_id.mjs --providerId=google --userId=${SOME_USER_ID} --commit
  19. */
  20. let COMMIT = false
  21. let PROVIDER_ID
  22. let USER_ID
  23. const setup = () => {
  24. const argv = minimist(process.argv.slice(2))
  25. COMMIT = argv.commit !== undefined
  26. PROVIDER_ID = argv.providerId
  27. USER_ID = argv.userId
  28. if (!COMMIT) {
  29. console.warn('Doing dry run. Add --commit to commit changes')
  30. }
  31. }
  32. async function main() {
  33. if (!PROVIDER_ID) {
  34. throw new Error('No --providerId argument provided')
  35. }
  36. if (!USER_ID) {
  37. throw new Error('No --userId argument provided')
  38. }
  39. await waitForDb()
  40. const auditLog = {
  41. initiatorId: undefined,
  42. ipAddress: '0.0.0.0',
  43. extraInfo: {
  44. script: true,
  45. },
  46. }
  47. const user = await UserGetter.promises.getUser(USER_ID, {
  48. thirdPartyIdentifiers: 1,
  49. })
  50. console.log(
  51. `Existing thirdPartyIdentifiers: ${JSON.stringify(
  52. user.thirdPartyIdentifiers
  53. )}`
  54. )
  55. console.log(`Removing third party identifier for provider: ${PROVIDER_ID}`)
  56. if (COMMIT) {
  57. const updatedUser = await ThirdPartyIdentityManager.promises.unlink(
  58. USER_ID,
  59. PROVIDER_ID,
  60. auditLog
  61. )
  62. console.log(
  63. `Remaining thirdPartyIdentifiers: ${JSON.stringify(
  64. updatedUser.thirdPartyIdentifiers
  65. )}`
  66. )
  67. }
  68. }
  69. setup()
  70. try {
  71. await main()
  72. process.exit(0)
  73. } catch (error) {
  74. console.error(error)
  75. process.exit(1)
  76. }