feat: add assessment, privacy and support flows
This commit is contained in:
1 parent
ee3738ee73
commit
e4f24fa202
339 files changed
+56454
-1126
No files matched your search
@@ -0,0 +1,207 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"lincodex/backend/internal/adapter/secondary/postgres/sqlc"
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
var _ outbound.PrivacyCohortRepository = privacyCohortRepository{}
|
||||
|
||||
// PrivacyCohortStore reads the current visible candidate cohort outside a
|
||||
// transaction. The single query provides one consistent publication and
|
||||
// hidden-organization snapshot.
|
||||
type PrivacyCohortStore struct {
|
||||
queries *sqlc.Queries
|
||||
}
|
||||
|
||||
func NewPrivacyCohortStore(pool *pgxpool.Pool) *PrivacyCohortStore {
|
||||
return &PrivacyCohortStore{queries: sqlc.New(standaloneDB{pool: pool})}
|
||||
}
|
||||
|
||||
func (unit *UnitOfWork) PrivacyCohort() outbound.PrivacyCohortRepository {
|
||||
return privacyCohortRepository{queries: unit.queries}
|
||||
}
|
||||
|
||||
func (unit *UnitOfWork) PrivacyView() outbound.PrivacyViewRepository {
|
||||
return privacyCohortRepository{queries: unit.queries}
|
||||
}
|
||||
|
||||
func (store *PrivacyCohortStore) ListVisibleCandidateViewSources(
|
||||
ctx context.Context,
|
||||
organizationID uuid.UUID,
|
||||
) ([]domain.CandidateViewSource, error) {
|
||||
return (privacyCohortRepository{queries: store.queries}).ListVisibleCandidateViewSources(ctx, organizationID)
|
||||
}
|
||||
|
||||
type privacyCohortRepository struct {
|
||||
queries *sqlc.Queries
|
||||
}
|
||||
|
||||
type privacyCohortCandidate struct {
|
||||
id uuid.UUID
|
||||
fields domain.CardFieldSet
|
||||
claims []domain.StoredProfileClaim
|
||||
acknowledgements []domain.ProfileParallelPeriodAcknowledgement
|
||||
workFormats []domain.QuestionnaireWorkFormat
|
||||
}
|
||||
|
||||
func (repository privacyCohortRepository) ListVisibleCandidateViewSources(
|
||||
ctx context.Context,
|
||||
organizationID uuid.UUID,
|
||||
) ([]domain.CandidateViewSource, error) {
|
||||
const location = "privacyCohortRepository.ListVisibleCandidateViewSources"
|
||||
if organizationID == uuid.Nil {
|
||||
return nil, fmt.Errorf("%s - organization ID must be set: %w", location, domain.ErrInvalidCandidateViewSource)
|
||||
}
|
||||
rows, err := repository.queries.ListVisibleCandidateViewSources(ctx, pgUUID(organizationID))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s - select visible sources: %w", location, markTransactionConflict(sanitizeConnectError(err)))
|
||||
}
|
||||
if len(rows) == 0 {
|
||||
return []domain.CandidateViewSource{}, nil
|
||||
}
|
||||
result := make([]domain.CandidateViewSource, 0, len(rows))
|
||||
current := privacyCohortCandidate{}
|
||||
for _, row := range rows {
|
||||
candidateID, err := privacyCohortUUID(row.UserID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s - source identity is invalid: %w", location, domain.ErrInvalidCandidateViewSource)
|
||||
}
|
||||
if current.id != uuid.Nil && current.id != candidateID {
|
||||
source, err := buildPrivacyCohortSource(current)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s - restore candidate source: %w", location, err)
|
||||
}
|
||||
result = append(result, source)
|
||||
current = privacyCohortCandidate{}
|
||||
}
|
||||
if current.id == uuid.Nil {
|
||||
current, err = newPrivacyCohortCandidate(candidateID, row.Fields, row.WorkFormats, row.ParallelPeriodAcknowledgements)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s - restore candidate source: %w", location, err)
|
||||
}
|
||||
}
|
||||
if row.Kind.Valid {
|
||||
claim, err := privacyCohortStoredClaim(row)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s - restore candidate claims: %w", location, err)
|
||||
}
|
||||
current.claims = append(current.claims, claim)
|
||||
}
|
||||
}
|
||||
source, err := buildPrivacyCohortSource(current)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s - restore candidate source: %w", location, err)
|
||||
}
|
||||
result = append(result, source)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func newPrivacyCohortCandidate(
|
||||
candidateID uuid.UUID,
|
||||
fieldCodes []string,
|
||||
workFormatCodes []string,
|
||||
acknowledgementsValue any,
|
||||
) (privacyCohortCandidate, error) {
|
||||
fields := make([]domain.CardField, 0, len(fieldCodes))
|
||||
for _, code := range fieldCodes {
|
||||
fields = append(fields, domain.CardField(code))
|
||||
}
|
||||
fieldSet, err := domain.NewCardFieldSet(fields)
|
||||
if err != nil {
|
||||
return privacyCohortCandidate{}, fmt.Errorf("invalid publication fields: %w", domain.ErrInvalidCandidateViewSource)
|
||||
}
|
||||
workFormats := make([]domain.QuestionnaireWorkFormat, 0, len(workFormatCodes))
|
||||
for _, code := range workFormatCodes {
|
||||
workFormats = append(workFormats, domain.QuestionnaireWorkFormat(code))
|
||||
}
|
||||
acknowledgementsJSON, err := privacyCohortEncodedJSONBytes(acknowledgementsValue)
|
||||
if err != nil {
|
||||
return privacyCohortCandidate{}, fmt.Errorf("invalid overlap acknowledgements: %w", domain.ErrInvalidCandidateProfile)
|
||||
}
|
||||
var acknowledgements []domain.ProfileParallelPeriodAcknowledgement
|
||||
if err := json.Unmarshal(acknowledgementsJSON, &acknowledgements); err != nil {
|
||||
return privacyCohortCandidate{}, fmt.Errorf("invalid overlap acknowledgements: %w", domain.ErrInvalidCandidateProfile)
|
||||
}
|
||||
return privacyCohortCandidate{
|
||||
id: candidateID, fields: fieldSet, workFormats: workFormats,
|
||||
acknowledgements: acknowledgements,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func privacyCohortStoredClaim(row sqlc.ListVisibleCandidateViewSourcesRow) (domain.StoredProfileClaim, error) {
|
||||
value, err := privacyCohortJSONBytes(row.SafeValue)
|
||||
if err != nil {
|
||||
return domain.StoredProfileClaim{}, fmt.Errorf("safe JSON type %T is invalid: %w", row.SafeValue, domain.ErrInvalidCandidateProfile)
|
||||
}
|
||||
if !row.Ordinal.Valid || !row.Status.Valid || !row.Origin.Valid || !row.TrustLevel.Valid {
|
||||
return domain.StoredProfileClaim{}, fmt.Errorf("claim metadata is incomplete: %w", domain.ErrInvalidCandidateProfile)
|
||||
}
|
||||
confirmedAt := time.Time{}
|
||||
if row.ConfirmedAt.Valid {
|
||||
confirmedAt = row.ConfirmedAt.Time.UTC()
|
||||
}
|
||||
return domain.StoredProfileClaim{
|
||||
Kind: row.Kind.String, Ordinal: int(row.Ordinal.Int16), Value: value,
|
||||
Status: row.Status.String, Origin: row.Origin.String, TrustLevel: int(row.TrustLevel.Int16),
|
||||
ExtractionVersion: row.ExtractionVersion.String, ConfirmedAt: confirmedAt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func buildPrivacyCohortSource(candidate privacyCohortCandidate) (domain.CandidateViewSource, error) {
|
||||
content, err := domain.RestoreCandidateProfileContentWithParallelPeriods(candidate.claims, candidate.acknowledgements)
|
||||
if err != nil {
|
||||
return domain.CandidateViewSource{}, fmt.Errorf("validate safe profile claims: %w", domain.ErrInvalidCandidateViewSource)
|
||||
}
|
||||
return domain.NewCandidateViewSource(
|
||||
candidate.id, candidate.fields, domain.CandidateViewProfileFromContent(content), nil,
|
||||
candidate.workFormats, nil,
|
||||
)
|
||||
}
|
||||
|
||||
func privacyCohortJSONBytes(value any) ([]byte, error) {
|
||||
switch typed := value.(type) {
|
||||
case []byte:
|
||||
return slices.Clone(typed), nil
|
||||
case json.RawMessage:
|
||||
return slices.Clone(typed), nil
|
||||
default:
|
||||
encoded, err := json.Marshal(typed)
|
||||
if err != nil {
|
||||
return nil, errors.New("JSON projection has an unexpected type")
|
||||
}
|
||||
return encoded, nil
|
||||
}
|
||||
}
|
||||
|
||||
func privacyCohortEncodedJSONBytes(value any) ([]byte, error) {
|
||||
switch typed := value.(type) {
|
||||
case string:
|
||||
return []byte(typed), nil
|
||||
case []byte:
|
||||
return slices.Clone(typed), nil
|
||||
case json.RawMessage:
|
||||
return slices.Clone(typed), nil
|
||||
default:
|
||||
return nil, errors.New("encoded JSON projection has an unexpected type")
|
||||
}
|
||||
}
|
||||
|
||||
func privacyCohortUUID(value pgtype.UUID) (uuid.UUID, error) {
|
||||
if !value.Valid || value.Bytes == uuid.Nil {
|
||||
return uuid.Nil, domain.ErrInvalidCandidateViewSource
|
||||
}
|
||||
return uuid.UUID(value.Bytes), nil
|
||||
}
|
||||
Reference in new issue
Block a user