Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/api/action_dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (h *ActionHandler) DispatchAction(ctx context.Context, req *connect.Request
// post-commit search listener, which reloads it fresh. We keep the
// load here to fail fast with NotFound before touching anything
// heavier downstream.
if _, err := h.store.Queries().GetDeviceByID(ctx, db.GetDeviceByIDParams{ID: req.Msg.DeviceId}); err != nil {
if _, err := h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: req.Msg.DeviceId}); err != nil {
return nil, handleGetError(ctx, err, ErrDeviceNotFound, "device not found")
}

Expand Down Expand Up @@ -707,7 +707,7 @@ func (h *ActionHandler) DispatchInstantAction(ctx context.Context, req *connect.
return nil, apiErrorCtx(ctx, ErrValidationFailed, connect.CodeInvalidArgument, "invalid instant action type: "+req.Msg.InstantAction.String())
}

_, err = h.store.Queries().GetDeviceByID(ctx, db.GetDeviceByIDParams{ID: req.Msg.DeviceId})
_, err = h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: req.Msg.DeviceId})
if err != nil {
return nil, handleGetError(ctx, err, ErrDeviceNotFound, "device not found")
}
Expand Down
2 changes: 1 addition & 1 deletion internal/api/assignment_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (h *AssignmentHandler) CreateAssignment(ctx context.Context, req *connect.R
// Validate target exists
switch req.Msg.TargetType {
case pm.AssignmentTargetType_ASSIGNMENT_TARGET_TYPE_DEVICE:
_, err := h.store.Queries().GetDeviceByID(ctx, db.GetDeviceByIDParams{ID: req.Msg.TargetId})
_, err := h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: req.Msg.TargetId})
if err != nil {
if store.IsNotFound(err) {
return nil, apiErrorCtx(ctx, ErrDeviceNotFound, connect.CodeNotFound, "device not found")
Expand Down
5 changes: 1 addition & 4 deletions internal/api/certificate_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/manchtools/power-manage/server/internal/eventtypes"
"github.com/manchtools/power-manage/server/internal/eventtypes/payloads"
"github.com/manchtools/power-manage/server/internal/store"
db "github.com/manchtools/power-manage/server/internal/store/generated"
)

// CertificateHandler handles certificate renewal RPCs.
Expand Down Expand Up @@ -47,9 +46,7 @@ func (h *CertificateHandler) RenewCertificate(ctx context.Context, req *connect.
}

// Verify the device exists and is not deleted
device, err := h.store.Queries().GetDeviceByID(ctx, db.GetDeviceByIDParams{
ID: deviceID,
})
device, err := h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: deviceID})
if err != nil {
if store.IsNotFound(err) {
return nil, apiErrorCtx(ctx, ErrDeviceNotFound, connect.CodeNotFound, "device not found")
Expand Down
70 changes: 32 additions & 38 deletions internal/api/device_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,21 @@ func (h *DeviceHandler) ListDevices(ctx context.Context, req *connect.Request[pm
}
}

var devices []db.DevicesProjection
var devices []store.Device

deviceRepo := h.store.Repos().Device
deviceFilter := store.ListDevicesFilter{
Limit: pageSize,
Offset: offset,
OwnerScope: filterUID,
}
switch req.Msg.StatusFilter {
case pm.DeviceStatus_DEVICE_STATUS_ONLINE:
devices, err = q.ListDevicesOnline(ctx, db.ListDevicesOnlineParams{
Limit: pageSize,
Offset: offset,
FilterUserID: filterUID,
})
devices, err = deviceRepo.ListOnline(ctx, deviceFilter)
case pm.DeviceStatus_DEVICE_STATUS_OFFLINE:
devices, err = q.ListDevicesOffline(ctx, db.ListDevicesOfflineParams{
Limit: pageSize,
Offset: offset,
FilterUserID: filterUID,
})
devices, err = deviceRepo.ListOffline(ctx, deviceFilter)
default:
devices, err = q.ListDevices(ctx, db.ListDevicesParams{
Limit: pageSize,
Offset: offset,
FilterUserID: filterUID,
})
devices, err = deviceRepo.List(ctx, deviceFilter)
}
if err != nil {
return nil, apiErrorCtx(ctx, ErrInternal, connect.CodeInternal, "failed to list devices")
Expand All @@ -89,11 +83,11 @@ func (h *DeviceHandler) ListDevices(ctx context.Context, req *connect.Request[pm
var count int64
switch req.Msg.StatusFilter {
case pm.DeviceStatus_DEVICE_STATUS_ONLINE:
count, err = q.CountDevicesOnline(ctx, filterUID)
count, err = deviceRepo.CountOnline(ctx, filterUID)
case pm.DeviceStatus_DEVICE_STATUS_OFFLINE:
count, err = q.CountDevicesOffline(ctx, filterUID)
count, err = deviceRepo.CountOffline(ctx, filterUID)
default:
count, err = q.CountDevices(ctx, filterUID)
count, err = deviceRepo.Count(ctx, filterUID)
}
if err != nil {
return nil, apiErrorCtx(ctx, ErrInternal, connect.CodeInternal, "failed to count devices")
Expand Down Expand Up @@ -141,9 +135,9 @@ func (h *DeviceHandler) GetDevice(ctx context.Context, req *connect.Request[pm.G
return nil, err
}

device, err := h.store.Queries().GetDeviceByID(ctx, db.GetDeviceByIDParams{
ID: req.Msg.Id,
FilterUserID: userFilterID(ctx, "GetDevice"),
device, err := h.store.Repos().Device.Get(ctx, store.GetDeviceKey{
ID: req.Msg.Id,
OwnerScope: userFilterID(ctx, "GetDevice"),
})
if err != nil {
return nil, handleGetError(ctx, err, ErrDeviceNotFound, "device not found")
Expand All @@ -166,7 +160,7 @@ func (h *DeviceHandler) SetDeviceLabel(ctx context.Context, req *connect.Request
}

// Verify device exists
_, err = h.store.Queries().GetDeviceByID(ctx, db.GetDeviceByIDParams{ID: req.Msg.Id})
_, err = h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: req.Msg.Id})
if err != nil {
return nil, handleGetError(ctx, err, ErrDeviceNotFound, "device not found")
}
Expand All @@ -187,7 +181,7 @@ func (h *DeviceHandler) SetDeviceLabel(ctx context.Context, req *connect.Request
}

// Read back updated device
device, err := h.store.Queries().GetDeviceByID(ctx, db.GetDeviceByIDParams{ID: req.Msg.Id})
device, err := h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: req.Msg.Id})
if err != nil {
return nil, apiErrorCtx(ctx, ErrInternal, connect.CodeInternal, "failed to get updated device")
}
Expand All @@ -209,7 +203,7 @@ func (h *DeviceHandler) RemoveDeviceLabel(ctx context.Context, req *connect.Requ
}

// Verify device exists
_, err = h.store.Queries().GetDeviceByID(ctx, db.GetDeviceByIDParams{ID: req.Msg.Id})
_, err = h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: req.Msg.Id})
if err != nil {
return nil, handleGetError(ctx, err, ErrDeviceNotFound, "device not found")
}
Expand All @@ -229,7 +223,7 @@ func (h *DeviceHandler) RemoveDeviceLabel(ctx context.Context, req *connect.Requ
}

// Read back updated device
device, err := h.store.Queries().GetDeviceByID(ctx, db.GetDeviceByIDParams{ID: req.Msg.Id})
device, err := h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: req.Msg.Id})
if err != nil {
return nil, apiErrorCtx(ctx, ErrInternal, connect.CodeInternal, "failed to get updated device")
}
Expand Down Expand Up @@ -298,7 +292,7 @@ func (h *DeviceHandler) AssignDevice(ctx context.Context, req *connect.Request[p
q := h.store.Queries()

// Verify device exists
_, err = q.GetDeviceByID(ctx, db.GetDeviceByIDParams{ID: req.Msg.DeviceId})
_, err = h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: req.Msg.DeviceId})
if err != nil {
return nil, handleGetError(ctx, err, ErrDeviceNotFound, "device not found")
}
Expand Down Expand Up @@ -374,7 +368,7 @@ func (h *DeviceHandler) AssignDevice(ctx context.Context, req *connect.Request[p
}

// Read back updated device
device, err := q.GetDeviceByID(ctx, db.GetDeviceByIDParams{ID: req.Msg.DeviceId})
device, err := h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: req.Msg.DeviceId})
if err != nil {
return nil, apiErrorCtx(ctx, ErrInternal, connect.CodeInternal, "failed to get updated device")
}
Expand Down Expand Up @@ -425,7 +419,7 @@ func (h *DeviceHandler) UnassignDevice(ctx context.Context, req *connect.Request
}

// Verify device exists
_, err = h.store.Queries().GetDeviceByID(ctx, db.GetDeviceByIDParams{ID: req.Msg.DeviceId})
_, err = h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: req.Msg.DeviceId})
if err != nil {
return nil, handleGetError(ctx, err, ErrDeviceNotFound, "device not found")
}
Expand Down Expand Up @@ -461,7 +455,7 @@ func (h *DeviceHandler) UnassignDevice(ctx context.Context, req *connect.Request
}

