diff --git a/src/prestamo/prestamo.service.ts b/src/prestamo/prestamo.service.ts index f52ec7c..de47656 100644 --- a/src/prestamo/prestamo.service.ts +++ b/src/prestamo/prestamo.service.ts @@ -450,15 +450,35 @@ export class PrestamoService { if (filtros.pagina) { options.take = 25; options.skip = (parseInt(filtros.pagina) - 1) * 25; - return this.fullInformacionPrestamoView - .findAndCount(options) - .then((infoPrestamos) => { - const prestamos: Prestamo[] = []; - for (let i = 0; i < infoPrestamos[0].length; i++) - prestamos.push(this.fullViewToPrestamo(infoPrestamos[0][i])); - return [prestamos, infoPrestamos[1]]; - }); + const activoTrue = + (typeof filtros.activo === 'boolean' && filtros.activo) || + (typeof filtros.activo === 'string' && filtros.activo === 'true'); + const canUseLightCount = + activoTrue && !filtros.usuario && !filtros.id_tipo_usuario; + + if (canUseLightCount) { + const { usuario: _u, id_tipo_usuario: _tu, ...countWhere } = + busqueda; + const [rows, total] = await Promise.all([ + this.fullInformacionPrestamoView.find(options), + this.informacionPrestamoView.count({ + where: countWhere as FindOptionsWhere, + }), + ]); + const prestamos = rows.map((row) => this.fullViewToPrestamo(row)); + + return [prestamos, total]; + } + + const infoPrestamos = await this.fullInformacionPrestamoView.findAndCount( + options, + ); + const prestamos: Prestamo[] = []; + + for (let i = 0; i < infoPrestamos[0].length; i++) + prestamos.push(this.fullViewToPrestamo(infoPrestamos[0][i])); + return [prestamos, infoPrestamos[1]]; } options.order = { id_institucion: 'ASC', id_prestamo: 'ASC' }; return this.fullInformacionPrestamoView.find(options);