New code verification service
Some checks failed
ci/woodpecker/push/bff Pipeline was successful
ci/woodpecker/push/db Pipeline was successful
ci/woodpecker/push/fx_ingestor Pipeline was successful
ci/woodpecker/push/billing_fees Pipeline was successful
ci/woodpecker/push/chain_gateway Pipeline was successful
ci/woodpecker/push/fx_oracle Pipeline was successful
ci/woodpecker/push/frontend Pipeline was successful
ci/woodpecker/push/nats Pipeline was successful
ci/woodpecker/push/ledger Pipeline was successful
ci/woodpecker/push/notification Pipeline was successful
ci/woodpecker/push/payments_orchestrator Pipeline was successful
ci/woodpecker/push/bump_version Pipeline failed
Some checks failed
ci/woodpecker/push/bff Pipeline was successful
ci/woodpecker/push/db Pipeline was successful
ci/woodpecker/push/fx_ingestor Pipeline was successful
ci/woodpecker/push/billing_fees Pipeline was successful
ci/woodpecker/push/chain_gateway Pipeline was successful
ci/woodpecker/push/fx_oracle Pipeline was successful
ci/woodpecker/push/frontend Pipeline was successful
ci/woodpecker/push/nats Pipeline was successful
ci/woodpecker/push/ledger Pipeline was successful
ci/woodpecker/push/notification Pipeline was successful
ci/woodpecker/push/payments_orchestrator Pipeline was successful
ci/woodpecker/push/bump_version Pipeline failed
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package notifications
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
messaging "github.com/tech/sendico/pkg/messaging/envelope"
|
||||
"github.com/tech/sendico/pkg/model"
|
||||
nm "github.com/tech/sendico/pkg/model/notification"
|
||||
"github.com/tech/sendico/pkg/mservice"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type confirmationCodePayload struct {
|
||||
AccountRef string `json:"accountRef"`
|
||||
Destination string `json:"destination"`
|
||||
Target string `json:"target"`
|
||||
Code string `json:"code"`
|
||||
}
|
||||
|
||||
type ConfirmationCodeNotification struct {
|
||||
messaging.Envelope
|
||||
payload confirmationCodePayload
|
||||
}
|
||||
|
||||
func (ccn *ConfirmationCodeNotification) Serialize() ([]byte, error) {
|
||||
data, err := json.Marshal(ccn.payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ccn.Envelope.Wrap(data)
|
||||
}
|
||||
|
||||
func newConfirmationEvent(action nm.NotificationAction) model.NotificationEvent {
|
||||
return model.NewNotification(mservice.Confirmations, action)
|
||||
}
|
||||
|
||||
func NewConfirmationCodeEnvelope(sender string, accountRef primitive.ObjectID, destination string, target model.ConfirmationTarget, code string) messaging.Envelope {
|
||||
return &ConfirmationCodeNotification{
|
||||
Envelope: messaging.CreateEnvelope(sender, newConfirmationEvent(nm.NAPending)),
|
||||
payload: confirmationCodePayload{
|
||||
AccountRef: accountRef.Hex(),
|
||||
Destination: destination,
|
||||
Target: string(target),
|
||||
Code: code,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package notifications
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/tech/sendico/pkg/db/account"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
me "github.com/tech/sendico/pkg/messaging/envelope"
|
||||
ch "github.com/tech/sendico/pkg/messaging/notifications/confirmation/handler"
|
||||
np "github.com/tech/sendico/pkg/messaging/notifications/processor"
|
||||
"github.com/tech/sendico/pkg/mlogger"
|
||||
"github.com/tech/sendico/pkg/model"
|
||||
nm "github.com/tech/sendico/pkg/model/notification"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type ConfirmationCodeProcessor struct {
|
||||
logger mlogger.Logger
|
||||
db account.DB
|
||||
handler ch.ConfirmationCodeHandler
|
||||
event model.NotificationEvent
|
||||
}
|
||||
|
||||
func (ccp *ConfirmationCodeProcessor) Process(ctx context.Context, envelope me.Envelope) error {
|
||||
var msg confirmationCodePayload
|
||||
if err := json.Unmarshal(envelope.GetData(), &msg); err != nil {
|
||||
ccp.logger.Warn("Failed to unmarshal confirmation code envelope", zap.Error(err), zap.String("topic", ccp.event.ToString()))
|
||||
return err
|
||||
}
|
||||
|
||||
accountRef, err := primitive.ObjectIDFromHex(msg.AccountRef)
|
||||
if err != nil {
|
||||
ccp.logger.Warn("Failed to restore account id from envelope", zap.Error(err), zap.String("topic", ccp.event.ToString()), zap.String("account_ref", msg.AccountRef))
|
||||
return err
|
||||
}
|
||||
|
||||
var account model.Account
|
||||
if err := ccp.db.Get(ctx, accountRef, &account); err != nil {
|
||||
ccp.logger.Warn("Failed to fetch account for confirmation code", zap.Error(err), zap.String("topic", ccp.event.ToString()), zap.String("account_ref", msg.AccountRef))
|
||||
return err
|
||||
}
|
||||
|
||||
target := model.ConfirmationTarget(msg.Target)
|
||||
if target != model.ConfirmationTargetLogin && target != model.ConfirmationTargetPayout {
|
||||
return merrors.InvalidArgument("invalid confirmation target", "target")
|
||||
}
|
||||
if msg.Code == "" {
|
||||
return merrors.InvalidArgument("empty confirmation code", "code")
|
||||
}
|
||||
if msg.Destination == "" {
|
||||
return merrors.InvalidArgument("empty destination", "destination")
|
||||
}
|
||||
|
||||
return ccp.handler(ctx, &account, msg.Destination, target, msg.Code)
|
||||
}
|
||||
|
||||
func (ccp *ConfirmationCodeProcessor) GetSubject() model.NotificationEvent {
|
||||
return ccp.event
|
||||
}
|
||||
|
||||
func NewConfirmationCodeProcessor(logger mlogger.Logger, db account.DB, handler ch.ConfirmationCodeHandler) np.EnvelopeProcessor {
|
||||
return &ConfirmationCodeProcessor{
|
||||
logger: logger.Named("confirmation_code_processor"),
|
||||
db: db,
|
||||
handler: handler,
|
||||
event: newConfirmationEvent(nm.NAPending),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user