feat: cadastro manual de acesso remoto e ajustes de horas
This commit is contained in:
parent
8e3cbc7a9a
commit
f3a7045691
16 changed files with 1549 additions and 207 deletions
|
|
@ -1321,6 +1321,86 @@ export const toggleActive = mutation({
|
|||
},
|
||||
})
|
||||
|
||||
export const updateRemoteAccess = mutation({
|
||||
args: {
|
||||
machineId: v.id("machines"),
|
||||
actorId: v.id("users"),
|
||||
provider: v.optional(v.string()),
|
||||
identifier: v.optional(v.string()),
|
||||
url: v.optional(v.string()),
|
||||
notes: v.optional(v.string()),
|
||||
clear: v.optional(v.boolean()),
|
||||
},
|
||||
handler: async (ctx, { machineId, actorId, provider, identifier, url, notes, clear }) => {
|
||||
const machine = await ctx.db.get(machineId)
|
||||
if (!machine) {
|
||||
throw new ConvexError("Máquina não encontrada")
|
||||
}
|
||||
|
||||
const actor = await ctx.db.get(actorId)
|
||||
if (!actor || actor.tenantId !== machine.tenantId) {
|
||||
throw new ConvexError("Acesso negado ao tenant da máquina")
|
||||
}
|
||||
|
||||
const normalizedRole = (actor.role ?? "AGENT").toUpperCase()
|
||||
if (normalizedRole !== "ADMIN" && normalizedRole !== "AGENT") {
|
||||
throw new ConvexError("Somente administradores e agentes podem ajustar o acesso remoto.")
|
||||
}
|
||||
|
||||
if (clear) {
|
||||
await ctx.db.patch(machineId, { remoteAccess: null, updatedAt: Date.now() })
|
||||
return { remoteAccess: null }
|
||||
}
|
||||
|
||||
const trimmedProvider = (provider ?? "").trim()
|
||||
const trimmedIdentifier = (identifier ?? "").trim()
|
||||
if (!trimmedProvider || !trimmedIdentifier) {
|
||||
throw new ConvexError("Informe provedor e identificador do acesso remoto.")
|
||||
}
|
||||
|
||||
let normalizedUrl: string | null = null
|
||||
if (url) {
|
||||
const trimmedUrl = url.trim()
|
||||
if (trimmedUrl) {
|
||||
if (!/^https?:\/\//i.test(trimmedUrl)) {
|
||||
throw new ConvexError("Informe uma URL válida iniciando com http:// ou https://.")
|
||||
}
|
||||
try {
|
||||
new URL(trimmedUrl)
|
||||
} catch {
|
||||
throw new ConvexError("Informe uma URL válida para o acesso remoto.")
|
||||
}
|
||||
normalizedUrl = trimmedUrl
|
||||
}
|
||||
}
|
||||
|
||||
const cleanedNotes = notes?.trim() ? notes.trim() : null
|
||||
const lastVerifiedAt = Date.now()
|
||||
|
||||
const remoteAccess = {
|
||||
provider: trimmedProvider,
|
||||
identifier: trimmedIdentifier,
|
||||
url: normalizedUrl,
|
||||
notes: cleanedNotes,
|
||||
lastVerifiedAt,
|
||||
metadata: {
|
||||
provider: trimmedProvider,
|
||||
identifier: trimmedIdentifier,
|
||||
url: normalizedUrl,
|
||||
notes: cleanedNotes,
|
||||
lastVerifiedAt,
|
||||
},
|
||||
}
|
||||
|
||||
await ctx.db.patch(machineId, {
|
||||
remoteAccess,
|
||||
updatedAt: Date.now(),
|
||||
})
|
||||
|
||||
return { remoteAccess }
|
||||
},
|
||||
})
|
||||
|
||||
export const remove = mutation({
|
||||
args: {
|
||||
machineId: v.id("machines"),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue