81 lines
2.0 KiB
Go
81 lines
2.0 KiB
Go
package verification
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
verificationmodel "git.sendico.io/tech/sxchnge/product/verification/model"
|
|
|
|
"git.sendico.io/tech/sxchnge/shared/errors"
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func (db *verificationDB) Consume(
|
|
ct context.Context,
|
|
accountRef bson.ObjectID,
|
|
purpose verificationmodel.VerificationPurpose,
|
|
contextRef string,
|
|
rawToken string,
|
|
) (*verificationmodel.VerificationToken, error) {
|
|
scope, err := newConsumeScope(accountRef, purpose, contextRef, rawToken)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
db.Logger.Debug("Verification consume started", scope.startFields()...)
|
|
|
|
result, execErr := db.tf.CreateTransaction().Execute(
|
|
ct,
|
|
func(ctx context.Context) (any, error) {
|
|
token, err := db.lookupConsumeToken(ctx, scope)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := db.validateConsumeToken(scope, token); err != nil {
|
|
return nil, err
|
|
}
|
|
return db.consumeMatchedToken(ctx, scope, token)
|
|
},
|
|
)
|
|
|
|
if execErr != nil {
|
|
db.persistConsumeMissAttempts(ct, scope, execErr)
|
|
}
|
|
|
|
return db.finishConsume(scope, result, execErr)
|
|
}
|
|
|
|
func (db *verificationDB) persistConsumeMissAttempts(ctx context.Context, scope consumeScope, err error) {
|
|
var miss consumeMissError
|
|
if !errors.As(err, &miss) {
|
|
return
|
|
}
|
|
db.incrementConsumeAttemptsForTokenIDs(ctx, scope, miss.tokenIDs)
|
|
}
|
|
|
|
func (db *verificationDB) finishConsume(
|
|
scope consumeScope,
|
|
result any,
|
|
err error,
|
|
) (*verificationmodel.VerificationToken, error) {
|
|
if err != nil {
|
|
db.Logger.Debug("Verification consume finished with error",
|
|
scope.logFields(zap.Error(err))...,
|
|
)
|
|
return nil, err
|
|
}
|
|
|
|
token, ok := result.(*verificationmodel.VerificationToken)
|
|
if !ok {
|
|
db.Logger.Warn("Verification consume returned unexpected result type",
|
|
scope.logFields(zap.String("result_type", "<non-verification-token>"))...,
|
|
)
|
|
return nil, merrors.Internal("unexpected token type")
|
|
}
|
|
|
|
db.Logger.Debug("Verification consume finished successfully",
|
|
scope.tokenLogFields(token)...,
|
|
)
|
|
return token, nil
|
|
}
|