Nest Store is a backend application designed to facilitate order management and real-time communication between users and admins. It provides a comprehensive API that supports user authentication, order management, and chatroom messaging via WebSocket.
- User Authentication: Secure user registration and login with JWT tokens.
- Order Management: Admins and users can create, view, and manage orders.
- Real-Time Chatrooms: Admins and users can interact through real-time WebSocket connections in dedicated chatrooms linked to orders.
- Role-Based Access Control: Differentiates user access between ADMIN and USER roles for endpoints.
-
Backend:
- NestJS: Framework for building efficient, scalable Node.js applications.
- Prisma: ORM for database interactions.
- Socket.IO: For real-time communication between the server and clients.
- JWT: For secure user authentication.
-
Database:
- MySQL (or other relational database)
- POST
/auth/register: Register a new user with email, password, and role (admin or user). - POST
/auth/login: Log in an existing user with email and password, receiving a JWT token. - GET
/users: Get all users (admin only). - GET
/users/:id: Get a specific user by ID (admin only).
- POST
/orders: Create a new order (authenticated users). - GET
/orders: View all orders (users only see their own, admins see all). - GET
/orders/:id: View a specific order. - PATCH
/orders/:id/status: Update the status of an order (admin only).
- GET
/chatroom: Get all chatrooms (users only see their own, admins see all). - GET
/chatroom/:chatroomId: View a specific chatroom. - PATCH
/chatroom/:chatroomId/close: Close a chatroom (admin only). - POST
/chatroom/:chatroomId/create-message: Send a message in a chatroom (user and admin). - GET
/chatroom/:chatroomId/messages: Get all messages in a specific chatroom.
- Namespace:
/chat - Events:
- sendMessage: Used to send messages in the chatroom.
- error: Emitted when an error occurs in the WebSocket communication.
-
Install dependencies:
npm install
-
Set up your
.envfile with the correct database URL and JWT Secret:DATABASE_URL="your-database-connection-url" JWT_SECRET=yoursecret -
Run Prisma migrations to set up your database:
npx prisma migrate deploy
-
Seed your database if needed:
npx prisma db seed
-
Start the application locally:
npm run start:dev
-
The application will run on
http://localhost:3000.
Use Postman to test the API endpoints. The documentation for the endpoints is available here.
You can run your end-to-end (e2e) tests in two ways:
-
Run All E2E Tests: To run all the e2e tests at once for your project, use the following command:
npm run test:e2e
This will run all the test suites for the various features (Auth, Chat, Order, etc.).
-
Run Specific E2E Tests: If you want to run tests for a specific feature, you can specify the feature name. The available options are:
authchatorderapp
To run the e2e tests for a specific feature, use the following command:
npm run test:e2e <feature-name>
Example:
-
To run tests for authentication (
auth):npm run test:e2e auth
-
To run tests for chat (
chat):npm run test:e2e chat
-
To run tests for orders (
order):npm run test:e2e order
-
To run tests for the app (
app):npm run test:e2e app
- To resolve errors that may arise from database conflicts when running tests together, consider using
maxWorkers: 1in the Jest config.
You can easily test the WebSocket connection using the HTML files provided in the project.
-
Start the application
-
Create two users - one admin and one user.
-
Login and generate auth tokens for both users.
-
Navigate to the
./test_htmlfolder in the project directory. -
Open the provided HTML files in your browser. User in one tab, Admin in the other.
-
Locate the script section in the HTML file where the WebSocket connection is set up.
-
Add your Auth Tokens and ChatroomID in the script, replacing the placeholder with your valid JWT token and a valid chatroomID. Place user token in the user HTML and admin token in the admin HTML file.
Example:
const chatroomId = 1; // Example chatroom ID const authToken = 'eyJhbGciOiJIUz...'; // Replace with your AUTH token const socket = io('http://localhost:3000/chat', { query: { chatroomId }, extraHeaders: { Authorization: `Bearer ${authToken}` } });
-
After adding the token, you can interact with the WebSocket server by sending messages in the chatroom.
-
You will receive real-time updates in the chatroom, and you can also test the
sendMessageevent to ensure it works properly.
This allows you to simulate real-time messaging and test the WebSocket functionality before integrating it into your frontend application.
I welcome contributions to the project! Please fork the repository, create a feature branch, and submit a pull request.
- Set up Error Logging and Monitoring for better debugging.
- Add Unit Tests for chatroom and order management services.
- Create Admin Dashboard for order management and user management.
- Improve WebSocket UI to better handle real-time messaging for users and admins.
- Integrate Frontend application to interact with API endpoints.