Connections view #130

Merged
jsegarra merged 5 commits from wbuezas/hedera-web-mindshore:feature/connections-view into beta 2025-03-28 13:33:10 +00:00
1 changed files with 72 additions and 27 deletions

View File

@ -1,5 +1,5 @@
<script setup> <script setup>
import { ref, onMounted, inject, onBeforeUnmount } from 'vue'; import { ref, onMounted, inject, onBeforeUnmount, h } from 'vue';
import { useRouter } from 'vue-router'; import { useRouter } from 'vue-router';
import { useI18n } from 'vue-i18n'; import { useI18n } from 'vue-i18n';
@ -12,12 +12,49 @@ import { useAppStore } from 'stores/app';
import { storeToRefs } from 'pinia'; import { storeToRefs } from 'pinia';
const { t } = useI18n(); const { t } = useI18n();
const jApi = inject('jApi'); const api = inject('api');
const router = useRouter(); const router = useRouter();
const userStore = useUserStore(); const userStore = useUserStore();
const appStore = useAppStore(); const appStore = useAppStore();
const { isHeaderMounted } = storeToRefs(appStore); const { isHeaderMounted } = storeToRefs(appStore);
const filter = {
include: [
{
relation: 'visitUser',
scope: {
include: [
{
relation: 'visitAccess',
scope: {
fields: ['id', 'agentFk'],
include: [
{
relation: 'visitAgent',
scope: {
fields: [
'platform',
'browser',
'version'
]
}
}
]
}
},
{
relation: 'user',
scope: {
fields: ['id', 'nickname', 'name']
}
}
],
fields: ['userFk', 'stamp', 'accessFk']
}
}
],
order: 'lastUpdate DESC'
};
const connections = ref([]); const connections = ref([]);
const loading = ref(false); const loading = ref(false);
const intervalId = ref(null); const intervalId = ref(null);
@ -25,20 +62,28 @@ const intervalId = ref(null);
const getConnections = async () => { const getConnections = async () => {
try { try {
loading.value = true; loading.value = true;
connections.value = await jApi.query(
`SELECT vu.userFk userId, vu.stamp, u.nickname, s.lastUpdate, const { data } = await api.get('/userSessions', {
a.platform, a.browser, a.version, u.name user params: { filter: JSON.stringify(filter) }
FROM userSession s });
JOIN visitUser vu ON vu.id = s.userVisitFk
JOIN visitAccess ac ON ac.id = vu.accessFk if (!data) {
JOIN visitAgent a ON a.id = ac.agentFk connections.value = [];
JOIN visit v ON v.id = a.visitFk return;
JOIN account.user u ON u.id = vu.userFk }
ORDER BY lastUpdate DESC`
); connections.value = data

Esto se podría simplificar
connections.value = data .map(({ visitUser = {}, ...rest }) => ({ ...rest, user: visitUser.user, stamp: visitUser.stamp, visitAgent: visitUser.visitAccess?.visitAgent })) .filter(({ user }) => user);

Esto se podría simplificar `connections.value = data .map(({ visitUser = {}, ...rest }) => ({ ...rest, user: visitUser.user, stamp: visitUser.stamp, visitAgent: visitUser.visitAccess?.visitAgent })) .filter(({ user }) => user); `

Aplicado.

Commit: d9445f72e3

Aplicado. Commit: https://gitea.verdnatura.es/verdnatura/hedera-web/commit/d9445f72e3353d6b7d072c9a8add59a9447311b1

En loopback se pueden crear scopes, de esta manera, no haria falta pasarle el filter.
Ahora ya está fusionada en salix, pero bueno, lo dejo como comentario a futuro

En loopback se pueden crear scopes, de esta manera, no haria falta pasarle el filter. Ahora ya está fusionada en salix, pero bueno, lo dejo como comentario a futuro
loading.value = false; .map(({ visitUser = {}, ...rest }) => ({
...rest,
user: visitUser.user,
stamp: visitUser.stamp,
visitAgent: visitUser.visitAccess?.visitAgent
}))
.filter(({ user }) => user);
} catch (error) { } catch (error) {
console.error('Error getting connections:', error); console.error('Error getting connections:', error);
} finally {
loading.value = false;
} }
}; };
@ -58,6 +103,13 @@ onMounted(async () => {
}); });
onBeforeUnmount(() => clearInterval(intervalId.value)); onBeforeUnmount(() => clearInterval(intervalId.value));
const renderAgentDetails = connection => {
const agent = connection.visitAgent;
return agent?.platform && agent?.browser && agent?.version
? h('span', `${agent.platform} - ${agent.browser} - ${agent.version}`)
: null;
};
</script> </script>
<template> <template>
@ -89,11 +141,11 @@ onBeforeUnmount(() => clearInterval(intervalId.value));
<CardList <CardList
v-for="(connection, index) in connections" v-for="(connection, index) in connections"
:key="index" :key="index"
:to="{ name: 'accessLog', params: { id: connection.userId } }" :to="{ name: 'accessLog', params: { id: connection.user?.id } }"
> >
<template #content> <template #content>
<span class="text-bold q-mb-sm"> <span class="text-bold q-mb-sm">
{{ connection.nickname }} {{ connection.user?.nickname }}
</span> </span>
<span> <span>
{{ {{
@ -107,23 +159,16 @@ onBeforeUnmount(() => clearInterval(intervalId.value));
) )
}}</span }}</span
> >
<span <component :is="renderAgentDetails(connection)" />
v-if="
connection.platform &&
connection.browser &&
connection.version
"
>
{{ connection.platform }} - {{ connection.browser }} -
{{ connection.version }}
</span>
</template> </template>
<template #actions> <template #actions>

Esto se podria simplificar, porque el uso x6 de connection.visitagent no mola
Seria una funcion en el v-if y otra funcion a la que le pases connection y te lo devuelva formateado
Otra opcion es
const renderAgentDetails = (connection) => { const agent = connection.visitAgent; return agent?.platform && agent?.browser && agent?.version ? h('span', ${agent.platform} - ${agent.browser} - ${agent.version}) : null; };

<component :is="renderAgentDetails(connection)" />

Esto se podria simplificar, porque el uso x6 de connection.visitagent no mola Seria una funcion en el v-if y otra funcion a la que le pases connection y te lo devuelva formateado Otra opcion es `const renderAgentDetails = (connection) => { const agent = connection.visitAgent; return agent?.platform && agent?.browser && agent?.version ? h('span', `${agent.platform} - ${agent.browser} - ${agent.version}`) : null; };` ` <component :is="renderAgentDetails(connection)" />`

Aplicado.

Commit: 365d4d31e1

Aplicado. Commit: https://gitea.verdnatura.es/verdnatura/hedera-web/commit/365d4d31e105d5ccb748c046ce01b3db53eddc45
<QBtn <QBtn
icon="people" icon="people"
flat flat
rounded rounded
@click.stop.prevent="supplantUser(connection.user)" @click.stop.prevent="
supplantUser(connection.user?.name)
"
> >
<QTooltip> <QTooltip>
{{ t('supplantUser') }} {{ t('supplantUser') }}