diff --git a/apps/api/src/devices/devices.service.ts b/apps/api/src/devices/devices.service.ts index e774611..18e1efb 100644 --- a/apps/api/src/devices/devices.service.ts +++ b/apps/api/src/devices/devices.service.ts @@ -104,27 +104,31 @@ export class DevicesService { const tieBreaker = uuidv4().substring(0, 8); const cursor = Buffer.from(`${cursorTimestamp}:${tieBreaker}`).toString('base64url'); - // Parse cursor from request if provided - let cursorCutoff = new Date(0); + // Build the Prisma where clause + const commandWhere: any = { + deviceId: device.id, + status: 'pending', + }; + + // Parse cursor from request if provided — skip filter if cursor is missing or invalid if (dto.cursor) { try { const decoded = Buffer.from(dto.cursor, 'base64url').toString('utf-8'); const parts = decoded.split(':'); if (parts.length >= 1) { - cursorCutoff = new Date(parts[0]); + const parsed = new Date(parts[0]); + if (!isNaN(parsed.getTime())) { + commandWhere.createdAt = { gt: parsed }; + } } } catch { - // Invalid cursor, return all pending commands + // Invalid cursor — ignore filter, return all pending commands } } // Find pending commands for this device const commands = await this.prisma.remoteCommand.findMany({ - where: { - deviceId: device.id, - status: 'pending', - createdAt: { gt: cursorCutoff }, - }, + where: commandWhere, select: { id: true, type: true,