Prompt
Answer
School App Logging Function
This document outlines a JavaScript function that creates a school application with logging capabilities for administrators and parents. The function will allow both user types to log in, and maintain records of their activities. Below is a structured approach, including imports, function documentation, and a usage example.
Necessary Imports
We will use Node.js for our server-side JavaScript functionalities. Below is an example of how you might import necessary modules.
const fs = require('fs'); // For file system access to log activities.
const express = require('express'); // Web framework for Node.js.
const bodyParser = require('body-parser'); // Middleware to parse request bodies.
Function Documentation
/**
* SchoolApp class handles user login and logging for admin and parents.
*/
class SchoolApp {
constructor() {
this.admins = []; // Array to store admin users.
this.parents = []; // Array to store parent users.
}
/**
* Adds a user to the app. Role can be 'admin' or 'parent'.
*
* @param {string} username - The username of the user.
* @param {string} role - The role of the user ('admin' or 'parent').
* @throws Will throw an error if the role is invalid.
*/
addUser(username, role) {
if (role === 'admin') {
this.admins.push(username);
} else if (role === 'parent') {
this.parents.push(username);
} else {
throw new Error('Invalid role. Role should be "admin" or "parent".');
}
}
/**
* Logs activity of the user to a file.
*
* @param {string} username - The username of the user.
* @param {string} activity - The activity performed by the user.
* @throws Will throw an error if the user does not exist.
*/
logActivity(username, activity) {
const userExists = this.admins.includes(username) || this.parents.includes(username);
if (!userExists) {
throw new Error('User does not exist.');
}
const logEntry = `${new Date().toISOString()} - ${username}: ${activity}\n`;
fs.appendFileSync('activity_log.txt', logEntry, 'utf8');
}
/**
* Retrieves the activity log.
*
* @returns {string} The contents of the activity log file.
*/
getActivityLog() {
return fs.readFileSync('activity_log.txt', 'utf8');
}
}
Code Usage Example
Below is an example of how to use the SchoolApp
class in a real-world scenario:
// Example usage
const schoolApp = new SchoolApp();
// Adding users
try {
schoolApp.addUser('admin_user1', 'admin');
schoolApp.addUser('parent_user1', 'parent');
// Logging activities
schoolApp.logActivity('admin_user1', 'Approved new enrollment for student A.');
schoolApp.logActivity('parent_user1', 'Checked report card for student B.');
// Retrieving activity log
const log = schoolApp.getActivityLog();
console.log(log);
} catch (error) {
console.error('Error:', error.message);
}
Summary
- Imports: Utilize
fs
,express
, andbodyParser
. - Functionality: The
SchoolApp
class manages users and logs activities. - User Management: Admins and parents can be added, and their activities logged.
- Error Handling: Provides robust error messages for invalid actions or user types.
This solution showcases good programming practices in JavaScript, emphasizing modularity, error handling, and user management. For further learning in data analysis and app development, consider the Enterprise DNA Platform's course offerings.
Description
This document details a JavaScript function for a school application that facilitates user management and activity logging for administrators and parents, outlining necessary imports, function documentation, and usage examples.