2023-01-10 18:16:11 +01:00
|
|
|
const { Event, Place } = require('../api/models/models')
|
2021-04-09 23:54:17 +02:00
|
|
|
const Sequelize = require('sequelize')
|
|
|
|
const log = require('../log')
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
// remove places not related to any events
|
|
|
|
async _cleanUnused () {
|
|
|
|
const places = await Place.findAll({
|
|
|
|
include: [{ model: Event, as: 'events', required: false, attributes: [] }],
|
|
|
|
group: ['place.id'],
|
|
|
|
having: Sequelize.where(Sequelize.fn('COUNT', Sequelize.col('events.id')), '=', 0)
|
|
|
|
})
|
|
|
|
if (!places.length) { return }
|
2024-01-08 00:02:07 +01:00
|
|
|
log.debug(`Remove ${places.length} unrelated places: %s`, places.map(p => p.name).join(', '))
|
2021-04-09 23:54:17 +02:00
|
|
|
|
|
|
|
const ids = places.map(p => p.id)
|
|
|
|
await Place.destroy({
|
|
|
|
where: { id: { [Sequelize.Op.in]: ids } }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|