started backend conversion to typescript
This commit is contained in:
19
api/src/db.ts
Normal file
19
api/src/db.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
// const mariadb = require('mariadb')
|
||||
import * as mariadb from 'mariadb';
|
||||
const dotenv = require('dotenv')
|
||||
dotenv.config();
|
||||
|
||||
|
||||
const pool = mariadb.createPool({
|
||||
host: process.env.DB_HOST,
|
||||
port: Number(process.env.DB_PORT),
|
||||
user: process.env.DB_USERNAME,
|
||||
password: process.env.DB_PASSWORD,
|
||||
connectionLimit: 5,
|
||||
connectTimeout: 10000, // give it more breathing room
|
||||
acquireTimeout: 15000,
|
||||
database: 'ranger_unit_tracker',
|
||||
ssl: false,
|
||||
});
|
||||
|
||||
module.exports = pool;
|
||||
65
api/src/index.js
Normal file
65
api/src/index.js
Normal file
@@ -0,0 +1,65 @@
|
||||
const dotenv = require('dotenv')
|
||||
dotenv.config();
|
||||
|
||||
const express = require('express')
|
||||
const cors = require('cors')
|
||||
|
||||
const app = express()
|
||||
|
||||
app.use(cors({
|
||||
origin: ['https://aj17thdev.nexuszone.net', 'http://localhost:5173'], // your SPA origins
|
||||
credentials: true
|
||||
}));
|
||||
|
||||
app.use(express.json())
|
||||
|
||||
app.set('trust proxy', 1);
|
||||
|
||||
const port = process.env.SERVER_PORT;
|
||||
|
||||
//session setup
|
||||
const path = require('path')
|
||||
const session = require('express-session')
|
||||
const passport = require('passport')
|
||||
const SQLiteStore = require('connect-sqlite3')(session);
|
||||
|
||||
app.use(session({
|
||||
secret: 'whatever',
|
||||
resave: false,
|
||||
saveUninitialized: false,
|
||||
store: new SQLiteStore({ db: 'sessions.db', dir: './' }),
|
||||
cookie: {
|
||||
httpOnly: true,
|
||||
sameSite: 'lax',
|
||||
domain: 'nexuszone.net'
|
||||
}
|
||||
}));
|
||||
app.use(passport.authenticate('session'));
|
||||
|
||||
// Mount route modules
|
||||
const applicationsRouter = require('./routes/applications');
|
||||
const { memberRanks, ranks } = require('./routes/ranks');
|
||||
const members = require('./routes/members');
|
||||
const loaHandler = require('./routes/loa')
|
||||
const { status, memberStatus } = require('./routes/statuses')
|
||||
const authRouter = require('./routes/auth')
|
||||
const { roles, memberRoles } = require('./routes/roles')
|
||||
|
||||
app.use('/application', applicationsRouter);
|
||||
app.use('/ranks', ranks);
|
||||
app.use('/memberRanks', memberRanks);
|
||||
app.use('/members', members);
|
||||
app.use('/loa', loaHandler);
|
||||
app.use('/status', status)
|
||||
app.use('/memberStatus', memberStatus)
|
||||
app.use('/roles', roles)
|
||||
app.use('/memberRoles', memberRoles)
|
||||
app.use('/', authRouter)
|
||||
|
||||
app.get('/ping', (req, res) => {
|
||||
res.status(200).json({ message: 'pong' });
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Example app listening on port ${port} `)
|
||||
})
|
||||
205
api/src/routes/applications.js
Normal file
205
api/src/routes/applications.js
Normal file
@@ -0,0 +1,205 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
|
||||
// DB pool (same as used in api/index.js)
|
||||
const pool = require('../db');
|
||||
|
||||
// POST /application
|
||||
router.post('/', async (req, res) => {
|
||||
try {
|
||||
const App = req.body?.App || {};
|
||||
|
||||
// TODO: replace with current user ID
|
||||
const memberId = 1;
|
||||
|
||||
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' });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /application/all
|
||||
router.get('/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 (err) {
|
||||
console.error(err);
|
||||
res.status(500);
|
||||
}
|
||||
});
|
||||
|
||||
// GET /application/:id
|
||||
router.get('/:id', async (req, res) => {
|
||||
let appID = req.params.id;
|
||||
|
||||
//TODO: Replace with real user Authorization and whatnot
|
||||
// if the application is not "me" and I am not a recruiter, deny access to the application (return 403 or whatever)
|
||||
if (appID === "me")
|
||||
appID = 2;
|
||||
|
||||
try {
|
||||
const conn = await pool.getConnection()
|
||||
|
||||
const application = await conn.query(
|
||||
`SELECT app.*,
|
||||
member.name AS member_name
|
||||
FROM applications AS app
|
||||
INNER JOIN members AS member ON member.id = app.member_id
|
||||
WHERE app.id = ?;`,
|
||||
[appID]
|
||||
);
|
||||
|
||||
if (!Array.isArray(application) || application.length === 0) {
|
||||
conn.release();
|
||||
return res.status(204).json("Application Not Found");
|
||||
}
|
||||
|
||||
const comments = await conn.query(`SELECT app.id AS comment_id,
|
||||
app.post_content,
|
||||
app.poster_id,
|
||||
app.post_time,
|
||||
app.last_modified,
|
||||
member.name AS poster_name
|
||||
FROM application_comments AS app
|
||||
INNER JOIN members AS member ON member.id = app.poster_id
|
||||
WHERE app.application_id = ?;`,
|
||||
[appID]);
|
||||
|
||||
conn.release()
|
||||
|
||||
const output = {
|
||||
application: application[0],
|
||||
comments,
|
||||
}
|
||||
return res.status(200).json(output);
|
||||
}
|
||||
catch (err) {
|
||||
console.error('Query failed:', err);
|
||||
return res.status(500).json({ error: 'Failed to load application' });
|
||||
}
|
||||
});
|
||||
|
||||
// POST /application/approve/:id
|
||||
router.post('/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' });
|
||||
}
|
||||
});
|
||||
|
||||
// POST /application/deny/:id
|
||||
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);
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
console.error('Approve failed:', err);
|
||||
res.status(500).json({ error: 'Failed to deny application' });
|
||||
}
|
||||
});
|
||||
|
||||
// POST /application/:id/comment
|
||||
router.post('/:id/comment', async (req, res) => {
|
||||
const appID = req.params.id;
|
||||
const data = req.body.message;
|
||||
const user = 1;
|
||||
|
||||
const sql = `INSERT INTO application_comments(
|
||||
application_id,
|
||||
poster_id,
|
||||
post_content
|
||||
)
|
||||
VALUES(?, ?, ?);`
|
||||
|
||||
try {
|
||||
const conn = await pool.getConnection();
|
||||
|
||||
const result = await conn.query(sql, [appID, user, data])
|
||||
console.log(result)
|
||||
if (result.affectedRows !== 1) {
|
||||
conn.release();
|
||||
throw new Error("Insert Failure")
|
||||
}
|
||||
|
||||
const getSQL = `SELECT app.id AS comment_id,
|
||||
app.post_content,
|
||||
app.poster_id,
|
||||
app.post_time,
|
||||
app.last_modified,
|
||||
member.name AS poster_name
|
||||
FROM application_comments AS app
|
||||
INNER JOIN members AS member ON member.id = app.poster_id
|
||||
WHERE app.id = ?; `;
|
||||
const comment = await conn.query(getSQL, [result.insertId])
|
||||
res.status(201).json(comment[0]);
|
||||
|
||||
} catch (err) {
|
||||
console.error('Comment failed:', err);
|
||||
res.status(500).json({ error: 'Could not post comment' });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
112
api/src/routes/auth.js
Normal file
112
api/src/routes/auth.js
Normal file
@@ -0,0 +1,112 @@
|
||||
const passport = require('passport');
|
||||
const OpenIDConnectStrategy = require('passport-openidconnect');
|
||||
const dotenv = require('dotenv');
|
||||
dotenv.config();
|
||||
|
||||
const express = require('express');
|
||||
const { param } = require('./applications');
|
||||
const router = express.Router();
|
||||
const pool = require('../db')
|
||||
const querystring = require('querystring');
|
||||
|
||||
|
||||
passport.use(new OpenIDConnectStrategy({
|
||||
issuer: process.env.AUTH_ISSUER,
|
||||
authorizationURL: 'https://sso.iceberg-gaming.com/application/o/authorize/',
|
||||
tokenURL: 'https://sso.iceberg-gaming.com/application/o/token/',
|
||||
userInfoURL: 'https://sso.iceberg-gaming.com/application/o/userinfo/',
|
||||
clientID: process.env.AUTH_CLIENT_ID,
|
||||
clientSecret: process.env.AUTH_CLIENT_SECRET,
|
||||
callbackURL: process.env.AUTH_REDIRECT_URI,
|
||||
scope: ['openid', 'profile']
|
||||
}, async function verify(issuer, sub, profile, jwtClaims, accessToken, refreshToken, params, cb) {
|
||||
|
||||
console.log('--- OIDC verify() called ---');
|
||||
console.log('issuer:', issuer);
|
||||
console.log('sub:', sub);
|
||||
console.log('profile:', JSON.stringify(profile, null, 2));
|
||||
console.log('id_token claims:', JSON.stringify(jwtClaims, null, 2));
|
||||
console.log('preferred_username:', jwtClaims?.preferred_username);
|
||||
|
||||
const con = await pool.getConnection();
|
||||
try {
|
||||
await con.beginTransaction();
|
||||
|
||||
//lookup existing user
|
||||
const existing = await con.query(`SELECT id FROM members WHERE authentik_issuer = ? AND authentik_sub = ? LIMIT 1;`, [issuer, sub]);
|
||||
console.log(existing)
|
||||
let memberId;
|
||||
//if member exists
|
||||
if (existing.length > 0) {
|
||||
console.log('member exists');
|
||||
memberId = existing[0].id;
|
||||
} else {
|
||||
console.log("creating member")
|
||||
//otherwise: create account
|
||||
const username = sub.username;
|
||||
|
||||
const result = await con.query(
|
||||
`INSERT INTO members (name, authentik_sub, authentik_issuer) VALUES (?, ?, ?)`,
|
||||
[username, sub, issuer]
|
||||
)
|
||||
memberId = result.insertId;
|
||||
}
|
||||
|
||||
console.log("hello world" + memberId);
|
||||
await con.commit();
|
||||
return cb(null, { memberId });
|
||||
} catch (error) {
|
||||
await con.rollback();
|
||||
return cb(error);
|
||||
} finally {
|
||||
con.release();
|
||||
}
|
||||
}));
|
||||
|
||||
router.get('/login', passport.authenticate('openidconnect'))
|
||||
router.get('/callback', passport.authenticate('openidconnect', {
|
||||
successRedirect: 'https://aj17thdev.nexuszone.net/',
|
||||
failureRedirect: 'https://aj17thdev.nexuszone.net/'
|
||||
}));
|
||||
|
||||
router.post('/logout', function (req, res, next) {
|
||||
req.logout(function (err) {
|
||||
if (err) { return next(err); }
|
||||
var params = {
|
||||
client_id: process.env.AUTH_CLIENT_ID,
|
||||
returnTo: 'https://aj17thdev.nexuszone.net/'
|
||||
};
|
||||
res.redirect(process.env.AUTH_DOMAIN + '/v2/logout?' + querystring.stringify(params));
|
||||
});
|
||||
});
|
||||
|
||||
passport.serializeUser(function (user, cb) {
|
||||
process.nextTick(function () {
|
||||
console.log(`serialize: ${user.memberId}`);
|
||||
cb(null, user);
|
||||
});
|
||||
});
|
||||
|
||||
passport.deserializeUser(function (user, cb) {
|
||||
process.nextTick(async function () {
|
||||
const memberID = user.memberId;
|
||||
|
||||
const con = await pool.getConnection();
|
||||
|
||||
var userData;
|
||||
try {
|
||||
userResults = await con.query(`SELECT id, name FROM members WHERE id = ?;`, [memberID])
|
||||
console.log(userResults)
|
||||
userData = userResults[0];
|
||||
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
} finally {
|
||||
con.release();
|
||||
}
|
||||
return cb(null, userData);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
module.exports = router;
|
||||
33
api/src/routes/calendar.js
Normal file
33
api/src/routes/calendar.js
Normal file
@@ -0,0 +1,33 @@
|
||||
const express = require('express');
|
||||
const r = express.Router();
|
||||
const ur = express.Router();
|
||||
const pool = require('../db');
|
||||
|
||||
//insert a new latest rank for a user
|
||||
ur.post('/', async (req, res) => {3
|
||||
try {
|
||||
const change = req.body?.change;
|
||||
console.log(change);
|
||||
await insertMemberRank(change);
|
||||
|
||||
res.sendStatus(201);
|
||||
} catch (err) {
|
||||
console.error('Insert failed:', err);
|
||||
res.status(500).json({ error: 'Failed to update ranks' });
|
||||
}
|
||||
});
|
||||
|
||||
//get all ranks
|
||||
r.get('/', async (req, res) => {
|
||||
try {
|
||||
const ranks = await getAllRanks();
|
||||
console.log(ranks);
|
||||
res.json(ranks);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports.ranks = r;
|
||||
module.exports.memberRanks = ur;
|
||||
57
api/src/routes/loa.js
Normal file
57
api/src/routes/loa.js
Normal file
@@ -0,0 +1,57 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
|
||||
// DB pool (same as used in api/index.js)
|
||||
const pool = require('../db');
|
||||
|
||||
//post a new LOA
|
||||
router.post("/", async (req, res) => {
|
||||
const { member_id, filed_date, start_date, end_date, reason } = req.body;
|
||||
|
||||
if (!member_id || !filed_date || !start_date || !end_date) {
|
||||
return res.status(400).json({ error: "Missing required fields" });
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await pool.query(
|
||||
`INSERT INTO leave_of_absences
|
||||
(member_id, filed_date, start_date, end_date, reason)
|
||||
VALUES (?, ?, ?, ?, ?)`,
|
||||
[member_id, filed_date, start_date, end_date, reason]
|
||||
);
|
||||
res.sendStatus(201);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).send('Something went wrong', error);
|
||||
}
|
||||
});
|
||||
|
||||
//get my current LOA
|
||||
router.get("/me", async (req, res) => {
|
||||
//TODO: implement current user getter
|
||||
const user = 89;
|
||||
|
||||
try {
|
||||
const result = await pool.query("SELECT * FROM leave_of_absences WHERE member_id = ?", [user])
|
||||
res.status(200).json(result)
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).send(error);
|
||||
}
|
||||
})
|
||||
|
||||
router.get('/all', async (req, res) => {
|
||||
try {
|
||||
const result = await pool.query(
|
||||
`SELECT loa.*, members.name
|
||||
FROM leave_of_absences AS loa
|
||||
INNER JOIN members ON loa.member_id = members.id;
|
||||
`);
|
||||
res.status(200).json(result)
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).send(error);
|
||||
}
|
||||
})
|
||||
|
||||
module.exports = router;
|
||||
81
api/src/routes/members.js
Normal file
81
api/src/routes/members.js
Normal file
@@ -0,0 +1,81 @@
|
||||
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
|
||||
v.*,
|
||||
CASE
|
||||
WHEN EXISTS (
|
||||
SELECT 1
|
||||
FROM leave_of_absences l
|
||||
WHERE l.member_id = v.member_id
|
||||
AND l.deleted = 0
|
||||
AND UTC_TIMESTAMP() BETWEEN l.start_date AND l.end_date
|
||||
) THEN 1 ELSE 0
|
||||
END AS on_loa
|
||||
FROM view_member_rank_status_all v;`);
|
||||
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', async (req, res) => {
|
||||
console.log(req.user);
|
||||
if (req.user === undefined)
|
||||
return res.sendStatus(401)
|
||||
|
||||
try {
|
||||
const LOAData = await pool.query(
|
||||
`SELECT *
|
||||
FROM leave_of_absences
|
||||
WHERE member_id = ?
|
||||
AND deleted = 0
|
||||
AND UTC_TIMESTAMP() BETWEEN start_date AND end_date;`, req.user.id)
|
||||
const userWithLOA = {
|
||||
...req.user,
|
||||
loa: LOAData
|
||||
};
|
||||
res.json(userWithLOA);
|
||||
} catch (error) {
|
||||
console.error('Error fetching LOA data:', error);
|
||||
return res.status(500).json({ error: 'Failed to fetch user data' });
|
||||
}
|
||||
})
|
||||
|
||||
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;
|
||||
34
api/src/routes/ranks.js
Normal file
34
api/src/routes/ranks.js
Normal file
@@ -0,0 +1,34 @@
|
||||
const express = require('express');
|
||||
const r = express.Router();
|
||||
const ur = express.Router();
|
||||
const { getAllRanks, insertMemberRank } = require('../services/rankService')
|
||||
const pool = require('../db');
|
||||
|
||||
//insert a new latest rank for a user
|
||||
ur.post('/', async (req, res) => {3
|
||||
try {
|
||||
const change = req.body?.change;
|
||||
console.log(change);
|
||||
await insertMemberRank(change);
|
||||
|
||||
res.sendStatus(201);
|
||||
} catch (err) {
|
||||
console.error('Insert failed:', err);
|
||||
res.status(500).json({ error: 'Failed to update ranks' });
|
||||
}
|
||||
});
|
||||
|
||||
//get all ranks
|
||||
r.get('/', async (req, res) => {
|
||||
try {
|
||||
const ranks = await getAllRanks();
|
||||
console.log(ranks);
|
||||
res.json(ranks);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports.ranks = r;
|
||||
module.exports.memberRanks = ur;
|
||||
118
api/src/routes/roles.js
Normal file
118
api/src/routes/roles.js
Normal file
@@ -0,0 +1,118 @@
|
||||
const express = require('express');
|
||||
const r = express.Router();
|
||||
const ur = express.Router();
|
||||
|
||||
const pool = require('../db');
|
||||
|
||||
//assign a member to a role
|
||||
ur.post('/', async (req, res) => {
|
||||
try {
|
||||
const body = req.body;
|
||||
const sql = `INSERT INTO members_roles (member_id, role_id) VALUES (?, ?);`;
|
||||
|
||||
await pool.query(sql, [body.member_id, body.role_id]);
|
||||
|
||||
res.sendStatus(201);
|
||||
} catch (err) {
|
||||
console.error('Insert failed:', err);
|
||||
res.status(500).json({ error: 'Failed to add to group' });
|
||||
}
|
||||
});
|
||||
|
||||
ur.delete('/', async (req, res) => {
|
||||
try {
|
||||
const body = req.body;
|
||||
console.log(body);
|
||||
|
||||
const sql = 'DELETE FROM members_roles WHERE member_id = ? AND role_id = ?'
|
||||
await pool.query(sql, [body.member_id, body.role_id])
|
||||
|
||||
res.sendStatus(200);
|
||||
}
|
||||
catch (err) {
|
||||
console.error("delete failed: ", err)
|
||||
res.status(500).json({ error: 'Failed to remove from group' });
|
||||
|
||||
}
|
||||
})
|
||||
|
||||
//get all roles
|
||||
r.get('/', async (req, res) => {
|
||||
try {
|
||||
const con = await pool.getConnection();
|
||||
|
||||
// Get all roles
|
||||
const roles = await con.query('SELECT * FROM roles;');
|
||||
|
||||
// Get all members for each role
|
||||
const membersRoles = await con.query(`
|
||||
SELECT mr.role_id, v.*
|
||||
FROM members_roles mr
|
||||
JOIN view_member_rank_status_all v ON mr.member_id = v.member_id
|
||||
`);
|
||||
|
||||
|
||||
// Group members by role_id
|
||||
const roleIdToMembers = {};
|
||||
for (const row of membersRoles) {
|
||||
if (!roleIdToMembers[row.role_id]) roleIdToMembers[row.role_id] = [];
|
||||
// Remove role_id from member object
|
||||
const { role_id, ...member } = row;
|
||||
roleIdToMembers[role_id].push(member);
|
||||
}
|
||||
|
||||
// Attach members to each role
|
||||
const result = roles.map(role => ({
|
||||
...role,
|
||||
members: roleIdToMembers[role.id] || []
|
||||
}));
|
||||
|
||||
con.release();
|
||||
res.json(result);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
});
|
||||
|
||||
//create a new role
|
||||
r.post('/', async (req, res) => {
|
||||
try {
|
||||
const { name, color, description } = req.body;
|
||||
console.log('Creating role:', { name, color, description });
|
||||
if (!name || !color) {
|
||||
return res.status(400).json({ error: 'Name and color are required' });
|
||||
}
|
||||
|
||||
const hexColorRegex = /^#([0-9A-Fa-f]{6})$/;
|
||||
if (!hexColorRegex.test(color)) {
|
||||
return res.status(400).json({ error: 'Color must be a valid hex color (#ffffff)' });
|
||||
}
|
||||
|
||||
const sql = `INSERT INTO roles (name, color, description) VALUES (?, ?, ?);`;
|
||||
const params = [name, color, description || null];
|
||||
|
||||
const result = await pool.query(sql, params);
|
||||
|
||||
res.status(201).json({ id: result.insertId, name, color, description });
|
||||
} catch (err) {
|
||||
console.error('Insert failed:', err);
|
||||
res.status(500).json({ error: 'Failed to create role' });
|
||||
}
|
||||
})
|
||||
|
||||
r.delete('/:id', async (req, res) => {
|
||||
try {
|
||||
const id = req.params.id;
|
||||
|
||||
const sql = 'DELETE FROM roles WHERE id = ?';
|
||||
const res = await pool.query(sql, [id]);
|
||||
res.sendStatus(200);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.sendStatus(500);
|
||||
}
|
||||
})
|
||||
|
||||
module.exports.roles = r;
|
||||
module.exports.memberRoles = ur;
|
||||
46
api/src/routes/statuses.js
Normal file
46
api/src/routes/statuses.js
Normal file
@@ -0,0 +1,46 @@
|
||||
const express = require('express');
|
||||
const status = express.Router();
|
||||
const memberStatus = express.Router();
|
||||
|
||||
const pool = require('../db');
|
||||
|
||||
//insert a new latest rank for a user
|
||||
memberStatus.post('/', async (req, res) => {
|
||||
// try {
|
||||
// const App = req.body?.App || {};
|
||||
|
||||
// // TODO: replace with current user ID
|
||||
// const memberId = 1;
|
||||
|
||||
// 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' });
|
||||
// }
|
||||
res.status(501).json({ error: 'Not implemented' });
|
||||
});
|
||||
|
||||
//get all statuses
|
||||
status.get('/', async (req, res) => {
|
||||
try {
|
||||
const result = await pool.query('SELECT * FROM statuses;');
|
||||
res.json(result);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports.status = status;
|
||||
module.exports.memberStatus = memberStatus;
|
||||
|
||||
// TODO, implement get all ranks route with SQL stirng SELECT id, name, short_name, category, sort_id FROM ranks;
|
||||
6
api/src/services/calendarService.d.ts
vendored
Normal file
6
api/src/services/calendarService.d.ts
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
export declare function createEvent(eventObject: any): Promise<void>;
|
||||
export declare function updateEvent(eventObject: any): Promise<void>;
|
||||
export declare function cancelEvent(eventID: any): Promise<void>;
|
||||
export declare function getShortEventsInRange(startDate: any, endDate: any): Promise<void>;
|
||||
export declare function getEventDetailed(eventID: any): Promise<void>;
|
||||
//# sourceMappingURL=calendarService.d.ts.map
|
||||
1
api/src/services/calendarService.d.ts.map
Normal file
1
api/src/services/calendarService.d.ts.map
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"calendarService.d.ts","sourceRoot":"","sources":["calendarService.ts"],"names":[],"mappings":"AAEA,wBAAsB,WAAW,CAAC,WAAW,KAAA,iBAE5C;AAED,wBAAsB,WAAW,CAAC,WAAW,KAAA,iBAE5C;AAED,wBAAsB,WAAW,CAAC,OAAO,KAAA,iBAExC;AAED,wBAAsB,qBAAqB,CAAC,SAAS,KAAA,EAAE,OAAO,KAAA,iBAE7D;AAED,wBAAsB,gBAAgB,CAAC,OAAO,KAAA,iBAE7C"}
|
||||
19
api/src/services/calendarService.js
Normal file
19
api/src/services/calendarService.js
Normal file
@@ -0,0 +1,19 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.createEvent = createEvent;
|
||||
exports.updateEvent = updateEvent;
|
||||
exports.cancelEvent = cancelEvent;
|
||||
exports.getShortEventsInRange = getShortEventsInRange;
|
||||
exports.getEventDetailed = getEventDetailed;
|
||||
const pool = require('../db');
|
||||
async function createEvent(eventObject) {
|
||||
}
|
||||
async function updateEvent(eventObject) {
|
||||
}
|
||||
async function cancelEvent(eventID) {
|
||||
}
|
||||
async function getShortEventsInRange(startDate, endDate) {
|
||||
}
|
||||
async function getEventDetailed(eventID) {
|
||||
}
|
||||
//# sourceMappingURL=calendarService.js.map
|
||||
1
api/src/services/calendarService.js.map
Normal file
1
api/src/services/calendarService.js.map
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"calendarService.js","sourceRoot":"","sources":["calendarService.ts"],"names":[],"mappings":";;AAEA,kCAEC;AAED,kCAEC;AAED,kCAEC;AAED,sDAEC;AAED,4CAEC;AApBD,MAAM,IAAI,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;AAEtB,KAAK,UAAU,WAAW,CAAC,WAAW;AAE7C,CAAC;AAEM,KAAK,UAAU,WAAW,CAAC,WAAW;AAE7C,CAAC;AAEM,KAAK,UAAU,WAAW,CAAC,OAAO;AAEzC,CAAC;AAEM,KAAK,UAAU,qBAAqB,CAAC,SAAS,EAAE,OAAO;AAE9D,CAAC;AAEM,KAAK,UAAU,gBAAgB,CAAC,OAAO;AAE9C,CAAC"}
|
||||
36
api/src/services/calendarService.ts
Normal file
36
api/src/services/calendarService.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
// const pool = require('../db');
|
||||
import pool from '../db';
|
||||
|
||||
export interface CalendarEvent {
|
||||
id: number;
|
||||
name: string;
|
||||
start: Date; // DATETIME → Date
|
||||
end: Date; // DATETIME → Date
|
||||
location: string;
|
||||
color: string; // CHAR(7) like "#3b82f6"
|
||||
description?: string | null;
|
||||
creator?: number | null; // foreign key to members.id, nullable
|
||||
cancelled: boolean; // TINYINT(1) → boolean
|
||||
created_at: Date; // TIMESTAMP → Date
|
||||
updated_at: Date; // TIMESTAMP → Date
|
||||
}
|
||||
|
||||
export async function createEvent(eventObject) {
|
||||
|
||||
}
|
||||
|
||||
export async function updateEvent(eventObject) {
|
||||
|
||||
}
|
||||
|
||||
export async function cancelEvent(eventID) {
|
||||
|
||||
}
|
||||
|
||||
export async function getShortEventsInRange(startDate, endDate) {
|
||||
|
||||
}
|
||||
|
||||
export async function getEventDetailed(eventID) {
|
||||
|
||||
}
|
||||
22
api/src/services/rankService.js
Normal file
22
api/src/services/rankService.js
Normal file
@@ -0,0 +1,22 @@
|
||||
const pool = require('../db');
|
||||
|
||||
async function getAllRanks() {
|
||||
const rows = await pool.query(
|
||||
'SELECT id, name, short_name, sort_id FROM ranks;'
|
||||
);
|
||||
return rows;
|
||||
}
|
||||
|
||||
async function insertMemberRank(change) {
|
||||
const sql = `
|
||||
INSERT INTO members_ranks (member_id, rank_id, event_date)
|
||||
VALUES (?, ?, ?);
|
||||
`;
|
||||
const params = [change.member_id, change.rank_id, change.date];
|
||||
await pool.query(sql, params);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getAllRanks,
|
||||
insertMemberRank
|
||||
};
|
||||
Reference in New Issue
Block a user