70 lines
2.0 KiB
Go
70 lines
2.0 KiB
Go
package events
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/danielgtaylor/huma/v2"
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/samcharles93/archie-core/modules/events/internal/store"
|
|
"github.com/samcharles93/archie-core/modules/governance"
|
|
)
|
|
|
|
// caller is the person a request acts for. A source owned by a person is
|
|
// visible only to that person.
|
|
func caller(ctx context.Context) string { return governance.CallerID(ctx) }
|
|
|
|
// labels maps the id of each source the caller may see to its label.
|
|
func (m *Module) labels(ctx context.Context) (map[uuid.UUID]string, error) {
|
|
sources, err := m.store.SourcesFor(ctx, m.orgID, caller(ctx))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make(map[uuid.UUID]string, len(sources))
|
|
for _, src := range sources {
|
|
out[src.ID] = src.Label
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// canUse reports whether the caller may use a source: it is the organisation's
|
|
// or the caller's own. A missing source can't be used.
|
|
func (m *Module) canUse(ctx context.Context, id uuid.UUID) (bool, error) {
|
|
src, err := m.reader.Source(ctx, m.orgID, id)
|
|
if errors.Is(err, store.ErrNotFound) {
|
|
return false, nil
|
|
}
|
|
if err != nil {
|
|
return false, fmt.Errorf("read source: %w", err)
|
|
}
|
|
return src.VisibleTo(caller(ctx)), nil
|
|
}
|
|
|
|
// visibleTrigger reads a trigger the caller may see. A trigger on someone
|
|
// else's person source reads as missing.
|
|
func (m *Module) visibleTrigger(ctx context.Context, rawID string) (store.Trigger, error) {
|
|
id, err := uuid.Parse(rawID)
|
|
if err != nil {
|
|
return store.Trigger{}, huma.Error400BadRequest("a trigger id is a UUID")
|
|
}
|
|
t, err := m.store.Trigger(ctx, m.orgID, id)
|
|
if errors.Is(err, store.ErrNotFound) {
|
|
return t, huma.Error404NotFound("no such trigger")
|
|
}
|
|
if err != nil {
|
|
return t, huma.Error500InternalServerError("couldn't read the trigger", err)
|
|
}
|
|
if t.SourceID.Valid {
|
|
ok, err := m.canUse(ctx, t.SourceID.UUID)
|
|
if err != nil {
|
|
return t, huma.Error500InternalServerError("couldn't read the trigger", err)
|
|
}
|
|
if !ok {
|
|
return t, huma.Error404NotFound("no such trigger")
|
|
}
|
|
}
|
|
return t, nil
|
|
}
|