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
@@ -18,12 +18,24 @@ import (
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
var _ outbound.NeedRepository = needRepository{}
|
||||
var (
|
||||
_ outbound.NeedRepository = needRepository{}
|
||||
_ outbound.NeedListRepository = needRepository{}
|
||||
_ outbound.NeedCreationResultRepository = needRepository{}
|
||||
)
|
||||
|
||||
func (unit *UnitOfWork) Needs() outbound.NeedRepository {
|
||||
return needRepository{queries: unit.queries}
|
||||
}
|
||||
|
||||
func (unit *UnitOfWork) NeedLists() outbound.NeedListRepository {
|
||||
return needRepository{queries: unit.queries}
|
||||
}
|
||||
|
||||
func (unit *UnitOfWork) NeedCreationResults() outbound.NeedCreationResultRepository {
|
||||
return needRepository{queries: unit.queries}
|
||||
}
|
||||
|
||||
type needRepository struct {
|
||||
queries *sqlc.Queries
|
||||
}
|
||||
@@ -99,6 +111,129 @@ func (repository needRepository) GetVersion(
|
||||
return restoreNeedSnapshot(location, encoded, organizationID, needID, version, 0, nil, nil)
|
||||
}
|
||||
|
||||
func (repository needRepository) List(ctx context.Context, query outbound.NeedListQuery) (outbound.NeedPage, error) {
|
||||
const location = "needRepository.List"
|
||||
if query.OrganizationID == uuid.Nil {
|
||||
return outbound.NeedPage{}, fmt.Errorf("%s - organization ID must be set: %w", location, domain.ErrInvalidNeed)
|
||||
}
|
||||
if query.Limit < 1 || query.Limit > outbound.MaxNeedPageSize {
|
||||
return outbound.NeedPage{}, fmt.Errorf("%s - limit must be 1 to %d: %w", location, outbound.MaxNeedPageSize, domain.ErrInvalidNeed)
|
||||
}
|
||||
params := sqlc.ListOrganizationNeedsParams{
|
||||
OrganizationID: pgUUID(query.OrganizationID),
|
||||
RowLimit: query.Limit + 1,
|
||||
}
|
||||
if query.After != nil {
|
||||
if err := validateNeedListPosition(location, *query.After); err != nil {
|
||||
return outbound.NeedPage{}, err
|
||||
}
|
||||
params.AfterCreatedAt = pgTime(query.After.CreatedAt)
|
||||
params.AfterNeedID = pgUUID(query.After.NeedID)
|
||||
}
|
||||
rows, err := repository.queries.ListOrganizationNeeds(ctx, params)
|
||||
if err != nil {
|
||||
return outbound.NeedPage{}, fmt.Errorf("%s - select current needs: %w", location, mapNeedError(err))
|
||||
}
|
||||
page := outbound.NeedPage{HasMore: len(rows) > int(query.Limit)}
|
||||
if page.HasMore {
|
||||
rows = rows[:query.Limit]
|
||||
}
|
||||
page.Items = make([]domain.Need, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
if !row.OrganizationID.Valid || !row.NeedID.Valid || !row.CreatedAt.Valid || !row.UpdatedAt.Valid || uuid.UUID(row.OrganizationID.Bytes) != query.OrganizationID {
|
||||
return outbound.NeedPage{}, fmt.Errorf("%s - current need row has invalid scope or metadata", location)
|
||||
}
|
||||
need, err := restoreNeedSnapshot(
|
||||
location, row.Snapshot, query.OrganizationID, uuid.UUID(row.NeedID.Bytes),
|
||||
row.CurrentVersion, row.RequirementsVersion, &row.CreatedAt.Time, &row.UpdatedAt.Time,
|
||||
)
|
||||
if err != nil {
|
||||
return outbound.NeedPage{}, err
|
||||
}
|
||||
page.Items = append(page.Items, need)
|
||||
}
|
||||
return page, nil
|
||||
}
|
||||
|
||||
func validateNeedListPosition(location string, position outbound.NeedListPosition) error {
|
||||
createdAt := position.CreatedAt
|
||||
if position.NeedID == uuid.Nil || createdAt.IsZero() || createdAt.Year() < 1 || createdAt.Year() > 9999 ||
|
||||
createdAt.Unix() < 0 || createdAt.Nanosecond()%int(time.Microsecond) != 0 {
|
||||
return fmt.Errorf("%s - pagination position is outside the stored need timestamp range: %w", location, domain.ErrInvalidNeed)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repository needRepository) SaveCreationResult(
|
||||
ctx context.Context,
|
||||
scope [outbound.IdempotencyDigestSize]byte,
|
||||
snapshot domain.NeedResponseSnapshot,
|
||||
) error {
|
||||
const location = "needRepository.SaveCreationResult"
|
||||
if scope == ([outbound.IdempotencyDigestSize]byte{}) {
|
||||
return fmt.Errorf("%s - idempotency scope must be set: %w", location, domain.ErrInvalidNeed)
|
||||
}
|
||||
_, encoded, err := encodeNeedCreationResult(snapshot)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - validate and encode response snapshot: %w", location, err)
|
||||
}
|
||||
if err := repository.queries.SaveOrganizationNeedCreationResult(ctx, sqlc.SaveOrganizationNeedCreationResultParams{
|
||||
ScopeHash: scope[:], Snapshot: encoded,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("%s - store response snapshot: %w", location, mapNeedError(err))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repository needRepository) GetCreationResult(
|
||||
ctx context.Context,
|
||||
scope [outbound.IdempotencyDigestSize]byte,
|
||||
) (domain.NeedResponseSnapshot, error) {
|
||||
const location = "needRepository.GetCreationResult"
|
||||
if scope == ([outbound.IdempotencyDigestSize]byte{}) {
|
||||
return domain.NeedResponseSnapshot{}, fmt.Errorf("%s - idempotency scope must be set: %w", location, domain.ErrInvalidNeed)
|
||||
}
|
||||
encoded, err := repository.queries.GetOrganizationNeedCreationResult(ctx, scope[:])
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return domain.NeedResponseSnapshot{}, outbound.ErrNeedCreationResultNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return domain.NeedResponseSnapshot{}, fmt.Errorf("%s - read response snapshot: %w", location, mapNeedError(err))
|
||||
}
|
||||
if len(encoded) > domain.MaxNeedCreationResultStorageBytes {
|
||||
return domain.NeedResponseSnapshot{}, fmt.Errorf("%s - stored response snapshot exceeds its bound", location)
|
||||
}
|
||||
decoder := json.NewDecoder(bytes.NewBufferString(encoded))
|
||||
decoder.DisallowUnknownFields()
|
||||
var snapshot domain.NeedResponseSnapshot
|
||||
if err := decoder.Decode(&snapshot); err != nil {
|
||||
return domain.NeedResponseSnapshot{}, fmt.Errorf("%s - stored response snapshot is invalid", location)
|
||||
}
|
||||
if err := decoder.Decode(new(any)); !errors.Is(err, io.EOF) {
|
||||
return domain.NeedResponseSnapshot{}, fmt.Errorf("%s - stored response snapshot has trailing data", location)
|
||||
}
|
||||
validated, _, err := encodeNeedCreationResult(snapshot)
|
||||
if err != nil {
|
||||
return domain.NeedResponseSnapshot{}, fmt.Errorf("%s - restore response snapshot: %w", location, err)
|
||||
}
|
||||
return validated, nil
|
||||
}
|
||||
|
||||
func encodeNeedCreationResult(snapshot domain.NeedResponseSnapshot) (domain.NeedResponseSnapshot, []byte, error) {
|
||||
validated, err := domain.RestoreNeedResponseSnapshot(snapshot)
|
||||
if err != nil {
|
||||
return domain.NeedResponseSnapshot{}, nil, err
|
||||
}
|
||||
encoded, err := json.Marshal(validated)
|
||||
if err != nil {
|
||||
return domain.NeedResponseSnapshot{}, nil, fmt.Errorf("encode need response snapshot: %w", err)
|
||||
}
|
||||
if len(encoded) > domain.MaxNeedCreationResultBytes {
|
||||
return domain.NeedResponseSnapshot{}, nil, fmt.Errorf("need response snapshot exceeds its storage bound: %w", domain.ErrInvalidNeed)
|
||||
}
|
||||
return validated, encoded, nil
|
||||
}
|
||||
|
||||
func (repository needRepository) Save(
|
||||
ctx context.Context,
|
||||
organizationID, needID uuid.UUID,
|
||||
@@ -216,7 +351,7 @@ func restoreNeedSnapshot(
|
||||
version, requirementsVersion int64,
|
||||
createdAt, updatedAt *time.Time,
|
||||
) (domain.Need, error) {
|
||||
if len(encoded) > domain.MaxNeedSnapshotBytes {
|
||||
if len(encoded) > domain.MaxNeedSnapshotStorageBytes {
|
||||
return domain.Need{}, fmt.Errorf("%s - stored snapshot exceeds its bound", location)
|
||||
}
|
||||
decoder := json.NewDecoder(bytes.NewBufferString(encoded))
|
||||
@@ -269,6 +404,9 @@ func mapNeedError(err error) error {
|
||||
if pgError.Code == foreignKeyViolationCode && pgError.ConstraintName == "organization_needs_organization_fkey" {
|
||||
return fmt.Errorf("postgres.mapNeedError - organization no longer exists: %w", outbound.ErrOrganizationNotFound)
|
||||
}
|
||||
if pgError.Code == foreignKeyViolationCode && pgError.ConstraintName == "organization_need_creation_results_idempotency_fkey" {
|
||||
return fmt.Errorf("postgres.mapNeedError - idempotency record no longer exists: %w", outbound.ErrIdempotencyRecordNotFound)
|
||||
}
|
||||
if pgError.Code == uniqueViolationCode && pgError.ConstraintName == "organization_needs_pkey" {
|
||||
return fmt.Errorf("postgres.mapNeedError - need ID already exists: %w", outbound.ErrNeedAlreadyExists)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user