veza/veza-backend-api/internal/handlers/kyc_handler.go
senke ef386e0ae3 fix(backend): commit swagger annotation pass + missing handler methods
routes_users.go (already on main) calls settingsHandler.GetPreferences /
UpdatePreferences and gdprExportHandler.ExportJSON, but the methods only
existed in the working tree — main wouldn't compile, so deploy.yml's
build-backend job was stuck on the same compile error every run.

Bundles the WIP swagger annotation sweep across chat / marketplace /
role / settings / gdpr / etc. handlers with the regenerated swagger.json,
swagger.yaml, docs.go and openapi.yaml.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 10:16:57 +02:00

117 lines
3.6 KiB
Go

package handlers
import (
"errors"
"net/http"
apperrors "veza-backend-api/internal/errors"
"veza-backend-api/internal/services"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
// KYCHandler handles seller identity verification endpoints (v0.13.5 TASK-MKT-001)
type KYCHandler struct {
kycService *services.KYCService
logger *zap.Logger
}
// NewKYCHandler creates a new KYC handler
func NewKYCHandler(kycService *services.KYCService, logger *zap.Logger) *KYCHandler {
return &KYCHandler{
kycService: kycService,
logger: logger,
}
}
// CreateVerificationRequest is the request body for starting KYC
type CreateVerificationRequest struct {
ReturnURL string `json:"return_url"`
}
// StartVerification creates a Stripe Identity verification session
// @Summary Start KYC verification
// @Description Initiate a Stripe Identity verification session for the seller.
// @Tags Sell
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param data body CreateVerificationRequest true "Return URL"
// @Success 201 {object} handlers.APIResponse{data=object}
// @Failure 401 {object} handlers.APIResponse "Unauthorized"
// @Router /api/v1/sell/kyc/start [post]
func (h *KYCHandler) StartVerification(c *gin.Context) {
if h.kycService == nil {
RespondWithAppError(c, apperrors.NewServiceUnavailableError("KYC verification is not available"))
return
}
userID, ok := GetUserIDUUID(c)
if !ok {
return
}
var req CreateVerificationRequest
_ = c.ShouldBindJSON(&req)
returnURL := req.ReturnURL
if returnURL == "" {
returnURL = c.Request.URL.Scheme + "://" + c.Request.Host + "/sell?kyc=complete"
}
session, err := h.kycService.CreateVerificationSession(c.Request.Context(), userID, returnURL)
if err != nil {
if errors.Is(err, services.ErrKYCAlreadyDone) {
RespondSuccess(c, http.StatusOK, gin.H{"status": "verified", "message": "Already verified"})
return
}
if errors.Is(err, services.ErrKYCNotAvailable) {
RespondWithAppError(c, apperrors.NewServiceUnavailableError("KYC verification is not available"))
return
}
if errors.Is(err, services.ErrNoStripeAccount) {
RespondWithAppError(c, apperrors.NewValidationError("Complete Stripe Connect onboarding first"))
return
}
h.logger.Error("StartVerification failed", zap.Error(err), zap.String("user_id", userID.String()))
RespondWithAppError(c, apperrors.NewInternalErrorWrap("Failed to start verification", err))
return
}
RespondSuccess(c, http.StatusCreated, session)
}
// GetVerificationStatus returns the current KYC status for a seller
// @Summary Get KYC status
// @Description Get the current identity verification status for the authenticated seller.
// @Tags Sell
// @Accept json
// @Produce json
// @Security BearerAuth
// @Success 200 {object} handlers.APIResponse{data=object{status=string}}
// @Failure 401 {object} handlers.APIResponse "Unauthorized"
// @Router /api/v1/sell/kyc/status [get]
func (h *KYCHandler) GetVerificationStatus(c *gin.Context) {
if h.kycService == nil {
RespondSuccess(c, http.StatusOK, gin.H{"status": "not_available"})
return
}
userID, ok := GetUserIDUUID(c)
if !ok {
return
}
status, err := h.kycService.GetVerificationStatus(c.Request.Context(), userID)
if err != nil {
if errors.Is(err, services.ErrKYCNotAvailable) {
RespondSuccess(c, http.StatusOK, gin.H{"status": "not_available"})
return
}
h.logger.Error("GetVerificationStatus failed", zap.Error(err), zap.String("user_id", userID.String()))
RespondWithAppError(c, apperrors.NewInternalErrorWrap("Failed to get verification status", err))
return
}
RespondSuccess(c, http.StatusOK, gin.H{"status": status})
}