Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Task - Javascript in practice: Course Assignment

Introduction

A doctor contacted you as a developer to solve a problem they have in their medical practice. This medical practice does not offer pre-booked appointments, therefore, all patients arrive and are served on a first-come, first-served basis. However, the receptionist struggles to keep track of which patient is next in line to see the doctor. This has caused unhappiness with patients who must wait longer than they should.

The doctor’s idea of the system he wants is as follows:

When a patient arrives, they open a Patients web interface. They can then be assigned a number by clicking on the “Take this number” button. The patient's number must be indicated on this screen. When the next patient arrives, they will do the same, also receiving a number.

The receptionist also has a web interface that she can use to see the next number in line to see the doctor. When she clicks on the “Call patient” button, the current number is removed from the object, and the next-in-line patient’s number must be shown on this interface.

Instruction

Use the techniques in the course to design and implement the application. The application must be designed using:

  • Microservices
  • Websockets
  • The relevant data structure
  • A web view to display the data
  • Documentation in the root README.md file

Gateway microservice

The gateway microservice must run on port 8000 (localhost:8000). The gateway must expose the following endpoints and routes to the correct microservice:

  • Patients – localhost:8000/patients
  • Receptionist – localhost:8000/receptionist
  • Server – localhost:8000/server

Server microservice

To ensure real-time client communication, you must use web sockets for the ‘server’ microservice.

The server microservice must run on port 8003 (localhost:8003) The web socket server will be the center point for the next-in-line data. Using a data structure that has been discussed in this course, create the server.

The data structure you must implement must follow the first-in, first-out (FIFO) principle. This data structure would serve as a container for elements where new elements are added to the end, and elements are removed from the front.

The server must generate the next random number between 0 and 1000. All the patients interfaces must be updated to indicate the 'Please take a number' field. If any of the patients select this number all the clients must be updated with the new random number displayed in the "Please take a number field"

Patients microservice

The patients microservice must run on port 8001 (localhost:8001).

The next in line number must be indicated on the patient's web view, with a “Please take number” button. The random number must be updated each time the server microservice generates a new number. When the patient clicks this button, the number the patient selected must be indicated on the patients web view as their number. This number must also be added to the data structure on the server, which must trigger the server to generate a new random number for the next patient.

Example view for the patients:

Receptionist microservice

The receptionist microservice must run on port 8002 (localhost:8002).

The receptionist interface must show the next patient’s number and a button to remove this number from the data object on the server microservice when the button is clicked. When this button is clicked, and the current patient’s number is removed from the object on the server microservice, the number of the patient next in line must be shown on the receptionist page.

Example view for the receptionist:

Documentation

All documentation must be in the root README.md file of your Git Classroom repository.

Document your design decisions, including why you chose to use the chosen data structure for the data object. Provide a brief explanation of your microservice architecture and how it works.


How to install and run the application

Using the Batch file

  1. Ensure Node.js is Installed: Make sure you have Node.js installed on your machine. You can download it from Node.js official website.

  2. Download the Repository: Download the repository to your local machine. You can do this by clicking the green "Code" button on the repository page, and then click "Download ZIP".

  3. Extract the ZIP file: Extract the ZIP file to a folder on your machine.

  4. Run the Batch File: Navigate to the folder where you extracted the ZIP file, and double-click the start.bat file. This will install the necessary dependencies and start the application.

  5. Access the Application: Once the application is running, you can access the different interfaces by opening the following URLs in your browser:

Manual Installation

  1. Ensure Node.js is Installed: Make sure you have Node.js installed on your machine. You can download it from Node.js official website.

  2. Download the Repository: Download the repository to your local machine. You can do this by clicking the green "Code" button on the repository page, and then click "Download ZIP".

  3. Extract the ZIP file: Extract the ZIP file to a folder on your machine.

  4. Install Dependencies: Open a terminal or command prompt, navigate to the folder where you extracted the ZIP file, and run the following command to install the dependencies:

npm install

NOTE: You have to create a .env file in the root directory of your project with the following environment variables listed below the instructions, and you have to start each microservice manually by running npm start in each microservice folder.

  1. Start the Application: After installing the dependencies and setting up a .env file, you can start the application by running the following command in each microservice folder:
