Skip to content

Commit

Permalink
feat(GDU): se agrega el atributo lastLogin (#1778)
Browse files Browse the repository at this point in the history
  • Loading branch information
maring0019 committed Mar 7, 2023
1 parent 5a1b633 commit 2788407
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
10 changes: 8 additions & 2 deletions auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,19 @@ export async function findUser(username) {
return null;
}


export async function updateUser(documento, nombre, apellido, password) {
return await AuthUsers.findOneAndUpdate(
{ usuario: documento },
{ password, nombre, apellido, lastLogin: new Date() },
);
}

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) => {
Expand Down Expand Up @@ -253,7 +258,8 @@ export async function createUser(data) {
nombre: organizacion.nombre,
activo: true,
permisos,
perfiles: []
perfiles: [],
lastLogin: new Date()
}];
user.organizaciones = organizaciones;

Expand Down
5 changes: 4 additions & 1 deletion auth/routes/routes.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion auth/schemas/authUsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface IAuthUsers {
_id: ObjectId;
nombre: string;
}[];
lastLogin: Date;
}[];
lastLogin: Date;
tipo?: String;
Expand Down Expand Up @@ -62,7 +63,8 @@ export const AuthUsersSchema = new mongoose.Schema({
perfiles: [{
_id: mongoose.Schema.Types.ObjectId,
nombre: String
}]
}],
lastLogin: Date
}],
lastLogin: Date,
tipo: {
Expand Down

0 comments on commit 2788407

Please sign in to comment.