Skip to content
Merged
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
55 changes: 41 additions & 14 deletions internal/api/v1beta1connect/v1beta1connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ package v1beta1connect
import (
"context"

"connectrpc.com/connect"
"github.com/raystack/frontier/billing/checkout"
"github.com/raystack/frontier/billing/customer"
"github.com/raystack/frontier/billing/subscription"
"github.com/raystack/frontier/core/authenticate"
"github.com/raystack/frontier/internal/api"
"github.com/raystack/frontier/pkg/errors"
frontierv1beta1connect "github.com/raystack/frontier/proto/v1beta1/frontierv1beta1connect"
)

Expand Down Expand Up @@ -126,35 +131,57 @@ func (h *ConnectHandler) GetBillingAccountFromOrgID(ctx context.Context, orgID s
func (h *ConnectHandler) GetOrgIDFromSubscriptionID(ctx context.Context, subscriptionID string) (string, error) {
sub, err := h.subscriptionService.GetByID(ctx, subscriptionID)
if err != nil {
return "", err
if errors.Is(err, subscription.ErrNotFound) {
return "", connect.NewError(connect.CodeNotFound, ErrNotFound)
}
if errors.Is(err, subscription.ErrInvalidUUID) || errors.Is(err, subscription.ErrInvalidID) {
return "", connect.NewError(connect.CodeInvalidArgument, err)
}
return "", connect.NewError(connect.CodeInternal, ErrInternalServerError)
}
// Get the billing account (customer) to find the org
customer, err := h.customerService.GetByID(ctx, sub.CustomerID)
cust, err := h.customerService.GetByID(ctx, sub.CustomerID)
if err != nil {
return "", err
if errors.Is(err, customer.ErrNotFound) {
return "", connect.NewError(connect.CodeNotFound, ErrNotFound)
}
return "", connect.NewError(connect.CodeInternal, ErrInternalServerError)
}
return customer.OrgID, nil
return cust.OrgID, nil
}

// GetOrgIDFromCheckoutID returns the organization ID for a given checkout
func (h *ConnectHandler) GetOrgIDFromCheckoutID(ctx context.Context, checkoutID string) (string, error) {
checkout, err := h.checkoutService.GetByID(ctx, checkoutID)
co, err := h.checkoutService.GetByID(ctx, checkoutID)
if err != nil {
return "", err
if errors.Is(err, checkout.ErrNotFound) {
return "", connect.NewError(connect.CodeNotFound, ErrNotFound)
}
if errors.Is(err, checkout.ErrInvalidUUID) || errors.Is(err, checkout.ErrInvalidID) {
return "", connect.NewError(connect.CodeInvalidArgument, err)
}
return "", connect.NewError(connect.CodeInternal, ErrInternalServerError)
}
// Get the billing account (customer) to find the org
customer, err := h.customerService.GetByID(ctx, checkout.CustomerID)
cust, err := h.customerService.GetByID(ctx, co.CustomerID)
if err != nil {
return "", err
if errors.Is(err, customer.ErrNotFound) {
return "", connect.NewError(connect.CodeNotFound, ErrNotFound)
}
return "", connect.NewError(connect.CodeInternal, ErrInternalServerError)
}
return customer.OrgID, nil
return cust.OrgID, nil
}

// GetOrgIDFromBillingAccountID returns the organization ID for a given billing account
func (h *ConnectHandler) GetOrgIDFromBillingAccountID(ctx context.Context, billingAccountID string) (string, error) {
customer, err := h.customerService.GetByID(ctx, billingAccountID)
cust, err := h.customerService.GetByID(ctx, billingAccountID)
if err != nil {
return "", err
if errors.Is(err, customer.ErrNotFound) {
return "", connect.NewError(connect.CodeNotFound, ErrNotFound)
}
if errors.Is(err, customer.ErrInvalidUUID) || errors.Is(err, customer.ErrInvalidID) {
return "", connect.NewError(connect.CodeInvalidArgument, err)
}
return "", connect.NewError(connect.CodeInternal, ErrInternalServerError)
}
return customer.OrgID, nil
return cust.OrgID, nil
}
Loading