
const { event } = require('../../../../lib/db');

const geojson = async function (req, res, next) {
	try {
		const events = await event.list(req.params.project, req.query);
		const response = events.filter(event => event.meta.geometry).map(event => {
			const feature = {
				type: "Feature",
				geometry: event.meta.geometry,
				properties: event
			};
			delete feature.properties.meta.geometry;
			return feature;
		});
		res.status(200).send(response);
		next();
	} catch (err) {
		next(err);
	}
}

module.exports = geojson;
