improve: code structure
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
// ./engage-api/get-activity.ts
|
// engage-api/get-activity.ts
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { readFile,writeFile,unlink } from 'fs/promises';
|
import { readFile,writeFile,unlink } from 'fs/promises';
|
||||||
import { resolve } from 'path';
|
import { resolve } from 'path';
|
||||||
@@ -11,7 +11,7 @@ interface ActivityResponse {
|
|||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Custom Error for Authentication ---
|
// Custom Error for Authentication
|
||||||
class AuthenticationError extends Error {
|
class AuthenticationError extends Error {
|
||||||
status: number;
|
status: number;
|
||||||
|
|
||||||
@@ -26,7 +26,7 @@ class AuthenticationError extends Error {
|
|||||||
const COOKIE_FILE_PATH = resolve(import.meta.dir, 'nkcs-engage.cookie.txt');
|
const COOKIE_FILE_PATH = resolve(import.meta.dir, 'nkcs-engage.cookie.txt');
|
||||||
let _inMemoryCookie: string | null = null;
|
let _inMemoryCookie: string | null = null;
|
||||||
|
|
||||||
// --- Cookie Cache Helper Functions ---
|
// Cookie Cache Helper Functions
|
||||||
async function loadCachedCookie(): Promise < string | null > {
|
async function loadCachedCookie(): Promise < string | null > {
|
||||||
if (_inMemoryCookie) {
|
if (_inMemoryCookie) {
|
||||||
logger.debug("Using in-memory cached cookie.");
|
logger.debug("Using in-memory cached cookie.");
|
||||||
@@ -93,10 +93,15 @@ async function testCookieValidity(cookieString: string): Promise<boolean> {
|
|||||||
'Cookie': cookieString,
|
'Cookie': cookieString,
|
||||||
'User-Agent': 'Mozilla/5.0 (Bun DSAS-CCA get-activity Module)',
|
'User-Agent': 'Mozilla/5.0 (Bun DSAS-CCA get-activity Module)',
|
||||||
};
|
};
|
||||||
const payload = { "activityID": "3350" };
|
const payload = {
|
||||||
|
"activityID": "3350"
|
||||||
|
};
|
||||||
|
|
||||||
logger.debug(`Attempt ${attempt}/${MAX_RETRIES}`);
|
logger.debug(`Attempt ${attempt}/${MAX_RETRIES}`);
|
||||||
await axios.post(url, payload, { headers, timeout: 20000 });
|
await axios.post(url, payload, {
|
||||||
|
headers,
|
||||||
|
timeout: 20000
|
||||||
|
});
|
||||||
|
|
||||||
logger.debug("Cookie test successful (API responded 2xx). Cookie is valid.");
|
logger.debug("Cookie test successful (API responded 2xx). Cookie is valid.");
|
||||||
return true;
|
return true;
|
||||||
@@ -117,12 +122,14 @@ async function testCookieValidity(cookieString: string): Promise<boolean> {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Core API Interaction Functions ---
|
// Core API Interaction Functions
|
||||||
async function getSessionId(): Promise < string | null > {
|
async function getSessionId(): Promise < string | null > {
|
||||||
const url = 'https://engage.nkcswx.cn/Login.aspx';
|
const url = 'https://engage.nkcswx.cn/Login.aspx';
|
||||||
try {
|
try {
|
||||||
const response = await axios.get(url, {
|
const response = await axios.get(url, {
|
||||||
headers: { 'User-Agent': 'Mozilla/5.0 (Bun DSAS-CCA get-activity Module)' }
|
headers: {
|
||||||
|
'User-Agent': 'Mozilla/5.0 (Bun DSAS-CCA get-activity Module)'
|
||||||
|
}
|
||||||
});
|
});
|
||||||
const setCookieHeader = response.headers['set-cookie'];
|
const setCookieHeader = response.headers['set-cookie'];
|
||||||
if (setCookieHeader && setCookieHeader.length > 0) {
|
if (setCookieHeader && setCookieHeader.length > 0) {
|
||||||
@@ -215,7 +222,9 @@ async function getActivityDetailsRaw(
|
|||||||
'User-Agent': 'Mozilla/5.0 (Bun DSAS-CCA get-activity Module)',
|
'User-Agent': 'Mozilla/5.0 (Bun DSAS-CCA get-activity Module)',
|
||||||
'X-Requested-With': 'XMLHttpRequest'
|
'X-Requested-With': 'XMLHttpRequest'
|
||||||
};
|
};
|
||||||
const payload = { "activityID": String(activityId) };
|
const payload = {
|
||||||
|
"activityID": String(activityId)
|
||||||
|
};
|
||||||
|
|
||||||
for (let attempt = 0; attempt < maxRetries; attempt++) {
|
for (let attempt = 0; attempt < maxRetries; attempt++) {
|
||||||
try {
|
try {
|
||||||
@@ -242,6 +251,7 @@ async function getActivityDetailsRaw(
|
|||||||
throw new AuthenticationError(`Received ${error.response.status} for activity ${activityId}`, error.response.status);
|
throw new AuthenticationError(`Received ${error.response.status} for activity ${activityId}`, error.response.status);
|
||||||
}
|
}
|
||||||
logger.error(`Attempt ${attempt + 1}/${maxRetries} for activity ${activityId} failed: ${error.message}`);
|
logger.error(`Attempt ${attempt + 1}/${maxRetries} for activity ${activityId} failed: ${error.message}`);
|
||||||
|
|
||||||
if (error.response) {
|
if (error.response) {
|
||||||
logger.error(`Status: ${error.response.status}, Data (getActivityDetailsRaw): ${ String(error.response.data).slice(0,100)}...`);
|
logger.error(`Status: ${error.response.status}, Data (getActivityDetailsRaw): ${ String(error.response.data).slice(0,100)}...`);
|
||||||
}
|
}
|
||||||
@@ -343,4 +353,4 @@ export async function fetchActivityData(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Optionally
|
// Optionally
|
||||||
export { clearCookieCache, testCookieValidity };
|
//export { clearCookieCache,testCookieValidity };
|
||||||
30
index.ts
30
index.ts
@@ -24,19 +24,9 @@ import {
|
|||||||
cleanupOrphanedS3Images
|
cleanupOrphanedS3Images
|
||||||
} from './services/cache-manager';
|
} from './services/cache-manager';
|
||||||
import { logger } from './utils/logger';
|
import { logger } from './utils/logger';
|
||||||
|
import type { ActivityData } from './models/activity'
|
||||||
|
|
||||||
// Define interfaces for our data structures
|
// Define interfaces for our data structures
|
||||||
interface ActivityData {
|
|
||||||
id?: string;
|
|
||||||
name?: string;
|
|
||||||
photo?: string;
|
|
||||||
lastCheck?: string;
|
|
||||||
source?: string;
|
|
||||||
error?: string;
|
|
||||||
cache?: string;
|
|
||||||
[key: string]: any;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface StaffData {
|
interface StaffData {
|
||||||
lastCheck?: string;
|
lastCheck?: string;
|
||||||
cache?: string;
|
cache?: string;
|
||||||
@@ -139,7 +129,6 @@ app.get('/v1/activity/list', async (req: Request, res: Response) => {
|
|||||||
const category = req.query.category as string | undefined;
|
const category = req.query.category as string | undefined;
|
||||||
const academicYear = req.query.academicYear as string | undefined;
|
const academicYear = req.query.academicYear as string | undefined;
|
||||||
const grade = req.query.grade as string | undefined;
|
const grade = req.query.grade as string | undefined;
|
||||||
|
|
||||||
// Validate academicYear format if provided (YYYY/YYYY)
|
// Validate academicYear format if provided (YYYY/YYYY)
|
||||||
if (academicYear !== undefined) {
|
if (academicYear !== undefined) {
|
||||||
const academicYearRegex = /^\d{4}\/\d{4}$/;
|
const academicYearRegex = /^\d{4}\/\d{4}$/;
|
||||||
@@ -147,7 +136,6 @@ app.get('/v1/activity/list', async (req: Request, res: Response) => {
|
|||||||
return res.status(400).json({ error: 'Invalid academicYear format. Expected format: YYYY/YYYY' });
|
return res.status(400).json({ error: 'Invalid academicYear format. Expected format: YYYY/YYYY' });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate grade if provided
|
// Validate grade if provided
|
||||||
let validGrade: number | null = null;
|
let validGrade: number | null = null;
|
||||||
if (grade !== undefined) {
|
if (grade !== undefined) {
|
||||||
@@ -168,7 +156,6 @@ app.get('/v1/activity/list', async (req: Request, res: Response) => {
|
|||||||
logger.info('No activity keys found in Redis for list.');
|
logger.info('No activity keys found in Redis for list.');
|
||||||
return res.json({});
|
return res.json({});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch all activity data in parallel
|
// Fetch all activity data in parallel
|
||||||
const allActivityDataPromises = activityKeys.map(async (key) => {
|
const allActivityDataPromises = activityKeys.map(async (key) => {
|
||||||
const activityId = key.substring(ACTIVITY_KEY_PREFIX.length);
|
const activityId = key.substring(ACTIVITY_KEY_PREFIX.length);
|
||||||
@@ -176,7 +163,6 @@ app.get('/v1/activity/list', async (req: Request, res: Response) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const allActivities = await Promise.all(allActivityDataPromises);
|
const allActivities = await Promise.all(allActivityDataPromises);
|
||||||
|
|
||||||
// First pass: collect all available categories for validation
|
// First pass: collect all available categories for validation
|
||||||
const availableCategories = new Set<string>();
|
const availableCategories = new Set<string>();
|
||||||
const availableAcademicYears = new Set<string>();
|
const availableAcademicYears = new Set<string>();
|
||||||
@@ -193,7 +179,6 @@ app.get('/v1/activity/list', async (req: Request, res: Response) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Validate category against available categories
|
// Validate category against available categories
|
||||||
if (category && !availableCategories.has(category)) {
|
if (category && !availableCategories.has(category)) {
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
@@ -201,7 +186,6 @@ app.get('/v1/activity/list', async (req: Request, res: Response) => {
|
|||||||
availableCategories: Array.from(availableCategories)
|
availableCategories: Array.from(availableCategories)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate academicYear against available years
|
// Validate academicYear against available years
|
||||||
if (academicYear && !availableAcademicYears.has(academicYear)) {
|
if (academicYear && !availableAcademicYears.has(academicYear)) {
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
@@ -209,7 +193,6 @@ app.get('/v1/activity/list', async (req: Request, res: Response) => {
|
|||||||
availableAcademicYears: Array.from(availableAcademicYears)
|
availableAcademicYears: Array.from(availableAcademicYears)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply filters and collect club data
|
// Apply filters and collect club data
|
||||||
allActivities.forEach((activityData: ActivityData | null) => {
|
allActivities.forEach((activityData: ActivityData | null) => {
|
||||||
if (activityData &&
|
if (activityData &&
|
||||||
@@ -217,17 +200,14 @@ app.get('/v1/activity/list', async (req: Request, res: Response) => {
|
|||||||
activityData.name &&
|
activityData.name &&
|
||||||
!activityData.error &&
|
!activityData.error &&
|
||||||
activityData.source !== 'api-fetch-empty') {
|
activityData.source !== 'api-fetch-empty') {
|
||||||
|
|
||||||
// Check if it matches category filter if provided
|
// Check if it matches category filter if provided
|
||||||
if (category && activityData.category !== category) {
|
if (category && activityData.category !== category) {
|
||||||
return; // Skip this activity
|
return; // Skip this activity
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if it matches academicYear filter if provided
|
// Check if it matches academicYear filter if provided
|
||||||
if (academicYear && activityData.academicYear !== academicYear) {
|
if (academicYear && activityData.academicYear !== academicYear) {
|
||||||
return; // Skip this activity
|
return; // Skip this activity
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if it matches grade filter if provided
|
// Check if it matches grade filter if provided
|
||||||
if (validGrade !== null) {
|
if (validGrade !== null) {
|
||||||
// Skip if grades are null
|
// Skip if grades are null
|
||||||
@@ -239,13 +219,11 @@ app.get('/v1/activity/list', async (req: Request, res: Response) => {
|
|||||||
|
|
||||||
const minGrade = parseInt(activityData.grades.min, 10);
|
const minGrade = parseInt(activityData.grades.min, 10);
|
||||||
const maxGrade = parseInt(activityData.grades.max, 10);
|
const maxGrade = parseInt(activityData.grades.max, 10);
|
||||||
|
|
||||||
// Skip if grade is out of range or if parsing fails
|
// Skip if grade is out of range or if parsing fails
|
||||||
if (isNaN(minGrade) || isNaN(maxGrade) || validGrade < minGrade || validGrade > maxGrade) {
|
if (isNaN(minGrade) || isNaN(maxGrade) || validGrade < minGrade || validGrade > maxGrade) {
|
||||||
return; // Skip this activity
|
return; // Skip this activity
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add to result object with name and photo
|
// Add to result object with name and photo
|
||||||
clubList[activityData.id] = {
|
clubList[activityData.id] = {
|
||||||
name: activityData.name,
|
name: activityData.name,
|
||||||
@@ -253,7 +231,6 @@ app.get('/v1/activity/list', async (req: Request, res: Response) => {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.info(`Returning list of ${Object.keys(clubList).length} valid clubs after filtering.`);
|
logger.info(`Returning list of ${Object.keys(clubList).length} valid clubs after filtering.`);
|
||||||
res.json(clubList);
|
res.json(clubList);
|
||||||
|
|
||||||
@@ -274,7 +251,6 @@ app.get('/v1/activity/category', async (_req: Request, res: Response) => {
|
|||||||
logger.info('No activity keys found in Redis for categories.');
|
logger.info('No activity keys found in Redis for categories.');
|
||||||
return res.json({});
|
return res.json({});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch all activity data in parallel
|
// Fetch all activity data in parallel
|
||||||
const allActivityDataPromises = activityKeys.map(async (key) => {
|
const allActivityDataPromises = activityKeys.map(async (key) => {
|
||||||
const activityId = key.substring(ACTIVITY_KEY_PREFIX.length);
|
const activityId = key.substring(ACTIVITY_KEY_PREFIX.length);
|
||||||
@@ -298,7 +274,6 @@ app.get('/v1/activity/category', async (_req: Request, res: Response) => {
|
|||||||
|
|
||||||
logger.info(`Returning list of ${Object.keys(categoryMap).length} categories.`);
|
logger.info(`Returning list of ${Object.keys(categoryMap).length} categories.`);
|
||||||
res.json(categoryMap);
|
res.json(categoryMap);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error('Error in /v1/activity/category endpoint:', error);
|
logger.error('Error in /v1/activity/category endpoint:', error);
|
||||||
res.status(500).json({ error: 'An internal server error occurred while generating category list.' });
|
res.status(500).json({ error: 'An internal server error occurred while generating category list.' });
|
||||||
@@ -316,7 +291,6 @@ app.get('/v1/activity/academicYear', async (_req: Request, res: Response) => {
|
|||||||
logger.info('No activity keys found in Redis for academic years.');
|
logger.info('No activity keys found in Redis for academic years.');
|
||||||
return res.json({});
|
return res.json({});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch all activity data in parallel
|
// Fetch all activity data in parallel
|
||||||
const allActivityDataPromises = activityKeys.map(async (key) => {
|
const allActivityDataPromises = activityKeys.map(async (key) => {
|
||||||
const activityId = key.substring(ACTIVITY_KEY_PREFIX.length);
|
const activityId = key.substring(ACTIVITY_KEY_PREFIX.length);
|
||||||
@@ -340,7 +314,6 @@ app.get('/v1/activity/academicYear', async (_req: Request, res: Response) => {
|
|||||||
|
|
||||||
logger.info(`Returning list of ${Object.keys(academicYearMap).length} academic years.`);
|
logger.info(`Returning list of ${Object.keys(academicYearMap).length} academic years.`);
|
||||||
res.json(academicYearMap);
|
res.json(academicYearMap);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error('Error in /v1/activity/academicYear endpoint:', error);
|
logger.error('Error in /v1/activity/academicYear endpoint:', error);
|
||||||
res.status(500).json({ error: 'An internal server error occurred while generating academic year list.' });
|
res.status(500).json({ error: 'An internal server error occurred while generating academic year list.' });
|
||||||
@@ -379,7 +352,6 @@ app.get('/v1/activity/:activityId', async (req: Request, res: Response) => {
|
|||||||
return res.status(404).json({ error: `Activity ${activityId} not found.`, ...liveActivity });
|
return res.status(404).json({ error: `Activity ${activityId} not found.`, ...liveActivity });
|
||||||
}
|
}
|
||||||
res.status(status).json(liveActivity);
|
res.status(status).json(liveActivity);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(`Error in /v1/activity/${activityId} endpoint:`, error);
|
logger.error(`Error in /v1/activity/${activityId} endpoint:`, error);
|
||||||
res.status(500).json({ error: 'An internal server error occurred.', cache: "ERROR" });
|
res.status(500).json({ error: 'An internal server error occurred.', cache: "ERROR" });
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
// src/models/activity.ts
|
// src/models/activity.ts
|
||||||
export interface ActivityData {
|
export interface ActivityData {
|
||||||
// Include all common properties
|
// Include all common properties
|
||||||
id?: string | null | undefined;
|
id?: string | null;
|
||||||
name?: string | null;
|
name?: string | null;
|
||||||
description?: string | null;
|
description?: string | null;
|
||||||
photo?: string | null | undefined;
|
photo?: string | null;
|
||||||
academicYear?: string | null;
|
academicYear?: string | null;
|
||||||
category?: string | null;
|
category?: string | null;
|
||||||
isPreSignup?: boolean | null;
|
isPreSignup?: boolean | null;
|
||||||
|
|||||||
@@ -77,7 +77,6 @@ async function processAndCacheActivity(activityId: string): Promise<ActivityData
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
structuredActivity.lastCheck = new Date().toISOString();
|
structuredActivity.lastCheck = new Date().toISOString();
|
||||||
await setActivityData(activityId, structuredActivity);
|
await setActivityData(activityId, structuredActivity);
|
||||||
return structuredActivity;
|
return structuredActivity;
|
||||||
|
|||||||
@@ -60,7 +60,6 @@ export async function uploadImageFromBase64(
|
|||||||
try {
|
try {
|
||||||
// First decode the base64 image
|
// First decode the base64 image
|
||||||
const imageBuffer = decodeBase64Image(base64Data);
|
const imageBuffer = decodeBase64Image(base64Data);
|
||||||
|
|
||||||
// Convert to AVIF format with quality 80 using Sharp
|
// Convert to AVIF format with quality 80 using Sharp
|
||||||
const avifBuffer = await sharp(imageBuffer)
|
const avifBuffer = await sharp(imageBuffer)
|
||||||
.avif({
|
.avif({
|
||||||
@@ -70,10 +69,8 @@ export async function uploadImageFromBase64(
|
|||||||
// effort: 4,
|
// effort: 4,
|
||||||
})
|
})
|
||||||
.toBuffer();
|
.toBuffer();
|
||||||
|
|
||||||
// Use .avif extension for the object key
|
// Use .avif extension for the object key
|
||||||
const objectKey = `${PUBLIC_URL_FILE_PREFIX}/activity-${activityId}-${uuidv4()}.avif`;
|
const objectKey = `${PUBLIC_URL_FILE_PREFIX}/activity-${activityId}-${uuidv4()}.avif`;
|
||||||
|
|
||||||
// Using Bun's S3Client file API
|
// Using Bun's S3Client file API
|
||||||
const s3File = s3Client.file(objectKey);
|
const s3File = s3Client.file(objectKey);
|
||||||
|
|
||||||
@@ -124,7 +121,6 @@ export async function listS3Objects(prefix: string): Promise<string[]> {
|
|||||||
objectKeys.push(item.key);
|
objectKeys.push(item.key);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Get the last key for pagination
|
// Get the last key for pagination
|
||||||
if (result.contents?.length > 0) {
|
if (result.contents?.length > 0) {
|
||||||
startAfter = result.contents[result.contents.length - 1]?.key;
|
startAfter = result.contents[result.contents.length - 1]?.key;
|
||||||
@@ -132,7 +128,6 @@ export async function listS3Objects(prefix: string): Promise<string[]> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
isTruncated = result.isTruncated || false;
|
isTruncated = result.isTruncated || false;
|
||||||
|
|
||||||
// Safety check to prevent infinite loops
|
// Safety check to prevent infinite loops
|
||||||
if (result.contents?.length === 0) {
|
if (result.contents?.length === 0) {
|
||||||
break;
|
break;
|
||||||
@@ -171,12 +166,10 @@ export async function deleteS3Objects(objectKeysArray: string[]): Promise<boolea
|
|||||||
|
|
||||||
for (let i = 0; i < objectKeysArray.length; i += BATCH_SIZE) {
|
for (let i = 0; i < objectKeysArray.length; i += BATCH_SIZE) {
|
||||||
const batch = objectKeysArray.slice(i, i + BATCH_SIZE);
|
const batch = objectKeysArray.slice(i, i + BATCH_SIZE);
|
||||||
|
|
||||||
// Process batch in parallel
|
// Process batch in parallel
|
||||||
const results = await Promise.allSettled(
|
const results = await Promise.allSettled(
|
||||||
batch.map(key => s3Client!.delete(key))
|
batch.map(key => s3Client!.delete(key))
|
||||||
);
|
);
|
||||||
|
|
||||||
// Count successes and failures
|
// Count successes and failures
|
||||||
for (const result of results) {
|
for (const result of results) {
|
||||||
if (result.status === 'fulfilled') {
|
if (result.status === 'fulfilled') {
|
||||||
@@ -205,7 +198,6 @@ export function constructS3Url(objectKey: string): string {
|
|||||||
if (!S3_ENDPOINT || !BUCKET_NAME) {
|
if (!S3_ENDPOINT || !BUCKET_NAME) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure S3_ENDPOINT does not end with a slash
|
// Ensure S3_ENDPOINT does not end with a slash
|
||||||
const s3Base = S3_ENDPOINT.replace(/\/$/, '');
|
const s3Base = S3_ENDPOINT.replace(/\/$/, '');
|
||||||
// Ensure BUCKET_NAME does not start or end with a slash
|
// Ensure BUCKET_NAME does not start or end with a slash
|
||||||
|
|||||||
Reference in New Issue
Block a user