Upsert PA host OK

This commit is contained in:
Douglas Barone 2021-01-14 15:51:40 -04:00
parent 80f81b4013
commit b9805d496b

View File

@ -120,22 +120,25 @@ function decryptKey(encryptedKey) {
}
async function addHost({ cidr, user, password, description, note }) {
try {
if (!CIDR_RE.test(cidr)) throw new Error('Este não é um CIDR válido')
const ipAddr = cidr.split('/')[0]
if (!isIPv4(ipAddr)) throw new Error('Este não é um IPv4 válido')
const net = ip.cidrSubnet(cidr)
if (net.subnetMaskLength > 32 || net.networkAddress == '0.0.0.0')
throw new Error('Esta não é uma combinação de IP/máscara IPv4 válida')
const pAHosts = await prisma.pAHost.findMany()
try {
const key = await getUserKey({ ipAddr, user, password })
const encryptedKey = encryptKey(key)
const id = pAHosts.find(pAHost => pAHost.cidr.split('/')[0] == ipAddr)?.id
const pAHost = {
cidr,
encryptedKey,
@ -144,11 +147,9 @@ async function addHost({ cidr, user, password, description, note }) {
note
}
const host = await prisma.pAHost.upsert({
where: { cidr: cidr },
create: pAHost,
update: pAHost
})
const host = id
? await prisma.pAHost.update({ where: { id }, data: pAHost })
: await prisma.pAHost.create({ data: pAHost })
return {
...host,