From 2788407ab4fb5dce0eebc1985c70ea5d9d8d0e09 Mon Sep 17 00:00:00 2001 From: maring0019 <57371507+maring0019@users.noreply.github.com> Date: Tue, 7 Mar 2023 09:46:01 -0300 Subject: [PATCH] feat(GDU): se agrega el atributo lastLogin (#1778) --- auth/auth.controller.ts | 10 ++++++++-- auth/routes/routes.ts | 5 ++++- auth/schemas/authUsers.ts | 4 +++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/auth/auth.controller.ts b/auth/auth.controller.ts index 372a40f9bb..379d58c5ec 100644 --- a/auth/auth.controller.ts +++ b/auth/auth.controller.ts @@ -115,7 +115,6 @@ export async function findUser(username) { return null; } - export async function updateUser(documento, nombre, apellido, password) { return await AuthUsers.findOneAndUpdate( { usuario: documento }, @@ -123,6 +122,12 @@ export async function updateUser(documento, nombre, apellido, password) { ); } +export async function updateOrganizacion(usuario, idOrg) { + const org = usuario.user.organizaciones.find(organizacion => idOrg.toString() === organizacion._id.toString()); + org.lastLogin = new Date(); + return await AuthUsers.update({ _id: usuario.user._id }, { $set: { organizaciones: usuario.user.organizaciones } }); +} + // FunciĆ³n interna que chequea si la cuenta mobile existe export const checkMobile = (profesionalId) => { return new Promise((resolve, reject) => { @@ -253,7 +258,8 @@ export async function createUser(data) { nombre: organizacion.nombre, activo: true, permisos, - perfiles: [] + perfiles: [], + lastLogin: new Date() }]; user.organizaciones = organizaciones; diff --git a/auth/routes/routes.ts b/auth/routes/routes.ts index 7a1f827991..faacf8e8fd 100644 --- a/auth/routes/routes.ts +++ b/auth/routes/routes.ts @@ -1,8 +1,9 @@ import { Modulos } from '../../core/tm/schemas/modulos.schema'; +import { findIndex } from 'core-js/core/array'; import * as express from 'express'; import * as mongoose from 'mongoose'; import { updateAccount } from '../../modules/mobileApp/controller/AuthController'; -import { checkMobile, findUser, generateTokenPayload, reset, setValidationTokenAndNotify, updateUser } from '../auth.controller'; +import { checkMobile, findUser, generateTokenPayload, reset, setValidationTokenAndNotify, updateUser, updateOrganizacion } from '../auth.controller'; import { checkPassword } from '../ldap.controller'; import { AuthUsers } from '../schemas/authUsers'; import { Organizacion } from './../../core/tm/schemas/organizacion'; @@ -84,9 +85,11 @@ router.get('/organizaciones', Auth.authenticate(), async (req: any, res, next) = router.post('/v2/organizaciones', Auth.authenticate(), async (req, res, next) => { const username = req.user.usuario.username || req.user.usuario; const orgId = mongoose.Types.ObjectId(req.body.organizacion); + const usuario = await findUser(username); const account_id = (req as any).user.account_id; const dto = await generateTokenPayload(username, orgId, account_id); + updateOrganizacion(usuario, orgId); if (dto) { return res.send({ token: dto.token diff --git a/auth/schemas/authUsers.ts b/auth/schemas/authUsers.ts index 050133af27..7b3a0061ff 100644 --- a/auth/schemas/authUsers.ts +++ b/auth/schemas/authUsers.ts @@ -21,6 +21,7 @@ export interface IAuthUsers { _id: ObjectId; nombre: string; }[]; + lastLogin: Date; }[]; lastLogin: Date; tipo?: String; @@ -62,7 +63,8 @@ export const AuthUsersSchema = new mongoose.Schema({ perfiles: [{ _id: mongoose.Schema.Types.ObjectId, nombre: String - }] + }], + lastLogin: Date }], lastLogin: Date, tipo: {