refactor(handler): extract company handlers + update READMEs

Backend:
- Extract 8 company handlers to company_handler.go (228 lines)
- handler.go reduced from 1254 to ~1026 lines
- Total refactoring: ~35% of original handler.go

READMEs updated:
- Backend: new architecture, test coverage table
- Marketplace: new pages (Orders, Inventory, Company, SellerDashboard), Vitest info
This commit is contained in:
Tiago Yamamoto 2025-12-20 07:58:37 -03:00
parent 23df78d9c3
commit e40517aac4
4 changed files with 309 additions and 253 deletions

View file

@ -46,23 +46,38 @@ Este é o núcleo de performance do SaveInMed, responsável por operações crí
backend/ backend/
├── cmd/ ├── cmd/
│ └── api/ │ └── api/
│ └── main.go # Entry point da aplicação │ └── main.go # Entry point da aplicação
├── internal/ ├── internal/
│ ├── config/ # Configurações │ ├── config/ # Configurações (100% coverage)
│ ├── domain/ # Modelos de domínio │ ├── domain/ # Modelos de domínio
│ ├── http/ │ ├── http/
│ │ ├── handler/ # Handlers HTTP │ │ ├── handler/ # Handlers HTTP (refatorado)
│ │ └── middleware/ # Middlewares (logging, compress) │ │ │ ├── handler.go # Auth, Products, Orders, etc
│ ├── payments/ # Integração Mercado Pago │ │ │ ├── company_handler.go # CRUD de empresas
│ │ │ └── dto.go # DTOs e funções utilitárias
│ │ └── middleware/ # Middlewares (95.9% coverage)
│ ├── payments/ # Integração MercadoPago (100% coverage)
│ ├── repository/ │ ├── repository/
│ │ └── postgres/ # Repositório PostgreSQL │ │ └── postgres/ # Repositório PostgreSQL
│ ├── server/ # Configuração do servidor │ ├── server/ # Configuração do servidor (74.7% coverage)
│ └── usecase/ # Casos de uso / lógica de negócio │ └── usecase/ # Casos de uso (64.7% coverage)
├── docs/ # Documentação Swagger ├── docs/ # Documentação Swagger
├── Dockerfile ├── Dockerfile
└── README.md └── README.md
``` ```
## 🧪 Cobertura de Testes
| Pacote | Cobertura |
|--------|-----------|
| `config` | **100%** ✅ |
| `middleware` | **95.9%** ✅ |
| `payments` | **100%** ✅ |
| `usecase` | **64.7%** |
| `server` | **74.7%** |
| `handler` | 6.6% |
## 🔧 Configuração ## 🔧 Configuração
### Variáveis de Ambiente ### Variáveis de Ambiente

View file