// Read back updated device
device, err := h.store.Queries().GetDeviceByID(ctx, db.GetDeviceByIDParams{ID: req.Msg.DeviceId})
device, err := h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: req.Msg.DeviceId})
if err != nil {
return nil, apiErrorCtx(ctx, ErrInternal, connect.CodeInternal, "failed to get updated device")
}
Expand All @@ -483,7 +477,7 @@ func (h *DeviceHandler) SetDeviceSyncInterval(ctx context.Context, req *connect.
}

// Verify device exists
_, err = h.store.Queries().GetDeviceByID(ctx, db.GetDeviceByIDParams{ID: req.Msg.Id})
_, err = h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: req.Msg.Id})
if err != nil {
return nil, handleGetError(ctx, err, ErrDeviceNotFound, "device not found")
}
Expand All @@ -504,7 +498,7 @@ func (h *DeviceHandler) SetDeviceSyncInterval(ctx context.Context, req *connect.
}

// Read back updated device
device, err := h.store.Queries().GetDeviceByID(ctx, db.GetDeviceByIDParams{ID: req.Msg.Id})
device, err := h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: req.Msg.Id})
if err != nil {
return nil, apiErrorCtx(ctx, ErrInternal, connect.CodeInternal, "failed to get updated device")
}
Expand All @@ -516,7 +510,7 @@ func (h *DeviceHandler) SetDeviceSyncInterval(ctx context.Context, req *connect.

// deviceToProtoCtx converts a database device projection to a protobuf device,
// populating assigned user/group IDs from junction tables.
func (h *DeviceHandler) deviceToProtoCtx(ctx context.Context, d db.DevicesProjection) *pm.Device {
func (h *DeviceHandler) deviceToProtoCtx(ctx context.Context, d store.Device) *pm.Device {
q := h.store.Queries()
var userIDs, groupIDs []string
if rows, err := q.ListDeviceAssignedUserIDs(ctx, d.ID); err == nil {
Expand All @@ -530,7 +524,7 @@ func (h *DeviceHandler) deviceToProtoCtx(ctx context.Context, d db.DevicesProjec

// deviceToProtoWithAssignments converts a database device projection to a protobuf
// device using pre-fetched assignment data. Used by list endpoints with batch queries.
func (h *DeviceHandler) deviceToProtoWithAssignments(d db.DevicesProjection, assignedUserIDs, assignedGroupIDs []string) *pm.Device {
func (h *DeviceHandler) deviceToProtoWithAssignments(d store.Device, assignedUserIDs, assignedGroupIDs []string) *pm.Device {
device := &pm.Device{
Id: d.ID,
Hostname: d.Hostname,
Expand Down Expand Up @@ -626,7 +620,7 @@ func (h *DeviceHandler) loadDeviceHostnamesByIDs(ctx context.Context, ids []stri
if len(ids) == 0 {
return nil
}
rows, err := h.store.Queries().GetDeviceHostnamesByIDs(ctx, ids)
rows, err := h.store.Repos().Device.HostnamesByIDs(ctx, ids)
if err != nil {
logEnrichmentErr("GetDeviceHostnamesByIDs", "device_id_count", fmt.Sprint(len(ids)), err)
return nil
Expand Down Expand Up @@ -819,7 +813,7 @@ func (h *DeviceHandler) CreateLuksToken(ctx context.Context, req *connect.Reques
}

// Verify device exists
_, err = h.store.Queries().GetDeviceByID(ctx, db.GetDeviceByIDParams{ID: req.Msg.DeviceId})
_, err = h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: req.Msg.DeviceId})
if err != nil {
return nil, handleGetError(ctx, err, ErrDeviceNotFound, "device not found")
}
Expand Down Expand Up @@ -907,7 +901,7 @@ func (h *DeviceHandler) RevokeLuksDeviceKey(ctx context.Context, req *connect.Re
}

// Verify device exists
_, err := h.store.Queries().GetDeviceByID(ctx, db.GetDeviceByIDParams{ID: req.Msg.DeviceId})
_, err := h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: req.Msg.DeviceId})
if err != nil {
return nil, handleGetError(ctx, err, ErrDeviceNotFound, "device not found")
}
Expand Down Expand Up @@ -1023,7 +1017,7 @@ func (h *DeviceHandler) ListDeviceAssignees(ctx context.Context, req *connect.Re
q := h.store.Queries()

// Verify device exists
_, err := q.GetDeviceByID(ctx, db.GetDeviceByIDParams{ID: req.Msg.DeviceId})
_, err := h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: req.Msg.DeviceId})
if err != nil {
return nil, handleGetError(ctx, err, ErrDeviceNotFound, "device not found")
}
Expand Down
6 changes: 3 additions & 3 deletions internal/api/internal_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (h *InternalHandler) VerifyDevice(ctx context.Context, req *connect.Request
return nil, apiErrorCtx(ctx, ErrValidationFailed, connect.CodeInvalidArgument, "device_id is required")
}

_, err := h.store.Queries().GetDeviceByID(ctx, db.GetDeviceByIDParams{ID: deviceID})
_, err := h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: deviceID})
if err != nil {
h.logger.Warn("device verification failed", "device_id", deviceID, "error", err)
return nil, apiErrorCtx(ctx, ErrDeviceNotFound, connect.CodeNotFound, "device not found or deleted")
Expand All @@ -82,7 +82,7 @@ func (h *InternalHandler) ProxySyncActions(ctx context.Context, req *connect.Req
}

// Verify the device exists and is not deleted.
if _, err := h.store.Queries().GetDeviceByID(ctx, db.GetDeviceByIDParams{ID: deviceID}); err != nil {
if _, err := h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: deviceID}); err != nil {
h.logger.Warn("sync actions for unknown/deleted device", "device_id", deviceID)
return nil, apiErrorCtx(ctx, ErrDeviceNotFound, connect.CodeNotFound, "device not found or deleted")
}
Expand All @@ -109,7 +109,7 @@ func (h *InternalHandler) ProxySyncActions(ctx context.Context, req *connect.Req
return nil, apiErrorCtx(ctx, ErrInternal, connect.CodeInternal, "failed to resolve actions")
}

syncInterval, err := h.store.Queries().GetDeviceSyncInterval(ctx, deviceID)
syncInterval, err := h.store.Repos().Device.SyncInterval(ctx, deviceID)
if err != nil {
h.logger.Warn("failed to get sync interval, using default", "device_id", deviceID, "error", err)
syncInterval = 0
Expand Down
5 changes: 1 addition & 4 deletions internal/api/logs_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

pm "github.com/manchtools/power-manage/sdk/gen/go/pm/v1"
"github.com/manchtools/power-manage/server/internal/store"
"github.com/manchtools/power-manage/server/internal/store/generated"
"github.com/manchtools/power-manage/server/internal/taskqueue"

"github.com/hibiken/asynq"
Expand All @@ -36,9 +35,7 @@ func (h *LogsHandler) QueryDeviceLogs(ctx context.Context, req *connect.Request[
msg := req.Msg

// Verify device exists
_, err := h.store.Queries().GetDeviceByID(ctx, generated.GetDeviceByIDParams{
ID: msg.DeviceId,
})
_, err := h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: msg.DeviceId})
if err != nil {
return nil, apiErrorCtx(ctx, ErrDeviceNotFound, connect.CodeNotFound, "device not found")
}
Comment on lines +38 to 41

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Preserve internal error semantics for device lookup.

This maps all repo errors to CodeNotFound. Internal DB/repo failures should return CodeInternal, otherwise outages are misreported as missing devices.

Suggested fix
-	_, err := h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: msg.DeviceId})
-	if err != nil {
-		return nil, apiErrorCtx(ctx, ErrDeviceNotFound, connect.CodeNotFound, "device not found")
-	}
+	_, err := h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: msg.DeviceId})
+	if err != nil {
+		if store.IsNotFound(err) {
+			return nil, apiErrorCtx(ctx, ErrDeviceNotFound, connect.CodeNotFound, "device not found")
+		}
+		return nil, apiErrorCtx(ctx, ErrInternal, connect.CodeInternal, "failed to look up device")
+	}

