Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion api/handlers/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
)

var ErrHostRequiredForBilling = errors.New("organisation host (assigned domain) is required for billing. Please set the assigned domain in the configuration")
var ErrOwnerEmailRequiredForBilling = errors.New("organisation owner email is required for billing")

type BillingHandler struct {
*Handler
Expand Down Expand Up @@ -58,7 +59,8 @@ func (h *BillingHandler) ensureOrganisationInBilling(w http.ResponseWriter, r *h

ownerEmail := h.getOwnerEmail(r.Context(), orgID)
if ownerEmail == "" {
h.A.Logger.Warnf("Failed to fetch owner email for organisation %s, using empty billing_email", orgID)
_ = render.Render(w, r, util.NewErrorResponse(ErrOwnerEmailRequiredForBilling.Error(), http.StatusUnprocessableEntity))
return true
}

orgData := billing.BillingOrganisation{
Expand Down
10 changes: 9 additions & 1 deletion services/create_organisation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"strings"
"time"

"github.com/dchest/uniuri"
Expand Down Expand Up @@ -122,10 +123,17 @@ func RunBillingOrganisationSync(
if logger == nil {
panic("RunBillingOrganisationSync: logger is required and must not be nil")
}

trimmedEmail := strings.TrimSpace(userEmail)
if trimmedEmail == "" {
logger.Error("create_organisation: owner email is empty, skipping billing sync", "org_id", org.UID)
return
}

orgData := billing.BillingOrganisation{
Name: org.Name,
ExternalID: org.UID,
BillingEmail: userEmail,
BillingEmail: trimmedEmail,
Host: billingHost,
}
resp, createErr := billingClient.CreateOrganisation(ctx, orgData)
Expand Down
18 changes: 18 additions & 0 deletions services/create_organisation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ import (
log "github.com/frain-dev/convoy/pkg/logger"
)

type panicOnCreateBillingClient struct {
*billing.MockBillingClient
}

func (panicOnCreateBillingClient) CreateOrganisation(context.Context, billing.BillingOrganisation) (*billing.Response[billing.BillingOrganisation], error) {
panic("CreateOrganisation must not be called when owner email is empty")
}

func provideCreateOrganisationService(ctrl *gomock.Controller, newOrg *datastore.OrganisationRequest, user *datastore.User) *CreateOrganisationService {
return &CreateOrganisationService{
OrgRepo: mocks.NewMockOrganisationRepository(ctrl),
Expand Down Expand Up @@ -215,4 +223,14 @@ func TestRunBillingOrganisationSync(t *testing.T) {

RunBillingOrganisationSync(ctx, mockBilling, org, cfg, userEmail, billingHost, mockOrgRepo, log.New("convoy", log.LevelInfo))
})

t.Run("skips_billing_sync_when_owner_email_is_empty", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockOrgRepo := mocks.NewMockOrganisationRepository(ctrl)
mockBilling := panicOnCreateBillingClient{MockBillingClient: &billing.MockBillingClient{}}

RunBillingOrganisationSync(ctx, &mockBilling, org, cfg, "", billingHost, mockOrgRepo, log.New("convoy", log.LevelInfo))
})
}
Loading