diff --git a/api/src/routes/applications.ts b/api/src/routes/applications.ts index 76c0eca..daf1267 100644 --- a/api/src/routes/applications.ts +++ b/api/src/routes/applications.ts @@ -2,11 +2,12 @@ const express = require('express'); const router = express.Router(); import pool from '../db'; -import { approveApplication, createApplication, getApplicationByID, getApplicationComments, getApplicationList, getMemberApplication } from '../services/applicationService'; +import { approveApplication, createApplication, denyApplication, getApplicationByID, getApplicationComments, getApplicationList, getMemberApplication } from '../services/applicationService'; import { MemberState, setUserState } from '../services/memberService'; import { getRankByName, insertMemberRank } from '../services/rankService'; import { ApplicationFull, CommentRow } from "@app/shared/types/application" import { assignUserToStatus } from '../services/statusService'; +import { Request, Response } from 'express'; // POST /application router.post('/', async (req, res) => { @@ -64,7 +65,6 @@ router.get('/me', async (req, res) => { // GET /application/:id router.get('/:id', async (req, res) => { let appID = req.params.id; - console.log("HELLO") try { const application = await getApplicationByID(appID); if (application === undefined) @@ -92,9 +92,6 @@ router.post('/approve/:id', async (req, res) => { const app = await getApplicationByID(appID); const result = await approveApplication(appID); - console.log("START"); - console.log(app, result); - //guard against failures if (result.affectedRows != 1) { throw new Error("Something went wrong approving the application"); @@ -119,26 +116,11 @@ router.post('/approve/:id', async (req, res) => { router.post('/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); - } - + const app = await getApplicationByID(appID); + await denyApplication(appID); + await setUserState(app.member_id, MemberState.Denied); + res.sendStatus(200); } catch (err) { console.error('Approve failed:', err); res.status(500).json({ error: 'Failed to deny application' }); @@ -146,10 +128,12 @@ router.post('/deny/:id', async (req, res) => { }); // POST /application/:id/comment -router.post('/:id/comment', async (req, res) => { +router.post('/:id/comment', async (req: Request, res: Response) => { const appID = req.params.id; const data = req.body.message; - const user = 1; + const user = req.user; + + console.log(user) const sql = `INSERT INTO application_comments( application_id, @@ -161,7 +145,7 @@ VALUES(?, ?, ?);` try { const conn = await pool.getConnection(); - const result = await conn.query(sql, [appID, user, data]) + const result = await conn.query(sql, [appID, user.id, data]) console.log(result) if (result.affectedRows !== 1) { conn.release(); diff --git a/api/src/services/applicationService.ts b/api/src/services/applicationService.ts index 8e94a08..1938189 100644 --- a/api/src/services/applicationService.ts +++ b/api/src/services/applicationService.ts @@ -1,5 +1,6 @@ import { ApplicationListRow, ApplicationRow, CommentRow } from "@app/shared/types/application"; import pool from "../db"; +import { error } from "console"; export async function createApplication(memberID: number, appVersion: number, app: string) { const sql = `INSERT INTO applications (member_id, app_version, app_data) VALUES (?, ?, ?);`; @@ -44,7 +45,7 @@ export async function getApplicationList(): Promise { return rows; } -export async function approveApplication(id) { +export async function approveApplication(id: number) { const sql = ` UPDATE applications SET approved_at = NOW() @@ -57,6 +58,24 @@ export async function approveApplication(id) { return result; } +export async function denyApplication(id: number) { + const sql = ` + UPDATE applications + SET denied_at = NOW() + WHERE id = ? + AND approved_at IS NULL + AND denied_at IS NULL + `; + + const result = await pool.execute(sql, id); + + if (result.affectedRows == 1) { + return + } else { + throw new Error(`"Something went wrong denying application with ID ${id}`); + } +} + export async function getApplicationComments(appID: number): Promise { return await pool.query(`SELECT app.id AS comment_id, app.post_content, diff --git a/ui/src/api/application.ts b/ui/src/api/application.ts index 85a3dc5..4193635 100644 --- a/ui/src/api/application.ts +++ b/ui/src/api/application.ts @@ -104,6 +104,7 @@ export async function postChatMessage(message: any, post_id: number) { const response = await fetch(`${addr}/application/${post_id}/comment`, { method: 'POST', + credentials: 'include', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(out), }) diff --git a/ui/src/pages/Application.vue b/ui/src/pages/Application.vue index 370bfb3..433f6ca 100644 --- a/ui/src/pages/Application.vue +++ b/ui/src/pages/Application.vue @@ -164,7 +164,7 @@ async function handleDeny(id) { -
+

Discussion