feat(assessment): find the attempt behind a leaked task text
This commit is contained in:
1 parent
bb917bf62e
commit
5a731c25b7
26 files changed
+2500
No files matched your search
@@ -0,0 +1,100 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"lincodex/backend/internal/adapter/secondary/postgres/sqlc"
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
var _ outbound.AssessmentLeakTraceRepository = assessmentLeakTraceRepository{}
|
||||
|
||||
func (unit *UnitOfWork) AssessmentLeakTraces() outbound.AssessmentLeakTraceRepository {
|
||||
return assessmentLeakTraceRepository{queries: unit.queries}
|
||||
}
|
||||
|
||||
type assessmentLeakTraceRepository struct {
|
||||
queries *sqlc.Queries
|
||||
}
|
||||
|
||||
func (repository assessmentLeakTraceRepository) ListRealAttempts(
|
||||
ctx context.Context,
|
||||
after domain.AssessmentAttemptID,
|
||||
limit int32,
|
||||
) ([]outbound.AssessmentTraceAttempt, error) {
|
||||
const location = "assessmentLeakTraceRepository.ListRealAttempts"
|
||||
if limit < 1 {
|
||||
return nil, fmt.Errorf("%s - a positive limit is required: %w", location, domain.ErrInvalidAssessmentLeakTrace)
|
||||
}
|
||||
rows, err := repository.queries.ListAssessmentTraceAttempts(ctx, sqlc.ListAssessmentTraceAttemptsParams{
|
||||
AfterID: pgUUID(after.UUID()), RowLimit: limit,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s - list attempts: %w", location, mapAssessmentError(err))
|
||||
}
|
||||
attempts := make([]outbound.AssessmentTraceAttempt, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
if !row.ID.Valid || !row.CreatedAt.Valid || !row.ExpiresAt.Valid {
|
||||
return nil, fmt.Errorf("%s - stored attempt is incomplete: %w", location, domain.ErrInvalidAssessmentLeakTrace)
|
||||
}
|
||||
attempts = append(attempts, outbound.AssessmentTraceAttempt{
|
||||
ID: domain.AssessmentAttemptID(row.ID.Bytes), CreatedAt: row.CreatedAt.Time.UTC(), ExpiresAt: row.ExpiresAt.Time.UTC(),
|
||||
})
|
||||
}
|
||||
return attempts, nil
|
||||
}
|
||||
|
||||
func (repository assessmentLeakTraceRepository) FindStatementsContaining(
|
||||
ctx context.Context,
|
||||
fragment string,
|
||||
limit int32,
|
||||
) ([]outbound.AssessmentTraceStatement, error) {
|
||||
const location = "assessmentLeakTraceRepository.FindStatementsContaining"
|
||||
if fragment == "" || limit < 1 {
|
||||
return nil, fmt.Errorf("%s - a fragment and a positive limit are required: %w", location, domain.ErrInvalidAssessmentLeakTrace)
|
||||
}
|
||||
rows, err := repository.queries.FindAssessmentTraceStatements(ctx, sqlc.FindAssessmentTraceStatementsParams{
|
||||
Fragment: fragment, RowLimit: limit,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s - find statements: %w", location, mapAssessmentError(err))
|
||||
}
|
||||
statements := make([]outbound.AssessmentTraceStatement, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
if !row.AttemptID.Valid {
|
||||
return nil, fmt.Errorf("%s - stored statement has no attempt: %w", location, domain.ErrInvalidAssessmentLeakTrace)
|
||||
}
|
||||
statements = append(statements, outbound.AssessmentTraceStatement{
|
||||
AttemptID: domain.AssessmentAttemptID(row.AttemptID.Bytes), Statement: row.Statement,
|
||||
})
|
||||
}
|
||||
return statements, nil
|
||||
}
|
||||
|
||||
func (repository assessmentLeakTraceRepository) RecordSearch(ctx context.Context, record domain.AssessmentLeakTraceRecord) error {
|
||||
const location = "assessmentLeakTraceRepository.RecordSearch"
|
||||
if err := record.Validate(); err != nil {
|
||||
return fmt.Errorf("%s - record is invalid: %w", location, err)
|
||||
}
|
||||
method := pgtype.Text{}
|
||||
if record.Method != "" {
|
||||
method = pgtype.Text{String: string(record.Method), Valid: true}
|
||||
}
|
||||
attempt := pgtype.UUID{}
|
||||
if !record.AttemptID.IsZero() {
|
||||
attempt = pgUUID(record.AttemptID.UUID())
|
||||
}
|
||||
if err := repository.queries.InsertAssessmentLeakTrace(ctx, sqlc.InsertAssessmentLeakTraceParams{
|
||||
ID: pgUUID(record.ID), Actor: string(record.Actor), SearchedAt: pgTime(record.SearchedAt),
|
||||
TextDigest: record.TextDigest[:], KeyVersion: record.KeyVersion, TextRunes: record.TextRunes,
|
||||
Status: string(record.Status), Method: method, AttemptID: attempt,
|
||||
Candidates: record.Candidates, EvidenceBits: record.EvidenceBits,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("%s - insert audit record: %w", location, mapAssessmentError(err))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user