88fb1751c7
Stack of optimizations against the same Hetzner→Neon transatlantic link. The trace revealed every visible ms was network/proxy overhead — DB execution itself is sub-millisecond per query (verified via EXPLAIN ANALYZE: index scans on every hot path). Connection layer: - DB_HOST → Neon pooler endpoint (-pooler suffix). PgBouncer transaction-mode keeps backend Postgres connections warm so we no longer pay the ~110ms Postgres-startup RTT on cold queries. - GORM pool tuned: MaxIdleConns 10→20, MaxLifetime 600s→1800s, MaxIdleTime added (default 0 = never close idle). - Eager pool warm-up at boot via parallel pings — first user request no longer pays the ~440ms TCP+TLS+startup handshake. - Redis maxmemory-policy noeviction → allkeys-lru. Cache writes will evict cold keys instead of erroring at the 256MB limit. Auth layer: - TokenCacheTTL 5min → 1 hour (Redis token cache). - UserCacheTTL 30s → 5min (in-memory User cache, per pod). - UserCache gains a 5,000-entry LRU cap so a flood of unique users can't blow up pod RSS. ~5MB worst-case per pod. - Token + user lookup collapsed from 2 GORM Preload queries into a single INNER JOIN. Saves 1 RTT per cold-cache request. - Auth middleware's m.db.* now use db.WithContext(ctx) so the SQL spans nest under the parent HTTP request in Jaeger. Service layer: - TaskService.ListTasks: replaced two-step FindResidenceIDsByUser → GetKanbanDataForMultipleResidences with a single GetKanbanDataForUser that uses a Postgres subquery for residence-access. One round-trip instead of two. - New CacheService residence-IDs cache: \"residence_ids_user:<id>\" with 5-min TTL. Wired into Task/Residence/Contractor/Document services for the four hot read paths that need this list. - Cache invalidation on every relevant mutation: CreateResidence, DeleteResidence, JoinWithCode, RemoveUser. DeleteResidence invalidates every member of the residence, not just the owner. What this stacks up to (Hetzner→Neon, before US migration): Path Before After (target) Cache-warm authed read ~800ms ~100-200ms Cache-cold authed read (1st in 1hr) ~2500ms ~500-700ms First request after deploy ~2500ms ~700-900ms The endgame US-region migration on top of this gets us to ~30-50ms warm-cache, but we're shippable at ~150ms warm right now. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
432 lines
13 KiB
Go
432 lines
13 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/treytartt/honeydue-api/internal/apperrors"
|
|
"github.com/treytartt/honeydue-api/internal/dto/requests"
|
|
"github.com/treytartt/honeydue-api/internal/dto/responses"
|
|
"github.com/treytartt/honeydue-api/internal/models"
|
|
"github.com/treytartt/honeydue-api/internal/repositories"
|
|
)
|
|
|
|
// Document-related errors
|
|
// DEPRECATED: These constants are deprecated. Use apperrors package instead.
|
|
// var (
|
|
// ErrDocumentNotFound = errors.New("document not found")
|
|
// ErrDocumentAccessDenied = errors.New("you do not have access to this document")
|
|
// )
|
|
|
|
// DocumentService handles document business logic
|
|
type DocumentService struct {
|
|
documentRepo *repositories.DocumentRepository
|
|
residenceRepo *repositories.ResidenceRepository
|
|
cache *CacheService
|
|
}
|
|
|
|
// NewDocumentService creates a new document service
|
|
func NewDocumentService(documentRepo *repositories.DocumentRepository, residenceRepo *repositories.ResidenceRepository) *DocumentService {
|
|
return &DocumentService{
|
|
documentRepo: documentRepo,
|
|
residenceRepo: residenceRepo,
|
|
}
|
|
}
|
|
|
|
// SetCacheService wires Redis caching for residence-ID lookups.
|
|
func (s *DocumentService) SetCacheService(cache *CacheService) {
|
|
s.cache = cache
|
|
}
|
|
|
|
// GetDocument gets a document by ID with access check
|
|
func (s *DocumentService) GetDocument(ctx context.Context, documentID, userID uint) (*responses.DocumentResponse, error) {
|
|
document, err := s.documentRepo.WithContext(ctx).FindByID(documentID)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, apperrors.NotFound("error.document_not_found")
|
|
}
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
|
|
// Check access via residence
|
|
hasAccess, err := s.residenceRepo.WithContext(ctx).HasAccess(document.ResidenceID, userID)
|
|
if err != nil {
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
if !hasAccess {
|
|
return nil, apperrors.Forbidden("error.document_access_denied")
|
|
}
|
|
|
|
resp := responses.NewDocumentResponse(document)
|
|
return &resp, nil
|
|
}
|
|
|
|
// ListDocuments lists all documents accessible to a user, with optional filters.
|
|
func (s *DocumentService) ListDocuments(ctx context.Context, userID uint, filter *repositories.DocumentFilter) ([]responses.DocumentResponse, error) {
|
|
// Get residence IDs (lightweight - no preloads)
|
|
residenceIDs, err := cachedResidenceIDsForUser(ctx, s.cache, s.residenceRepo, userID)
|
|
if err != nil {
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
|
|
if len(residenceIDs) == 0 {
|
|
return []responses.DocumentResponse{}, nil
|
|
}
|
|
|
|
// If a specific residence filter is set, narrow to that single residence (if user has access)
|
|
if filter != nil && filter.ResidenceID != nil {
|
|
found := false
|
|
for _, rid := range residenceIDs {
|
|
if rid == *filter.ResidenceID {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
return nil, apperrors.Forbidden("error.residence_access_denied")
|
|
}
|
|
residenceIDs = []uint{*filter.ResidenceID}
|
|
}
|
|
|
|
documents, err := s.documentRepo.WithContext(ctx).FindByUserFiltered(residenceIDs, filter)
|
|
if err != nil {
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
|
|
return responses.NewDocumentListResponse(documents), nil
|
|
}
|
|
|
|
// ListWarranties lists all warranty documents
|
|
func (s *DocumentService) ListWarranties(ctx context.Context, userID uint) ([]responses.DocumentResponse, error) {
|
|
// Get residence IDs (lightweight - no preloads)
|
|
residenceIDs, err := cachedResidenceIDsForUser(ctx, s.cache, s.residenceRepo, userID)
|
|
if err != nil {
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
|
|
if len(residenceIDs) == 0 {
|
|
return []responses.DocumentResponse{}, nil
|
|
}
|
|
|
|
documents, err := s.documentRepo.WithContext(ctx).FindWarranties(residenceIDs)
|
|
if err != nil {
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
|
|
return responses.NewDocumentListResponse(documents), nil
|
|
}
|
|
|
|
// CreateDocument creates a new document
|
|
func (s *DocumentService) CreateDocument(ctx context.Context, req *requests.CreateDocumentRequest, userID uint) (*responses.DocumentResponse, error) {
|
|
// Check residence access
|
|
hasAccess, err := s.residenceRepo.WithContext(ctx).HasAccess(req.ResidenceID, userID)
|
|
if err != nil {
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
if !hasAccess {
|
|
return nil, apperrors.Forbidden("error.residence_access_denied")
|
|
}
|
|
|
|
documentType := req.DocumentType
|
|
if documentType == "" {
|
|
documentType = models.DocumentTypeGeneral
|
|
}
|
|
|
|
document := &models.Document{
|
|
ResidenceID: req.ResidenceID,
|
|
CreatedByID: userID,
|
|
Title: req.Title,
|
|
Description: req.Description,
|
|
DocumentType: documentType,
|
|
FileURL: req.FileURL,
|
|
FileName: req.FileName,
|
|
FileSize: req.FileSize,
|
|
MimeType: req.MimeType,
|
|
PurchaseDate: req.PurchaseDate,
|
|
ExpiryDate: req.ExpiryDate,
|
|
PurchasePrice: req.PurchasePrice,
|
|
Vendor: req.Vendor,
|
|
SerialNumber: req.SerialNumber,
|
|
ModelNumber: req.ModelNumber,
|
|
TaskID: req.TaskID,
|
|
IsActive: true,
|
|
}
|
|
|
|
if err := s.documentRepo.WithContext(ctx).Create(document); err != nil {
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
|
|
// Create images if provided
|
|
for _, imageURL := range req.ImageURLs {
|
|
if imageURL != "" {
|
|
img := &models.DocumentImage{
|
|
DocumentID: document.ID,
|
|
ImageURL: imageURL,
|
|
}
|
|
if err := s.documentRepo.WithContext(ctx).CreateDocumentImage(img); err != nil {
|
|
// Log but don't fail the whole operation
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
|
|
// Reload with relations
|
|
document, err = s.documentRepo.WithContext(ctx).FindByID(document.ID)
|
|
if err != nil {
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
|
|
resp := responses.NewDocumentResponse(document)
|
|
return &resp, nil
|
|
}
|
|
|
|
// UpdateDocument updates a document
|
|
func (s *DocumentService) UpdateDocument(ctx context.Context, documentID, userID uint, req *requests.UpdateDocumentRequest) (*responses.DocumentResponse, error) {
|
|
document, err := s.documentRepo.WithContext(ctx).FindByID(documentID)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, apperrors.NotFound("error.document_not_found")
|
|
}
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
|
|
// Check access
|
|
hasAccess, err := s.residenceRepo.WithContext(ctx).HasAccess(document.ResidenceID, userID)
|
|
if err != nil {
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
if !hasAccess {
|
|
return nil, apperrors.Forbidden("error.document_access_denied")
|
|
}
|
|
|
|
// Apply updates
|
|
if req.Title != nil {
|
|
document.Title = *req.Title
|
|
}
|
|
if req.Description != nil {
|
|
document.Description = *req.Description
|
|
}
|
|
if req.DocumentType != nil {
|
|
document.DocumentType = *req.DocumentType
|
|
}
|
|
if req.FileURL != nil {
|
|
document.FileURL = *req.FileURL
|
|
}
|
|
if req.FileName != nil {
|
|
document.FileName = *req.FileName
|
|
}
|
|
if req.FileSize != nil {
|
|
document.FileSize = req.FileSize
|
|
}
|
|
if req.MimeType != nil {
|
|
document.MimeType = *req.MimeType
|
|
}
|
|
if req.PurchaseDate != nil {
|
|
document.PurchaseDate = req.PurchaseDate
|
|
}
|
|
if req.ExpiryDate != nil {
|
|
document.ExpiryDate = req.ExpiryDate
|
|
}
|
|
if req.PurchasePrice != nil {
|
|
document.PurchasePrice = req.PurchasePrice
|
|
}
|
|
if req.Vendor != nil {
|
|
document.Vendor = *req.Vendor
|
|
}
|
|
if req.SerialNumber != nil {
|
|
document.SerialNumber = *req.SerialNumber
|
|
}
|
|
if req.ModelNumber != nil {
|
|
document.ModelNumber = *req.ModelNumber
|
|
}
|
|
if req.TaskID != nil {
|
|
document.TaskID = req.TaskID
|
|
}
|
|
|
|
if err := s.documentRepo.WithContext(ctx).Update(document); err != nil {
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
|
|
// Reload
|
|
document, err = s.documentRepo.WithContext(ctx).FindByID(documentID)
|
|
if err != nil {
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
|
|
resp := responses.NewDocumentResponse(document)
|
|
return &resp, nil
|
|
}
|
|
|
|
// DeleteDocument soft-deletes a document
|
|
func (s *DocumentService) DeleteDocument(ctx context.Context, documentID, userID uint) error {
|
|
document, err := s.documentRepo.WithContext(ctx).FindByID(documentID)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return apperrors.NotFound("error.document_not_found")
|
|
}
|
|
return apperrors.Internal(err)
|
|
}
|
|
|
|
// Check access
|
|
hasAccess, err := s.residenceRepo.WithContext(ctx).HasAccess(document.ResidenceID, userID)
|
|
if err != nil {
|
|
return apperrors.Internal(err)
|
|
}
|
|
if !hasAccess {
|
|
return apperrors.Forbidden("error.document_access_denied")
|
|
}
|
|
|
|
if err := s.documentRepo.WithContext(ctx).Delete(documentID); err != nil {
|
|
return apperrors.Internal(err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// ActivateDocument activates a document
|
|
func (s *DocumentService) ActivateDocument(ctx context.Context, documentID, userID uint) (*responses.DocumentResponse, error) {
|
|
// First check if document exists (even if inactive)
|
|
var document models.Document
|
|
if err := s.documentRepo.WithContext(ctx).FindByIDIncludingInactive(documentID, &document); err != nil {
|
|
return nil, apperrors.NotFound("error.document_not_found")
|
|
}
|
|
|
|
// Check access
|
|
hasAccess, err := s.residenceRepo.WithContext(ctx).HasAccess(document.ResidenceID, userID)
|
|
if err != nil {
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
if !hasAccess {
|
|
return nil, apperrors.Forbidden("error.document_access_denied")
|
|
}
|
|
|
|
if err := s.documentRepo.WithContext(ctx).Activate(documentID); err != nil {
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
|
|
// Reload
|
|
doc, err := s.documentRepo.WithContext(ctx).FindByID(documentID)
|
|
if err != nil {
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
|
|
resp := responses.NewDocumentResponse(doc)
|
|
return &resp, nil
|
|
}
|
|
|
|
// DeactivateDocument deactivates a document
|
|
func (s *DocumentService) DeactivateDocument(ctx context.Context, documentID, userID uint) (*responses.DocumentResponse, error) {
|
|
document, err := s.documentRepo.WithContext(ctx).FindByID(documentID)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, apperrors.NotFound("error.document_not_found")
|
|
}
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
|
|
// Check access
|
|
hasAccess, err := s.residenceRepo.WithContext(ctx).HasAccess(document.ResidenceID, userID)
|
|
if err != nil {
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
if !hasAccess {
|
|
return nil, apperrors.Forbidden("error.document_access_denied")
|
|
}
|
|
|
|
if err := s.documentRepo.WithContext(ctx).Deactivate(documentID); err != nil {
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
|
|
document.IsActive = false
|
|
resp := responses.NewDocumentResponse(document)
|
|
return &resp, nil
|
|
}
|
|
|
|
// UploadDocumentImage adds an image to an existing document
|
|
func (s *DocumentService) UploadDocumentImage(ctx context.Context, documentID, userID uint, imageURL, caption string) (*responses.DocumentResponse, error) {
|
|
document, err := s.documentRepo.WithContext(ctx).FindByID(documentID)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, apperrors.NotFound("error.document_not_found")
|
|
}
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
|
|
// Check access via residence
|
|
hasAccess, err := s.residenceRepo.WithContext(ctx).HasAccess(document.ResidenceID, userID)
|
|
if err != nil {
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
if !hasAccess {
|
|
return nil, apperrors.Forbidden("error.document_access_denied")
|
|
}
|
|
|
|
img := &models.DocumentImage{
|
|
DocumentID: documentID,
|
|
ImageURL: imageURL,
|
|
Caption: caption,
|
|
}
|
|
if err := s.documentRepo.WithContext(ctx).CreateDocumentImage(img); err != nil {
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
|
|
// Reload with relations
|
|
document, err = s.documentRepo.WithContext(ctx).FindByID(documentID)
|
|
if err != nil {
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
|
|
resp := responses.NewDocumentResponse(document)
|
|
return &resp, nil
|
|
}
|
|
|
|
// DeleteDocumentImage removes an image from a document
|
|
func (s *DocumentService) DeleteDocumentImage(ctx context.Context, documentID, imageID, userID uint) (*responses.DocumentResponse, error) {
|
|
// Find the image first
|
|
image, err := s.documentRepo.WithContext(ctx).FindImageByID(imageID)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, apperrors.NotFound("error.document_image_not_found")
|
|
}
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
|
|
// Verify image belongs to the specified document
|
|
if image.DocumentID != documentID {
|
|
return nil, apperrors.NotFound("error.document_image_not_found")
|
|
}
|
|
|
|
// Find parent document to check access
|
|
document, err := s.documentRepo.WithContext(ctx).FindByID(documentID)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, apperrors.NotFound("error.document_not_found")
|
|
}
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
|
|
// Check access via residence
|
|
hasAccess, err := s.residenceRepo.WithContext(ctx).HasAccess(document.ResidenceID, userID)
|
|
if err != nil {
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
if !hasAccess {
|
|
return nil, apperrors.Forbidden("error.document_access_denied")
|
|
}
|
|
|
|
if err := s.documentRepo.WithContext(ctx).DeleteDocumentImage(imageID); err != nil {
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
|
|
// Reload with relations
|
|
document, err = s.documentRepo.WithContext(ctx).FindByID(documentID)
|
|
if err != nil {
|
|
return nil, apperrors.Internal(err)
|
|
}
|
|
|
|
resp := responses.NewDocumentResponse(document)
|
|
return &resp, nil
|
|
}
|