|
| 1 | +package hooks |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "entgo.io/ent" |
| 7 | + "github.com/samber/lo" |
| 8 | + |
| 9 | + "github.com/theopenlane/core/internal/ent/eventqueue" |
| 10 | + "github.com/theopenlane/core/internal/ent/generated" |
| 11 | + "github.com/theopenlane/core/internal/workflows" |
| 12 | + "github.com/theopenlane/core/pkg/gala" |
| 13 | +) |
| 14 | + |
| 15 | +// RegisterGalaDocumentAssociationListeners registers listeners that link |
| 16 | +// referenced controls to documents asynchronously after document creation. |
| 17 | +func RegisterGalaDocumentAssociationListeners(registry *gala.Registry) ([]gala.ListenerID, error) { |
| 18 | + return gala.RegisterListeners(registry, |
| 19 | + documentAssociationDefinition(generated.TypeActionPlan), |
| 20 | + documentAssociationDefinition(generated.TypeInternalPolicy), |
| 21 | + documentAssociationDefinition(generated.TypeProcedure), |
| 22 | + ) |
| 23 | +} |
| 24 | + |
| 25 | +func documentAssociationDefinition(schemaType string) gala.Definition[eventqueue.MutationGalaPayload] { |
| 26 | + topic := eventqueue.MutationTopic(eventqueue.MutationConcernDirect, schemaType) |
| 27 | + |
| 28 | + return gala.Definition[eventqueue.MutationGalaPayload]{ |
| 29 | + Topic: topic, |
| 30 | + Name: "document.associations." + schemaType, |
| 31 | + Operations: []string{ent.OpCreate.String()}, |
| 32 | + Handle: handleDocumentAssociationCreated, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func handleDocumentAssociationCreated(ctx gala.HandlerContext, payload eventqueue.MutationGalaPayload) error { |
| 37 | + ctx, client, ok := eventqueue.ClientFromHandler(ctx) |
| 38 | + if !ok { |
| 39 | + return nil |
| 40 | + } |
| 41 | + |
| 42 | + documentID, ok := eventqueue.MutationEntityID(payload, ctx.Envelope.Headers.Properties) |
| 43 | + if !ok || documentID == "" { |
| 44 | + return nil |
| 45 | + } |
| 46 | + |
| 47 | + switch payload.MutationType { |
| 48 | + case generated.TypeActionPlan: |
| 49 | + return parseActionPlanAssociations(ctx.Context, client, documentID) |
| 50 | + case generated.TypeInternalPolicy: |
| 51 | + return parseInternalPolicyAssociations(ctx.Context, client, documentID) |
| 52 | + case generated.TypeProcedure: |
| 53 | + return parseProcedureAssociations(ctx.Context, client, documentID) |
| 54 | + default: |
| 55 | + return nil |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func parseActionPlanAssociations(ctx context.Context, client *generated.Client, documentID string) error { |
| 60 | + doc, err := client.ActionPlan.Get(ctx, documentID) |
| 61 | + if err != nil { |
| 62 | + if generated.IsNotFound(err) { |
| 63 | + return nil |
| 64 | + } |
| 65 | + |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + links := getDocumentAssociationsForDetails(ctx, client, doc.Details) |
| 70 | + if links == nil || len(links.controlIDs) == 0 { |
| 71 | + return nil |
| 72 | + } |
| 73 | + |
| 74 | + return client.ActionPlan.UpdateOneID(doc.ID). |
| 75 | + SetRevision(doc.Revision). |
| 76 | + AddControlIDs(lo.Uniq(links.controlIDs)...). |
| 77 | + Exec(workflows.AllowContext(ctx)) |
| 78 | +} |
| 79 | + |
| 80 | +func parseInternalPolicyAssociations(ctx context.Context, client *generated.Client, documentID string) error { |
| 81 | + doc, err := client.InternalPolicy.Get(ctx, documentID) |
| 82 | + if err != nil { |
| 83 | + if generated.IsNotFound(err) { |
| 84 | + return nil |
| 85 | + } |
| 86 | + |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + links := getDocumentAssociationsForDetails(ctx, client, doc.Details) |
| 91 | + if links == nil || !links.hasAssociations() { |
| 92 | + return nil |
| 93 | + } |
| 94 | + |
| 95 | + update := client.InternalPolicy.UpdateOneID(doc.ID).SetRevision(doc.Revision) |
| 96 | + if len(links.controlIDs) > 0 { |
| 97 | + update.AddControlIDs(lo.Uniq(links.controlIDs)...) |
| 98 | + } |
| 99 | + |
| 100 | + if len(links.subcontrolIDs) > 0 { |
| 101 | + update.AddSubcontrolIDs(lo.Uniq(links.subcontrolIDs)...) |
| 102 | + } |
| 103 | + |
| 104 | + return update.Exec(workflows.AllowContext(ctx)) |
| 105 | +} |
| 106 | + |
| 107 | +func parseProcedureAssociations(ctx context.Context, client *generated.Client, documentID string) error { |
| 108 | + doc, err := client.Procedure.Get(ctx, documentID) |
| 109 | + if err != nil { |
| 110 | + if generated.IsNotFound(err) { |
| 111 | + return nil |
| 112 | + } |
| 113 | + |
| 114 | + return err |
| 115 | + } |
| 116 | + |
| 117 | + links := getDocumentAssociationsForDetails(ctx, client, doc.Details) |
| 118 | + if links == nil || !links.hasAssociations() { |
| 119 | + return nil |
| 120 | + } |
| 121 | + |
| 122 | + update := client.Procedure.UpdateOneID(doc.ID).SetRevision(doc.Revision) |
| 123 | + if len(links.controlIDs) > 0 { |
| 124 | + update.AddControlIDs(lo.Uniq(links.controlIDs)...) |
| 125 | + } |
| 126 | + |
| 127 | + if len(links.subcontrolIDs) > 0 { |
| 128 | + update.AddSubcontrolIDs(lo.Uniq(links.subcontrolIDs)...) |
| 129 | + } |
| 130 | + |
| 131 | + return update.Exec(workflows.AllowContext(ctx)) |
| 132 | +} |
| 133 | + |
| 134 | +func getDocumentAssociationsForDetails(ctx context.Context, client *generated.Client, details string) *edgeLinks { |
| 135 | + if details == "" { |
| 136 | + return nil |
| 137 | + } |
| 138 | + |
| 139 | + orgControls := getOrganizationControlsFromClient(ctx, client) |
| 140 | + if orgControls == nil { |
| 141 | + return nil |
| 142 | + } |
| 143 | + |
| 144 | + return findControlMatches(details, orgControls) |
| 145 | +} |
| 146 | + |
| 147 | +func (e *edgeLinks) hasAssociations() bool { |
| 148 | + return e != nil && (len(e.controlIDs) > 0 || len(e.subcontrolIDs) > 0) |
| 149 | +} |
0 commit comments