summaryrefslogtreecommitdiff
path: root/bin/cleanup
blob: edf72bf20495a7008eea17905eec877401363ccc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env node

const logger = require('../lib/logger')
const models = require('../lib/models')

logger.info('Cleaning up notes that should already have been removed. Sorry.')

async function cleanup () {
  await models.Note.findAll({
    include: [{
      model: models.User,
      as: 'owner',
      attributes: ['id']
    }],
    attributes: ['id', 'ownerId']
  }).then(async function (notes) {
    for (let i = 0, noteCount = notes.length; i < noteCount; i++) {
      const item = notes[i]
      if (item.ownerId != null && !item.owner) {
        await models.Note.destroy({
          where: {
            id: item.id
          }
        })
        await models.Revision.destroy({
          where: {
            noteId: item.id
          }
        })
        await models.Author.destroy({
          where: {
            noteId: item.id
          }
        })
        logger.info(`Deleted note ${item.id} from user ${item.ownerId}`)
      }
    }
  })
  await models.Author.findAll({
    include: [{
      model: models.User,
      as: 'user',
      attributes: ['id']
    }],
    attributes: ['id', 'userId']
  }).then(async function (authors) {
    for (let i = 0, authorCount = authors.length; i < authorCount; i++) {
      const item = authors[i]
      if (item.userId != null && !item.user) {
        await models.Author.destroy({
          where: {
            id: item.id
          }
        })
        logger.info(`Deleted authorship ${item.id} from user ${item.userId}`)
      }
    }
  })

  await models.Author.findAll({
    include: [{
      model: models.Note,
      as: 'note',
      attributes: ['id']
    }],
    attributes: ['id', 'noteId']
  }).then(async function (authors) {
    for (let i = 0, authorCount = authors.length; i < authorCount; i++) {
      const item = authors[i]
      if (item.noteId != null && !item.note) {
        await models.Author.destroy({
          where: {
            id: item.id
          }
        })
        logger.info(`Deleted authorship ${item.id} from note ${item.noteId}`)
      }
    }
  })

  await models.Revision.findAll({
    include: [{
      model: models.Note,
      as: 'note',
      attributes: ['id']
    }],
    attributes: ['id', 'noteId']
  }).then(async function (revisions) {
    for (let i = 0, revisionCount = revisions.length; i < revisionCount; i++) {
      const item = revisions[i]
      if (item.noteId != null && !item.note) {
        await models.Revision.destroy({
          where: {
            id: item.id
          }
        })
        logger.info(`Deleted revision ${item.id} from note ${item.userId}`)
      }
    }
  })
  process.exit(0)
}

cleanup()