refactored notificatoin / tgsettle responsibility boundaries
This commit is contained in:
@@ -36,11 +36,34 @@ func (crn *ConfirmationResultNotification) Serialize() ([]byte, error) {
|
||||
return crn.Envelope.Wrap(data)
|
||||
}
|
||||
|
||||
type ConfirmationDispatchNotification struct {
|
||||
messaging.Envelope
|
||||
payload model.ConfirmationRequestDispatch
|
||||
}
|
||||
|
||||
func (cdn *ConfirmationDispatchNotification) Serialize() ([]byte, error) {
|
||||
data, err := json.Marshal(cdn.payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cdn.Envelope.Wrap(data)
|
||||
}
|
||||
|
||||
func confirmationRequestEvent() model.NotificationEvent {
|
||||
return model.NewNotification(mservice.Notifications, nm.NAConfirmationRequest)
|
||||
}
|
||||
|
||||
func confirmationResultEvent(sourceService, rail string) model.NotificationEvent {
|
||||
sourceService, rail = normalizeSourceRail(sourceService, rail)
|
||||
return model.NewNotification(mservice.Verification, nm.NotificationAction(sourceService+"."+rail))
|
||||
}
|
||||
|
||||
func confirmationDispatchEvent(sourceService, rail string) model.NotificationEvent {
|
||||
sourceService, rail = normalizeSourceRail(sourceService, rail)
|
||||
return model.NewNotification(mservice.Verification, nm.NotificationAction(sourceService+"."+rail+".dispatch"))
|
||||
}
|
||||
|
||||
func normalizeSourceRail(sourceService, rail string) (string, string) {
|
||||
action := strings.TrimSpace(sourceService)
|
||||
if action == "" {
|
||||
action = "unknown"
|
||||
@@ -51,7 +74,7 @@ func confirmationResultEvent(sourceService, rail string) model.NotificationEvent
|
||||
rail = "default"
|
||||
}
|
||||
rail = strings.ToLower(rail)
|
||||
return model.NewNotification(mservice.Verification, nm.NotificationAction(action+"."+rail))
|
||||
return action, rail
|
||||
}
|
||||
|
||||
func NewConfirmationRequestEnvelope(sender string, request *model.ConfirmationRequest) messaging.Envelope {
|
||||
@@ -75,3 +98,14 @@ func NewConfirmationResultEnvelope(sender string, result *model.ConfirmationResu
|
||||
payload: payload,
|
||||
}
|
||||
}
|
||||
|
||||
func NewConfirmationDispatchEnvelope(sender string, dispatch *model.ConfirmationRequestDispatch, sourceService, rail string) messaging.Envelope {
|
||||
var payload model.ConfirmationRequestDispatch
|
||||
if dispatch != nil {
|
||||
payload = *dispatch
|
||||
}
|
||||
return &ConfirmationDispatchNotification{
|
||||
Envelope: messaging.CreateEnvelope(sender, confirmationDispatchEvent(sourceService, rail)),
|
||||
payload: payload,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,6 +58,29 @@ func (crp *ConfirmationResultProcessor) GetSubject() model.NotificationEvent {
|
||||
return crp.event
|
||||
}
|
||||
|
||||
type ConfirmationDispatchProcessor struct {
|
||||
logger mlogger.Logger
|
||||
handler ch.ConfirmationDispatchHandler
|
||||
event model.NotificationEvent
|
||||
}
|
||||
|
||||
func (cdp *ConfirmationDispatchProcessor) Process(ctx context.Context, envelope me.Envelope) error {
|
||||
var msg model.ConfirmationRequestDispatch
|
||||
if err := json.Unmarshal(envelope.GetData(), &msg); err != nil {
|
||||
cdp.logger.Warn("Failed to decode confirmation dispatch envelope", zap.Error(err), zap.String("topic", cdp.event.ToString()))
|
||||
return err
|
||||
}
|
||||
if cdp.handler == nil {
|
||||
cdp.logger.Warn("Confirmation dispatch handler is not configured", zap.String("topic", cdp.event.ToString()))
|
||||
return nil
|
||||
}
|
||||
return cdp.handler(ctx, &msg)
|
||||
}
|
||||
|
||||
func (cdp *ConfirmationDispatchProcessor) GetSubject() model.NotificationEvent {
|
||||
return cdp.event
|
||||
}
|
||||
|
||||
func NewConfirmationRequestProcessor(logger mlogger.Logger, handler ch.ConfirmationRequestHandler) np.EnvelopeProcessor {
|
||||
if logger != nil {
|
||||
logger = logger.Named("confirmation_request_processor")
|
||||
@@ -79,3 +102,14 @@ func NewConfirmationResultProcessor(logger mlogger.Logger, sourceService, rail s
|
||||
event: confirmationResultEvent(sourceService, rail),
|
||||
}
|
||||
}
|
||||
|
||||
func NewConfirmationDispatchProcessor(logger mlogger.Logger, sourceService, rail string, handler ch.ConfirmationDispatchHandler) np.EnvelopeProcessor {
|
||||
if logger != nil {
|
||||
logger = logger.Named("confirmation_dispatch_processor")
|
||||
}
|
||||
return &ConfirmationDispatchProcessor{
|
||||
logger: logger,
|
||||
handler: handler,
|
||||
event: confirmationDispatchEvent(sourceService, rail),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user