Skip to content

Commit 96bca2a

Browse files
cleanup
Signed-off-by: Sarah Funkhouser <147884153+golanglemonade@users.noreply.github.com>
1 parent 65a10a4 commit 96bca2a

3 files changed

Lines changed: 13 additions & 19 deletions

File tree

internal/ent/hooks/listeners_entitlements.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ func handleOrganizationSettingMutationGala(ctx gala.HandlerContext, payload even
7171
}
7272
}
7373

74-
// handleOrganizationSubscriptionDeactivationGala deactivates an organization's customer subscription when deleted.
75-
// The cascade delete of everything the organization owns is handled by its own listener, it must not
76-
// be gated on entitlements being enabled
74+
// handleOrganizationSubscriptionDeactivationGala deactivates an organization's customer subscription when deleted
7775
func handleOrganizationSubscriptionDeactivationGala(ctx gala.HandlerContext, payload eventqueue.MutationGalaPayload) error {
7876
inv, ok := newEntitlementInvocation(ctx, payload, softDeleteAllowContext)
7977
if !ok {
@@ -103,7 +101,7 @@ func handleOrganizationSubscriptionDeactivationGala(ctx gala.HandlerContext, pay
103101
return nil
104102
}
105103

106-
// handleOrganizationCreatedGala reconciles entitlements after organization creation.
104+
// handleOrganizationCreatedGala reconciles entitlements after organization creation
107105
func handleOrganizationCreatedGala(ctx gala.HandlerContext, payload eventqueue.MutationGalaPayload) error {
108106
inv, ok := newEntitlementInvocation(ctx, payload, orgAllowContext)
109107
if !ok {
@@ -113,7 +111,7 @@ func handleOrganizationCreatedGala(ctx gala.HandlerContext, payload eventqueue.M
113111
return inv.reconcile()
114112
}
115113

116-
// handleOrganizationSettingsUpdateOneGala updates Stripe customer details for billing changes.
114+
// handleOrganizationSettingsUpdateOneGala updates Stripe customer details for billing changes
117115
func handleOrganizationSettingsUpdateOneGala(ctx gala.HandlerContext, payload eventqueue.MutationGalaPayload) error {
118116
if !lo.SomeBy([]string{"billing_email", "billing_phone", "billing_address"}, func(field string) bool {
119117
return eventqueue.MutationFieldChanged(payload, field)

internal/ent/hooks/listeners_organization_cleanup.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ import (
1616
"github.com/theopenlane/core/pkg/logx"
1717
)
1818

19-
// RegisterGalaOrganizationCleanupListeners registers the organization cascade delete on Gala.
20-
// This is deliberately independent of the entitlement listeners, the cascade has to run whether or
21-
// not billing is configured, otherwise the records an organization owns are left behind entirely
19+
// RegisterGalaOrganizationCleanupListeners registers the organization cascade delete on Gala
2220
func RegisterGalaOrganizationCleanupListeners(registry *gala.Registry) ([]gala.ListenerID, error) {
2321
return gala.RegisterListeners(registry,
2422
gala.Definition[eventqueue.MutationGalaPayload]{
@@ -35,8 +33,7 @@ func RegisterGalaOrganizationCleanupListeners(registry *gala.Registry) ([]gala.L
3533
}
3634

3735
// handleOrganizationCascadeDelete removes everything an organization owns once it is deleted.
38-
// The records are hard deleted and their history rows purged, leaving them soft deleted would keep
39-
// the rows, the uploaded objects and the full field values in the history tables indefinitely
36+
// The records are hard deleted and their history rows purged along with files stored in object storage
4037
func handleOrganizationCascadeDelete(ctx gala.HandlerContext, payload eventqueue.MutationGalaPayload) error {
4138
handlerCtx, client, ok := eventqueue.ClientFromHandler(ctx)
4239
if !ok {
@@ -50,30 +47,34 @@ func handleOrganizationCascadeDelete(ctx gala.HandlerContext, payload eventqueue
5047

5148
cleanupCtx := entgen.NewContext(organizationCleanupContext(handlerCtx.Context), client)
5249

50+
cleanupCtx = logx.WithFields(cleanupCtx, logx.LogFields{
51+
"organization_id": orgID,
52+
})
53+
5354
if err := entgen.OrganizationEdgeCleanup(cleanupCtx, orgID); err != nil {
54-
logx.FromContext(cleanupCtx).Error().Err(err).Str("organization_id", orgID).
55+
logx.FromContext(cleanupCtx).Error().Err(err).
5556
Msg("failed to cascade delete organization edges")
5657

5758
return err
5859
}
5960

6061
// this has to run before the organization row is removed
6162
if err := entgen.PurgeOrganizationHistory(cleanupCtx, organization.ID(orgID)); err != nil {
62-
logx.FromContext(cleanupCtx).Error().Err(err).Str("organization_id", orgID).
63+
logx.FromContext(cleanupCtx).Error().Err(err).
6364
Msg("failed to purge organization history")
6465

6566
return err
6667
}
6768

6869
// the organization row goes last, once everything it owned is gone
6970
if _, err := client.Organization.Delete().Where(organization.ID(orgID)).Exec(cleanupCtx); err != nil {
70-
logx.FromContext(cleanupCtx).Error().Err(err).Str("organization_id", orgID).
71+
logx.FromContext(cleanupCtx).Error().Err(err).
7172
Msg("failed to delete organization")
7273

7374
return err
7475
}
7576

76-
logx.FromContext(cleanupCtx).Info().Str("organization_id", orgID).Msg("organization cascade delete completed")
77+
logx.FromContext(cleanupCtx).Info().Msg("organization cascade delete completed")
7778

7879
return nil
7980
}
@@ -84,8 +85,6 @@ func organizationCleanupContext(ctx context.Context) context.Context {
8485
allowCtx := privacy.DecisionContext(ctx, privacy.Allow)
8586
allowCtx = auth.WithCaller(allowCtx, auth.NewWebhookCaller(""))
8687

87-
// without this the cascaded deletes are rewritten into soft deletes by the mixin, which also
88-
// means the file hook never fires and the uploaded objects are orphaned in object storage
8988
allowCtx = entx.SkipSoftDelete(allowCtx)
9089

9190
return contextx.WithPurgeHistory(allowCtx)

internal/graphapi/organization_cascade_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ import (
1616
"github.com/theopenlane/core/internal/ent/historygenerated/taskhistory"
1717
)
1818

19-
// TestMutationDeleteOrganizationCascade covers what happens to the records an organization owns once
20-
// it is deleted, they must be hard deleted, their history rows purged and the objects backing any
21-
// files removed from object storage rather than left orphaned
2219
func TestMutationDeleteOrganizationCascade(t *testing.T) {
2320
orgUser := suite.seedFreshMinimalOrgUsers(t, false)
2421

0 commit comments

Comments
 (0)