delete_old_writefull_refresh_tokens.mjs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import minimist from 'minimist'
  2. import { scriptRunner } from './lib/ScriptRunner.mjs'
  3. import { db } from '../app/src/infrastructure/mongodb.mjs'
  4. const argv = minimist(process.argv.slice(2))
  5. async function main() {
  6. const { expirationDate, commit } = argv
  7. const cutOffDate = new Date(expirationDate)
  8. const { _id: writefullId } = await db.oauthApplications.findOne({
  9. id: 'writefull',
  10. })
  11. const count = await db.oauthAccessTokens.countDocuments(
  12. {
  13. accessTokenExpiresAt: { $lte: cutOffDate },
  14. oauthApplication_id: writefullId,
  15. },
  16. { readPreference: 'secondaryPreferred' }
  17. )
  18. console.log(`${count} access tokens expired before ${cutOffDate}`)
  19. if (commit) {
  20. await db.oauthAccessTokens.deleteMany({
  21. oauthApplication_id: writefullId,
  22. accessTokenExpiresAt: { $lte: cutOffDate },
  23. })
  24. } else {
  25. console.log('use --commit to delete expired tokens')
  26. }
  27. }
  28. try {
  29. await scriptRunner(main)
  30. console.log('Done.')
  31. process.exit(0)
  32. } catch (error) {
  33. console.error({ error })
  34. process.exit(1)
  35. }