diff --git a/database/accounts/accounts.js b/database/accounts/accounts.js index 869810e..a8bab35 100644 --- a/database/accounts/accounts.js +++ b/database/accounts/accounts.js @@ -85,6 +85,14 @@ async function edit(id, email, password, isAdmin) { return new User(id, email, isAdmin); } +async function remove(id) { + const query = `DELETE FROM accounts.users + WHERE user_id = $1 + RETURNING email, admin;`; + const row = (await database.executeQuery(query, [id]))[0]; + return new User(id, row[0], row[1]); +} + async function retrieveAll() { const query = `SELECT user_id, email, admin FROM accounts.users @@ -109,6 +117,7 @@ async function getFromID(id) { exports.create = create; exports.edit = edit; +exports.remove = remove; exports.retrieveAll = retrieveAll; exports.getFromID = getFromID; exports.passport = passport; \ No newline at end of file diff --git a/public/scripts/manage/account.js b/public/scripts/manage/account.js index 65c9f4e..0ab23a3 100644 --- a/public/scripts/manage/account.js +++ b/public/scripts/manage/account.js @@ -45,3 +45,4 @@ async function checkDataValidity() { else submitButton.disabled = true; } +Form.addRemoveFunction(deleteButton, submissionForm, "account"); \ No newline at end of file diff --git a/routes/manage.js b/routes/manage.js index 9e038f0..fe6f204 100644 --- a/routes/manage.js +++ b/routes/manage.js @@ -150,7 +150,9 @@ router.post('/account', adminLoggedIn, (req, res, next) => { const isAdmin = !!req.body.admin; const accountID = req.body.account; + const remove = req.body.remove; + if(remove) accounts.remove(accountID).then(res.redirect('/manage')); if(accountID) accounts.edit(accountID, email, password, isAdmin).then(res.redirect('/manage')); else accounts.create(req.body.email, req.body.password, !!req.body.admin).then(res.redirect('/manage')); });