How andersonkaiti/ws-rede-ct works
Project Type and Purpose
• This is a standalone RESTful API backend (“API da RedeCT”) built with Node.js/Express, TypeScript and Prisma for PostgreSQL.
• Its responsibilities: authentication (JWT + bcrypt), user & team management, news, certifications, pendencies, file uploads to Firebase Storage, and interactive docs via OpenAPI.
Architectural Overview
• Layered (MVC-style) architecture with clear separation:
• HTTP Layer (bin/server.ts, src/app.ts, src/routes)
• Controller Layer (src/controllers)
• Service Layer (src/services + src/factories/services)
• Repository/Data Access Layer (src/repositories + Prisma ORM)
• Infrastructure/Config (src/config, .env, Docker, Prisma migrations)
• External dependencies: PostgreSQL database, Firebase Storage, OpenAPI docs endpoint.
Main Components
a. Clients & Entry Point
• External clients (mobile/web) or API consumers.
• HTTP server bootstrap (bin/server.ts).
• Express app module (src/app.ts).
b. Routing & Middleware
• Route definitions (src/routes/*.ts).
• Global middleware (CORS, JSON parser), auth middleware (JWT), multer file‐upload middleware.
c. Controllers
• One controller per resource (auth, users, news, teams, certifications, pendencies).
• Handles HTTP request → calls services.
d. Services
• Auth services: JWT issuance/validation, bcrypt password hashing.
• Firebase Storage service for file uploads.
• Any other business logic.
e. Repositories & Database
• Prisma Client (schema.prisma) as ORM.
• Repositories implement interfaces (icertification-repository, iuser-repository, etc).
• PostgreSQL database (hosted via Docker Compose).
f. OpenAPI Documentation
• OpenAPI spec modules (src/openapi) powering /docs endpoint.
g. External Systems
• Firebase Storage bucket.
• PostgreSQL instance (docker).
Interactions & Data Flow
1. Client → HTTP(S) Request → Express (bin/server.ts → src/app.ts)
2. Request passes through global middleware (CORS, JSON parsing) → auth middleware if protected → multer for file uploads if applicable.
3. Routed to appropriate controller (e.g., Sign-In Controller).
4. Controller delegates to a Service (e.g., JWT service, Firebase service, business service).
5. Service uses one or more Repositories to perform DB reads/writes via Prisma.
6. For file operations, service calls Firebase Storage API.
7. Responses bubble back: Service → Controller → Express → Client.
8. API docs served by OpenAPI module at /docs.
Architectural Patterns & Principles
• Layered Separation of Concerns
• Factory Pattern for creating controllers, services, repositories, and middleware (src/factories).
• Dependency Injection via factories to decouple implementations from interfaces.
• Interface-based repositories for testability and future swapping.
• Environment-based configuration (src/config/env.ts).
• Declarative DB migrations and seeding via Prisma.