2019-04-03 00:25:12 +02:00
const crypto = require ( 'crypto' )
const moment = require ( 'moment' )
const { Op } = require ( 'sequelize' )
const lodash = require ( 'lodash' )
2019-06-06 23:54:32 +02:00
const { event : Event , comment : Comment , tag : Tag , place : Place , notification : Notification } = require ( '../models' )
2019-05-30 12:04:14 +02:00
const Sequelize = require ( 'sequelize' )
2019-04-03 00:25:12 +02:00
const eventController = {
async addComment ( req , res ) {
// comment could be added to an event or to another comment
let event = await Event . findOne ( { where : { activitypub _id : { [ Op . eq ] : req . body . id } } } )
if ( ! event ) {
const comment = await Comment . findOne ( { where : { activitypub _id : { [ Op . eq ] : req . body . id } } , include : Event } )
event = comment . event
}
const comment = new Comment ( req . body )
event . addComment ( comment )
res . json ( comment )
} ,
async getMeta ( req , res ) {
2019-05-30 12:04:14 +02:00
const places = await Place . findAll ( {
2019-05-30 12:12:51 +02:00
order : [ [ Sequelize . literal ( 'weigth' ) , 'DESC' ] ] ,
2019-05-30 12:04:14 +02:00
attributes : {
2019-06-26 14:44:21 +02:00
include : [ [ Sequelize . fn ( 'count' , Sequelize . col ( 'events.placeId' ) ) , 'weigth' ] ] ,
2019-05-30 12:12:51 +02:00
exclude : [ 'weigth' , 'createdAt' , 'updatedAt' ]
2019-05-30 12:04:14 +02:00
} ,
2019-05-30 12:12:51 +02:00
include : [ { model : Event , attributes : [ ] } ] ,
group : [ 'place.id' ]
2019-05-30 12:04:14 +02:00
} )
const tags = await Tag . findAll ( {
2019-06-07 17:02:33 +02:00
order : [ [ 'weigth' , 'DESC' ] ] ,
2019-05-30 12:04:14 +02:00
attributes : {
exclude : [ 'createdAt' , 'updatedAt' ]
2019-06-26 14:44:21 +02:00
} ,
2019-05-30 12:12:51 +02:00
} )
2019-05-30 12:04:14 +02:00
2019-04-03 00:25:12 +02:00
res . json ( { tags , places } )
} ,
async getNotifications ( event ) {
function match ( event , filters ) {
// matches if no filter specified
if ( ! filters ) return true
// check for visibility
if ( typeof filters . is _visible !== 'undefined' && filters . is _visible !== event . is _visible ) return false
if ( ! filters . tags && ! filters . places ) return true
if ( ! filters . tags . length && ! filters . places . length ) return true
if ( filters . tags . length ) {
const m = lodash . intersection ( event . tags . map ( t => t . tag ) , filters . tags )
if ( m . length > 0 ) return true
}
if ( filters . places . length ) {
if ( filters . places . find ( p => p === event . place . name ) ) {
return true
}
}
}
const notifications = await Notification . findAll ( )
// get notification that matches with selected event
return notifications . filter ( notification => match ( event , notification . filters ) )
} ,
async updateTag ( req , res ) {
const tag = await Tag . findByPk ( req . body . tag )
if ( tag ) {
res . json ( await tag . update ( req . body ) )
} else {
res . sendStatus ( 404 )
}
} ,
async updatePlace ( req , res ) {
const place = await Place . findByPk ( req . body . id )
await place . update ( req . body )
res . json ( place )
} ,
2019-06-14 23:26:13 +02:00
// TODO retrieve next/prev event also
// select id, start_datetime, title from events where start_datetime > (select start_datetime from events where id=89) order by start_datetime limit 20;
2019-04-03 00:25:12 +02:00
async get ( req , res ) {
const id = req . params . event _id
2019-06-26 14:44:21 +02:00
let event = await Event . findByPk ( id , {
plain : true ,
attributes : { exclude : [ 'createdAt' , 'updatedAt' ] } ,
2019-06-14 23:26:13 +02:00
include : [
2019-06-26 14:44:21 +02:00
{ model : Tag , attributes : [ 'tag' , 'weigth' ] , through : { attributes : [ ] } } ,
{ model : Place , attributes : [ 'name' , 'address' ] } ,
Comment
2019-06-07 17:02:33 +02:00
] ,
2019-06-26 14:44:21 +02:00
order : [ [ Comment , 'id' , 'DESC' ] ]
2019-05-30 12:04:14 +02:00
} )
2019-06-25 01:05:38 +02:00
2019-06-12 22:26:28 +02:00
if ( event ) {
res . json ( event )
} else {
res . sendStatus ( 404 )
}
2019-04-03 00:25:12 +02:00
} ,
async confirm ( req , res ) {
const id = req . params . event _id
const event = await Event . findByPk ( id )
try {
await event . update ( { is _visible : true } )
// insert notification
const notifications = await eventController . getNotifications ( event )
await event . setNotifications ( notifications )
res . sendStatus ( 200 )
} catch ( e ) {
res . sendStatus ( 404 )
}
} ,
async unconfirm ( req , res ) {
const id = req . params . event _id
const event = await Event . findByPk ( id )
try {
await event . update ( { is _visible : false } )
res . sendStatus ( 200 )
} catch ( e ) {
res . sendStatus ( 404 )
}
} ,
async getUnconfirmed ( req , res ) {
const events = await Event . findAll ( {
where : {
is _visible : false
} ,
order : [ [ 'start_datetime' , 'ASC' ] ] ,
include : [ Tag , Place ]
} )
res . json ( events )
} ,
async addNotification ( req , res ) {
try {
const notification = {
filters : { is _visible : true } ,
email : req . body . email ,
type : 'mail' ,
remove _code : crypto . randomBytes ( 16 ) . toString ( 'hex' )
}
await Notification . create ( notification )
res . sendStatus ( 200 )
} catch ( e ) {
res . sendStatus ( 404 )
}
} ,
async delNotification ( req , res ) {
const remove _code = req . params . code
try {
const notification = await Notification . findOne ( { where : { remove _code : { [ Op . eq ] : remove _code } } } )
await notification . destroy ( )
} catch ( e ) {
return res . sendStatus ( 404 )
}
res . sendStatus ( 200 )
} ,
async getAll ( req , res ) {
// this is due how v-calendar shows dates
const start = moment ( ) . year ( req . params . year ) . month ( req . params . month )
2019-06-26 14:44:21 +02:00
. startOf ( 'month' ) . startOf ( 'isoWeek' )
2019-06-21 23:52:18 +02:00
let end = moment ( ) . utc ( ) . year ( req . params . year ) . month ( req . params . month ) . endOf ( 'month' )
2019-04-03 00:25:12 +02:00
const shownDays = end . diff ( start , 'days' )
2019-06-26 14:44:21 +02:00
if ( shownDays <= 35 ) end = end . add ( 1 , 'week' )
end = end . endOf ( 'isoWeek' )
2019-04-03 00:25:12 +02:00
const events = await Event . findAll ( {
2019-04-29 00:27:29 +02:00
where : {
is _visible : true ,
[ Op . and ] : [
2019-06-26 14:44:21 +02:00
Sequelize . literal ( ` start_datetime >= ${ start . unix ( ) } ` ) ,
Sequelize . literal ( ` start_datetime <= ${ end . unix ( ) } ` )
2019-04-29 00:27:29 +02:00
]
} ,
2019-05-30 12:12:51 +02:00
order : [
2019-06-07 17:02:33 +02:00
[ 'start_datetime' , 'ASC' ] ,
2019-05-30 12:12:51 +02:00
[ Tag , 'weigth' , 'DESC' ]
] ,
2019-04-29 00:27:29 +02:00
include : [
2019-06-26 18:37:02 +02:00
{ model : Tag , required : false , attributes : [ 'tag' , 'weigth' ] } ,
2019-05-30 12:12:51 +02:00
{ model : Place , required : false , attributes : [ 'id' , 'name' , 'address' ] }
2019-04-29 00:27:29 +02:00
]
2019-04-03 00:25:12 +02:00
} )
res . json ( events )
}
}
module . exports = eventController