82 lines
2.5 KiB
Go
82 lines
2.5 KiB
Go
package notifications
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
me "github.com/tech/sendico/pkg/messaging/envelope"
|
|
ch "github.com/tech/sendico/pkg/messaging/notifications/confirmations/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"
|
|
)
|
|
|
|
type ConfirmationRequestProcessor struct {
|
|
logger mlogger.Logger
|
|
handler ch.ConfirmationRequestHandler
|
|
event model.NotificationEvent
|
|
}
|
|
|
|
func (crp *ConfirmationRequestProcessor) Process(ctx context.Context, envelope me.Envelope) error {
|
|
var msg model.ConfirmationRequest
|
|
if err := json.Unmarshal(envelope.GetData(), &msg); err != nil {
|
|
crp.logger.Warn("Failed to decode confirmation request envelope", zap.Error(err), zap.String("topic", crp.event.ToString()))
|
|
return err
|
|
}
|
|
if crp.handler == nil {
|
|
crp.logger.Warn("Confirmation request handler is not configured", zap.String("topic", crp.event.ToString()))
|
|
return nil
|
|
}
|
|
return crp.handler(ctx, &msg)
|
|
}
|
|
|
|
func (crp *ConfirmationRequestProcessor) GetSubject() model.NotificationEvent {
|
|
return crp.event
|
|
}
|
|
|
|
type ConfirmationResultProcessor struct {
|
|
logger mlogger.Logger
|
|
handler ch.ConfirmationResultHandler
|
|
event model.NotificationEvent
|
|
}
|
|
|
|
func (crp *ConfirmationResultProcessor) Process(ctx context.Context, envelope me.Envelope) error {
|
|
var msg model.ConfirmationResult
|
|
if err := json.Unmarshal(envelope.GetData(), &msg); err != nil {
|
|
crp.logger.Warn("Failed to decode confirmation result envelope", zap.Error(err), zap.String("topic", crp.event.ToString()))
|
|
return err
|
|
}
|
|
if crp.handler == nil {
|
|
crp.logger.Warn("Confirmation result handler is not configured", zap.String("topic", crp.event.ToString()))
|
|
return nil
|
|
}
|
|
return crp.handler(ctx, &msg)
|
|
}
|
|
|
|
func (crp *ConfirmationResultProcessor) GetSubject() model.NotificationEvent {
|
|
return crp.event
|
|
}
|
|
|
|
func NewConfirmationRequestProcessor(logger mlogger.Logger, handler ch.ConfirmationRequestHandler) np.EnvelopeProcessor {
|
|
if logger != nil {
|
|
logger = logger.Named("confirmation_request_processor")
|
|
}
|
|
return &ConfirmationRequestProcessor{
|
|
logger: logger,
|
|
handler: handler,
|
|
event: confirmationRequestEvent(),
|
|
}
|
|
}
|
|
|
|
func NewConfirmationResultProcessor(logger mlogger.Logger, sourceService, rail string, handler ch.ConfirmationResultHandler) np.EnvelopeProcessor {
|
|
if logger != nil {
|
|
logger = logger.Named("confirmation_result_processor")
|
|
}
|
|
return &ConfirmationResultProcessor{
|
|
logger: logger,
|
|
handler: handler,
|
|
event: confirmationResultEvent(sourceService, rail),
|
|
}
|
|
}
|