@@ -65,6 +65,7 @@ export function createPayload(user, authOrg, prof) {
6565 } ,
6666 profesional : prof && String ( prof . _id ) ,
6767 permisos : [ ...user . permisosGlobales , ...authOrg . permisos ] ,
68+ fechaVencimiento : authOrg . fechaVencimiento ,
6869 feature : { ...( user . configuracion || { } ) }
6970 } ;
7071}
@@ -77,6 +78,7 @@ export async function findTokenData(username: number, organizacion: ObjectId) {
7778 const pProfesional = Profesional . findOne ( { documento : String ( username ) , habilitado : { $ne : false } } , { nombre : true , apellido : true } ) ;
7879 const [ auth , prof ] : [ any , any ] = await Promise . all ( [ pAuth , pProfesional ] ) ;
7980 if ( auth ) {
81+ await checkAndInactivateExpired ( auth ) ;
8082 const authOrganizacion = auth . organizaciones . find ( item => String ( item . _id ) === String ( organizacion ) ) ;
8183 return {
8284 usuario : auth ,
@@ -130,6 +132,7 @@ export async function findUser(username) {
130132 const pProfesional = Profesional . findOne ( { documento : username , habilitado : { $ne : false } } , { matriculas : true , especialidad : true } ) ;
131133 const [ auth , prof ] = await Promise . all ( [ pAuth , pProfesional ] ) ;
132134 if ( auth ) {
135+ await checkAndInactivateExpired ( auth ) ;
133136 return {
134137 user : auth ,
135138 profesional : prof
@@ -138,6 +141,25 @@ export async function findUser(username) {
138141 return null ;
139142}
140143
144+ /**
145+ * Chequea las organizaciones del usuario e inactiva las que tienen fecha de vencimiento cumplida.
146+ * @param {any } user Instancia de AuthUsers
147+ */
148+ export async function checkAndInactivateExpired ( user ) {
149+ let changed = false ;
150+ const now = new Date ( ) ;
151+ user . organizaciones . forEach ( org => {
152+ if ( org . activo && org . fechaVencimiento && org . fechaVencimiento < now ) {
153+ org . activo = false ;
154+ changed = true ;
155+ }
156+ } ) ;
157+ if ( changed ) {
158+ user . audit ( userScheduler ) ;
159+ await user . save ( ) ;
160+ }
161+ }
162+
141163export async function updateUser ( documento , nombre , apellido , password ) {
142164 return await AuthUsers . findOneAndUpdate (
143165 { usuario : documento } ,
0 commit comments