Skip to content

Commit 6acf83b

Browse files
committed
auth module with unit tests done
1 parent fa29a9a commit 6acf83b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+3585
-547
lines changed

.env.development

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Application
2+
PORT=3000
3+
NODE_ENV=development
4+
5+
# Database
6+
DB_TYPE=postgres
7+
DB_HOST=localhost
8+
DB_PORT=5432
9+
DB_USERNAME=postgres
10+
DB_PASSWORD=postgres
11+
DB_NAME=movie_management
12+
DB_SYNCHRONIZE=true
13+
DB_LOGGING=true
14+
15+
# JWT
16+
JWT_SECRET=5f2308cac6152783a093846d6e82b60c15087376c07d8599161c4ed1f5654247
17+
JWT_EXPIRES_IN=1h

.env.production

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
PORT=3000
2+
NODE_ENV=production
3+
4+
# Production Database -- set in production
5+
DB_TYPE=postgres
6+
DB_HOST=production-db-host
7+
DB_PORT=5432
8+
DB_USERNAME=secure_username
9+
DB_PASSWORD=secure_password
10+
DB_NAME=movie_management_prod
11+
DB_SYNCHRONIZE=false
12+
DB_LOGGING=false
13+
14+
# JWT Production settings
15+
JWT_SECRET=production_secret_key -- set in production
16+
JWT_EXPIRES_IN=15m

.env.test

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
PORT=3001
2+
NODE_ENV=test
3+
4+
# Test Database (using SQLite for tests)
5+
DB_TYPE=sqlite
6+
DB_NAME=:memory:
7+
DB_SYNCHRONIZE=true
8+
9+
# JWT for testing
10+
JWT_SECRET=454b41d943f87fa73a4bd50e7f55b09255b0f481d8e5f724eacb5d8acd34b446
11+
JWT_EXPIRES_IN=1h

DEVELOPMENT.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
- Event storming
44

55
## Step 2 Define base domain
6-
- define common base classes for all domains
6+
- define common base classes for all domains
7+

OOP.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
// target is Calculator.prototype which will later become the __proto__ of instances
3+
/**
4+
*
5+
* target is the prototype object of the class (the object that contains all non-static methods). It is not the methods themselves,
6+
* but rather the object that holds those methods.
7+
To be specific:
8+
9+
target is exactly Calculator.prototype
10+
It contains all non-static methods defined in the class
11+
It also contains the constructor property (pointing back to the class)
12+
It will become the __proto__ of all instances of the class
13+
*/
14+
15+
16+
function AddHelperMethod(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
17+
console.log(Object.getOwnPropertyDescriptors(target))
18+
// Add a helper method to the class prototype
19+
target.helper = function () {
20+
console.log('Helper method added by decorator');
21+
};
22+
}
23+
24+
class Calculator {
25+
@AddHelperMethod
26+
add(a: number, b: number) {
27+
return a + b;
28+
}
29+
}
30+
31+
const calc = new Calculator();
32+
(calc as any).helper(); // Works because decorator added it to prototype
33+
console.log(calc.add(2, 5))
34+
35+
36+
function Animal() {}
37+
38+
console.log(Animal.prototype)
39+
40+
Animal.prototype.breathe = () => console.log('Breathing');
41+
42+
function Dog() {}
43+
Dog.prototype = Object.create(Animal.prototype)
44+
Dog.prototype.bark = function() {
45+
console.log('dfd')
46+
}
47+
48+
const dog = new Dog()
49+
dog.breathe()
50+
dog.bark()
51+
console.log(dog instanceof Dog)

README.md