As per coding guidelines "Errors must use apiErrorCtx with proper Connect error codes. Never silently ignore errors."

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
_, err := h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: msg.DeviceId})
if err != nil {
return nil, apiErrorCtx(ctx, ErrDeviceNotFound, connect.CodeNotFound, "device not found")
}
_, err := h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: msg.DeviceId})
if err != nil {
if store.IsNotFound(err) {
return nil, apiErrorCtx(ctx, ErrDeviceNotFound, connect.CodeNotFound, "device not found")
}
return nil, apiErrorCtx(ctx, ErrInternal, connect.CodeInternal, "failed to look up device")
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/api/logs_handler.go` around lines 38 - 41, The current device lookup
in h.store.Repos().Device.Get maps any error to ErrDeviceNotFound/CodeNotFound;
change it to distinguish not-found vs internal errors by using errors.Is (or
equivalent) against the repository's sentinel not-found error (e.g.,
store.ErrNotFound) after calling h.store.Repos().Device.Get, returning
apiErrorCtx(ctx, ErrDeviceNotFound, connect.CodeNotFound, "device not found")
only for not-found and returning apiErrorCtx(ctx, err, connect.CodeInternal,
"internal error retrieving device") for all other errors so internal DB/repo
failures yield connect.CodeInternal and preserve the original error.

Expand Down
9 changes: 2 additions & 7 deletions internal/api/osquery_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

pm "github.com/manchtools/power-manage/sdk/gen/go/pm/v1"
"github.com/manchtools/power-manage/server/internal/store"
"github.com/manchtools/power-manage/server/internal/store/generated"
"github.com/manchtools/power-manage/server/internal/taskqueue"

"google.golang.org/protobuf/types/known/timestamppb"
Expand Down Expand Up @@ -48,9 +47,7 @@ func (h *OSQueryHandler) DispatchOSQuery(ctx context.Context, req *connect.Reque
}

// Verify device exists
_, err := h.store.Queries().GetDeviceByID(ctx, generated.GetDeviceByIDParams{
ID: msg.DeviceId,
})
_, err := h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: msg.DeviceId})
if err != nil {
return nil, apiErrorCtx(ctx, ErrDeviceNotFound, connect.CodeNotFound, "device not found")
}
Comment on lines +50 to 53

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Differentiate not-found from repo/internal failures in both existence checks.

Both checks currently return CodeNotFound for any repository error. This hides backend failures and returns incorrect semantics to clients.

Suggested fix (apply in both methods)
-	_, err := h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: msg.DeviceId})
-	if err != nil {
-		return nil, apiErrorCtx(ctx, ErrDeviceNotFound, connect.CodeNotFound, "device not found")
-	}
+	_, err := h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: msg.DeviceId})
+	if err != nil {
+		if store.IsNotFound(err) {
+			return nil, apiErrorCtx(ctx, ErrDeviceNotFound, connect.CodeNotFound, "device not found")
+		}
+		return nil, apiErrorCtx(ctx, ErrInternal, connect.CodeInternal, "failed to look up device")
+	}

As per coding guidelines "Errors must use apiErrorCtx with proper Connect error codes. Never silently ignore errors."

Also applies to: 209-212

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/api/osquery_handler.go` around lines 51 - 54, The repo Get call's
error is always mapped to CodeNotFound; change both existence checks (the call
to h.store.Repos().Device.Get and the similar check later) to distinguish a
repository "not found" sentinel from other errors: use errors.Is(err,
store.ErrNotFound) (or the repo package's equivalent) to return apiErrorCtx(ctx,
ErrDeviceNotFound, connect.CodeNotFound, "device not found"), and for any other
non-nil err return apiErrorCtx(ctx, err, connect.CodeInternal, "failed to fetch
device") (i.e., map repository/internal failures to connect.CodeInternal rather
than CodeNotFound).

Expand Down Expand Up @@ -195,9 +192,7 @@ func (h *OSQueryHandler) RefreshDeviceInventory(ctx context.Context, req *connec
msg := req.Msg

// Verify device exists
_, err := h.store.Queries().GetDeviceByID(ctx, generated.GetDeviceByIDParams{
ID: msg.DeviceId,
})
_, err := h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: msg.DeviceId})
if err != nil {
return nil, apiErrorCtx(ctx, ErrDeviceNotFound, connect.CodeNotFound, "device not found")
}
Expand Down
6 changes: 1 addition & 5 deletions internal/api/terminal_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/manchtools/power-manage/server/internal/eventtypes/payloads"
"github.com/manchtools/power-manage/server/internal/gateway/registry"
"github.com/manchtools/power-manage/server/internal/store"
"github.com/manchtools/power-manage/server/internal/store/generated"
"github.com/manchtools/power-manage/server/internal/terminal"
)

Expand Down Expand Up @@ -112,10 +111,7 @@ func (h *TerminalHandler) StartTerminal(ctx context.Context, req *connect.Reques
// to userCtx.ID, which masked admin access to bulk-enrolled
// (unassigned) devices.
filterUserID := userFilterID(ctx, "StartTerminal")
if _, err := h.store.Queries().GetDeviceByID(ctx, generated.GetDeviceByIDParams{
ID: req.Msg.DeviceId,
FilterUserID: filterUserID,
}); err != nil {
if _, err := h.store.Repos().Device.Get(ctx, store.GetDeviceKey{ID: req.Msg.DeviceId, OwnerScope: filterUserID}); err != nil {
if store.IsNotFound(err) {
return nil, apiErrorCtx(ctx, ErrDeviceNotFound, connect.CodeNotFound, "device not found")
}
Expand Down
13 changes: 6 additions & 7 deletions internal/api/user_selection_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/manchtools/power-manage/server/internal/eventtypes"
"github.com/manchtools/power-manage/server/internal/eventtypes/payloads"
"github.com/manchtools/power-manage/server/internal/store"
db "github.com/manchtools/power-manage/server/internal/store/generated"
)

// UserSelectionHandler handles user selection RPCs.
Expand Down Expand Up @@ -41,9 +40,9 @@ func (h *UserSelectionHandler) SetUserSelection(ctx context.Context, req *connec
}

// Verify device access (non-admins can only access assigned devices)
_, err = h.store.Queries().GetDeviceByID(ctx, db.GetDeviceByIDParams{
ID: req.Msg.DeviceId,
FilterUserID: userFilterID(ctx, "ListDevices"),
_, err = h.store.Repos().Device.Get(ctx, store.GetDeviceKey{
ID: req.Msg.DeviceId,
OwnerScope: userFilterID(ctx, "ListDevices"),
})
if err != nil {
return nil, apiErrorCtx(ctx, ErrDeviceNotFound, connect.CodeNotFound, "device not found")
Expand Down Expand Up @@ -110,9 +109,9 @@ func (h *UserSelectionHandler) ListAvailableActions(ctx context.Context, req *co
}

// Verify device access (non-admins can only access assigned devices)
_, err := h.store.Queries().GetDeviceByID(ctx, db.GetDeviceByIDParams{
ID: req.Msg.DeviceId,
FilterUserID: userFilterID(ctx, "ListDevices"),
_, err := h.store.Repos().Device.Get(ctx, store.GetDeviceKey{
ID: req.Msg.DeviceId,
OwnerScope: userFilterID(ctx, "ListDevices"),
})
if err != nil {
return nil, apiErrorCtx(ctx, ErrDeviceNotFound, connect.CodeNotFound, "device not found")
Expand Down
Loading