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,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package notifications
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/tech/sendico/pkg/db/account"
|
||||
"github.com/tech/sendico/pkg/db/invitation"
|
||||
mih "github.com/tech/sendico/pkg/messaging/notifications/invitation/handler"
|
||||
no "github.com/tech/sendico/pkg/messaging/notifications/object"
|
||||
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"
|
||||
"github.com/tech/sendico/pkg/mservice"
|
||||
"github.com/tech/sendico/pkg/mutil/mzap"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type InvitationNotificaionProcessor struct {
|
||||
np.EnvelopeProcessor
|
||||
logger mlogger.Logger
|
||||
handler mih.InvitationHandler
|
||||
db invitation.DB
|
||||
adb account.DB
|
||||
}
|
||||
|
||||
func (ianp *InvitationNotificaionProcessor) onInvitation(
|
||||
ctx context.Context,
|
||||
objectType mservice.Type,
|
||||
objectRef, actorAccountRef primitive.ObjectID,
|
||||
action nm.NotificationAction,
|
||||
) error {
|
||||
var invitation model.Invitation
|
||||
if err := ianp.db.Unprotected().Get(ctx, objectRef, &invitation); err != nil {
|
||||
ianp.logger.Warn("Failed to fetch invitation object", zap.Error(err), mzap.ObjRef("object_ref", objectRef))
|
||||
return err
|
||||
}
|
||||
var account model.Account
|
||||
if err := ianp.adb.Get(ctx, actorAccountRef, &account); err != nil {
|
||||
ianp.logger.Warn("Failed to fetch actor account", zap.Error(err), mzap.ObjRef("actor_account_ref", actorAccountRef))
|
||||
return err
|
||||
}
|
||||
return ianp.handler(ctx, &account, &invitation)
|
||||
}
|
||||
|
||||
func NewInvitationMessageProcessor(
|
||||
logger mlogger.Logger,
|
||||
handler mih.InvitationHandler,
|
||||
db invitation.DB,
|
||||
adb account.DB,
|
||||
action nm.NotificationAction,
|
||||
) np.EnvelopeProcessor {
|
||||
l := logger.Named(mservice.Invitations)
|
||||
res := &InvitationNotificaionProcessor{
|
||||
logger: l,
|
||||
db: db,
|
||||
adb: adb,
|
||||
handler: handler,
|
||||
}
|
||||
res.EnvelopeProcessor = no.NewObjectChangedMessageProcessor(l, mservice.Invitations, action, res.onInvitation)
|
||||
return res
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
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"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
type NResultNotification struct {
|
||||
messaging.Envelope
|
||||
result *model.NotificationResult
|
||||
}
|
||||
|
||||
func (nrn *NResultNotification) Serialize() ([]byte, error) {
|
||||
msg := gmessaging.NotificationSentEvent{
|
||||
UserID: nrn.result.UserID,
|
||||
Channel: nrn.result.Channel,
|
||||
Locale: nrn.result.Locale,
|
||||
TemplateID: nrn.result.TemplateID,
|
||||
Status: &gmessaging.OperationResult{
|
||||
IsSuccessful: nrn.result.Result.IsSuccessful,
|
||||
ErrorDescription: nrn.result.Result.Error,
|
||||
},
|
||||
}
|
||||
data, err := proto.Marshal(&msg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nrn.Envelope.Wrap(data)
|
||||
}
|
||||
|
||||
func NewNRNotification() model.NotificationEvent {
|
||||
return model.NewNotification(mservice.Notifications, nm.NASent)
|
||||
}
|
||||
|
||||
func NewNResultNotification(sender string, result *model.NotificationResult) messaging.Envelope {
|
||||
return &NResultNotification{
|
||||
Envelope: messaging.CreateEnvelope(sender, NewNRNotification()),
|
||||
result: result,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package notifications
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
me "github.com/tech/sendico/pkg/messaging/envelope"
|
||||
gmessaging "github.com/tech/sendico/pkg/messaging/internal/generated"
|
||||
nh "github.com/tech/sendico/pkg/messaging/notifications/notification/handler"
|
||||
np "github.com/tech/sendico/pkg/messaging/notifications/processor"
|
||||
"github.com/tech/sendico/pkg/mlogger"
|
||||
"github.com/tech/sendico/pkg/model"
|
||||
"go.uber.org/zap"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
type NResultNotificaionProcessor struct {
|
||||
logger mlogger.Logger
|
||||
handler nh.NResultHandler
|
||||
event model.NotificationEvent
|
||||
}
|
||||
|
||||
func (nrp *NResultNotificaionProcessor) Process(ctx context.Context, envelope me.Envelope) error {
|
||||
var msg gmessaging.NotificationSentEvent
|
||||
if err := proto.Unmarshal(envelope.GetData(), &msg); err != nil {
|
||||
nrp.logger.Warn("Failed to unmarshall envelope", zap.Error(err), zap.String("topic", nrp.event.ToString()))
|
||||
return err
|
||||
}
|
||||
nresult := &model.NotificationResult{
|
||||
AmpliEvent: model.AmpliEvent{
|
||||
UserID: msg.UserID,
|
||||
},
|
||||
Channel: msg.Channel,
|
||||
TemplateID: msg.TemplateID,
|
||||
Locale: msg.Locale,
|
||||
Result: model.OperationResult{
|
||||
IsSuccessful: msg.Status.IsSuccessful,
|
||||
Error: msg.Status.ErrorDescription,
|
||||
},
|
||||
}
|
||||
return nrp.handler(ctx, nresult)
|
||||
}
|
||||
|
||||
func (nrp *NResultNotificaionProcessor) GetSubject() model.NotificationEvent {
|
||||
return nrp.event
|
||||
}
|
||||
|
||||
func NewAccountMessageProcessor(logger mlogger.Logger, handler nh.NResultHandler) np.EnvelopeProcessor {
|
||||
return &NResultNotificaionProcessor{
|
||||
logger: logger.Named("message_processor"),
|
||||
handler: handler,
|
||||
event: NewNRNotification(),
|
||||
}
|
||||
}
|
||||
46
api/pkg/messaging/internal/notifications/object/object.go
Normal file
46
api/pkg/messaging/internal/notifications/object/object.go
Normal file
@@ -0,0 +1,46 @@
|
||||
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 ObjectNotification struct {
|
||||
messaging.Envelope
|
||||
actorAccountRef primitive.ObjectID
|
||||
objectRef primitive.ObjectID
|
||||
}
|
||||
|
||||
func (acn *ObjectNotification) Serialize() ([]byte, error) {
|
||||
var msg gmessaging.ObjectUpdatedEvent
|
||||
msg.ActorAccountRef = acn.actorAccountRef.Hex()
|
||||
msg.ObjectRef = acn.objectRef.Hex()
|
||||
data, err := proto.Marshal(&msg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return acn.Envelope.Wrap(data)
|
||||
}
|
||||
|
||||
func NewObjectNotification(t mservice.Type, action nm.NotificationAction) model.NotificationEvent {
|
||||
return model.NewNotification(t, action)
|
||||
}
|
||||
|
||||
func NewObjectImp(
|
||||
sender string,
|
||||
actorAccountRef primitive.ObjectID,
|
||||
objectType mservice.Type,
|
||||
objectRef primitive.ObjectID,
|
||||
action nm.NotificationAction,
|
||||
) messaging.Envelope {
|
||||
return &ObjectNotification{
|
||||
Envelope: messaging.CreateEnvelope(sender, NewObjectNotification(objectType, action)),
|
||||
actorAccountRef: actorAccountRef,
|
||||
objectRef: objectRef,
|
||||
}
|
||||
}
|
||||
55
api/pkg/messaging/internal/notifications/object/processor.go
Normal file
55
api/pkg/messaging/internal/notifications/object/processor.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package notifications
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
me "github.com/tech/sendico/pkg/messaging/envelope"
|
||||
gmessaging "github.com/tech/sendico/pkg/messaging/internal/generated"
|
||||
moh "github.com/tech/sendico/pkg/messaging/notifications/object/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"
|
||||
"github.com/tech/sendico/pkg/mservice"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.uber.org/zap"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
type ObjectNotificaionProcessor struct {
|
||||
logger mlogger.Logger
|
||||
handler moh.ObjectUpdateHandler
|
||||
event model.NotificationEvent
|
||||
}
|
||||
|
||||
func (ounp *ObjectNotificaionProcessor) Process(ctx context.Context, envelope me.Envelope) error {
|
||||
var msg gmessaging.ObjectUpdatedEvent
|
||||
if err := proto.Unmarshal(envelope.GetData(), &msg); err != nil {
|
||||
ounp.logger.Warn("Failed to unmarshall envelope", zap.Error(err), zap.String("topic", ounp.event.ToString()))
|
||||
return err
|
||||
}
|
||||
actorAccountRef, err := primitive.ObjectIDFromHex(msg.ActorAccountRef)
|
||||
if err != nil {
|
||||
ounp.logger.Warn("Failed to restore actor account reference", zap.Error(err), zap.String("topic", ounp.event.ToString()), zap.String("actor_account_ref", msg.ActorAccountRef))
|
||||
return err
|
||||
}
|
||||
objectRef, err := primitive.ObjectIDFromHex(msg.ObjectRef)
|
||||
if err != nil {
|
||||
ounp.logger.Warn("Failed to restore object reference", zap.Error(err), zap.String("topic", ounp.event.ToString()), zap.String("object_ref", msg.ObjectRef))
|
||||
return err
|
||||
}
|
||||
|
||||
return ounp.handler(ctx, envelope.GetSignature().GetType(), objectRef, actorAccountRef, envelope.GetSignature().GetAction())
|
||||
}
|
||||
|
||||
func (acnp *ObjectNotificaionProcessor) GetSubject() model.NotificationEvent {
|
||||
return acnp.event
|
||||
}
|
||||
|
||||
func NewObjectChangeMessageProcessor(logger mlogger.Logger, handler moh.ObjectUpdateHandler, objectType mservice.Type, action nm.NotificationAction) np.EnvelopeProcessor {
|
||||
return &ObjectNotificaionProcessor{
|
||||
logger: logger.Named("message_processor"),
|
||||
handler: handler,
|
||||
event: NewObjectNotification(objectType, action),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user