Fixed format of events
This commit is contained in:
@@ -21,7 +21,7 @@ const getAllEvents = async (req, res) => {
|
||||
|
||||
const filterEvents = async (req, res) => {
|
||||
try {
|
||||
const { id, 'event-places': eventPlaces, 'event-types': eventTypes, display } = req.query;
|
||||
const request = req.body;
|
||||
const where = {
|
||||
[Op.or]: [
|
||||
{
|
||||
@@ -36,7 +36,7 @@ const filterEvents = async (req, res) => {
|
||||
]
|
||||
};
|
||||
|
||||
if (id === 'all') {
|
||||
if (request.id === 'all') {
|
||||
const events = await Event.findAll({
|
||||
where,
|
||||
include: [
|
||||
@@ -49,7 +49,7 @@ const filterEvents = async (req, res) => {
|
||||
return res.json({ events });
|
||||
}
|
||||
|
||||
if (id === 'home') {
|
||||
if (request.id === 'home') {
|
||||
const events = await Event.findAll({
|
||||
where: {
|
||||
alsoOnHomepage: 1,
|
||||
@@ -65,23 +65,23 @@ const filterEvents = async (req, res) => {
|
||||
return res.json({ events });
|
||||
}
|
||||
|
||||
if (!id && !eventPlaces && !eventTypes) {
|
||||
return res.json({ events: [], eventPlaces: [], eventTypes: [], contactPersons: [] });
|
||||
if (!request.id && !request.places && !request.types) {
|
||||
return res.json({ events: [], places: [], types: [], contactPersons: [] });
|
||||
}
|
||||
|
||||
if (id) {
|
||||
where.id = id;
|
||||
if (request.id) {
|
||||
where.id = request.id;
|
||||
}
|
||||
|
||||
if (eventPlaces) {
|
||||
if (request.places && request.places.length > 0) {
|
||||
where.event_place_id = {
|
||||
[Op.in]: eventPlaces.split('|').map(id => parseInt(id))
|
||||
[Op.in]: request.places.map(id => parseInt(id))
|
||||
};
|
||||
}
|
||||
|
||||
if (eventTypes) {
|
||||
if (request.types && request.types.length > 0) {
|
||||
where.eventTypeId = {
|
||||
[Op.in]: eventTypes.split('|').map(id => parseInt(id))
|
||||
[Op.in]: request.types.map(id => parseInt(id))
|
||||
};
|
||||
}
|
||||
|
||||
@@ -94,8 +94,7 @@ const filterEvents = async (req, res) => {
|
||||
{ model: ContactPerson, as: 'contactPersons', through: { attributes: [] } }
|
||||
]
|
||||
});
|
||||
|
||||
const displayFields = display ? display.split('|') : [];
|
||||
const displayFields = request.display ? request.display : [];
|
||||
|
||||
const filteredEvents = events.map(event => {
|
||||
const filteredEvent = { ...event.toJSON() };
|
||||
|
||||
@@ -18,6 +18,28 @@ const getAllInstitutions = async (req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
const getInstitutionById = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const institution = await Institution.findByPk(id, {
|
||||
include: [
|
||||
{
|
||||
model: ContactPerson,
|
||||
as: 'contactPersons',
|
||||
through: { attributes: [] }
|
||||
}
|
||||
]
|
||||
});
|
||||
if (!institution) {
|
||||
return res.status(404).json({ error: 'Institution not found' });
|
||||
}
|
||||
res.json(institution);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: 'Failed to fetch institution' });
|
||||
console.error(error);
|
||||
}
|
||||
};
|
||||
|
||||
const createInstitution = async (req, res) => {
|
||||
try {
|
||||
const { contactPersonIds, ...institutionData } = req.body;
|
||||
@@ -70,6 +92,7 @@ const deleteInstitution = async (req, res) => {
|
||||
|
||||
module.exports = {
|
||||
getAllInstitutions,
|
||||
getInstitutionById,
|
||||
createInstitution,
|
||||
updateInstitution,
|
||||
deleteInstitution
|
||||
|
||||
@@ -49,24 +49,21 @@ exports.deleteWorship = async (req, res) => {
|
||||
};
|
||||
|
||||
exports.getFilteredWorships = async (req, res) => {
|
||||
const { location, orderBy } = req.query;
|
||||
const { location, order } = req.query;
|
||||
const where = {};
|
||||
|
||||
if (location && location !== '-1') {
|
||||
if (location.includes('|')) {
|
||||
const locationsArray = location.split('|');
|
||||
const locations = JSON.parse(location);
|
||||
if (location && locations.length > 0) {
|
||||
where.eventPlaceId = {
|
||||
[Sequelize.Op.in]: locationsArray
|
||||
[Sequelize.Op.in]: locations
|
||||
}
|
||||
} else {
|
||||
where.eventPlaceId = location;
|
||||
}
|
||||
}
|
||||
|
||||
where.date = {
|
||||
[Op.gte]: new Date(), // Only include events from today onwards
|
||||
};
|
||||
|
||||
console.log(where, order);
|
||||
try {
|
||||
const worships = await Worship.findAll({
|
||||
where,
|
||||
@@ -74,7 +71,7 @@ exports.getFilteredWorships = async (req, res) => {
|
||||
model: EventPlace,
|
||||
as: 'eventPlace',
|
||||
},
|
||||
order: [orderBy.split(' ')],
|
||||
order: [order.split(' ')],
|
||||
});
|
||||
res.status(200).json(worships);
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user