ultisuite-backend/internal/users/provision.go
R3D347HR4Y 95196f7777 Add mail attachment and draft management features
- Introduced new functionality for managing email attachments and drafts in the mail API.
- Added handlers for listing, uploading, and downloading message attachments in `internal/api/mail/handlers_attachments.go`.
- Implemented draft management endpoints for creating, updating, and deleting drafts in `internal/api/mail/handlers_drafts.go`.
- Created new service methods for handling draft and attachment operations in `internal/api/mail/drafts.go` and `internal/api/mail/storage.go`.
- Added validation and error handling for draft and attachment operations.
- Included unit tests for draft and folder functionalities in `internal/api/mail/drafts_test.go` and `internal/api/mail/folders_test.go`.
- Updated API routes to support new draft and attachment features, enhancing overall mail management capabilities.
2026-05-22 17:14:36 +02:00

52 lines
1.2 KiB
Go

package users
import (
"context"
"fmt"
"strings"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/ultisuite/ulti-backend/internal/auth"
)
// ProvisionEmail returns the email stored for a newly provisioned user.
func ProvisionEmail(claims *auth.Claims) string {
if claims == nil {
return ""
}
email := strings.TrimSpace(claims.Email)
if email != "" {
return email
}
return claims.Sub + "@unknown.ultimail.local"
}
// EnsureUser inserts or updates the Ultimail user row for OIDC claims and returns the internal UUID.
func EnsureUser(ctx context.Context, db *pgxpool.Pool, claims *auth.Claims) (string, error) {
if db == nil {
return "", fmt.Errorf("database not configured")
}
if claims == nil || strings.TrimSpace(claims.Sub) == "" {
return "", fmt.Errorf("missing subject claim")
}
email := ProvisionEmail(claims)
name := strings.TrimSpace(claims.Name)
var userID string
err := db.QueryRow(ctx, `
INSERT INTO users (external_id, email, name)
VALUES ($1, $2, $3)
ON CONFLICT (external_id) DO UPDATE SET
email = EXCLUDED.email,
name = EXCLUDED.name,
updated_at = NOW()
RETURNING id
`, claims.Sub, email, name).Scan(&userID)
if err != nil {
return "", fmt.Errorf("provision user: %w", err)
}
return userID, nil
}