gancio-upstream/server/helpers/tag.js
2023-11-20 17:09:38 +01:00

22 lines
709 B
JavaScript

const { Event, Tag } = require('../api/models/models')
const Sequelize = require('sequelize')
const log = require('../log')
module.exports = {
// remove tags not related to any events
async _cleanUnused () {
const tags = await Tag.findAll({
include: [{ model: Event, as: 'events', required: false, attributes: [], through: { attributes: [] } }],
group: ['tag.tag'],
having: Sequelize.where(Sequelize.fn('COUNT', Sequelize.col('events.id')), '=', 0)
})
if (!tags.length) { return }
log.info(`Remove ${tags.length} orphan tags (${tags.map(p => p.tag).join(', ')})`)
await Tag.destroy({
where: { tag: { [Sequelize.Op.in]: tags.map(p => p.tag) } }
})
}
}