-
Notifications
You must be signed in to change notification settings - Fork 30
/
billing_check.go
74 lines (65 loc) · 2.18 KB
/
billing_check.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package v1beta1
import (
"context"
"fmt"
"github.com/raystack/frontier/billing/customer"
"github.com/raystack/frontier/billing/entitlement"
"github.com/raystack/frontier/core/audit"
"github.com/raystack/frontier/core/relation"
"github.com/raystack/frontier/internal/bootstrap/schema"
grpczap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
frontierv1beta1 "github.com/raystack/frontier/proto/v1beta1"
)
type EntitlementService interface {
Check(ctx context.Context, customerID, featureID string) (bool, error)
CheckPlanEligibility(ctx context.Context, customerID string) error
}
func (h Handler) CheckFeatureEntitlement(ctx context.Context, request *frontierv1beta1.CheckFeatureEntitlementRequest) (*frontierv1beta1.CheckFeatureEntitlementResponse, error) {
logger := grpczap.Extract(ctx)
checkStatus, err := h.entitlementService.Check(ctx, request.GetBillingId(), request.GetFeature())
if err != nil {
logger.Error(err.Error())
return nil, grpcInternalServerError
}
return &frontierv1beta1.CheckFeatureEntitlementResponse{
Status: checkStatus,
}, nil
}
// CheckPlanEntitlement is only currently used to restrict seat based plans
func (h Handler) CheckPlanEntitlement(ctx context.Context, obj relation.Object) error {
// only check for project or org
var orgID string
switch obj.Namespace {
case schema.ProjectNamespace:
proj, err := h.projectService.Get(ctx, obj.ID)
if err != nil {
return err
}
orgID = proj.Organization.ID
case schema.OrganizationNamespace:
org, err := h.orgService.Get(ctx, obj.ID)
if err != nil {
return err
}
orgID = org.ID
}
if orgID != "" {
customers, err := h.customerService.List(ctx, customer.Filter{
OrgID: orgID,
})
if err != nil {
return err
}
for _, customr := range customers {
audit.GetAuditor(ctx, orgID).LogWithAttrs(audit.BillingEntitlementCheckedEvent, audit.Target{
ID: customr.ID,
Type: "billing_account",
}, map[string]string{})
if err := h.entitlementService.CheckPlanEligibility(ctx, customr.ID); err != nil {
return fmt.Errorf("%s: %w", entitlement.ErrPlanEntitlementFailed, err)
}
}
}
// default condition is true for now to avoid false positives
return nil
}