clean unused places/tags #107 and unused fields
This commit is contained in:
@@ -25,23 +25,22 @@ const eventController = {
|
||||
|
||||
async _getMeta () {
|
||||
const places = await Place.findAll({
|
||||
where: { confirmed: true },
|
||||
order: [[Sequelize.literal('weigth'), 'DESC']],
|
||||
attributes: {
|
||||
include: [[Sequelize.fn('count', Sequelize.col('events.placeId')), 'weigth']],
|
||||
exclude: ['createdAt', 'updatedAt']
|
||||
},
|
||||
include: [{ model: Event, attributes: [] }],
|
||||
include: [{ model: Event, where: { is_visible: true }, required: true, attributes: [] }],
|
||||
group: ['place.id']
|
||||
})
|
||||
|
||||
const tags = await Tag.findAll({
|
||||
where: { confirmed: true },
|
||||
raw: true,
|
||||
order: [['weigth', 'DESC']],
|
||||
order: [[Sequelize.literal('w'), 'DESC']],
|
||||
attributes: {
|
||||
exclude: ['createdAt', 'updatedAt']
|
||||
}
|
||||
include: [[Sequelize.fn('COUNT', Sequelize.col('tag.tag')), 'w']]
|
||||
},
|
||||
include: [{ model: Event, where: { is_visible: true }, attributes: [], through: { attributes: [] }, required: true }],
|
||||
group: ['tag.tag']
|
||||
})
|
||||
|
||||
return { places, tags }
|
||||
@@ -178,14 +177,6 @@ const eventController = {
|
||||
try {
|
||||
event.is_visible = true
|
||||
|
||||
// confirm tag & place if needed
|
||||
if (!event.place.confirmed) {
|
||||
await event.place.update({ confirmed: true })
|
||||
}
|
||||
|
||||
await Tag.update({ confirmed: true },
|
||||
{ where: { confirmed: false, tag: { [Op.in]: event.tags.map(t => t.tag) } } })
|
||||
|
||||
await event.save()
|
||||
|
||||
res.sendStatus(200)
|
||||
@@ -295,8 +286,7 @@ const eventController = {
|
||||
const [place] = await Place.findOrCreate({
|
||||
where: { name: body.place_name },
|
||||
defaults: {
|
||||
address: body.place_address,
|
||||
confirmed: !!req.user
|
||||
address: body.place_address
|
||||
}
|
||||
})
|
||||
|
||||
@@ -305,9 +295,8 @@ const eventController = {
|
||||
|
||||
// create/assign tags
|
||||
if (body.tags) {
|
||||
await Tag.bulkCreate(body.tags.map(t => ({ tag: t, confirmed: !!req.user })), { ignoreDuplicates: true })
|
||||
await Tag.bulkCreate(body.tags.map(t => ({ tag: t })), { ignoreDuplicates: true })
|
||||
const tags = await Tag.findAll({ where: { tag: { [Op.in]: body.tags } } })
|
||||
await Promise.all(tags.map(t => t.update({ weigth: Number(t.weigth) + 1, confirmed: true })))
|
||||
await event.addTags(tags)
|
||||
event.tags = tags
|
||||
}
|
||||
@@ -456,14 +445,19 @@ const eventController = {
|
||||
const events = await Event.findAll({
|
||||
where,
|
||||
attributes: {
|
||||
exclude: ['slug', 'likes', 'boost', 'userId', 'is_visible', 'createdAt', 'updatedAt', 'placeId', 'description', 'resources']
|
||||
// include: [[Sequelize.fn('COUNT', Sequelize.col('activitypub_id')), 'ressources']]
|
||||
exclude: ['slug', 'likes', 'boost', 'userId', 'is_visible', 'createdAt', 'updatedAt', 'description', 'resources']
|
||||
},
|
||||
order: ['start_datetime', [Tag, 'weigth', 'DESC']],
|
||||
order: ['start_datetime', Sequelize.literal('(SELECT COUNT("tagTag") FROM event_tags WHERE "tagTag" = tag) DESC')],
|
||||
include: [
|
||||
{ model: Resource, required: false, attributes: ['id'] },
|
||||
{ model: Tag, attributes: ['tag'], required: !!tags, ...where_tags, through: { attributes: [] } },
|
||||
{ model: Place, required: false, attributes: ['id', 'name', 'address'] }
|
||||
{
|
||||
model: Tag,
|
||||
attributes: ['tag'],
|
||||
required: !!tags,
|
||||
...where_tags,
|
||||
through: { attributes: [] }
|
||||
},
|
||||
{ model: Place, required: true, attributes: ['id', 'name', 'address'] }
|
||||
]
|
||||
}).catch(e => {
|
||||
log.error(e)
|
||||
|
||||
@@ -51,6 +51,7 @@ Event.belongsTo(User)
|
||||
User.hasMany(Event)
|
||||
|
||||
Event.belongsToMany(Tag, { through: 'event_tags' })
|
||||
Tag.belongsToMany(Event, { through: 'event_tags' })
|
||||
|
||||
Event.belongsToMany(Notification, { through: EventNotification })
|
||||
Notification.belongsToMany(Event, { through: EventNotification })
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
const { Model, DataTypes } = require('sequelize')
|
||||
const sequelize = require('./index')
|
||||
|
||||
// const Event = require('./event')
|
||||
class Place extends Model {}
|
||||
|
||||
Place.init({
|
||||
@@ -11,14 +10,7 @@ Place.init({
|
||||
index: true,
|
||||
allowNull: false
|
||||
},
|
||||
address: DataTypes.STRING,
|
||||
confirmed: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
defaultValue: true,
|
||||
allowNull: false
|
||||
}
|
||||
address: DataTypes.STRING
|
||||
}, { sequelize, modelName: 'place' })
|
||||
|
||||
// Place.hasMany(Event)
|
||||
|
||||
module.exports = Place
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
const { Model, DataTypes } = require('sequelize')
|
||||
// const Event = require('./event')
|
||||
const sequelize = require('./index')
|
||||
|
||||
class Tag extends Model {}
|
||||
@@ -10,19 +9,7 @@ Tag.init({
|
||||
allowNull: false,
|
||||
index: true,
|
||||
primaryKey: true
|
||||
},
|
||||
weigth: {
|
||||
type: DataTypes.INTEGER,
|
||||
defaultValue: 0,
|
||||
allowNull: false
|
||||
},
|
||||
confirmed: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
defaultValue: true,
|
||||
allowNull: false
|
||||
}
|
||||
}, { sequelize, modelName: 'tag' })
|
||||
|
||||
// Tag.belongsToMany(Event, { through: 'event_tags' })
|
||||
|
||||
module.exports = Tag
|
||||
|
||||
Reference in New Issue
Block a user