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

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

module.exports = geojson;
