This is the codebase for the Ufinity interview test assignment. The system provides the features to Teachers to perform administrative functions for their students
- NodeJS v18.x.x
- Docker
S/N | Name | Type | Description |
---|---|---|---|
1 | database | dir | Contains the DDL query for the database |
2 | src | dir | Contains the config, models, routes, controllers, services, utils, rules, test, errors, middleware, and const directories |
3 | README.md | file | This file |
S/N | Application | Exposed Port |
---|---|---|
1 | database (Development Environment) | 33306 |
1 | database (Test Environment) | 33307 |
2 | application | 3000 |
All the commands listed below should be run in the ./ufinity_interview_assignment
directory.
npm install
Start the project in the local environment, which also starts the necessary dependencies such as the database.
npm start
Start the application in watch mode.
npm run start:dev
To execute the automated tests.
npm test
To remove the existing test database container service.
npm run test:clear
To execute ESLint and check your code for any potential issues or errors.
npm run lint
To execute Prettier and format your code according to the specified rules.
npm run prettier
- Clone the project repository from this URL: https://github.com/CkKean/ufinity_interview_assignment.git
- Open the codebase in your preferred IDE (e.g., VS Code)
- Start Docker Desktop to run the MySQL database
- Run
npm install
to install the project dependencies - Run
npm start
to start the project. The application is ready for use if displays the following message. Please check FAQ if there are any issues.
✔ Container teacher-administration-system-mysql Running 0.0s
> [email protected] start
> ts-node ./src/server.ts
2023-07-05 15:50:40 [database.ts] INFO Executing (default): SELECT 1+1 AS result
2023-07-05 15:50:40 [server.ts] INFO Application started at http://localhost:3000
- You should be able to send a GET request to the following endpoint and receive a 200 response:
http://localhost:3000/api/healthCheck/
The test suites are located in the ufinity_interview_assignment/src/test
directory. There is one test suite with 31 test cases for the Student APIs. A default set of dummy data will be created before executing the test script. The data will remain for every test run for consistency. Please check FAQ if has any issues with executions.
- Start Docker Desktop to run the MySQL database.
- Run
npm test
to run the tests. The test result will be displayed if there are no problems on init database container. Please check FAQ if there are any issues.
Test Suites: 1 passed, 1 total
Tests: 31 passed, 31 total
Snapshots: 0 total
Time: 8.037 s, estimated 31 s
- Run
npm run test:clear
to remove the existing test database. Note: It is better to run step 3 to clear the existing database to make consistent data.
The Postman APIs json file is provided in the ufinity_interview_assignment/api.postman_collection.json
.
The following APIs are available for the test assignment:
- Register one or more students to a specified teacher. The API behaves differently based on the conditions below:
- a. If the student exists but is unassigned, they will be assigned to the specified teacher.
- b. If the student does not exist, a new student record will be created and assigned to the specified teacher.
- c. If the student exists and is already assigned, no action will be taken.
- Request:
- Method: POST
- URL: /api/retrievefornotifications
- Headers:
- Content-Type: application/json
- Request Body:
{ "teacher": "[email protected]", "notification": "Hello students! @[email protected] @[email protected]" }
- Response:
- Status: 204 No Content
2. GET http://localhost:3000/api/[email protected]
- Retrieve a list of students who are common to a given list of teachers.
- Request:
- Method: GET
- URL: /api/commonstudents
- Request example:
/api/[email protected]
- Headers:
- Content-Type: application/json
- Response:
- Status: 200 OK
- Response Body:
{ "students": [ "[email protected]", "[email protected]", "[email protected]", "[email protected]" ] }
- Suspend a specified student.
- Request:
- Method: POST
- URL: /api/suspend
- Headers:
- Content-Type: application/json
- Request Body:
{ "student": "[email protected]" }
- Response:
- Status: 204 No Content
- Retrieve a list of students that can receive a given notification by specific teacher. The list of students retrieved should not contain any duplicates/repetitions.
- Request:
- Method: POST
- URL: /api/retrievefornotifications
- Headers:
- Content-Type: application/json
- Example 1:
- Request Body:
{ "teacher": "[email protected]", "notification": "Hello students!" }
- Response:
Status: 200 OK
Body:
{ "students": [ "[email protected]", "[email protected]", ] }
- Request Body:
- Example 2:
- Request Body:
{ "teacher": "[email protected]", "notification": "Hello students! @[email protected] @[email protected]" }
- Response:
Status: 200 OK
Body:
{ "students": [ "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", ] }
- Request Body:
- Create a new teacher record.
- Request
- Method: POST
- URL: /api/teachers
- Headers:
- Content-Type: application/json
- Request Body:
{ "teacher_email": "[email protected]" }
- Response:
- Status: 200 OK
- This endpoint is used to check if the system is running or not.
- Request
- Method: GET
- URL: /api/healthCheck
- Headers:
- Content-Type: application/json
- Response:
- Status: 200 OK
- Response Body: OK
The application uses a MySQL database to store data. The database schema is automatically created when the MySQL Docker container is initialized for the first time.
The database has three tables as ERD shows:
- student: Stores student information
- teacher: Stores teacher information
- teacher_student_relationship: Stores the relationship between students and teachers
There are two environments in which the application operates: test and development.
-
The test environment is used for running automated tests. It has a separate MySQL database for testing purposes. The test database configuration can be customized in the .env file and add it into the /src directory. The default test database configuration is as follows:
- Host: 127.0.0.1
- Port: 33307
- Password: 'password'
- user: 'root'
- Schema: 'teacher-administration-system-test'
-
The development environment is used for local development and testing. It has its own MySQL database. The development database configuration can be customized in the .env file and add it into the /src directory. The default test database configuration is as follows:
- Host: 127.0.0.1
- Port: 33306
- Password: 'password'
- user: 'root'
- Schema: 'teacher-administration-system'
The following is the default sample data used for Student APIs:
-
Students:
-
Teachers:
-
Teacher-Student Relationships:
Note: The student with email "[email protected]" is suspended and will not receive notifications.
If you encounter the following error when running npm start
, it is due to the slow startup of your database container.
Please run npm start
again.
[server.js] ERROR SequelizeConnectionError: Connection lost: The server closed the connection.
[server.js] ERROR Unable to start application
If you encounter the following error when running npm test
, it is due to the slow startup of your database container.
Please run npm test
again. It might display partial test cases passed result if the following error occurs.
[server.js] ERROR SequelizeConnectionError: Connection lost: The server closed the connection.
[server.js] ERROR Unable to start application