105 lines
4.2 KiB
JavaScript
105 lines
4.2 KiB
JavaScript
// main.js
|
|
import express from 'express';
|
|
import dotenv from 'dotenv';
|
|
import { fetchActivityData } from './engage-api/get-activity.mjs';
|
|
import { structActivityData } from './engage-api/struct-activity.mjs';
|
|
import { structStaffData } from './engage-api/struct-staff.mjs';
|
|
|
|
// Load environment variables from .env file
|
|
dotenv.config();
|
|
|
|
// --- Configuration ---
|
|
const USERNAME = process.env.API_USERNAME;
|
|
const PASSWORD = process.env.API_PASSWORD;
|
|
const PORT = process.env.PORT || 3000; // Default to port 3000 if not specified
|
|
const FIXED_STAFF_ACTIVITY_ID = process.env.FIXED_STAFF_ACTIVITY_ID || '3350';
|
|
|
|
// --- Initialize Express App ---
|
|
const app = express();
|
|
|
|
// Middleware to parse JSON request bodies (useful if you add POST/PUT later)
|
|
app.use(express.json());
|
|
|
|
// --- API Endpoints ---
|
|
|
|
// GET Endpoint: Welcome message
|
|
app.get('/', (req, res) => {
|
|
res.send('Welcome to the DSAS CCA API!<br/><br/>\
|
|
API Endpoints:<br/>\
|
|
GET /v1/activity/:activityId (ID must be 1-4 digits)<br/>\
|
|
GET /v1/staffs');
|
|
});
|
|
|
|
// GET Endpoint: Fetch structured activity data by ID
|
|
app.get('/v1/activity/:activityId', async (req, res) => {
|
|
let { activityId } = req.params; // Extract activityId from URL parameter
|
|
|
|
// Validate activityId: should be 1 to 4 digits
|
|
if (!/^\d{1,4}$/.test(activityId)) {
|
|
return res.status(400).json({
|
|
error: 'Invalid Activity ID format. Activity ID must be 1 to 4 digits (e.g., 1, 0001, 9999).'
|
|
});
|
|
}
|
|
|
|
if (!USERNAME || !PASSWORD) {
|
|
console.error('API username or password not configured. Check .env file or environment variables.');
|
|
return res.status(500).json({ error: 'Server configuration error.' });
|
|
}
|
|
|
|
console.log(`Workspaceing activity details for activity ID: ${activityId}`);
|
|
try {
|
|
const activityJson = await fetchActivityData(activityId, USERNAME, PASSWORD);
|
|
if (activityJson) {
|
|
const structuredActivity = await structActivityData(activityJson);
|
|
res.json(structuredActivity); // Assuming structActivityData returns a JSON-friendly object
|
|
} else {
|
|
res.status(404).json({ error: `Could not retrieve details for activity ${activityId}.` });
|
|
}
|
|
} catch (error) {
|
|
console.error(`Error fetching activity ${activityId}:`, error);
|
|
res.status(500).json({ error: 'An internal server error occurred while fetching activity data.' });
|
|
}
|
|
});
|
|
|
|
// GET Endpoint: Fetch fixed structured staff data
|
|
app.get('/v1/staffs', async (req, res) => {
|
|
if (!USERNAME || !PASSWORD) {
|
|
console.error('API username or password not configured. Check .env file or environment variables.');
|
|
return res.status(500).json({ error: 'Server configuration error.' });
|
|
}
|
|
|
|
console.log(`Workspaceing staff details (using fixed activity ID: ${FIXED_STAFF_ACTIVITY_ID})`);
|
|
try {
|
|
const activityJson = await fetchActivityData(FIXED_STAFF_ACTIVITY_ID, USERNAME, PASSWORD);
|
|
if (activityJson) {
|
|
const staffMap = await structStaffData(activityJson); // This returns a Map
|
|
|
|
// Convert Map to a plain object for JSON serialization
|
|
const staffObject = Object.fromEntries(staffMap);
|
|
res.json(staffObject);
|
|
} else {
|
|
res.status(404).json({ error: `Could not retrieve base data using activity ID ${FIXED_STAFF_ACTIVITY_ID} to get staff details.` });
|
|
}
|
|
} catch (error) {
|
|
console.error(`Error fetching staff data (for fixed activity ID ${FIXED_STAFF_ACTIVITY_ID}):`, error);
|
|
res.status(500).json({ error: 'An internal server error occurred while fetching staff data.' });
|
|
}
|
|
});
|
|
|
|
// --- Start the Server ---
|
|
app.listen(PORT, () => {
|
|
console.log(`Server is running on http://localhost:${PORT}`);
|
|
console.log('API Endpoints:');
|
|
console.log(` GET /v1/activity/:activityId (ID must be 1-4 digits)`);
|
|
console.log(` GET /v1/staffs`);
|
|
if (!USERNAME || !PASSWORD) {
|
|
console.warn('Warning: API_USERNAME or API_PASSWORD is not set. Please configure them in your .env file or environment variables.');
|
|
}
|
|
});
|
|
|
|
// Graceful shutdown (optional but good practice)
|
|
process.on('SIGINT', () => {
|
|
console.log('Server shutting down...');
|
|
// Perform any cleanup here
|
|
process.exit(0);
|
|
}); |