Files
miriamgemeinde/controllers/worshipController.js
2024-09-06 23:39:02 +02:00

123 lines
3.3 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' });
}
};