service backend
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package notifications
|
||||
|
||||
import (
|
||||
messaging "github.com/tech/sendico/pkg/messaging/envelope"
|
||||
gmessaging "github.com/tech/sendico/pkg/messaging/internal/generated"
|
||||
"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"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
type AccountNotification struct {
|
||||
messaging.Envelope
|
||||
accountRef primitive.ObjectID
|
||||
}
|
||||
|
||||
func (acn *AccountNotification) Serialize() ([]byte, error) {
|
||||
var msg gmessaging.AccountCreatedEvent
|
||||
msg.AccountRef = acn.accountRef.Hex()
|
||||
data, err := proto.Marshal(&msg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return acn.Envelope.Wrap(data)
|
||||
}
|
||||
|
||||
func NewAccountNotification(action nm.NotificationAction) model.NotificationEvent {
|
||||
return model.NewNotification(mservice.Accounts, action)
|
||||
}
|
||||
|
||||
func NewAccountImp(sender string, accountRef primitive.ObjectID, action nm.NotificationAction) messaging.Envelope {
|
||||
return &AccountNotification{
|
||||
Envelope: messaging.CreateEnvelope(sender, NewAccountNotification(action)),
|
||||
accountRef: accountRef,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package notifications
|
||||
|
||||
import (
|
||||
messaging "github.com/tech/sendico/pkg/messaging/envelope"
|
||||
gmessaging "github.com/tech/sendico/pkg/messaging/internal/generated"
|
||||
"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"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
type PasswordResetNotification struct {
|
||||
messaging.Envelope
|
||||
accountRef primitive.ObjectID
|
||||
resetToken string
|
||||
}
|
||||
|
||||
func (prn *PasswordResetNotification) Serialize() ([]byte, error) {
|
||||
var msg gmessaging.PasswordResetEvent
|
||||
msg.AccountRef = prn.accountRef.Hex()
|
||||
msg.ResetToken = prn.resetToken
|
||||
data, err := proto.Marshal(&msg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return prn.Envelope.Wrap(data)
|
||||
}
|
||||
|
||||
func NewPasswordResetNotification(action nm.NotificationAction) model.NotificationEvent {
|
||||
return model.NewNotification(mservice.Accounts, action)
|
||||
}
|
||||
|
||||
func NewPasswordResetImp(sender string, accountRef primitive.ObjectID, resetToken string, action nm.NotificationAction) messaging.Envelope {
|
||||
return &PasswordResetNotification{
|
||||
Envelope: messaging.CreateEnvelope(sender, NewPasswordResetNotification(action)),
|
||||
accountRef: accountRef,
|
||||
resetToken: resetToken,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package notifications
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/tech/sendico/pkg/db/account"
|
||||
me "github.com/tech/sendico/pkg/messaging/envelope"
|
||||
gmessaging "github.com/tech/sendico/pkg/messaging/internal/generated"
|
||||
mah "github.com/tech/sendico/pkg/messaging/notifications/account/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"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
type PasswordResetNotificationProcessor struct {
|
||||
logger mlogger.Logger
|
||||
handler mah.PasswordResetHandler
|
||||
db account.DB
|
||||
event model.NotificationEvent
|
||||
}
|
||||
|
||||
func (prnp *PasswordResetNotificationProcessor) Process(ctx context.Context, envelope me.Envelope) error {
|
||||
var msg gmessaging.PasswordResetEvent
|
||||
if err := proto.Unmarshal(envelope.GetData(), &msg); err != nil {
|
||||
prnp.logger.Warn("Failed to unmarshall envelope", zap.Error(err), zap.String("topic", prnp.event.ToString()))
|
||||
return err
|
||||
}
|
||||
accountRef, err := primitive.ObjectIDFromHex(msg.AccountRef)
|
||||
if err != nil {
|
||||
prnp.logger.Warn("Failed to restore object ID", zap.Error(err), zap.String("topic", prnp.event.ToString()), zap.String("account_ref", msg.AccountRef))
|
||||
return err
|
||||
}
|
||||
var account model.Account
|
||||
if err := prnp.db.Get(ctx, accountRef, &account); err != nil {
|
||||
prnp.logger.Warn("Failed to fetch account", zap.Error(err), zap.String("topic", prnp.event.ToString()), zap.String("account_ref", msg.AccountRef))
|
||||
return err
|
||||
}
|
||||
return prnp.handler(ctx, &account, msg.ResetToken)
|
||||
}
|
||||
|
||||
func (prnp *PasswordResetNotificationProcessor) GetSubject() model.NotificationEvent {
|
||||
return prnp.event
|
||||
}
|
||||
|
||||
func NewPasswordResetMessageProcessor(logger mlogger.Logger, handler mah.PasswordResetHandler, db account.DB, action nm.NotificationAction) np.EnvelopeProcessor {
|
||||
event := NewPasswordResetNotification(action)
|
||||
return &PasswordResetNotificationProcessor{
|
||||
logger: logger.Named("password_reset_processor"),
|
||||
handler: handler,
|
||||
db: db,
|
||||
event: event,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package notifications
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/tech/sendico/pkg/db/account"
|
||||
me "github.com/tech/sendico/pkg/messaging/envelope"
|
||||
gmessaging "github.com/tech/sendico/pkg/messaging/internal/generated"
|
||||
mah "github.com/tech/sendico/pkg/messaging/notifications/account/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"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
type AccoountNotificaionProcessor struct {
|
||||
logger mlogger.Logger
|
||||
handler mah.AccountHandler
|
||||
db account.DB
|
||||
event model.NotificationEvent
|
||||
}
|
||||
|
||||
func (acnp *AccoountNotificaionProcessor) Process(ctx context.Context, envelope me.Envelope) error {
|
||||
var msg gmessaging.AccountCreatedEvent
|
||||
if err := proto.Unmarshal(envelope.GetData(), &msg); err != nil {
|
||||
acnp.logger.Warn("Failed to unmarshall envelope", zap.Error(err), zap.String("topic", acnp.event.ToString()))
|
||||
return err
|
||||
}
|
||||
accountRef, err := primitive.ObjectIDFromHex(msg.AccountRef)
|
||||
if err != nil {
|
||||
acnp.logger.Warn("Failed to restore object ID", zap.Error(err), zap.String("topic", acnp.event.ToString()), zap.String("account_ref", msg.AccountRef))
|
||||
return err
|
||||
}
|
||||
var account model.Account
|
||||
if err := acnp.db.Get(ctx, accountRef, &account); err != nil {
|
||||
acnp.logger.Warn("Failed to fetch account", zap.Error(err), zap.String("topic", acnp.event.ToString()), zap.String("account_ref", msg.AccountRef))
|
||||
return err
|
||||
}
|
||||
return acnp.handler(ctx, &account)
|
||||
}
|
||||
|
||||
func (acnp *AccoountNotificaionProcessor) GetSubject() model.NotificationEvent {
|
||||
return acnp.event
|
||||
}
|
||||
|
||||
func NewAccountMessageProcessor(logger mlogger.Logger, handler mah.AccountHandler, db account.DB, action nm.NotificationAction) np.EnvelopeProcessor {
|
||||
event := NewAccountNotification(action)
|
||||
return &AccoountNotificaionProcessor{
|
||||
logger: logger.Named("message_processor"),
|
||||
handler: handler,
|
||||
db: db,
|
||||
event: event,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user