@ -0,0 +1,239 @@
package handler
import (
"errors"
"net/http"
"strings"
"github.com/saveinmed/backend-go/internal/domain"
"github.com/saveinmed/backend-go/internal/http/middleware"
)
// CreateCompany godoc
// @Summary Registro de empresas
// @Description Cadastra farmácia, distribuidora ou administrador com CNPJ e licença sanitária.
// @Tags Empresas
// @Accept json
// @Produce json
// @Param company body registerCompanyRequest true "Dados da empresa"
// @Success 201 {object} domain.Company
// @Router /api/v1/companies [post]
func (h *Handler) CreateCompany(w http.ResponseWriter, r *http.Request) {
var req registerCompanyRequest
if err := decodeJSON(r.Context(), r, &req); err != nil {
writeError(w, http.StatusBadRequest, err)
return
}
company := &domain.Company{
Role: req.Role,
CNPJ: req.CNPJ,
CorporateName: req.CorporateName,
LicenseNumber: req.LicenseNumber,
}
if err := h.svc.RegisterCompany(r.Context(), company); err != nil {
writeError(w, http.StatusInternalServerError, err)
return
}
writeJSON(w, http.StatusCreated, company)
}
// ListCompanies godoc
// @Summary Lista empresas
// @Tags Empresas
// @Produce json
// @Success 200 {array} domain.Company
// @Router /api/v1/companies [get]
func (h *Handler) ListCompanies(w http.ResponseWriter, r *http.Request) {
companies, err := h.svc.ListCompanies(r.Context())
if err != nil {
writeError(w, http.StatusInternalServerError, err)
return
}
writeJSON(w, http.StatusOK, companies)
}
// GetCompany godoc
// @Summary Obter empresa
// @Tags Empresas
// @Produce json
// @Param id path string true "Company ID"
// @Success 200 {object} domain.Company
// @Failure 404 {object} map[string]string
// @Router /api/v1/companies/{id} [get]
func (h *Handler) GetCompany(w http.ResponseWriter, r *http.Request) {
id, err := parseUUIDFromPath(r.URL.Path)
if err != nil {
writeError(w, http.StatusBadRequest, err)
return
}
company, err := h.svc.GetCompany(r.Context(), id)
if err != nil {
writeError(w, http.StatusNotFound, err)
return
}
writeJSON(w, http.StatusOK, company)
}
// UpdateCompany godoc
// @Summary Atualizar empresa
// @Tags Empresas
// @Accept json
// @Produce json
// @Param id path string true "Company ID"
// @Param payload body updateCompanyRequest true "Campos para atualização"
// @Success 200 {object} domain.Company
// @Failure 400 {object} map[string]string
// @Failure 404 {object} map[string]string
// @Router /api/v1/companies/{id} [patch]
func (h *Handler) UpdateCompany(w http.ResponseWriter, r *http.Request) {
id, err := parseUUIDFromPath(r.URL.Path)
if err != nil {
writeError(w, http.StatusBadRequest, err)
return
}
var req updateCompanyRequest
if err := decodeJSON(r.Context(), r, &req); err != nil {
writeError(w, http.StatusBadRequest, err)
return
}
company, err := h.svc.GetCompany(r.Context(), id)
if err != nil {
writeError(w, http.StatusNotFound, err)
return
}
if req.Role != nil {
company.Role = *req.Role
}
if req.CNPJ != nil {
company.CNPJ = *req.CNPJ
}
if req.CorporateName != nil {
company.CorporateName = *req.CorporateName
}
if req.LicenseNumber != nil {
company.LicenseNumber = *req.LicenseNumber
}
if req.IsVerified != nil {
company.IsVerified = *req.IsVerified
}
if err := h.svc.UpdateCompany(r.Context(), company); err != nil {
writeError(w, http.StatusInternalServerError, err)
return
}
writeJSON(w, http.StatusOK, company)
}
// DeleteCompany godoc
// @Summary Remover empresa
// @Tags Empresas
// @Param id path string true "Company ID"
// @Success 204 ""
// @Failure 400 {object} map[string]string
// @Failure 404 {object} map[string]string
// @Router /api/v1/companies/{id} [delete]
func (h *Handler) DeleteCompany(w http.ResponseWriter, r *http.Request) {
id, err := parseUUIDFromPath(r.URL.Path)
if err != nil {
writeError(w, http.StatusBadRequest, err)
return
}
if err := h.svc.DeleteCompany(r.Context(), id); err != nil {
writeError(w, http.StatusBadRequest, err)
return
}
w.WriteHeader(http.StatusNoContent)
}
// VerifyCompany godoc
// @Summary Verificar empresa
// @Tags Empresas
// @Security BearerAuth
// @Param id path string true "Company ID"
// @Success 200 {object} domain.Company
// @Router /api/v1/companies/{id}/verify [patch]
// VerifyCompany toggles the verification flag for a company (admin only).
func (h *Handler) VerifyCompany(w http.ResponseWriter, r *http.Request) {
if !strings.HasSuffix(r.URL.Path, "/verify") {
http.NotFound(w, r)
return
}
id, err := parseUUIDFromPath(r.URL.Path)
if err != nil {
writeError(w, http.StatusBadRequest, err)
return
}
company, err := h.svc.VerifyCompany(r.Context(), id)
if err != nil {
writeError(w, http.StatusInternalServerError, err)
return
}
writeJSON(w, http.StatusOK, company)
}
// GetMyCompany godoc
// @Summary Obter minha empresa
// @Tags Empresas
// @Security BearerAuth
// @Produce json
// @Success 200 {object} domain.Company
// @Router /api/v1/companies/me [get]
// GetMyCompany returns the company linked to the authenticated user.
func (h *Handler) GetMyCompany(w http.ResponseWriter, r *http.Request) {
claims, ok := middleware.GetClaims(r.Context())
if !ok || claims.CompanyID == nil {
writeError(w, http.StatusBadRequest, errors.New("missing company context"))
return
}
company, err := h.svc.GetCompany(r.Context(), *claims.CompanyID)
if err != nil {
writeError(w, http.StatusNotFound, err)
return
}
writeJSON(w, http.StatusOK, company)
}
// GetCompanyRating godoc
// @Summary Obter avaliação da empresa
// @Tags Empresas
// @Produce json
// @Param id path string true "Company ID"
// @Success 200 {object} domain.CompanyRating
// @Router /api/v1/companies/{id}/rating [get]
// GetCompanyRating exposes the average score for a company.
func (h *Handler) GetCompanyRating(w http.ResponseWriter, r *http.Request) {
if !strings.HasSuffix(r.URL.Path, "/rating") {
http.NotFound(w, r)
return
}
id, err := parseUUIDFromPath(r.URL.Path)
if err != nil {
writeError(w, http.StatusBadRequest, err)
return
}
rating, err := h.svc.GetCompanyRating(r.Context(), id)
if err != nil {
writeError(w, http.StatusInternalServerError, err)
return
}
writeJSON(w, http.StatusOK, rating)
}

