Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix status board #168

Merged
merged 5 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"build": "lerna run build",
"build:prod": "lerna run build:prod",
"lint": "lerna run lint",
"lint:fix": "lerna run lint:fix",
"postinstall": "lerna bootstrap",
"start": "lerna run start --parallel",
"test": "lerna run test --parallel"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ export abstract class ConnectorPlugin {
return this.document.courses;
}

/**
* Returns true, if this plugin is active.
*
* @readonly
* @type {boolean}
* @memberof ConnectorPlugin
*/
public get isActive(): boolean {
return this.document.active;
}

/**
* Returns true, if this plugin is an default handler for not
* assigned courses.
Expand Down
27 changes: 24 additions & 3 deletions packages/backend/src/controllers/connectors/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ class ConnectorService {
loggerFile.info('Got new message publish order');

this.#connectors.forEach(connector => {
// See if course is assigned to this connector - skip if not
if (connector.courses.indexOf(courseId) === -1) return;
// Check if connector is active and course is assigned to this connector - skip if not
if (!connector.isActive || connector.courses.indexOf(courseId) === -1) return;

messageWasSent = true;

Expand All @@ -119,7 +119,7 @@ class ConnectorService {

// If not, send the message to all default connectors
this.#connectors.forEach(connector => {
if (connector.isDefault) connector.send(message);
if (connector.isDefault && connector.isActive) connector.send(message);
});
}

Expand Down Expand Up @@ -224,6 +224,27 @@ class ConnectorService {
return connector.Document;
}

/**
* Returns an status array that contains
* 1. the amount of connectors
* 2. the amount of active connectors
* 3. the amount of default connectors
*
* @type {Readonly<[number, number, number]>} status array
* @memberof ConnectorService
*/
get status(): [number, number, number] {
const connectorsLength = this.#connectors.length;
const connectorsActiveLength = this.#connectors.filter(con => con.isActive).length;
const connectorsDefaultLength = this.#connectors.filter(con => con.isDefault).length;

return [
connectorsLength,
connectorsActiveLength,
connectorsDefaultLength
];
}

/**
* Returns an array of readonly documents of all connectors
*
Expand Down
16 changes: 13 additions & 3 deletions packages/backend/src/controllers/status/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import { ApiSuccess } from '../../utils/api';
import { fetchEnrolledCourses } from '../moodle/fetch';
import { getBaseUrl } from '../moodle/index';
import { MoodleSettings } from '../moodle/schemas/moodle.schema';
import { connectorService } from '../connectors/service';

/**
* Handles GET /api/settings/status requests and responds
* with a JSON object containing all currently available status info.
* GET /api/status
* Responds with a JSON object containing all currently available status info.
*
* @param req Request
* @param res Response
* @param next NextFunction
*/
export async function getStatusRequest(req: Request, res: Response, next: NextFunction) {
export async function statusGetRequest(req: Request, res: Response, next: NextFunction) {
try {

// TODO: Better handling in #19
Expand All @@ -31,11 +32,20 @@ export async function getStatusRequest(req: Request, res: Response, next: NextFu
const moodleLastFetchTimestamp = await MoodleSettings.getLastFetch();
const moodleNextFetchTimestamp = await MoodleSettings.getNextFetch();

const [
connectorsLength,
connectorsActiveLength,
connectorsDefaultLength
] = connectorService.status;

const responseObject = {
moodleConnectionStatus,
moodleLastFetchTimestamp,
moodleNextFetchTimestamp,
moodleCurrentFetchInterval,
connectorsLength,
connectorsActiveLength,
connectorsDefaultLength,
};

const response = new ApiSuccess(200, responseObject);
Expand Down
2 changes: 2 additions & 0 deletions packages/backend/src/routes/index.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { connectorsRoutes } from './connectors.routes';
import { settingsRoutes } from './settings.routes';
import { isAuth } from '../controllers/authentication';
import { manualFetchRequest } from '../controllers/moodle';
import { statusGetRequest } from '../controllers/status/status';
export const router = Router();

// register routes
Expand All @@ -13,3 +14,4 @@ router.use(authRoutes);
router.use(connectorsRoutes);
router.use('/settings', isAuth, settingsRoutes);
router.get('/fetch', isAuth, manualFetchRequest);
router.get('/status', isAuth, statusGetRequest);
2 changes: 0 additions & 2 deletions packages/backend/src/routes/settings.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Router } from 'express';
import { setRefreshRateRequest, getRefreshRateRequest } from '../controllers/moodle/refreshRate';
import { getCourseListRequest, setCourseRequest } from '../controllers/courseList/courseList';
import { adminAdministratorPostRequest, adminAdministratorGetRequest,adminAdministratorDeleteRequest } from '../controllers/administrator';
import { getStatusRequest } from '../controllers/status/status';
export const settingsRoutes = Router();

// register routes
Expand All @@ -13,4 +12,3 @@ settingsRoutes.put('/courses/:id', setCourseRequest);
settingsRoutes.post('/administrators', adminAdministratorPostRequest);
settingsRoutes.get('/administrators', adminAdministratorGetRequest);
settingsRoutes.delete('/administrators/:username', adminAdministratorDeleteRequest);
settingsRoutes.get('/status', getStatusRequest);
Loading