Organize backend into product executable and shared Go modules
This commit is contained in:
1 parent
981e84a4b4
commit
f0720ec66b
382 files changed
+1944
-6989
No files matched your search
@@ -0,0 +1,80 @@
|
||||
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
|
||||
}
|
||||
Reference in new issue
Block a user