|
1 | 1 | import { auth } from '@/app/lib/auth'; |
2 | 2 | import { db } from '@db'; |
| 3 | +import { randomUUID } from 'node:crypto'; |
3 | 4 | import { type NextRequest, NextResponse } from 'next/server'; |
4 | 5 | import { z } from 'zod'; |
5 | 6 |
|
@@ -69,11 +70,43 @@ export async function POST(req: NextRequest) { |
69 | 70 | }); |
70 | 71 |
|
71 | 72 | if (existing && existing.memberId !== member.id) { |
72 | | - // Device belongs to a different member — prevent hijacking |
73 | | - return NextResponse.json( |
74 | | - { error: 'Device is already registered to another user in this organization' }, |
75 | | - { status: 409 }, |
76 | | - ); |
| 73 | + // Serial number belongs to a different member. This happens when multiple |
| 74 | + // machines report the same generic serial (e.g. "System Serial Number", |
| 75 | + // "To Be Filled By O.E.M."). Instead of blocking, treat the serial as |
| 76 | + // unreliable and register with a generated fallback serial. |
| 77 | + // Format: "fallback:<originalSerial>:<uuid>" — self-documenting and unique. |
| 78 | + const fallback = await db.device.findFirst({ |
| 79 | + where: { |
| 80 | + hostname, |
| 81 | + memberId: member.id, |
| 82 | + organizationId, |
| 83 | + serialNumber: { startsWith: `fallback:${serialNumber}:` }, |
| 84 | + }, |
| 85 | + }); |
| 86 | + |
| 87 | + if (fallback) { |
| 88 | + device = await db.device.update({ |
| 89 | + where: { id: fallback.id }, |
| 90 | + data: { name, platform, osVersion, hardwareModel, agentVersion }, |
| 91 | + }); |
| 92 | + } else { |
| 93 | + const fallbackSerial = `fallback:${serialNumber}:${randomUUID()}`; |
| 94 | + device = await db.device.create({ |
| 95 | + data: { |
| 96 | + name, |
| 97 | + hostname, |
| 98 | + platform, |
| 99 | + osVersion, |
| 100 | + serialNumber: fallbackSerial, |
| 101 | + hardwareModel, |
| 102 | + agentVersion, |
| 103 | + memberId: member.id, |
| 104 | + organizationId, |
| 105 | + }, |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + return NextResponse.json({ deviceId: device.id }); |
77 | 110 | } |
78 | 111 |
|
79 | 112 | if (existing) { |
|
0 commit comments