diff --git a/app/api/routes/routes.py b/app/api/routes/routes.py index 7885c74..41dd257 100644 --- a/app/api/routes/routes.py +++ b/app/api/routes/routes.py @@ -132,6 +132,35 @@ def get_loans(): response = LoanService.process_request(filters) return response + +@api.route('/recent-loans', methods=['GET']) +# @token_required +def get_recent_loans(): + # Extract query parameters for filtering + filters = { + 'username': request.args.get('id'), + 'email': request.args.get('customer_id'), + 'account_id': request.args.get('account_id'), + 'status': request.args.get('status'), + 'tenor': request.args.get('tenor'), + 'offer_id': request.args.get('offer_id'), + 'product_id': request.args.get('product_id'), + 'transaction_id': request.args.get('transaction_id'), + 'original_transaction': request.args.get('original_transaction'), + 'start_date': request.args.get('start_date'), + 'end_date': request.args.get('end_date'), + 'due_before': request.args.get('due_before'), + 'due_after': request.args.get('due_after'), + 'page': request.args.get('page', 1), + 'limit': request.args.get('limit', 20) + } + # logger.info(f"Get loans request received with filters: {filters}") + + response = LoanService.process_request(filters, True) + return response + + + @api.route('/transactions', methods=['GET']) # @token_required def get_transactions(): diff --git a/app/api/services/loan_service.py b/app/api/services/loan_service.py index 5ad082b..39516fc 100644 --- a/app/api/services/loan_service.py +++ b/app/api/services/loan_service.py @@ -13,7 +13,7 @@ class LoanService: """ @staticmethod - def process_request(filters=None): + def process_request(filters=None, recent_only=False): """ Process the get loans request. @@ -78,7 +78,8 @@ class LoanService: due_before=due_before, due_after=due_after, page=page, - limit=limit + limit=limit, + recent_only=recent_only, ) logger.info(f"Result from loans model cme back ") diff --git a/app/models/loan.py b/app/models/loan.py index 0b1f35c..3b8af78 100644 --- a/app/models/loan.py +++ b/app/models/loan.py @@ -55,7 +55,7 @@ class Loan(db.Model): @classmethod def get_all_loans(cls, id=None, customer_id=None, account_id=None, status=None, tenor=None, offer_id=None, product_id=None, start_date=None, end_date=None, due_before=None, due_after=None, - transaction_id=None, original_transaction=None, page=1, limit=20): + transaction_id=None, original_transaction=None, page=1, limit=20, recent_only= False): """ Get all loans with optional filtering @@ -79,55 +79,58 @@ class Loan(db.Model): """ query = cls.query logger.info(f"Get all loan models from loans model cme back") - # Apply filters if provided - if id: - query = query.filter(cls.id == id) + if recent_only: + query = query.order_by(cls.created_at.desc()).limit(10) + else: + # Apply filters if provided + if id: + query = query.filter(cls.id == id) - if customer_id: - query = query.filter(cls.customer_id == customer_id) + if customer_id: + query = query.filter(cls.customer_id == customer_id) - if account_id: - query = query.filter(cls.account_id == account_id) + if account_id: + query = query.filter(cls.account_id == account_id) - if status: - query = query.filter(cls.status == status) + if status: + query = query.filter(cls.status == status) - if tenor: - query = query.filter(cls.tenor == tenor) + if tenor: + query = query.filter(cls.tenor == tenor) - if offer_id: - query = query.filter(cls.offer_id == offer_id) + if offer_id: + query = query.filter(cls.offer_id == offer_id) - if product_id: - query = query.filter(cls.product_id == product_id) + if product_id: + query = query.filter(cls.product_id == product_id) - if transaction_id: - query = query.filter(cls.transaction_id == transaction_id) + if transaction_id: + query = query.filter(cls.transaction_id == transaction_id) - if original_transaction: - query = query.filter(cls.original_transaction == original_transaction) + if original_transaction: + query = query.filter(cls.original_transaction == original_transaction) - if start_date: - query = query.filter(cls.created_at >= start_date) + if start_date: + query = query.filter(cls.created_at >= start_date) - if end_date: - query = query.filter(cls.created_at <= end_date) + if end_date: + query = query.filter(cls.created_at <= end_date) - if due_before: - query = query.filter(cls.due_date <= due_before) + if due_before: + query = query.filter(cls.due_date <= due_before) - if due_after: - query = query.filter(cls.due_date >= due_after) + if due_after: + query = query.filter(cls.due_date >= due_after) - # Order by created_at descending (newest first) - query = query.order_by(cls.created_at.desc()) + # Order by created_at descending (newest first) + query = query.order_by(cls.created_at.desc()) - # Get total count before pagination - total_count = query.count() + # Get total count before pagination + total_count = query.count() - # Apply pagination - offset = (page - 1) * limit - query = query.limit(limit).offset(offset) + # Apply pagination + offset = (page - 1) * limit + query = query.limit(limit).offset(offset) return query.all(), total_count