Skip to content

Commit

Permalink
feat(payments): improve webhooks translate response
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-nicolas committed Sep 13, 2024
1 parent 3718e0e commit 6a11b77
Show file tree
Hide file tree
Showing 10 changed files with 422 additions and 320 deletions.
47 changes: 27 additions & 20 deletions components/payments/internal/connectors/engine/plugins/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,28 +195,35 @@ func (i *impl) TranslateWebhook(ctx context.Context, req models.TranslateWebhook
return models.TranslateWebhookResponse{}, err
}

res := models.TranslateWebhookResponse{
IdempotencyKey: resp.IdempotencyKey,
}
responses := make([]models.WebhookResponse, 0, len(resp.Responses))
for _, response := range resp.Responses {
r := models.WebhookResponse{
IdempotencyKey: response.IdempotencyKey,
}

switch v := resp.Translated.(type) {
case *services.TranslateWebhookResponse_Payment:
p, err := grpc.TranslateProtoPayment(v.Payment)
if err != nil {
return models.TranslateWebhookResponse{}, err
switch v := response.Translated.(type) {
case *services.TranslateWebhookResponse_Response_Payment:
p, err := grpc.TranslateProtoPayment(v.Payment)
if err != nil {
return models.TranslateWebhookResponse{}, err
}
r.Payment = &p
case *services.TranslateWebhookResponse_Response_Account:
a := grpc.TranslateProtoAccount(v.Account)
r.Account = &a
case *services.TranslateWebhookResponse_Response_ExternalAccount:
a := grpc.TranslateProtoAccount(v.ExternalAccount)
r.ExternalAccount = &a
default:
return models.TranslateWebhookResponse{}, errors.New("unknown translated webhook type")
}
res.Payment = &p
case *services.TranslateWebhookResponse_Account:
a := grpc.TranslateProtoAccount(v.Account)
res.Account = &a
case *services.TranslateWebhookResponse_ExternalAccount:
a := grpc.TranslateProtoAccount(v.ExternalAccount)
res.ExternalAccount = &a
default:
return models.TranslateWebhookResponse{}, errors.New("unknown translated webhook type")
}

return res, nil

responses = append(responses, r)
}

return models.TranslateWebhookResponse{
Responses: responses,
}, nil
}

var _ models.Plugin = &impl{}
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,33 @@ func (w Workflow) runHandleWebhooks(
return errors.Wrap(err, "failed to translate webhook")
}

if err := workflow.ExecuteChildWorkflow(
workflow.WithChildOptions(
ctx,
workflow.ChildWorkflowOptions{
WorkflowID: fmt.Sprintf("store-webhook-%s-%s", handleWebhooks.ConnectorID.String(), resp.IdempotencyKey),
TaskQueue: handleWebhooks.ConnectorID.Reference,
ParentClosePolicy: enums.PARENT_CLOSE_POLICY_ABANDON,
WorkflowIDReusePolicy: enums.WORKFLOW_ID_REUSE_POLICY_ALLOW_DUPLICATE_FAILED_ONLY,
for _, response := range resp.Responses {
if err := workflow.ExecuteChildWorkflow(
workflow.WithChildOptions(
ctx,
workflow.ChildWorkflowOptions{
WorkflowID: fmt.Sprintf("store-webhook-%s-%s", handleWebhooks.ConnectorID.String(), response.IdempotencyKey),
TaskQueue: handleWebhooks.ConnectorID.Reference,
ParentClosePolicy: enums.PARENT_CLOSE_POLICY_ABANDON,
WorkflowIDReusePolicy: enums.WORKFLOW_ID_REUSE_POLICY_ALLOW_DUPLICATE_FAILED_ONLY,
},
),
RunStoreWebhookTranslation,
StoreWebhookTranslation{
ConnectorID: handleWebhooks.ConnectorID,
Account: response.Account,
ExternalAccount: response.ExternalAccount,
Payment: response.Payment,
},
),
RunStoreWebhookTranslation,
StoreWebhookTranslation{
ConnectorID: handleWebhooks.ConnectorID,
Account: resp.Account,
ExternalAccount: resp.ExternalAccount,
Payment: resp.Payment,
},
).Get(ctx, nil); err != nil {
applicationError := &temporal.ApplicationError{}
if errors.As(err, &applicationError) {
if applicationError.Type() != "ChildWorkflowExecutionAlreadyStartedError" {
return err
).Get(ctx, nil); err != nil {
applicationError := &temporal.ApplicationError{}
if errors.As(err, &applicationError) {
if applicationError.Type() != "ChildWorkflowExecutionAlreadyStartedError" {
return err
}
} else {
return errors.Wrap(err, "running store workflow")
}
} else {
return errors.Wrap(err, "running store workflow")
}
}

Expand Down
Loading

0 comments on commit 6a11b77

Please sign in to comment.