Update Prisma. Bugfix: set user correct in non identified devices

This commit is contained in:
Douglas Barone 2020-11-26 14:56:01 -04:00
parent 412c8f2ae6
commit 31d067f99d
12 changed files with 148 additions and 78 deletions

13
pti.code-workspace Normal file
View File

@ -0,0 +1,13 @@
{
"folders": [
{
"path": "web"
},
{
"path": "server"
},
{
"path": "."
}
]
}

View File

@ -10,7 +10,7 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@babel/polyfill": "^7.12.1", "@babel/polyfill": "^7.12.1",
"@prisma/client": "^2.11.0", "@prisma/client": "^2.12.1",
"activedirectory2": "^2.1.0", "activedirectory2": "^2.1.0",
"apollo-server": "^2.19.0", "apollo-server": "^2.19.0",
"apollo-server-plugin-response-cache": "^0.5.6", "apollo-server-plugin-response-cache": "^0.5.6",
@ -32,7 +32,7 @@
"@babel/node": "^7.0.0", "@babel/node": "^7.0.0",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0", "@babel/plugin-proposal-object-rest-spread": "^7.0.0",
"@babel/preset-env": "^7.0.0", "@babel/preset-env": "^7.0.0",
"@prisma/cli": "^2.11.0", "@prisma/cli": "^2.12.1",
"nodemon": "^2.0.6" "nodemon": "^2.0.6"
} }
}, },
@ -2286,33 +2286,33 @@
"devOptional": true "devOptional": true
}, },
"node_modules/@prisma/cli": { "node_modules/@prisma/cli": {
"version": "2.11.0", "version": "2.12.1",
"resolved": "https://registry.npmjs.org/@prisma/cli/-/cli-2.11.0.tgz", "resolved": "https://registry.npmjs.org/@prisma/cli/-/cli-2.12.1.tgz",
"integrity": "sha512-RphW+1SPrEKgpuE5RFM0mv3BeVTF8MCRIyBt35Z9Z/E4YI30qgEWfZu6VfsNDarHRsFiJRKC73wx/aMQ2rLp4g==", "integrity": "sha512-obkwK95dEeifCdVehG0rS0BlPQGLsOtc9U1MgbrjNX3MnhXQdwROnvymfPB3DBlNyoLoHGklPgi9UlwBokNXcQ==",
"devOptional": true, "devOptional": true,
"hasInstallScript": true, "hasInstallScript": true,
"dependencies": { "dependencies": {
"@prisma/bar": "0.0.1", "@prisma/bar": "^0.0.1",
"@prisma/engines": "2.11.0-10.58369335532e47bdcec77a2f1e7c1fb83a463918" "@prisma/engines": "2.12.0-18.cf0680a1bfe8d5e743dc659cc7f08009f9587d58"
}, },
"bin": { "bin": {
"prisma": "build/index.js", "prisma": "build/index.js",
"prisma2": "build/index.js" "prisma2": "build/index.js"
}, },
"engines": { "engines": {
"node": ">=10" "node": ">=10.4"
} }
}, },
"node_modules/@prisma/client": { "node_modules/@prisma/client": {
"version": "2.11.0", "version": "2.12.1",
"resolved": "https://registry.npmjs.org/@prisma/client/-/client-2.11.0.tgz", "resolved": "https://registry.npmjs.org/@prisma/client/-/client-2.12.1.tgz",
"integrity": "sha512-BF7K/yi5fAnrt7MelQqUueJyl06IGmIxf+7f5RxFSvyO6xZMbOYxhW21kV2wt10mOIS0khQbo0xY6w/8jViJuQ==", "integrity": "sha512-HP4/E9sRdxw/FB7XP4EeRa5ri8Lp1U/L7G4VAA95aM8C+8ARioQHMNDpEjC83NrOrOr4EcaZV5pXDDQL1H+F0g==",
"hasInstallScript": true, "hasInstallScript": true,
"dependencies": { "dependencies": {
"@prisma/engines-version": "2.11.0-10.58369335532e47bdcec77a2f1e7c1fb83a463918" "@prisma/engines-version": "2.12.0-18.cf0680a1bfe8d5e743dc659cc7f08009f9587d58"
}, },
"engines": { "engines": {
"node": ">=10" "node": ">=10.4"
}, },
"peerDependencies": { "peerDependencies": {
"@prisma/cli": "*" "@prisma/cli": "*"
@ -2324,16 +2324,16 @@
} }
}, },
"node_modules/@prisma/engines": { "node_modules/@prisma/engines": {
"version": "2.11.0-10.58369335532e47bdcec77a2f1e7c1fb83a463918", "version": "2.12.0-18.cf0680a1bfe8d5e743dc659cc7f08009f9587d58",
"resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-2.11.0-10.58369335532e47bdcec77a2f1e7c1fb83a463918.tgz", "resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-2.12.0-18.cf0680a1bfe8d5e743dc659cc7f08009f9587d58.tgz",
"integrity": "sha512-0WaUybWM7J5zQuG/zYLbV+ZKx9/nzS7Ruu7Y0K2lXJKy3Z9koeVttq+Xt7tVmUX9TLgI1Rwhb9R2e1JMNDWbsw==", "integrity": "sha512-F6RmUZ5JpPWxmGvVDji8c4gepHIGkvYbtuFi0IoDDJVaCVo8yS656stciKFyswI6/BLWXa0X47/MIMbz6nzw7g==",
"devOptional": true, "devOptional": true,
"hasInstallScript": true "hasInstallScript": true
}, },
"node_modules/@prisma/engines-version": { "node_modules/@prisma/engines-version": {
"version": "2.11.0-10.58369335532e47bdcec77a2f1e7c1fb83a463918", "version": "2.12.0-18.cf0680a1bfe8d5e743dc659cc7f08009f9587d58",
"resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-2.11.0-10.58369335532e47bdcec77a2f1e7c1fb83a463918.tgz", "resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-2.12.0-18.cf0680a1bfe8d5e743dc659cc7f08009f9587d58.tgz",
"integrity": "sha512-qlkW4dKoW1dUnperWPuhFriZ/NTHlsKLhBbebxRa8qMuD3o37SvWIDGLjFOQx1N0Eb4H04rI3XxgjkWLFVlZCw==" "integrity": "sha512-IHb/Jag1Wmoq5tLZhOHP5zqLHEXqQEfrHb6l0drIBSvh2AF7yWQ3yyuD0ZEb1Nq37SvbBgop5wrWMOU8YWFTGQ=="
}, },
"node_modules/@protobufjs/aspromise": { "node_modules/@protobufjs/aspromise": {
"version": "1.1.2", "version": "1.1.2",
@ -10741,33 +10741,33 @@
"devOptional": true "devOptional": true
}, },
"@prisma/cli": { "@prisma/cli": {
"version": "2.11.0", "version": "2.12.1",
"resolved": "https://registry.npmjs.org/@prisma/cli/-/cli-2.11.0.tgz", "resolved": "https://registry.npmjs.org/@prisma/cli/-/cli-2.12.1.tgz",
"integrity": "sha512-RphW+1SPrEKgpuE5RFM0mv3BeVTF8MCRIyBt35Z9Z/E4YI30qgEWfZu6VfsNDarHRsFiJRKC73wx/aMQ2rLp4g==", "integrity": "sha512-obkwK95dEeifCdVehG0rS0BlPQGLsOtc9U1MgbrjNX3MnhXQdwROnvymfPB3DBlNyoLoHGklPgi9UlwBokNXcQ==",
"devOptional": true, "devOptional": true,
"requires": { "requires": {
"@prisma/bar": "0.0.1", "@prisma/bar": "^0.0.1",
"@prisma/engines": "2.11.0-10.58369335532e47bdcec77a2f1e7c1fb83a463918" "@prisma/engines": "2.12.0-18.cf0680a1bfe8d5e743dc659cc7f08009f9587d58"
} }
}, },
"@prisma/client": { "@prisma/client": {
"version": "2.11.0", "version": "2.12.1",
"resolved": "https://registry.npmjs.org/@prisma/client/-/client-2.11.0.tgz", "resolved": "https://registry.npmjs.org/@prisma/client/-/client-2.12.1.tgz",
"integrity": "sha512-BF7K/yi5fAnrt7MelQqUueJyl06IGmIxf+7f5RxFSvyO6xZMbOYxhW21kV2wt10mOIS0khQbo0xY6w/8jViJuQ==", "integrity": "sha512-HP4/E9sRdxw/FB7XP4EeRa5ri8Lp1U/L7G4VAA95aM8C+8ARioQHMNDpEjC83NrOrOr4EcaZV5pXDDQL1H+F0g==",
"requires": { "requires": {
"@prisma/engines-version": "2.11.0-10.58369335532e47bdcec77a2f1e7c1fb83a463918" "@prisma/engines-version": "2.12.0-18.cf0680a1bfe8d5e743dc659cc7f08009f9587d58"
} }
}, },
"@prisma/engines": { "@prisma/engines": {
"version": "2.11.0-10.58369335532e47bdcec77a2f1e7c1fb83a463918", "version": "2.12.0-18.cf0680a1bfe8d5e743dc659cc7f08009f9587d58",
"resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-2.11.0-10.58369335532e47bdcec77a2f1e7c1fb83a463918.tgz", "resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-2.12.0-18.cf0680a1bfe8d5e743dc659cc7f08009f9587d58.tgz",
"integrity": "sha512-0WaUybWM7J5zQuG/zYLbV+ZKx9/nzS7Ruu7Y0K2lXJKy3Z9koeVttq+Xt7tVmUX9TLgI1Rwhb9R2e1JMNDWbsw==", "integrity": "sha512-F6RmUZ5JpPWxmGvVDji8c4gepHIGkvYbtuFi0IoDDJVaCVo8yS656stciKFyswI6/BLWXa0X47/MIMbz6nzw7g==",
"devOptional": true "devOptional": true
}, },
"@prisma/engines-version": { "@prisma/engines-version": {
"version": "2.11.0-10.58369335532e47bdcec77a2f1e7c1fb83a463918", "version": "2.12.0-18.cf0680a1bfe8d5e743dc659cc7f08009f9587d58",
"resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-2.11.0-10.58369335532e47bdcec77a2f1e7c1fb83a463918.tgz", "resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-2.12.0-18.cf0680a1bfe8d5e743dc659cc7f08009f9587d58.tgz",
"integrity": "sha512-qlkW4dKoW1dUnperWPuhFriZ/NTHlsKLhBbebxRa8qMuD3o37SvWIDGLjFOQx1N0Eb4H04rI3XxgjkWLFVlZCw==" "integrity": "sha512-IHb/Jag1Wmoq5tLZhOHP5zqLHEXqQEfrHb6l0drIBSvh2AF7yWQ3yyuD0ZEb1Nq37SvbBgop5wrWMOU8YWFTGQ=="
}, },
"@protobufjs/aspromise": { "@protobufjs/aspromise": {
"version": "1.1.2", "version": "1.1.2",

View File

@ -30,12 +30,12 @@
"@babel/node": "^7.0.0", "@babel/node": "^7.0.0",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0", "@babel/plugin-proposal-object-rest-spread": "^7.0.0",
"@babel/preset-env": "^7.0.0", "@babel/preset-env": "^7.0.0",
"@prisma/cli": "^2.11.0", "@prisma/cli": "^2.12.1",
"nodemon": "^2.0.6" "nodemon": "^2.0.6"
}, },
"dependencies": { "dependencies": {
"@babel/polyfill": "^7.12.1", "@babel/polyfill": "^7.12.1",
"@prisma/client": "^2.11.0", "@prisma/client": "^2.12.1",
"activedirectory2": "^2.1.0", "activedirectory2": "^2.1.0",
"apollo-server": "^2.19.0", "apollo-server": "^2.19.0",
"apollo-server-plugin-response-cache": "^0.5.6", "apollo-server-plugin-response-cache": "^0.5.6",

View File

@ -82,7 +82,7 @@ class ResetToken {
} }
const user = await prisma.resetToken const user = await prisma.resetToken
.findOne({ .findUnique({
where: { id: resetToken.id } where: { id: resetToken.id }
}) })
.user() .user()

View File

@ -17,7 +17,7 @@ class User {
*/ */
async init() { async init() {
try { try {
let user = await prisma.user.findOne({ let user = await prisma.user.findUnique({
where: { sAMAccountName: this.username } where: { sAMAccountName: this.username }
}) })

View File

@ -91,12 +91,19 @@ const Query = {
} }
}, },
wifiDevices: async (_, { identifiedOnly }) => { wifiDevices: async (_, { identifiedOnly, nonIdentifiedOnly }) => {
if (identifiedOnly && nonIdentifiedOnly)
throw new Error('Invalid combination of filters')
updateDBWithOnlineDevices() updateDBWithOnlineDevices()
return prisma.wifiDevice.findMany({ return prisma.wifiDevice.findMany({
orderBy: [{ lastSeen: 'desc' }], orderBy: [{ lastSeen: 'desc' }],
where: identifiedOnly ? { NOT: { userId: null } } : {}, where: identifiedOnly
? { NOT: { userId: null } }
: nonIdentifiedOnly
? { userId: null }
: {},
include: { user: true } include: { user: true }
}) })
}, },

View File

@ -2,10 +2,10 @@ import prisma from '../prisma'
const ResetToken = { const ResetToken = {
creator(_) { creator(_) {
return prisma.resetToken.findOne({ where: { id: _.id } }).creator() return prisma.resetToken.findUnique({ where: { id: _.id } }).creator()
}, },
user(_) { user(_) {
return prisma.resetToken.findOne({ where: { id: _.id } }).user() return prisma.resetToken.findUnique({ where: { id: _.id } }).user()
}, },
expiration: _ => _.expiration.toISOString(), expiration: _ => _.expiration.toISOString(),
createdAt: _ => _.createdAt.toISOString() createdAt: _ => _.createdAt.toISOString()

View File

@ -39,7 +39,7 @@ const User = {
return [] return []
return prisma.user return prisma.user
.findOne({ .findUnique({
where: { where: {
sAMAccountName: _.sAMAccountName sAMAccountName: _.sAMAccountName
} }

View File

@ -25,7 +25,8 @@ const typeDefs = gql`
wifiDevices( wifiDevices(
search: String = "" search: String = ""
identifiedOnly: Boolean = true identifiedOnly: Boolean = false
nonIdentifiedOnly: Boolean = false
): [WifiDevice]! ): [WifiDevice]!
userPresence(search: String = ""): [UserPresence!] @auth(roles: ["watcher"]) userPresence(search: String = ""): [UserPresence!] @auth(roles: ["watcher"])

View File

@ -46,7 +46,7 @@ export async function getOnlineWifiDevices() {
const onlineDevices = await getDevices() const onlineDevices = await getDevices()
const hydratedOnlineDevices = onlineDevices.map(client => ({ const hydratedOnlineDevices = onlineDevices.map(client => ({
user: client.Name == 'unknown' ? undefined : client.Name, user: client.Name == 'unknown' ? null : client.Name,
oui: ouiFinder(client.macaddr), oui: ouiFinder(client.macaddr),
mac: client.macaddr, mac: client.macaddr,
hostname: client.HN == 'unknown' ? undefined : client.HN, hostname: client.HN == 'unknown' ? undefined : client.HN,

View File

@ -29,22 +29,33 @@ async function updateDBWithOnlineDevices() {
const onlineDevices = [...onlineUnifiDevices, ...onlineCiscoDevices] const onlineDevices = [...onlineUnifiDevices, ...onlineCiscoDevices]
for (const onlineDevice of onlineDevices) { for (const device of onlineDevices) {
const device = { if (!device.user)
...onlineDevice, await prisma.$executeRaw(
user: onlineDevice.user 'UPDATE "WifiDevice" SET "userId" = null WHERE mac = $1',
? { connect: { sAMAccountName: onlineDevice.user } } device.mac
: undefined )
}
try { try {
await prisma.wifiDevice.upsert({ await prisma.wifiDevice.upsert({
where: { mac: onlineDevice.mac }, where: { mac: device.mac },
create: { ...device, firstSeen: device.firstSeen || new Date() }, create: {
update: device ...device,
firstSeen: device.firstSeen || new Date(),
user: device.user
? { connect: { sAMAccountName: device.user } }
: undefined
},
update: {
...device,
user: device.user
? { connect: { sAMAccountName: device.user } }
: undefined
}
}) })
} catch (e) { } catch (e) {
if (e.code != 'P2016') console.log('[wifiDevice upsert error]', e) if (!['P2016'].includes(e.code))
console.log('[wifiDevice upsert error]', e)
} }
} }

View File

@ -1,44 +1,78 @@
<template> <template>
<v-container fluid> <v-container fluid>
<div v-if="$apollo.queries.wifiDevices.loading"> <v-data-iterator
Carregando informações... :items="computedWifiDevices"
</div> :items-per-page.sync="itemsPerPage"
<v-data-table :page="page"
v-if="!$apollo.queries.wifiDevices.loading"
:headers="headers"
:items="items"
:options="{ itemsPerPage: 15 }"
> >
</v-data-table> <template #header>
<v-toolbar flat>
Dispositivos Wi-Fi
<v-spacer />
<v-btn
:loading="$apollo.queries.wifiDevices.loading"
color="primary"
text
@click="$apollo.queries.wifiDevices.refresh()"
>
<v-icon left>mdi-refresh</v-icon> Atualizar
</v-btn>
</v-toolbar>
</template>
<template #default="{ items }">
<v-row>
<v-col
v-for="item in items"
:key="item.mac"
cols="12"
md="4"
class="grow"
>
<v-card>
<v-card-text>
<Avatar :src="item.user.thumbnailPhoto" />
<p>
{{ item.user.displayName }}
</p>
<p>
{{ item.essid }}
</p>
{{ item.status }}
{{ item.mac }}
{{ item.ip }}
{{ item.controller }}
</v-card-text>
</v-card>
</v-col>
</v-row>
</template>
<template #footer> Footer </template>
</v-data-iterator>
</v-container> </v-container>
</template> </template>
<script> <script>
import gql from 'graphql-tag' import gql from 'graphql-tag'
import Avatar from '../components/Avatar.vue'
export default { export default {
name: 'WifiDevices', name: 'WifiDevices',
components: { Avatar },
data: () => ({ data: () => ({
headers: [ itemsPerPage: 6,
// { text: 'Hostname', value: 'hostname' }, itemsPerPageArray: [15, 30, 60, 120],
{ text: 'Usuário', value: 'user' }, page: 1
{ text: 'OUI', value: 'oui' },
{ text: 'AP', value: 'apName' },
{ text: 'Controladora', value: 'controller' },
{ text: 'SSID', value: 'essid' },
{ text: 'Status', value: 'status' },
{ text: 'Endereço MAC', value: 'mac' }
]
}), }),
apollo: { apollo: {
wifiDevices: { wifiDevices: {
fetchPolicy: 'cache-and-network', fetchPolicy: 'cache-and-network',
query: gql` query: gql`
query { query {
wifiDevices { wifiDevices(nonIdentifiedOnly: true) {
user { user {
displayName displayName
sAMAccountName sAMAccountName
thumbnailPhoto
} }
oui oui
mac mac
@ -57,11 +91,15 @@ export default {
} }
}, },
computed: { computed: {
items() { computedWifiDevices() {
return this.wifiDevices?.map(device => ({ return this.wifiDevices?.map(device => ({
...device, ...device,
hostname: device.hostname || 'Desconhecido', hostname: device.hostname || 'Desconhecido',
user: device.user.displayName user: device.user || {
displayName: 'Não identificado',
sAMAccountName: null,
thumbnailPhoto: null
}
})) }))
} }
} }