+169-72
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,196 @@
1-
<p align="center">
2-
<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="120" alt="Nest Logo" /></a>
3-
</p>
4-
5-
[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456
6-
[circleci-url]: https://circleci.com/gh/nestjs/nest
7-
8-
<p align="center">A progressive <a href="http://nodejs.org" target="_blank">Node.js</a> framework for building efficient and scalable server-side applications.</p>
9-
<p align="center">
10-
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
11-
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a>
12-
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/dm/@nestjs/common.svg" alt="NPM Downloads" /></a>
13-
<a href="https://circleci.com/gh/nestjs/nest" target="_blank"><img src="https://img.shields.io/circleci/build/github/nestjs/nest/master" alt="CircleCI" /></a>
14-
<a href="https://coveralls.io/github/nestjs/nest?branch=master" target="_blank"><img src="https://coveralls.io/repos/github/nestjs/nest/badge.svg?branch=master#9" alt="Coverage" /></a>
15-
<a href="https://discord.gg/G7Qnnhy" target="_blank"><img src="https://img.shields.io/badge/discord-online-brightgreen.svg" alt="Discord"/></a>
16-
<a href="https://opencollective.com/nest#backer" target="_blank"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a>
17-
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a>
18-
<a href="https://paypal.me/kamilmysliwiec" target="_blank"><img src="https://img.shields.io/badge/Donate-PayPal-ff3f59.svg" alt="Donate us"/></a>
19-
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://img.shields.io/badge/Support%20us-Open%20Collective-41B883.svg" alt="Support us"></a>
20-
<a href="https://twitter.com/nestframework" target="_blank"><img src="https://img.shields.io/twitter/follow/nestframework.svg?style=social&label=Follow" alt="Follow us on Twitter"></a>
21-
</p>
22-
<!--[![Backers on Open Collective](https://opencollective.com/nest/backers/badge.svg)](https://opencollective.com/nest#backer)
23-
[![Sponsors on Open Collective](https://opencollective.com/nest/sponsors/badge.svg)](https://opencollective.com/nest#sponsor)-->
24-
25-
## Description
26-
27-
[Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
28-
29-
## Project setup
1+
# Movie Management System
302

3+
A RESTful API for managing movies, built with NestJS and structured according to Domain-Driven Design principles.
4+
5+
## Project Description
6+
7+
This Movie Management System allows:
8+
- User registration and authentication (managers and customers)
9+
- Movie management with age restrictions and multiple sessions
10+
- Ticket purchasing
11+
- Movie watching (with valid tickets)
12+
- User watch history tracking
13+
14+
## Architecture Overview
15+
16+
This project follows a Hexagonal Architecture (Ports and Adapters) approach within the Domain-Driven Design methodology:
17+
18+
![Hexagonal Architecture Diagram](https://raw.githubusercontent.com/username/movie-management-system/main/docs/hex-architecture.png)
19+
20+
### Key Components:
21+
22+
1. **Domain Layer (Core):**
23+
- Contains business entities, value objects, and domain events
24+
- Defines ports (interfaces) that the outside world must implement
25+
- Houses core business logic
26+
27+
2. **Application Layer:**
28+
- Orchestrates use cases through application services
29+
- Implements command handlers to process user intents
30+
- Coordinates between domain and infrastructure
31+
32+
3. **Adapters Layer:**
33+
- **Primary/Driving Adapters:** REST controllers, GraphQL resolvers, DTOs
34+
- **Secondary/Driven Adapters:** Database implementations, external service integrations
35+
36+
## Technology Stack
37+
38+
- **Framework:** NestJS 9.x
39+
- **Language:** TypeScript 4.x
40+
- **Database:** PostgreSQL (production), SQLite (testing)
41+
- **ORM:** TypeORM
42+
- **Authentication:** JWT, Passport
43+
- **Testing:** Jest
44+
- **Documentation:** Swagger/OpenAPI
45+
- **Architecture Patterns:** Domain-Driven Design, Hexagonal Architecture, CQRS
46+
47+
## Getting Started
48+
49+
### Prerequisites
50+
51+
- Node.js 14.x or later
52+
- npm 7.x or later
53+
- PostgreSQL (for production)
54+
55+
### Installation
56+
57+
1. Clone the repository:
58+
```bash
59+
git clone https://github.com/yourusername/movie-management-system.git
60+
cd movie-management-system
61+
```
62+
63+
2. Install dependencies:
64+
```bash
65+
npm install
66+
```
67+
68+
3. Set up environment variables:
69+
```bash
70+
cp .env.example .env
71+
# Edit .env with your configuration
72+
```
73+
74+
4. Start the development server:
75+
```bash
76+
npm run start:dev
77+
```
78+
79+
### Testing
80+
81+
Run unit tests:
3182
```bash
32-
$ npm install
83+
npm test
3384
```
3485

35-
## Compile and run the project
36-
86+
Run end-to-end tests:
3787
```bash
38-
# development
39-
$ npm run start
88+
npm run test:e2e
89+
```
4090

41-
# watch mode
42-
$ npm run start:dev
91+
### API Documentation
4392

44-
# production mode
45-
$ npm run start:prod
93+
Once the application is running, you can access the Swagger documentation at:
94+
```
95+
http://localhost:3000/api
4696
```
4797

48-
## Run tests
98+
## Project Structure
4999

50-
```bash
51-
# unit tests
52-
$ npm run test
100+
```
101+
src/
102+
├── modules/ # Feature modules (bounded contexts)
103+
│ ├── auth/ # Authentication bounded context
104+
│ │ ├── domain/ # Domain layer (hexagon core)
105+
│ │ │ ├── models/ # Entities, aggregates, value objects
106+
│ │ │ ├── events/ # Domain events
107+
│ │ │ ├── ports/ # Secondary ports (interfaces)
108+
│ │ │ └── services/ # Domain services
109+
│ │ ├── application/ # Application layer
110+
│ │ │ ├── services/ # Use case orchestration
111+
│ │ │ └── commands/ # CQRS commands
112+
│ │ ├── adapters/ # Adapters layer
113+
│ │ │ ├── primary/ # Driving adapters (controllers)
114+
│ │ │ └── secondary/ # Driven adapters (repositories)
115+
│ │ └── auth.module.ts # NestJS module configuration
116+
│ ├── movie/ # Movie management context
117+
│ └── ticket/ # Ticket management context
118+
├── common/ # Shared code
119+
│ ├── filters/ # Global exception filters
120+
│ ├── interceptors/ # Global interceptors
121+
│ └── decorators/ # Custom decorators
122+
└── main.ts # Application entry point
123+
```
53124

54-
# e2e tests
55-
$ npm run test:e2e
125+
## Implementation Approach
56126

57-
# test coverage
58-
$ npm run test:cov
59-
```
127+
### Domain-Driven Design
60128

61-
## Deployment
129+
The application is structured around key business domains:
130+
1. **Authentication Domain:** User management, login, registration
131+
2. **Movie Domain:** Movie creation, updates, sessions
132+
3. **Ticket Domain:** Ticket purchases, validation
62133

63-
When you're ready to deploy your NestJS application to production, there are some key steps you can take to ensure it runs as efficiently as possible. Check out the [deployment documentation](https://docs.nestjs.com/deployment) for more information.
134+
Each domain follows Clean Architecture principles with clear separation of:
135+
- **Domain Models:** Pure business logic
136+
- **Application Services:** Use case orchestration
137+
- **Infrastructure:** Technical implementations (databases, external services)
64138

65-
If you are looking for a cloud-based platform to deploy your NestJS application, check out [Mau](https://mau.nestjs.com), our official platform for deploying NestJS applications on AWS. Mau makes deployment straightforward and fast, requiring just a few simple steps:
139+
### Hexagonal Architecture
66140

67-
```bash
68-
$ npm install -g mau
69-
$ mau deploy
70-
```
141+
The hexagonal architecture (ports and adapters) allows us to:
142+
- Keep the domain layer free from infrastructure concerns
143+
- Easily replace implementation details (e.g., database, security providers)
144+
- Make the system more testable by mocking adapters
145+
- Maintain a clear boundary between business logic and technical implementations
146+
147+
## Development Decisions
148+
149+
1. **Authentication Strategy:**
150+
- JWT-based authentication for stateless API interactions
151+
- Role-based access control (RBAC) for authorization
152+
- Strong password hashing with bcrypt
153+
154+
2. **Database Approach:**
155+
- Repository pattern for data access abstraction
156+
- TypeORM for database interactions with clear domain separation
157+
- Entities mapped properly to domain objects
158+
159+
3. **Error Handling:**
160+
- Centralized exception handling
161+
- Domain-specific exceptions
162+
- Proper HTTP status codes and error messages
71163

72-
With Mau, you can deploy your application in just a few clicks, allowing you to focus on building features rather than managing infrastructure.
164+
4. **Performance Considerations:**
165+
- Optimized database queries
166+
- Proper indexing strategy
167+
- Request timeout handling
73168

74-
## Resources
169+
## Challenges Faced
75170

76-
Check out a few resources that may come in handy when working with NestJS:
171+
During development, several challenges were addressed:
77172

78-
- Visit the [NestJS Documentation](https://docs.nestjs.com) to learn more about the framework.
79-
- For questions and support, please visit our [Discord channel](https://discord.gg/G7Qnnhy).
80-
- To dive deeper and get more hands-on experience, check out our official video [courses](https://courses.nestjs.com/).
81-
- Deploy your application to AWS with the help of [NestJS Mau](https://mau.nestjs.com) in just a few clicks.
82-
- Visualize your application graph and interact with the NestJS application in real-time using [NestJS Devtools](https://devtools.nestjs.com).
83-
- Need help with your project (part-time to full-time)? Check out our official [enterprise support](https://enterprise.nestjs.com).
84-
- To stay in the loop and get updates, follow us on [X](https://x.com/nestframework) and [LinkedIn](https://linkedin.com/company/nestjs).
85-
- Looking for a job, or have a job to offer? Check out our official [Jobs board](https://jobs.nestjs.com).
173+
1. **Maintaining Clean Architecture:**
174+
- Ensuring domain models remain free of infrastructure concerns
175+
- Avoiding anemic domain models by placing business logic appropriately
86176

87-
## Support
177+
2. **TypeORM Integration:**
178+
- Properly mapping between domain entities and ORM entities
179+
- Managing relations while respecting domain boundaries
88180

89-
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
181+
3. **Testing Strategy:**
182+
- Creating effective unit tests for domain logic
183+
- Setting up integration tests with a test database
184+
- Mocking external dependencies appropriately
90185

91-
## Stay in touch
186+
## Future Improvements
92187

93-
- Author - [Kamil Myśliwiec](https://twitter.com/kammysliwiec)
94-
- Website - [https://nestjs.com](https://nestjs.com/)
95-
- Twitter - [@nestframework](https://twitter.com/nestframework)
188+
1. **Caching Layer:** Implement Redis caching for frequently accessed data
189+
2. **Session Conflict Detection:** Advanced algorithm to prevent double-booking
190+
3. **Recommendation System:** Movie recommendations based on watch history
191+
4. **Reporting Module:** Advanced analytics for managers
192+
5. **Microservices Architecture:** Split into microservices for better scalability
96193

97194
## License
98195

99-
Nest is [MIT licensed](https://github.com/nestjs/nest/blob/master/LICENSE).
196+
This project is licensed under the MIT License - see the LICENSE file for details.

0 commit comments

Comments
 (0)