diff --git a/backend/README.md b/backend/README.md index 3f90cd4..48bbfee 100644 --- a/backend/README.md +++ b/backend/README.md @@ -46,23 +46,38 @@ Este é o núcleo de performance do SaveInMed, responsável por operações crí backend/ ├── cmd/ │ └── api/ -│ └── main.go # Entry point da aplicação +│ └── main.go # Entry point da aplicação ├── internal/ -│ ├── config/ # Configurações -│ ├── domain/ # Modelos de domínio +│ ├── config/ # Configurações (100% coverage) +│ ├── domain/ # Modelos de domínio │ ├── http/ -│ │ ├── handler/ # Handlers HTTP -│ │ └── middleware/ # Middlewares (logging, compress) -│ ├── payments/ # Integração Mercado Pago +│ │ ├── handler/ # Handlers HTTP (refatorado) +│ │ │ ├── handler.go # Auth, Products, Orders, etc +│ │ │ ├── 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/ -│ │ └── postgres/ # Repositório PostgreSQL -│ ├── server/ # Configuração do servidor -│ └── usecase/ # Casos de uso / lógica de negócio -├── docs/ # Documentação Swagger +│ │ └── postgres/ # Repositório PostgreSQL +│ ├── server/ # Configuração do servidor (74.7% coverage) +│ └── usecase/ # Casos de uso (64.7% coverage) +├── docs/ # Documentação Swagger ├── Dockerfile └── 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 ### Variáveis de Ambiente diff --git a/backend/internal/http/handler/company_handler.go b/backend/internal/http/handler/company_handler.go new file mode 100644 index 0000000..618481b --- /dev/null +++ b/backend/internal/http/handler/company_handler.go @@ -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) +} diff --git a/backend/internal/http/handler/handler.go b/backend/internal/http/handler/handler.go index 0415036..01b0960 100644 --- a/backend/internal/http/handler/handler.go +++ b/backend/internal/http/handler/handler.go @@ -113,235 +113,6 @@ func (h *Handler) Login(w http.ResponseWriter, r *http.Request) { 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 // @Summary Cadastro de produto com rastreabilidade de lote // @Tags Produtos diff --git a/marketplace/README.md b/marketplace/README.md index 6ab01da..9bc41bb 100644 --- a/marketplace/README.md +++ b/marketplace/README.md @@ -66,35 +66,66 @@ Este é o frontend do marketplace SaveInMed, onde farmácias e distribuidoras po marketplace/ ├── src/ │ ├── components/ -│ │ └── ProtectedRoute.tsx # Componente de rota protegida +│ │ └── ProtectedRoute.tsx # Componente de rota protegida │ ├── context/ -│ │ └── AuthContext.tsx # Contexto de autenticação +│ │ └── AuthContext.tsx # Contexto de autenticação │ ├── hooks/ │ │ └── usePersistentFilters.ts # Hook para filtros persistentes │ ├── layouts/ -│ │ └── Shell.tsx # Layout principal +│ │ └── Shell.tsx # Layout principal │ ├── pages/ -│ │ ├── Cart.tsx # Página do carrinho -│ │ ├── Checkout.tsx # Página de checkout -│ │ ├── Dashboard.tsx # Dashboard do usuário -│ │ ├── Login.tsx # Página de login -│ │ └── Profile.tsx # Página de perfil +│ │ ├── Cart.tsx # Página do carrinho +│ │ ├── Checkout.tsx # Página de checkout +│ │ ├── Company.tsx # Perfil da empresa [NEW] +│ │ ├── Dashboard.tsx # Dashboard do usuário +│ │ ├── 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/ -│ │ └── apiClient.ts # Cliente API configurado +│ │ └── apiClient.ts # Cliente API configurado │ ├── stores/ -│ │ └── cartStore.ts # Store Zustand do carrinho +│ │ └── cartStore.ts # Store Zustand do carrinho +│ ├── test/ +│ │ └── setup.ts # Setup Vitest │ ├── types/ -│ │ └── product.ts # Tipos TypeScript -│ ├── App.tsx # Componente raiz -│ ├── main.tsx # Entry point -│ └── index.css # Estilos globais +│ │ └── product.ts # Tipos TypeScript +│ ├── App.tsx # Componente raiz +│ ├── main.tsx # Entry point +│ └── index.css # Estilos globais ├── index.html ├── vite.config.ts +├── vitest.config.ts # Config de testes ├── tailwind.config.ts ├── tsconfig.json └── 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 ### Variáveis de Ambiente