diff --git a/app/api/services/select_offer.py b/app/api/services/select_offer.py index 15e5c10..e2908e3 100644 --- a/app/api/services/select_offer.py +++ b/app/api/services/select_offer.py @@ -3,6 +3,7 @@ from marshmallow import ValidationError from app.api.helpers.response_helper import ResponseHelper from app.api.services.base_service import BaseService from app.api.enums import TransactionType +from app.models.transaction_offers import TransactionOffer from app.utils.logger import logger from app.api.schemas.select_offer import SelectOfferSchema from app.extensions import db @@ -57,14 +58,20 @@ class SelectOfferService(BaseService): # Get the offer by product ID offer = Offer.get_offer_by_product_id(product_id) + transaction_offer = TransactionOffer.get_transaction_offer(transaction_offer_id=offer_id) + + if not transaction_offer: + logger.error(f"offer {offer_id} not found for customer {customer_id} and transaction {transaction_id}.") + return ResponseHelper.error(result_description="Offer not found.") + db.session.flush() - if amount < offer.min_amount: - logger.error(f"The amount {amount} is less than the minimum allowed offer amount {offer.min_amount}.") + if amount < transaction_offer.min_amount: + logger.error(f"The amount {amount} is less than the minimum allowed offer amount {transaction_offer.min_amount}.") return ResponseHelper.error(result_description="The amount is less than the minimum allowed offer amount.") - elif amount > offer.max_amount: - logger.error(f"The amount {amount} is greater than the minimum allowed offer amount {offer.min_amount}.") - return ResponseHelper.error(result_description="The amount is greater than the maximum allowed offer amount.") + elif amount > transaction_offer.eligible_amount: + logger.error(f"The amount {amount} is greater than the eligible offer amount {transaction_offer.eligible_amount}.") + return ResponseHelper.error(result_description="The amount is greater than the eligible offer amount.") diff --git a/app/models/transaction_offers.py b/app/models/transaction_offers.py index a14943f..b0e7508 100644 --- a/app/models/transaction_offers.py +++ b/app/models/transaction_offers.py @@ -85,6 +85,13 @@ class TransactionOffer(db.Model): return cls.query.filter_by(customer_id=customer_id) \ .order_by(cls.created_at.desc()) \ .first() + + @classmethod + def get_transaction_offer(cls, transaction_offer_id): + """ + Returns a transaction offer by its ID. + """ + return cls.query.get(transaction_offer_id)