npm start
  1. Access the Application: Once the application is running, you can access the different interfaces by opening the following URLs in your browser:

Environment variables (.env)

Create a .env file in the root directory of your project with the following content:

# Gateway microservice
GATEWAY_PORT=8000

# Patients microservice
PATIENT_PORT=8001

# Receptionist microservice
RECEPTIONIST_PORT=8002

# Server microservice
SERVER_PORT=8003

# Enable debug mode by uncommenting the line below
# DEBUG=True

Microservice directories

  1. /server
  2. /receptionist
  3. /patient
  4. /gateway

How to use the application

Patients Interface

The Patients Interface allows patients to take a number when they arrive at the medical practice. To use the Patients Interface, follow these steps:

  1. Open the Patients Interface in your browser by visiting http://localhost:8000/patients.
  2. Click the "Take this number" button to receive a number.
  3. The number you receive will be displayed on the screen.

Receptionist Interface

The Receptionist Interface allows the receptionist to call the next patient in line to see the doctor. To use the Receptionist Interface, follow these steps:

  1. Open the Receptionist Interface in your browser by visiting http://localhost:8000/receptionist.
  2. Click the "Call patient" button to call the next patient in line.
  3. The number of the patient called will be removed from the list, and the next patient's number will be displayed on the screen.

Design Decisions

Server

Package Type: CommonJS

The server is modularized into separate components: a Queue class and a socketHandler. It listens on port 8003.

Data Structure: The Queue class maintains an array of objects, ensuring there is always one object present, even if the last number is removed. The last object represents the upcoming number in the queue. The class uses enqueue and dequeue methods to add or remove objects/numbers. Each object contains a number and a timestamp property, indicating when the patient took the number.

Queue Example:

{
  '0': { number: 117, timestamp: '2025-02-06T15:06:04.238Z' },
  '1': { number: 69, timestamp: '2025-02-06T15:09:04.770Z' },
  '2': { number: 415, timestamp: null }
}

Patient 0, with number 117, took the number at 15:06:04. Patient 1, with number 69, took the number at 15:09:04. Patient 2, with number 415, is the upcoming patient and has not yet taken the number.

The socketHandler module initializes a new queue on startup, listens for connections and events using the Socket.IO package, and logs activities to the console. It manages client interactions and updates connected clients as needed.

Patient

Package Type: CommonJS

The Patient terminal provides a straightforward user interface. The Express application runs on port 8001, serving a basic interface through the indexRoute. The client-side script establishes a WebSocket connection to the server using Socket.IO, managing loading and error states. Upon connection, it requests the queue and updates the upcoming number and virtual ticket display based on server messages. If all numbers are served, the virtual ticket is hidden. Reconnection functionality is included to handle service disruptions. When the user takes a number, the script sends a command to the WebSocket server to generate a new upcoming number and updates the interface accordingly. Errors are displayed as messages to maintain responsiveness.

Receptionist

Package Type: CommonJS

The Receptionist terminal also offers a simple user interface. The Express application runs on port 8002, serving a basic interface through the indexRoute. The client-side script establishes a WebSocket connection to the server using Socket.IO, managing loading and error states. Upon connection, it requests the queue and determines if there are more than one number. If only one object is present, it represents the next number in the queue, and a "No patients" message is shown. If there are two or more objects, the oldest element is the next patient in line, and the interface displays the upcoming patient and "Call patient" button. Reconnection functionality is included to handle service disruptions. When the user calls a patient, the script sends a command to the WebSocket server to remove the first number and updates the interface accordingly. Errors are displayed as messages to maintain responsiveness.

Gateway

Package Type: Module

Initially designed to be the sole connection between clients and microservices, the gateway was later optimized to handle only HTTP traffic, allowing client scripts to connect directly to the Socket.IO server. This change was based on feedback from the instructor. The gateway handles HTTP requests based on the URL: /patients serves the Patient Microservice, and /receptionist serves the Receptionist Microservice, while client-side JavaScript is served from its own public directory.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages