89 lines
3.2 KiB
Go
89 lines
3.2 KiB
Go
package assessment
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
// CurrentSignalNoticeVersion is the version of the notice text that lists the
|
|
// collected assessment signals. Change it together with the client text so that
|
|
// every candidate acknowledges the new wording.
|
|
const CurrentSignalNoticeVersion int32 = 1
|
|
|
|
// ErrSignalNoticeVersionMismatch reports an acknowledgement for a notice
|
|
// version other than the one the server requires.
|
|
var ErrSignalNoticeVersionMismatch = errors.New("assessment signal notice version is not current")
|
|
|
|
// SignalNoticeState is the acknowledgement state of the current notice version.
|
|
type SignalNoticeState struct {
|
|
Version int32
|
|
Acknowledged bool
|
|
AcknowledgedAt time.Time
|
|
}
|
|
|
|
// GetSignalNotice reads whether the candidate acknowledged the current notice.
|
|
// The record is not a consent and does not influence signal storage.
|
|
func (service *SignalService) GetSignalNotice(ctx context.Context, actorID uuid.UUID) (SignalNoticeState, error) {
|
|
const location = "SignalService.GetSignalNotice"
|
|
state := SignalNoticeState{Version: CurrentSignalNoticeVersion}
|
|
err := service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
|
if err := requireAttemptCandidate(ctx, unit, actorID, location); err != nil {
|
|
return err
|
|
}
|
|
acknowledgedAt, err := unit.AssessmentSignalNotices().Get(ctx, actorID, CurrentSignalNoticeVersion)
|
|
if errors.Is(err, outbound.ErrAssessmentSignalNoticeNotAcknowledged) {
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
return fmt.Errorf("%s - read acknowledgement: %w", location, err)
|
|
}
|
|
state.Acknowledged, state.AcknowledgedAt = true, acknowledgedAt
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return SignalNoticeState{}, fmt.Errorf("%s - transaction: %w", location, err)
|
|
}
|
|
return state, nil
|
|
}
|
|
|
|
// AcknowledgeSignalNotice records the first acknowledgement of the current
|
|
// notice version. A repeat keeps the stored time, so a retry after a lost
|
|
// response succeeds. Another version is refused without storing anything.
|
|
func (service *SignalService) AcknowledgeSignalNotice(
|
|
ctx context.Context,
|
|
actorID uuid.UUID,
|
|
version int32,
|
|
) (SignalNoticeState, error) {
|
|
const location = "SignalService.AcknowledgeSignalNotice"
|
|
if version != CurrentSignalNoticeVersion {
|
|
return SignalNoticeState{}, fmt.Errorf("%s - compare version: %w", location, ErrSignalNoticeVersionMismatch)
|
|
}
|
|
state := SignalNoticeState{Version: CurrentSignalNoticeVersion, Acknowledged: true}
|
|
err := service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
|
if err := requireAttemptCandidate(ctx, unit, actorID, location); err != nil {
|
|
return err
|
|
}
|
|
now, err := unit.AssessmentBank().DatabaseTime(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - read database time: %w", location, err)
|
|
}
|
|
state.AcknowledgedAt, err = unit.AssessmentSignalNotices().Acknowledge(
|
|
ctx, actorID, CurrentSignalNoticeVersion, now.UTC().Truncate(time.Microsecond),
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - store acknowledgement: %w", location, err)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return SignalNoticeState{}, fmt.Errorf("%s - transaction: %w", location, err)
|
|
}
|
|
return state, nil
|
|
}
|