Skip to content
quarry Artifact [736d27e0bf]
Login

Artifact [736d27e0bf]

Artifact 736d27e0bf1b4027df5e5a1abf697d2fda7e8791a98da7940fb39e6c52f06ea6:


package api

import (
	"context"
	"fmt"
	"slices"

	"craftdesign.group/quarry/internal/ops"
)

type opsAlertTestIn struct {
	// Name is the site admin to send to; empty is the caller.
	Name string `json:"name" validate:"max=64"`
}

type opsAlertTestOut struct {
	To string `json:"to"`
}

// registerOpsAlertTest lets a site admin prove that an operational alert
// reaches a person: inbox, push and mail, through the same workflow and
// the same code a real alert takes. Until a real condition fires there
// is otherwise no way to know delivery works.
func registerOpsAlertTest(r *ops.Registry, d Deps) error {
	return ops.Register(r, ops.Op[opsAlertTestIn, opsAlertTestOut]{
		Name:  "ops.alert.test",
		Caps:  "s",
		Scope: ops.ScopeFleet,
		Doc: "Send one operational alert marked as a test to one site admin, through the path a real alert takes " +
			"(inbox, push and mail), to prove delivery. `name` is who receives it and defaults to the caller; " +
			"it must be a site admin who is a person, since alerts never go to agents. It changes nothing a real alert reads: " +
			"a test does not count as having been told of anything.",
		Kind:  ops.Command,
		Auth:  ops.Admin,
		Route: "POST /api/v1/ops/alert-test",
		Run: func(ctx context.Context, in opsAlertTestIn) (opsAlertTestOut, error) {
			if d.OpsAlert == nil {
				return opsAlertTestOut{}, fmt.Errorf("this forge sends no operational alerts: %w", ops.ErrUnavailable)
			}
			who := in.Name
			if who == "" {
				who = ops.User(ctx)
			}
			if !slices.Contains(d.OpsAlert.people(), who) {
				return opsAlertTestOut{}, fmt.Errorf("%s is not a site admin who is a person, and alerts go to nobody else: %w", who, ops.ErrInvalid)
			}
			d.OpsAlert.test(who)
			return opsAlertTestOut{To: who}, nil
		},
	})
}