Database integration for application actions

This commit is contained in:
2025-08-25 16:00:40 -04:00
parent ffb00e5f22
commit 9a40cd3967
5 changed files with 321 additions and 15 deletions

View File

@@ -1,11 +1,17 @@
const dotenv = require('dotenv')
dotenv.config();
const express = require('express')
const cors = require('cors')
const app = express()
app.use(cors())
app.use(express.json())
const port = 3000
const port = 3000;
const pool = require('./db')
let applicationData = {
app: null,
@@ -13,19 +19,72 @@ let applicationData = {
status: null,
};
app.post('/application', (req, res) => {
const data = req.body;
applicationData.app = data.App;
applicationData.status = "Pending";
console.log(applicationData);
res.send('Application received');
app.post('/application', async (req, res) => {
try {
const App = req.body?.App || {};
if (!app) return res.status(400).json({ error: 'Missing App payload' });
// TODO: replace with current user ID
const memberId = 2;
const sql = `INSERT INTO applications (member_id, app_version, app_data) VALUES (?, ?, ?);`;
const appVersion = 1;
const params = [memberId, appVersion, JSON.stringify(App)]
console.log(params)
await pool.query(sql, params);
res.sendStatus(201);
} catch (err) {
console.error('Insert failed:', err);
res.status(500).json({ error: 'Failed to save application' });
}
});
app.get('/application/me', (req, res) => {
if (applicationData.app) {
res.send(applicationData);
} else {
res.status(204).send();
app.get('/application/me', async (req, res) => {
try {
// TODO: replace with current user ID
const applicationId = 1;
const rows = await pool.query(
'SELECT * FROM applications WHERE id = ?',
[applicationId]
);
if (!Array.isArray(rows) || rows.length === 0) {
return res.sendStatus(204);
}
return res.status(200).json(rows[0]);
} catch (err) {
console.error('Query failed:', err);
return res.status(500).json({ error: 'Failed to load application' });
}
});
app.get('/application/all', async (req, res) => {
try {
const sql = `SELECT
member.name AS member_name,
app.id,
app.member_id,
app.submitted_at,
app.app_status
FROM applications AS app
LEFT JOIN members AS member
ON member.id = app.member_id;`
const rows = await pool.query(sql);
res.status(200).json(rows);
} catch {
console.error(err);
res.status(500);
}
});
@@ -35,6 +94,64 @@ app.post('/application/message', (req, res) => {
res.status(200).send();
});
app.post('/application/approve/:id', async (req, res) => {
const appID = req.params.id;
const sql = `
UPDATE applications
SET approved_at = NOW()
WHERE id = ?
AND approved_at IS NULL
AND denied_at IS NULL
`;
try {
const result = await pool.execute(sql, appID);
console.log(result);
if (result.affectedRows === 0) {
res.status(400).json('Something went wrong approving the application');
}
if (result.affectedRows == 1) {
res.sendStatus(200);
}
} catch (err) {
console.error('Approve failed:', err);
res.status(500).json({ error: 'Failed to approve application' });
}
});
app.post('/application/deny/:id', async (req, res) => {
const appID = req.params.id;
const sql = `
UPDATE applications
SET denied_at = NOW()
WHERE id = ?
AND approved_at IS NULL
AND denied_at IS NULL
`;
try {
const result = await pool.execute(sql, appID);
console.log(result);
if (result.affectedRows === 0) {
res.status(400).json('Something went wrong denying the application');
}
if (result.affectedRows == 1) {
res.sendStatus(200);
}
} catch (err) {
console.error('Approve failed:', err);
res.status(500).json({ error: 'Failed to deny application' });
}
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})