View file

@ -113,235 +113,6 @@ func (h *Handler) Login(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, authResponse{Token: token, ExpiresAt: exp}) writeJSON(w, http.StatusOK, authResponse{Token: token, ExpiresAt: exp})
} }
// CreateCompany godoc
// @Summary Registro de empresas
// @Description Cadastra farmácia, distribuidora ou administrador com CNPJ e licença sanitária.
// @Tags Empresas
// @Accept json
// @Produce json
// @Param company body registerCompanyRequest true "Dados da empresa"
// @Success 201 {object} domain.Company
// @Router /api/v1/companies [post]
func (h *Handler) CreateCompany(w http.ResponseWriter, r *http.Request) {
var req registerCompanyRequest
if err := decodeJSON(r.Context(), r, &req); err != nil {
writeError(w, http.StatusBadRequest, err)
return
}
company := &domain.Company{
Role: req.Role,
CNPJ: req.CNPJ,
CorporateName: req.CorporateName,
LicenseNumber: req.LicenseNumber,
}
if err := h.svc.RegisterCompany(r.Context(), company); err != nil {
writeError(w, http.StatusInternalServerError, err)
return
}
writeJSON(w, http.StatusCreated, company)
}
// ListCompanies godoc
// @Summary Lista empresas
// @Tags Empresas
// @Produce json
// @Success 200 {array} domain.Company
// @Router /api/v1/companies [get]
func (h *Handler) ListCompanies(w http.ResponseWriter, r *http.Request) {
companies, err := h.svc.ListCompanies(r.Context())
if err != nil {
writeError(w, http.StatusInternalServerError, err)
return
}
writeJSON(w, http.StatusOK, companies)
}
// GetCompany godoc
// @Summary Obter empresa
// @Tags Empresas
// @Produce json
// @Param id path string true "Company ID"
// @Success 200 {object} domain.Company
// @Failure 404 {object} map[string]string
// @Router /api/v1/companies/{id} [get]
func (h *Handler) GetCompany(w http.ResponseWriter, r *http.Request) {
id, err := parseUUIDFromPath(r.URL.Path)
if err != nil {
writeError(w, http.StatusBadRequest, err)
return
}
company, err := h.svc.GetCompany(r.Context(), id)
if err != nil {
writeError(w, http.StatusNotFound, err)
return
}
writeJSON(w, http.StatusOK, company)
}
// UpdateCompany godoc
// @Summary Atualizar empresa
// @Tags Empresas
// @Accept json
// @Produce json
// @Param id path string true "Company ID"
// @Param payload body updateCompanyRequest true "Campos para atualização"
// @Success 200 {object} domain.Company
// @Failure 400 {object} map[string]string
// @Failure 404 {object} map[string]string
// @Router /api/v1/companies/{id} [patch]
func (h *Handler) UpdateCompany(w http.ResponseWriter, r *http.Request) {
id, err := parseUUIDFromPath(r.URL.Path)
if err != nil {
writeError(w, http.StatusBadRequest, err)
return
}
var req updateCompanyRequest
if err := decodeJSON(r.Context(), r, &req); err != nil {
writeError(w, http.StatusBadRequest, err)
return
}
company, err := h.svc.GetCompany(r.Context(), id)
if err != nil {
writeError(w, http.StatusNotFound, err)
return
}
if req.Role != nil {
company.Role = *req.Role
}
if req.CNPJ != nil {
company.CNPJ = *req.CNPJ
}
if req.CorporateName != nil {
company.CorporateName = *req.CorporateName
}
if req.LicenseNumber != nil {
company.LicenseNumber = *req.LicenseNumber
}
if req.IsVerified != nil {
company.IsVerified = *req.IsVerified
}
if err := h.svc.UpdateCompany(r.Context(), company); err != nil {
writeError(w, http.StatusInternalServerError, err)
return
}
writeJSON(w, http.StatusOK, company)
}
// DeleteCompany godoc
// @Summary Remover empresa
// @Tags Empresas
// @Param id path string true "Company ID"
// @Success 204 ""
// @Failure 400 {object} map[string]string
// @Failure 404 {object} map[string]string
// @Router /api/v1/companies/{id} [delete]
func (h *Handler) DeleteCompany(w http.ResponseWriter, r *http.Request) {
id, err := parseUUIDFromPath(r.URL.Path)
if err != nil {
writeError(w, http.StatusBadRequest, err)
return
}
if err := h.svc.DeleteCompany(r.Context(), id); err != nil {
writeError(w, http.StatusBadRequest, err)
return
}
w.WriteHeader(http.StatusNoContent)
}
// VerifyCompany godoc
// @Summary Verificar empresa
// @Tags Empresas
// @Security BearerAuth
// @Param id path string true "Company ID"
// @Success 200 {object} domain.Company
// @Router /api/v1/companies/{id}/verify [patch]
// VerifyCompany toggles the verification flag for a company (admin only).
func (h *Handler) VerifyCompany(w http.ResponseWriter, r *http.Request) {
if !strings.HasSuffix(r.URL.Path, "/verify") {
http.NotFound(w, r)
return
}
id, err := parseUUIDFromPath(r.URL.Path)
if err != nil {
writeError(w, http.StatusBadRequest, err)
return
}
company, err := h.svc.VerifyCompany(r.Context(), id)
if err != nil {
writeError(w, http.StatusInternalServerError, err)
return
}
writeJSON(w, http.StatusOK, company)
}
// GetMyCompany godoc
// @Summary Obter minha empresa
// @Tags Empresas
// @Security BearerAuth
// @Produce json
// @Success 200 {object} domain.Company
// @Router /api/v1/companies/me [get]
// GetMyCompany returns the company linked to the authenticated user.
func (h *Handler) GetMyCompany(w http.ResponseWriter, r *http.Request) {
claims, ok := middleware.GetClaims(r.Context())
if !ok || claims.CompanyID == nil {
writeError(w, http.StatusBadRequest, errors.New("missing company context"))
return
}
company, err := h.svc.GetCompany(r.Context(), *claims.CompanyID)
if err != nil {
writeError(w, http.StatusNotFound, err)
return
}
writeJSON(w, http.StatusOK, company)
}
// GetCompanyRating godoc
// @Summary Obter avaliação da empresa
// @Tags Empresas
// @Produce json
// @Param id path string true "Company ID"
// @Success 200 {object} domain.CompanyRating
// @Router /api/v1/companies/{id}/rating [get]
// GetCompanyRating exposes the average score for a company.
func (h *Handler) GetCompanyRating(w http.ResponseWriter, r *http.Request) {
if !strings.HasSuffix(r.URL.Path, "/rating") {
http.NotFound(w, r)
return
}
id, err := parseUUIDFromPath(r.URL.Path)
if err != nil {
writeError(w, http.StatusBadRequest, err)
return
}
rating, err := h.svc.GetCompanyRating(r.Context(), id)
if err != nil {
writeError(w, http.StatusInternalServerError, err)
return
}
writeJSON(w, http.StatusOK, rating)
}
// CreateProduct godoc // CreateProduct godoc
// @Summary Cadastro de produto com rastreabilidade de lote // @Summary Cadastro de produto com rastreabilidade de lote
// @Tags Produtos // @Tags Produtos

