Skip to content

Commit 555c089

Browse files
feat(deleter): write an audit record for each checkout removed during org delete (#1839)
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent 2930c71 commit 555c089

4 files changed

Lines changed: 124 additions & 0 deletions

File tree

‎core/audit/audit.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ const (
9696
ResourceDeletedEvent EventName = "app.resource.deleted"
9797

9898
BillingAccountDetailsUpdatedEvent EventName = "app.billing.account.details.updated"
99+
BillingCheckoutDeletedEvent EventName = "app.billing.checkout.deleted"
99100
)
100101

101102
var systemEvents = []EventName{
@@ -111,6 +112,7 @@ var systemEvents = []EventName{
111112
OrgCreatedEvent,
112113
OrgDeletedEvent,
113114
OrgDisabledEvent,
115+
BillingCheckoutDeletedEvent,
114116
}
115117

116118
func IsSystemEvent(event EventName) bool {

‎core/deleter/mocks/checkout_service.go‎

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎core/deleter/service.go‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010

1111
"github.com/raystack/frontier/core/authenticate"
1212

13+
"github.com/raystack/frontier/billing/checkout"
14+
1315
"github.com/raystack/frontier/billing/invoice"
1416

1517
"github.com/raystack/frontier/billing/customer"
@@ -105,6 +107,7 @@ type InvoiceService interface {
105107
}
106108

107109
type CheckoutService interface {
110+
List(ctx context.Context, filter checkout.Filter) ([]checkout.Checkout, error)
108111
DeleteByCustomer(ctx context.Context, customerID string) error
109112
}
110113

@@ -328,9 +331,39 @@ func (d Service) DeleteCustomers(ctx context.Context, id string) error {
328331
if err := d.invoiceService.DeleteByCustomer(ctx, c); err != nil {
329332
return fmt.Errorf("failed to delete org while deleting a billing account invoices[%s]: %w", c.ID, err)
330333
}
334+
checkouts, err := d.checkoutService.List(ctx, checkout.Filter{CustomerID: c.ID})
335+
if err != nil {
336+
return fmt.Errorf("failed to delete org while listing billing account checkouts[%s]: %w", c.ID, err)
337+
}
331338
if err := d.checkoutService.DeleteByCustomer(ctx, c.ID); err != nil {
332339
return fmt.Errorf("failed to delete org while deleting a billing account checkouts[%s]: %w", c.ID, err)
333340
}
341+
// the checkout rows are gone after this; the audit records keep the
342+
// provider references so the sessions can still be found on the provider.
343+
// The records are only written when ctx carries the audit service (the
344+
// API path seeds it); otherwise audit.NewLogger falls back to a noop
345+
auditLogger := audit.NewLogger(ctx, id)
346+
for _, ch := range checkouts {
347+
attrs := map[string]string{
348+
"provider_id": ch.ProviderID,
349+
"customer_id": ch.CustomerID,
350+
"plan_id": ch.PlanID,
351+
"product_id": ch.ProductID,
352+
"state": ch.State,
353+
"payment_status": ch.PaymentStatus,
354+
}
355+
for k, v := range attrs {
356+
if v == "" {
357+
delete(attrs, k)
358+
}
359+
}
360+
if err := auditLogger.LogWithAttrs(audit.BillingCheckoutDeletedEvent, audit.Target{
361+
ID: ch.ID,
362+
Type: "billing_checkout",
363+
}, attrs); err != nil {
364+
slog.WarnContext(ctx, "failed to write audit log", "error", err, "event", audit.BillingCheckoutDeletedEvent, "checkout_id", ch.ID)
365+
}
366+
}
334367
if err := d.creditService.DeleteByAccountID(ctx, c.ID); err != nil {
335368
return fmt.Errorf("failed to delete org while deleting a billing account transactions[%s]: %w", c.ID, err)
336369
}

‎core/deleter/service_test.go‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/google/uuid"
9+
"github.com/raystack/frontier/billing/checkout"
910
"github.com/raystack/frontier/billing/customer"
1011
"github.com/raystack/frontier/billing/invoice"
1112
"github.com/raystack/frontier/core/deleter"
@@ -139,6 +140,8 @@ func TestDeleteOrganization(t *testing.T) {
139140
// billing teardown
140141
m.subSvc.EXPECT().DeleteByCustomer(mock.Anything, c).Return(nil)
141142
m.invocSvc.EXPECT().DeleteByCustomer(mock.Anything, c).Return(nil)
143+
m.checkoutSvc.EXPECT().List(mock.Anything, checkout.Filter{CustomerID: "cust-1"}).
144+
Return([]checkout.Checkout{{ID: "chk-1", ProviderID: "cs_1", CustomerID: "cust-1"}}, nil)
142145
m.checkoutSvc.EXPECT().DeleteByCustomer(mock.Anything, "cust-1").Return(nil)
143146
m.creditSvc.EXPECT().DeleteByAccountID(mock.Anything, "cust-1").Return(nil)
144147
m.custSvc.EXPECT().Delete(mock.Anything, "cust-1").Return(nil)
@@ -282,6 +285,11 @@ func TestDeleteCustomers(t *testing.T) {
282285
Return([]customer.Customer{c}, nil)
283286
m.subSvc.EXPECT().DeleteByCustomer(mock.Anything, c).Return(nil)
284287
m.invocSvc.EXPECT().DeleteByCustomer(mock.Anything, c).Return(nil)
288+
m.checkoutSvc.EXPECT().List(mock.Anything, checkout.Filter{CustomerID: "cust-1"}).
289+
Return([]checkout.Checkout{
290+
{ID: "chk-1", ProviderID: "cs_1", CustomerID: "cust-1", PlanID: "plan-1", State: "complete", PaymentStatus: "paid"},
291+
{ID: "chk-2", ProviderID: "cs_2", CustomerID: "cust-1", State: "expired"},
292+
}, nil)
285293
m.checkoutSvc.EXPECT().DeleteByCustomer(mock.Anything, "cust-1").Return(nil)
286294
m.creditSvc.EXPECT().DeleteByAccountID(mock.Anything, "cust-1").Return(nil)
287295
m.custSvc.EXPECT().Delete(mock.Anything, "cust-1").Return(nil)
@@ -298,6 +306,8 @@ func TestDeleteCustomers(t *testing.T) {
298306
Return([]customer.Customer{c}, nil)
299307
m.subSvc.EXPECT().DeleteByCustomer(mock.Anything, c).Return(nil)
300308
m.invocSvc.EXPECT().DeleteByCustomer(mock.Anything, c).Return(nil)
309+
m.checkoutSvc.EXPECT().List(mock.Anything, checkout.Filter{CustomerID: "cust-no-provider"}).
310+
Return([]checkout.Checkout{}, nil)
301311
m.checkoutSvc.EXPECT().DeleteByCustomer(mock.Anything, "cust-no-provider").Return(nil)
302312
m.creditSvc.EXPECT().DeleteByAccountID(mock.Anything, "cust-no-provider").Return(nil)
303313
m.custSvc.EXPECT().Delete(mock.Anything, "cust-no-provider").Return(nil)
@@ -314,13 +324,31 @@ func TestDeleteCustomers(t *testing.T) {
314324
Return([]customer.Customer{c}, nil)
315325
m.subSvc.EXPECT().DeleteByCustomer(mock.Anything, c).Return(nil)
316326
m.invocSvc.EXPECT().DeleteByCustomer(mock.Anything, c).Return(nil)
327+
m.checkoutSvc.EXPECT().List(mock.Anything, checkout.Filter{CustomerID: "cust-1"}).
328+
Return([]checkout.Checkout{}, nil)
317329
m.checkoutSvc.EXPECT().DeleteByCustomer(mock.Anything, "cust-1").
318330
Return(errors.New("checkout delete failed"))
319331
// strict mocks: custSvc.Delete must not be called
320332

321333
err := m.build().DeleteCustomers(context.Background(), "org-1")
322334
assert.ErrorContains(t, err, "checkout delete failed")
323335
})
336+
337+
t.Run("checkout list failure stops the customer delete", func(t *testing.T) {
338+
m := newMocks(t)
339+
340+
c := customer.Customer{ID: "cust-1", ProviderID: "stripe-1"}
341+
m.custSvc.EXPECT().List(mock.Anything, customer.Filter{OrgID: "org-1"}).
342+
Return([]customer.Customer{c}, nil)
343+
m.subSvc.EXPECT().DeleteByCustomer(mock.Anything, c).Return(nil)
344+
m.invocSvc.EXPECT().DeleteByCustomer(mock.Anything, c).Return(nil)
345+
m.checkoutSvc.EXPECT().List(mock.Anything, checkout.Filter{CustomerID: "cust-1"}).
346+
Return(nil, errors.New("checkout list failed"))
347+
// strict mocks: checkoutSvc.DeleteByCustomer and custSvc.Delete must not be called
348+
349+
err := m.build().DeleteCustomers(context.Background(), "org-1")
350+
assert.ErrorContains(t, err, "checkout list failed")
351+
})
324352
}
325353

326354
func TestRemoveUsersFromOrg(t *testing.T) {

0 commit comments

Comments
 (0)