
const { setSurvey } = require('../../connection');

async function bbox (projectId) {

	const client = await setSurvey(projectId);

	const srid = "(SELECT ST_SRID(geometry) FROM preplot_lines LIMIT 1)"
	const text = `SELECT ST_AsGeoJSON(ST_Transform(ST_SetSRID(ST_Extent(geometry), ${srid}), 4326)) AS bbox FROM preplot_lines;`

	const res = await client.query(text);
	client.release();
	if (res.rows && res.rows.length) {
		return res.rows[0].bbox;
	} else {
		throw {status: 404};
	}
}

module.exports = bbox;
