unlink_third_party_id.mjs 2.0 KB

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