// Express routing
const express = require('express');
const router = express.Router();
// Database connection
const db = require('../db');
// Error handling functions
const errorHandling = require('../error_handling');

/* CLEAR DATA method */
router.delete('/clear_data', async function (req, res, next) {
    try {
        await db.query('TRUNCATE read_notifications CASCADE;');
        await db.query('TRUNCATE notifications CASCADE;');
        await db.query('TRUNCATE orders CASCADE;');
        await db.query('TRUNCATE requests CASCADE;');
        await db.query('TRUNCATE parts CASCADE;');
        await db.query('TRUNCATE suppliers CASCADE;');
        await db.query('DELETE FROM users WHERE type != $1;', ['instructor']);
        await db.query('TRUNCATE teams CASCADE;');
        return res.status(204).send();
    } catch (error) {
        return res
            .status(500)
            .json(errorHandling.return_error_500(error.message));
    }
});

module.exports = router;
