diff --git a/api/index.js b/api/index.js index 2c1dd02..dbe2640 100644 --- a/api/index.js +++ b/api/index.js @@ -6,22 +6,32 @@ const cors = require('cors') const app = express() -app.use(cors()) +app.use(cors({ + origin: ['https://aj17thdev.nexuszone.net', 'http://localhost:5173'], // your SPA origins + credentials: true +})); + app.use(express.json()) const port = 3000; // Mount route modules const applicationsRouter = require('./routes/applications'); -const { memberRanks, ranks} = require('./routes/ranks'); -const members = require('./routes/users'); +const { memberRanks, ranks } = require('./routes/ranks'); +const members = require('./routes/members'); const loaHandler = require('./routes/loa') +const { status, memberStatus } = require('./routes/statuses') app.use('/application', applicationsRouter); app.use('/ranks', ranks); app.use('/userRoles', memberRanks); app.use('/members', members); app.use('/loa', loaHandler); +app.use('/status', status) +app.use('/memberStatus', memberStatus) +app.get('/ping', (req, res) => { + res.status(200).json({ message: 'pong' }); +}); app.listen(port, () => { console.log(`Example app listening on port ${port} `) diff --git a/api/routes/members.js b/api/routes/members.js new file mode 100644 index 0000000..cd72cd2 --- /dev/null +++ b/api/routes/members.js @@ -0,0 +1,51 @@ +const express = require('express'); +const router = express.Router(); + +// DB pool (same as used in api/index.js) +const pool = require('../db'); + +//create a new user? +router.post('/', async (req, res) => { + +}); + +//get all users +router.get('/', async (req, res) => { + try { + const result = await pool.query('SELECT * FROM view_member_rank_status_all;'); + return res.status(200).json(result); + } catch (err) { + console.error('Error fetching users:', err); + return res.status(500).json({ error: 'Failed to fetch users' }); + } +}); + +router.get('/me', (req, res) => { + console.log(req.user); + res.json(req.user); +}) + +router.get('/:id', async (req, res) => { + try { + const userId = req.params.id; + const result = await pool.query('SELECT * FROM view_member_rank_status_all WHERE id = $1;', [userId]); + if (result.rows.length === 0) { + return res.status(404).json({ error: 'User not found' }); + } + return res.status(200).json(result.rows[0]); + } catch (err) { + console.error('Error fetching user:', err); + return res.status(500).json({ error: 'Failed to fetch user' }); + } +}); + +//update a user's display name (stub) +router.put('/:id/displayname', async (req, res) => { + // Stub: not implemented yet + return res.status(501).json({ error: 'Update display name not implemented' }); +}); + + + + +module.exports = router; diff --git a/ui/src/api/application.ts b/ui/src/api/application.ts index 31fb3e2..14e72de 100644 --- a/ui/src/api/application.ts +++ b/ui/src/api/application.ts @@ -69,11 +69,11 @@ export enum Status { Accepted = "Accepted", Denied = "Denied", } - -const addr = "localhost:3000" +// @ts-ignore +const addr = import.meta.env.VITE_APIHOST; export async function loadApplication(id: number | string): Promise { - const res = await fetch(`http://${addr}/application/${id}`) + const res = await fetch(`${addr}/application/${id}`) if (res.status === 204) return null if (!res.ok) throw new Error('Failed to load application') const json = await res.json() @@ -86,7 +86,7 @@ export async function postApplication(val: any) { let out = { "App": val, } - const res = await fetch(`http://${addr}/application`, { + const res = await fetch(`${addr}/application`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(out), @@ -100,7 +100,7 @@ export async function postChatMessage(message: any, post_id: number) { message: message } - const response = await fetch(`http://${addr}/application/${post_id}/comment`, { + const response = await fetch(`${addr}/application/${post_id}/comment`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(out), @@ -110,7 +110,7 @@ export async function postChatMessage(message: any, post_id: number) { } export async function getAllApplications() { - const res = await fetch(`http://${addr}/application/all`) + const res = await fetch(`${addr}/application/all`) if (res.ok) { return res.json() @@ -120,7 +120,7 @@ export async function getAllApplications() { } export async function approveApplication(id: Number) { - const res = await fetch(`http://${addr}/application/approve/${id}`, { method: 'POST' }) + const res = await fetch(`${addr}/application/approve/${id}`, { method: 'POST' }) if (!res.ok) { console.error("Something went wrong approving the application") @@ -128,9 +128,9 @@ export async function approveApplication(id: Number) { } export async function denyApplication(id: Number) { - const res = await fetch(`http://${addr}/application/deny/${id}`, { method: 'POST' }) + const res = await fetch(`${addr}/application/deny/${id}`, { method: 'POST' }) if (!res.ok) { - console.error("Something went wrong approving the application") + console.error("Something went wrong denying the application") } } \ No newline at end of file diff --git a/ui/src/api/loa.ts b/ui/src/api/loa.ts index 3e10105..f3d1af1 100644 --- a/ui/src/api/loa.ts +++ b/ui/src/api/loa.ts @@ -5,11 +5,11 @@ export type LOARequest = { end_date: string; // ISO 8601 string reason?: string; }; - -const addr = "localhost:3000"; +// @ts-ignore +const addr = import.meta.env.VITE_APIHOST; export async function submitLOA(request: LOARequest): Promise<{ id?: number; error?: string }> { - const res = await fetch(`http://${addr}/loa`, { + const res = await fetch(`${addr}/loa`, { method: "POST", headers: { "Content-Type": "application/json", @@ -25,7 +25,7 @@ export async function submitLOA(request: LOARequest): Promise<{ id?: number; err } export async function getMyLOA(): Promise { - const res = await fetch(`http://${addr}/loa/me`, { + const res = await fetch(`${addr}/loa/me`, { method: "GET", headers: { "Content-Type": "application/json", diff --git a/ui/src/api/member.ts b/ui/src/api/member.ts index acb05e5..2d2d2a2 100644 --- a/ui/src/api/member.ts +++ b/ui/src/api/member.ts @@ -7,11 +7,11 @@ export type Member = { status_date: string | null; }; - -const addr = "localhost:3000" +// @ts-ignore +const addr = import.meta.env.VITE_APIHOST; export async function getMembers(): Promise { - const response = await fetch(`http://${addr}/members`); + const response = await fetch(`${addr}/members`); if (!response.ok) { throw new Error("Failed to fetch members"); } diff --git a/ui/src/api/rank.ts b/ui/src/api/rank.ts index 14718df..eaf3e93 100644 --- a/ui/src/api/rank.ts +++ b/ui/src/api/rank.ts @@ -5,12 +5,11 @@ export type Rank = { sortOrder: number } -const addr = "localhost:3000" +// @ts-ignore +const addr = import.meta.env.VITE_APIHOST; - -// Placeholder: fetch list of ranks export async function getRanks(): Promise { - const res = await fetch(`http://${addr}/ranks`) + const res = await fetch(`${addr}/ranks`) if (res.ok) { return res.json() @@ -22,7 +21,7 @@ export async function getRanks(): Promise { // Placeholder: submit a rank change export async function submitRankChange(memberId: number, rankId: number): Promise<{ ok: boolean }> { - const res = await fetch(`http://${addr}/rank`, { + const res = await fetch(`${addr}/rank`, { method: "POST", headers: { "Content-Type": "application/json", diff --git a/ui/vite.config.js b/ui/vite.config.js index 689a61e..e04dc43 100644 --- a/ui/vite.config.js +++ b/ui/vite.config.js @@ -18,4 +18,7 @@ export default defineConfig({ '@': fileURLToPath(new URL('./src', import.meta.url)) }, }, + server: { + allowedHosts: ["aj17thdev.nexuszone.net"] + } })