View file

@ -66,35 +66,66 @@ Este é o frontend do marketplace SaveInMed, onde farmácias e distribuidoras po
marketplace/ marketplace/
├── src/ ├── src/
│ ├── components/ │ ├── components/
│ │ └── ProtectedRoute.tsx # Componente de rota protegida │ │ └── ProtectedRoute.tsx # Componente de rota protegida
│ ├── context/ │ ├── context/
│ │ └── AuthContext.tsx # Contexto de autenticação │ │ └── AuthContext.tsx # Contexto de autenticação
│ ├── hooks/ │ ├── hooks/
│ │ └── usePersistentFilters.ts # Hook para filtros persistentes │ │ └── usePersistentFilters.ts # Hook para filtros persistentes
│ ├── layouts/ │ ├── layouts/
│ │ └── Shell.tsx # Layout principal │ │ └── Shell.tsx # Layout principal
│ ├── pages/ │ ├── pages/
│ │ ├── Cart.tsx # Página do carrinho │ │ ├── Cart.tsx # Página do carrinho
│ │ ├── Checkout.tsx # Página de checkout │ │ ├── Checkout.tsx # Página de checkout
│ │ ├── Dashboard.tsx # Dashboard do usuário │ │ ├── Company.tsx # Perfil da empresa [NEW]
│ │ ├── Login.tsx # Página de login │ │ ├── Dashboard.tsx # Dashboard do usuário
│ │ └── Profile.tsx # Página de perfil │ │ ├── Inventory.tsx # Gestão de estoque [NEW]
│ │ ├── Login.tsx # Página de login
│ │ ├── Orders.tsx # Pedidos [NEW]
│ │ ├── Profile.tsx # Página de perfil
│ │ └── SellerDashboard.tsx # Dashboard vendedor [NEW]
│ ├── services/ │ ├── services/
│ │ └── apiClient.ts # Cliente API configurado │ │ └── apiClient.ts # Cliente API configurado
│ ├── stores/ │ ├── stores/
│ │ └── cartStore.ts # Store Zustand do carrinho │ │ └── cartStore.ts # Store Zustand do carrinho
│ ├── test/
│ │ └── setup.ts # Setup Vitest
│ ├── types/ │ ├── types/
│ │ └── product.ts # Tipos TypeScript │ │ └── product.ts # Tipos TypeScript
│ ├── App.tsx # Componente raiz │ ├── App.tsx # Componente raiz
│ ├── main.tsx # Entry point │ ├── main.tsx # Entry point
│ └── index.css # Estilos globais │ └── index.css # Estilos globais
├── index.html ├── index.html
├── vite.config.ts ├── vite.config.ts
├── vitest.config.ts # Config de testes
├── tailwind.config.ts ├── tailwind.config.ts
├── tsconfig.json ├── tsconfig.json
└── README.md └── README.md
``` ```
## 🧪 Testes
O projeto utiliza **Vitest** para testes unitários:
```bash
# Executar testes
npm test
# Executar testes com coverage
npm run test:coverage
# Executar testes uma vez (CI)
npm test -- --run
```
### Cobertura Atual
| Categoria | Testes |
|-----------|--------|
| `cartStore` | 15 ✅ |
| `apiClient` | 7 ✅ |
| `usePersistentFilters` | 5 ✅ |
| **Total** | **27** ✅ |
## 🔧 Configuração ## 🔧 Configuração
### Variáveis de Ambiente ### Variáveis de Ambiente