unlink_third_party_id.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. const { waitForDb } = require('../app/src/infrastructure/mongodb')
  2. const minimist = require('minimist')
  3. const ThirdPartyIdentityManager = require('../app/src/Features/User/ThirdPartyIdentityManager')
  4. const UserGetter = require('../app/src/Features/User/UserGetter')
  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.js --providerId=google --userId=${SOME_USER_ID}
  17. * - commit:
  18. * node scripts/unlink_third_party_id.js --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. main()
  71. .then(() => {
  72. process.exit(0)
  73. })
  74. .catch(err => {
  75. console.error(err)
  76. process.exit(1)
  77. })