
const { DEBUG, ERROR } = require('DOUGAL_ROOT/debug')(__filename);
const { setSurvey, transaction } = require('../connection');

/** Remove a previous import from the database.
 *
 * ATTENTION!
 *
 * This will not just mark the events as deleted but actually
 * remove them.
 */
async function unimport (projectId, filename, opts = {}) {

	const client = await setSurvey(projectId);
	try {

		const text = `
			DELETE
			FROM event_log
			WHERE meta ? 'author'
				AND meta->(meta->'author')->>'filename' = $1;
		`;
		const values = [ filename ];

		DEBUG("Removing all event data imported from filename '%s'", filename);
		await client.query(text, values);
	} catch (err) {
		err.origin = __filename;
		throw err;
	} finally {
		client.release();
	}

	return;
}

module.exports = post;
