Better filtering

This commit is contained in:
Douglas Barone 2020-12-17 08:57:38 -04:00
parent 5eb7976884
commit 738bae1a15

View File

@ -3,26 +3,32 @@
import axios from 'axios' import axios from 'axios'
import prisma from '../prisma' import prisma from '../prisma'
import https from 'https' import https from 'https'
import { subMinutes } from 'date-fns'
import qs from 'qs' import qs from 'qs'
const DEBOUNCE_TIME_IN_MS = 5000
const TIMEOUT_IN_MINUTES = '3'
const httpsAgent = new https.Agent({ const httpsAgent = new https.Agent({
rejectUnauthorized: false rejectUnauthorized: false
}) })
let working = false let working = false
const DEBOUNCE_TIME_IN_MS = 5000
async function updateUserIdMappings() { async function updateUserIdMappings() {
if (working) return -1 if (working) return -1
working = true working = true
const now = new Date()
const timeoutThreshold = subMinutes(now, TIMEOUT_IN_MINUTES)
try { try {
const wifiDevices = await prisma.wifiDevice.findMany({ const wifiDevices = await prisma.wifiDevice.findMany({
where: { where: {
userId: { not: null }, userId: { not: null },
status: 'ONLINE', status: 'ONLINE',
lastSeen: { gt: timeoutThreshold },
ip: { startsWith: process.env.PA_NET } ip: { startsWith: process.env.PA_NET }
}, },
select: { select: {
@ -31,12 +37,10 @@ async function updateUserIdMappings() {
} }
}) })
const timeout = '3'
const entries = wifiDevices.reduce( const entries = wifiDevices.reduce(
(entries, device) => (entries, device) =>
entries + entries +
`<entry name="ifms\\${device.user.sAMAccountName}" ip="${device.ip}" timeout="${timeout}"/>\n`, `<entry name="ifms\\${device.user.sAMAccountName}" ip="${device.ip}" timeout="${TIMEOUT_IN_MINUTES}"/>\n`,
'' ''
) )