Files
miriamgemeinde/controllers/worshipController.js

180 lines
4.9 KiB
JavaScript

const { Worship, EventPlace, Sequelize, sequelize } = require('../models');
const { Op, fn, literal } = require('sequelize');
const jwt = require('jsonwebtoken');
const { isTokenBlacklisted, addTokenToBlacklist } = require('../utils/blacklist');
function isAuthorized(req) {
const authHeader = req.header('Authorization');
if (!authHeader) {
return false;
}
const token = authHeader.replace('Bearer ', '');
if (isTokenBlacklisted(token)) {
console.log('Token is blacklisted');
return false;
}
try {
const decoded = jwt.verify(token, 'zTxVgptmPl9!_dr%xxx9999(dd)');
req.user = decoded;
return true;
} catch (err) {
console.log('Token verification failed, adding to blacklist:', err.message);
addTokenToBlacklist(token);
return false;
}
}
exports.getAllWorships = async (req, res) => {
try {
const authorized = isAuthorized(req);
const worships = await Worship.findAll({
where: {
date: {
[Op.gt]: literal("DATE_SUB(NOW(), INTERVAL 4 WEEK)")
},
},
attributes: authorized ? undefined : { exclude: ['sacristanService'] },
order: [
['date', 'ASC'],
['time', 'ASC']
],
});
res.status(200).json(worships);
} catch (error) {
res.status(500).json({ message: 'Fehler beim Abrufen der Gottesdienste' });
}
};
exports.createWorship = async (req, res) => {
try {
const worship = await Worship.create(req.body);
res.status(201).json(worship);
} catch (error) {
console.log(error);
res.status(500).json({ message: 'Fehler beim Erstellen des Gottesdienstes' });
}
};
exports.updateWorship = async (req, res) => {
try {
const worship = await Worship.findByPk(req.params.id);
if (worship) {
await worship.update(req.body);
res.status(200).json(worship);
} else {
res.status(404).json({ message: 'Gottesdienst nicht gefunden' });
}
} catch (error) {
res.status(500).json({ message: 'Fehler beim Aktualisieren des Gottesdienstes' });
}
};
exports.deleteWorship = async (req, res) => {
try {
const worship = await Worship.findByPk(req.params.id);
if (worship) {
await worship.destroy();
res.status(200).json({ message: 'Gottesdienst erfolgreich gelöscht' });
} else {
res.status(404).json({ message: 'Gottesdienst nicht gefunden' });
}
} catch (error) {
res.status(500).json({ message: 'Fehler beim Löschen des Gottesdienstes' });
}
};
exports.getFilteredWorships = async (req, res) => {
const { location, order } = req.query;
const where = {};
if (order.trim() === '') {
order = 'date DESC';
}
const locations = JSON.parse(location);
if (location && locations.length > 0) {
where.eventPlaceId = {
[Sequelize.Op.in]: locations
}
}
where.date = {
[Op.gte]: fn('CURDATE'),
};
try {
const authorized = isAuthorized(req);
console.log(authorized);
const worships = await Worship.findAll({
where,
attributes: authorized ? undefined : { exclude: ['sacristanService'] },
include: {
model: EventPlace,
as: 'eventPlace',
},
order: [
['date', 'ASC'],
['time', 'ASC']
],
});
res.status(200).json(worships);
} catch (error) {
console.log(error);
res.status(500).json({ message: 'Fehler beim Abrufen der gefilterten Gottesdienste' });
}
};
exports.getWorshipOptions = async (req, res) => {
try {
// Alle eindeutigen Organizer-Werte abrufen
const organizers = await Worship.findAll({
attributes: [[sequelize.fn('DISTINCT', sequelize.col('organizer')), 'organizer']],
where: {
organizer: {
[Op.not]: null,
[Op.ne]: ''
}
},
raw: true
});
// Alle eindeutigen Sacristan-Service-Werte abrufen
const sacristanServices = await Worship.findAll({
attributes: [[sequelize.fn('DISTINCT', sequelize.col('sacristanService')), 'sacristanService']],
where: {
sacristanService: {
[Op.not]: null,
[Op.ne]: ''
}
},
raw: true
});
// Strings aufteilen (kommasepariert) und alle eindeutigen Werte sammeln
const organizerSet = new Set();
organizers.forEach(item => {
if (item.organizer) {
item.organizer.split(',').forEach(org => {
const trimmed = org.trim();
if (trimmed) organizerSet.add(trimmed);
});
}
});
const sacristanSet = new Set();
sacristanServices.forEach(item => {
if (item.sacristanService) {
item.sacristanService.split(',').forEach(sac => {
const trimmed = sac.trim();
if (trimmed) sacristanSet.add(trimmed);
});
}
});
res.status(200).json({
organizers: Array.from(organizerSet).sort(),
sacristanServices: Array.from(sacristanSet).sort()
});
} catch (error) {
console.log(error);
res.status(500).json({ message: 'Fehler beim Abrufen der Worship-Optionen' });
}
};