How silascunha97/api_para_treinos works
Purpose and Entry Points
Workout Tracker is a TypeScript backend for recording people, exercise catalog entries, workout sessions, and performed sets, with JWT-based account access. Runtime bootstrapping is through src/index.ts, which assembles the Express/Apollo application from src/main/app.ts; the GraphQL server is defined in src/presentation/graphql/server.ts and exposed at /graphql.
Architectural Shape
The repository follows dependency inversion across four layers:
• Domain: entities, DTOs, and contracts independent of frameworks: src/domain/entities/, src/domain/repositories/, src/domain/providers/.
• Application: operation-specific orchestration through use cases: src/application/use-cases/auth/, src/application/use-cases/exercicio/.
• Infrastructure: Prisma persistence plus replaceable service adapters: src/infrastructure/repositories/, src/infrastructure/providers/.
• Presentation/composition: GraphQL schema and resolvers invoke factory-built use cases: src/presentation/graphql/, src/main/factories/.
Workout Domain and Flows
The core model is Pessoa → SessaoTreino → Serie, with each series linked to an Exercicio. Domain representations are in src/domain/entities/Pessoa.ts, src/domain/entities/SessaoTreino.ts, and src/domain/entities/Serie.ts; API contracts are carried by src/domain/dtos/.
A typical mutation/query flow is GraphQL resolver → factory-composed use case → domain repository interface → Prisma/cached repository → PostgreSQL. Current operations include person creation, exercise creation/listing, and session completion, centered in src/application/use-cases/pessoa/CriarPessoaUseCase.ts, src/application/use-cases/exercicio/CriarExercicioUseCase.ts, and src/application/use-cases/exercicio/FinalizarSessaoTreinoUseCase.ts.
Authentication Boundary
Authentication is isolated behind provider and repository interfaces, preventing use cases from importing bcrypt, JWT, or Prisma. Account registration and password login are implemented in src/application/use-cases/auth/CriarContaUseCase.ts and src/application/use-cases/auth/AutenticarComSenhaUseCase.ts.
Concrete boundaries are bcrypt/JWT adapters (src/infrastructure/providers/BcryptAdapter.ts, src/infrastructure/providers/JwtAdapter.ts) and persisted user/refresh-token repositories (src/infrastructure/repositories/PrismaUsuarioRepository.ts, src/infrastructure/repositories/PrismaRefreshTokenRepository.ts). GraphQL context extracts Bearer credentials and guards protected operations through src/presentation/graphql/context/index.ts and src/presentation/graphql/guards/graph.guards.ts. Refresh-token rotation is stateful in PostgreSQL; access-token validation is stateless JWT.
GraphQL Presentation and Composition
Schema definitions and resolver registration are concentrated under src/presentation/graphql/typeDefs/index.ts and src/presentation/graphql/resolvers/index.ts, with feature resolvers such as src/presentation/graphql/resolvers/authResolvers.ts. Resolvers remain an adapter layer: dependencies are wired in focused composition roots, including src/main/factories/authFactory.ts and src/main/factories/exercicioFactory.ts. Presentation errors are normalized by src/presentation/errors/formatError.ts.
Persistence, Caching, and Async Metrics
PostgreSQL is the system of record, modeled in prisma/schema.prisma and accessed through the Prisma client at src/infrastructure/database/prisma/prismaClient.ts. Prisma repositories implement domain contracts; cache-decorated exercise/session repositories introduce Redis as an optional read boundary: src/infrastructure/repositories/CachedExercicioRepository.ts, src/infrastructure/repositories/CachedSessaoTreinoRepository.ts, and src/infrastructure/providers/RedisAdapter.ts.
Workout metric work is separated asynchronously through RabbitMQ (src/infrastructure/providers/RabbitMQAdapter.ts) and a consumer worker, src/infrastructure/workers/treinoMetricsWorker.ts, avoiding metric processing on the synchronous GraphQL request path.
Runtime and Infrastructure
The material runtime is Node.js 20+ with TypeScript, Express, and Apollo Server; PostgreSQL 16, Redis, and RabbitMQ are the external state/messaging services implied by adapters. Local/container deployment boundaries are captured in Dockerfile and docker-compose.yml; Prisma configuration is in prisma.config.ts. Environment typing and secrets/configuration contracts begin at src/@types/environment.d.ts and .env.example.
Main entry points: FinalizarSessaoTreinoUseCase.ts Workout use cases · server.ts GraphQL server · authFactory.ts Use-case factories · CachedExercicioRepository.ts Cached workout repositories
Components 18
Runtime
- Node bootstrap
src/index.ts - Express/Apollo app
src/main/app.ts
Presentation & Composition
- GraphQL server · Exposes the API at /graphql.
src/presentation/graphql/server.ts - Schema & resolvers
src/presentation/graphql/resolvers/index.ts - Auth context & guards
src/presentation/graphql/context/index.ts - Use-case factories
src/main/factories/authFactory.ts
Application
- Workout use cases · Creates people and exercises and completes workout sessions.
src/application/use-cases/exercicio/FinalizarSessaoTreinoUseCase.ts - Auth use cases · Registers accounts and authenticates password login.
src/application/use-cases/auth/AutenticarComSenhaUseCase.ts
Domain
- Workout model · Pessoa owns sessions; sessions contain series linked to exercises.
src/domain/entities/SessaoTreino.ts - Repository & provider contracts
Infrastructure
- Prisma repositories
src/infrastructure/repositories/PrismaUsuarioRepository.ts - Cached workout repositories · Adds optional Redis reads for exercises and sessions.
src/infrastructure/repositories/CachedExercicioRepository.ts - Bcrypt & JWT adapters
src/infrastructure/providers/JwtAdapter.ts - Prisma client
src/infrastructure/database/prisma/prismaClient.ts - PostgreSQL · Stores workout data, users, and refresh-token rotation state.
prisma/schema.prisma - Redis
src/infrastructure/providers/RedisAdapter.ts - RabbitMQ
src/infrastructure/providers/RabbitMQAdapter.ts - Training metrics worker · Processes workout metrics off the request path.
src/infrastructure/workers/treinoMetricsWorker.ts
Connections 20
- Node bootstrap → Express/Apollo app: starts
- Express/Apollo app → GraphQL server: mounts
- GraphQL server → Schema & resolvers: serves
- GraphQL server → Auth context & guards: builds context
- Schema & resolvers → Use-case factories: obtains use cases
- Use-case factories → Workout use cases: wires
- Use-case factories → Auth use cases: wires
- Auth context & guards → Bcrypt & JWT adapters: validates access JWT
- Workout use cases → Workout model: orchestrates
- Workout use cases → Repository & provider contracts: depends on
- Auth use cases → Repository & provider contracts: depends on
- Repository & provider contracts → Prisma repositories: implemented by
- Repository & provider contracts → Cached workout repositories: implemented by
- Repository & provider contracts → Bcrypt & JWT adapters: implemented by
- Prisma repositories → Prisma client: uses
- Cached workout repositories → Redis: reads/writes cache
- Cached workout repositories → Prisma repositories: falls through to
- Prisma client → PostgreSQL: persists
- Workout use cases → RabbitMQ: publishes metric work
- RabbitMQ → Training metrics worker: delivers jobs