Preserve machine identity based on hardware
- Add by_tenant_hostname index for hostname-based lookup - Add third search in register mutation for hardware matching - Search by hostname + MAC/serial when fingerprint/email differ - Fallback to MAC/serial match across all tenant machines - Preserves ticket history when user changes on same physical machine 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
376e81c9c7
commit
bb82efa9d3
2 changed files with 37 additions and 0 deletions
|
|
@ -521,11 +521,13 @@ export const register = mutation({
|
|||
const now = Date.now()
|
||||
const metadataPatch = args.metadata && typeof args.metadata === "object" ? (args.metadata as Record<string, unknown>) : undefined
|
||||
|
||||
// Busca 1: fingerprint exato (tenant + slug + hostname + MACs hash)
|
||||
let existing = await ctx.db
|
||||
.query("machines")
|
||||
.withIndex("by_tenant_fingerprint", (q) => q.eq("tenantId", tenantId).eq("fingerprint", fingerprint))
|
||||
.first()
|
||||
|
||||
// Busca 2: por email + validacao de hardware (fallback se fingerprint mudou mas email igual)
|
||||
if (!existing) {
|
||||
const collaboratorEmail = extractCollaboratorEmail(metadataPatch ?? args.metadata)
|
||||
if (collaboratorEmail) {
|
||||
|
|
@ -539,6 +541,40 @@ export const register = mutation({
|
|||
}
|
||||
}
|
||||
|
||||
// Busca 3: por hostname + validacao de hardware (fallback se o usuario mudou mas e a mesma maquina fisica)
|
||||
// Isso garante que o historico de tickets da maquina seja preservado independente do usuario
|
||||
if (!existing) {
|
||||
const hostnameLower = args.hostname.trim().toLowerCase()
|
||||
const candidates = await ctx.db
|
||||
.query("machines")
|
||||
.withIndex("by_tenant_hostname", (q) => q.eq("tenantId", tenantId).eq("hostname", args.hostname))
|
||||
.collect()
|
||||
// Procura uma maquina com hostname igual E hardware compativel (MAC ou serial)
|
||||
for (const candidate of candidates) {
|
||||
if (matchesExistingHardware(candidate, identifiers, args.hostname)) {
|
||||
existing = candidate
|
||||
break
|
||||
}
|
||||
}
|
||||
// Se nao encontrou por hostname exato, tenta busca mais ampla por hardware
|
||||
if (!existing) {
|
||||
// Busca maquinas do mesmo tenant e verifica se alguma tem MAC/serial compativel
|
||||
const allMachines = await ctx.db
|
||||
.query("machines")
|
||||
.withIndex("by_tenant", (q) => q.eq("tenantId", tenantId))
|
||||
.collect()
|
||||
for (const candidate of allMachines) {
|
||||
// Verifica se compartilha MAC ou serial (hardware fisico)
|
||||
const sharedMac = candidate.macAddresses.some((mac) => identifiers.macs.includes(mac))
|
||||
const sharedSerial = candidate.serialNumbers.some((serial) => identifiers.serials.includes(serial))
|
||||
if (sharedMac || sharedSerial) {
|
||||
existing = candidate
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let machineId: Id<"machines">
|
||||
|
||||
if (existing) {
|
||||
|
|
|
|||
|
|
@ -647,6 +647,7 @@ export default defineSchema({
|
|||
.index("by_tenant_company", ["tenantId", "companyId"])
|
||||
.index("by_tenant_fingerprint", ["tenantId", "fingerprint"])
|
||||
.index("by_tenant_assigned_email", ["tenantId", "assignedUserEmail"])
|
||||
.index("by_tenant_hostname", ["tenantId", "hostname"])
|
||||
.index("by_auth_email", ["authEmail"]),
|
||||
|
||||
usbPolicyEvents: defineTable({
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue