This is a Node.js REST API for managing properties, locations, and user roles (Tenants, Landlords, Agents, and Staff). The API allows CRUD operations for properties and locations and includes features such as user authentication, role-based access control, image uploads to AWS S3, and more. The project follows best practices in structure, architecture, and testing.
- User Authentication: Register and login for users with role (Tenant, Landlord, Agent, Staff).
- Role-Based Access Control: Restrict access based on user roles and staff permissions (SuperUser, Editor, etc.).
- Property and Location Management: CRUD operations for properties and locations with fields like title, description, price, images, and location details (province, city, suburb, coordinates).
- Image Upload: Upload and store property images on AWS S3.
- Forgot Password and OTP Verification: Reset password functionality with email OTP verification.
- Automated Testing: Unit tests for controllers to ensure code reliability.
- Node.js & Express: Backend framework.
- MongoDB with Mongoose: Database and ODM for schema management.
- JWT: JSON Web Tokens for authentication.
- AWS S3: Storage for property images.
- Jest: Testing framework for unit tests.
- dotenv: Environment variable management.
- GitHub Actions: Continuous integration (CI) for running tests automatically on each push.
- Node.js >= 16
- MongoDB
- AWS S3 account for image uploads
-
Clone this repository:
git clone https://github.com/codenamerhu/property-management-api.git cd property-management-api
-
Install dependencies:
npm install
-
Configure environment variables (see Environment Variables).
-
Start the server:
npm start
The server will run at
http://localhost:3000
.
.
├── src
│ ├── controllers # Controllers for handling requests
│ ├── models # Mongoose models for MongoDB
│ ├── routes # Route definitions
│ ├── middleware # Authentication and role verification middleware
│ ├── services # AWS S3 upload and other services
│ └── tests # Unit tests for controllers
└── README.md
To run this project, you need to set up the following environment variables in a .env
file at the root:
-
General Configuration
PORT=3000 MONGODB_URI=your-mongodb-uri JWT_SECRET=your-jwt-secret
-
AWS Configuration
AWS_ACCESS_KEY_ID=your-aws-access-key-id AWS_SECRET_ACCESS_KEY=your-aws-secret-access-key AWS_BUCKET_NAME=your-s3-bucket-name
- POST
/auth/register
- Register a user with role (Tenant, Landlord, Agent, Staff). - POST
/auth/login
- Login and receive a JWT token. - POST
/auth/forgot-password
- Send OTP for password reset. - POST
/auth/reset-password
- Reset password using OTP.
- POST
/properties
- Create a property (Landlords/Agents only). - GET
/properties
- Get all properties. - GET
/properties/:id
- Get a single property by ID. - PUT
/properties/:id
- Update a property by ID (requires ownership). - DELETE
/properties/:id
- Delete a property by ID (requires ownership).
- POST
/locations
- Create a new location. - GET
/locations
- Get all locations. - GET
/locations/:id
- Get a single location by ID. - PUT
/locations/:id
- Update a location. - DELETE
/locations/:id
- Delete a location.
- SuperUser: Full access to all features.
- Editor: Limited access to certain CRUD operations.
This project uses Jest for testing. You can run tests with:
npm test
Tests include:
- Authentication tests (registration, login, role validation).
- Property tests (create, read, update, delete).
- Location tests (CRUD operations).
This project uses GitHub Actions for continuous integration. Each time code is pushed to the repository or a pull request is made, GitHub Actions runs the test suite to ensure code reliability.
You can find the GitHub Actions configuration in .github/workflows/test.yml
.
Contributions are welcome! If you'd like to add a new feature or fix a bug, please follow these guidelines:
- Fork the repository and create your branch from
main
. - Install all dependencies to ensure compatibility.
- Write Unit Tests: For any new features or changes, add unit tests to maintain code reliability.
- Commit changes with clear messages.
- Push your branch and submit a Pull Request.
Please ensure all tests pass in GitHub Actions before submitting your PR.
- Add support for advanced search and filtering on properties.
- Implement pagination for property and location listings.
- Improve role-based access control with custom permissions.
- Add more unit and integration tests.
This project is designed to follow best practices in architecture, security, and testability. Contributions are welcome!