diff --git a/app/api/schemas/repayment.py b/app/api/schemas/repayment.py index 90e1b61..b0d4df1 100644 --- a/app/api/schemas/repayment.py +++ b/app/api/schemas/repayment.py @@ -5,9 +5,8 @@ class RepaymentSchema(Schema): type = fields.Str(required=False) msisdn = fields.Str(required=False) #optional debtId = fields.Str(required=True) - productId = fields.Str(required=True) transactionId = fields.Str(required=True) accountId = fields.Str(required=True) customerId = fields.Str(required=True) - channel = fields.Str(required=True) + loanRef = fields.Str(required=True) initiatedBy = fields.Str(required=False) diff --git a/app/api/services/repayment.py b/app/api/services/repayment.py index c580b60..4aef0f8 100644 --- a/app/api/services/repayment.py +++ b/app/api/services/repayment.py @@ -32,20 +32,22 @@ class RepaymentService(BaseService): customer_id = validated_data.get('customerId') request_id = validated_data.get('requestId') loan_id = validated_data.get('debtId') - product_id = validated_data.get('productId') account_id = validated_data.get('accountId') - customer = Customer.get_customer_with_loan_list(customer_id) + loan_ref = validated_data.get('loanRef') + # customer = Customer.get_customer_with_loan_list(customer_id) transaction_id = validated_data.get('transactionId') initiated_by = validated_data.get('initiatedBy') if(RepaymentService.validate_account_ownership(account_id = account_id, customer_id = customer_id)): + # Check loan exists + loan = Loan.get_customer_loan(loan_id = loan_id, customer_id = customer_id) + # Save the repayment details repayment = Repayment.create_repayment( customer_id = customer_id, - loan_id = loan_id, - product_id = product_id, - transaction_id=transaction_id + loan = loan, + transaction_id = transaction_id ) if not repayment: @@ -66,7 +68,8 @@ class RepaymentService(BaseService): response_data = { "transactionId": transaction_id, "customerId": customer_id, - "productId": product_id, + "productId": loan.product_id, + "loanRef": loan_ref, "debtId": loan_id } diff --git a/app/api/services/select_offer.py b/app/api/services/select_offer.py index dc8f912..c73be44 100644 --- a/app/api/services/select_offer.py +++ b/app/api/services/select_offer.py @@ -59,8 +59,11 @@ class SelectOfferService(BaseService): db.session.flush() - if(amount < offer.min_amount): + if amount < offer.min_amount: return ResponseHelper.error(result_description="The amount is less than the minimum allowed offer amount.") + elif amount > offer.max_amount: + return ResponseHelper.error(result_description="The amount is greater than the maximum allowed offer amount.") + charges = SelectOfferService.calculate_charges(offer, amount) diff --git a/app/models/customer.py b/app/models/customer.py index 882dd34..883d34f 100644 --- a/app/models/customer.py +++ b/app/models/customer.py @@ -48,6 +48,8 @@ class Customer(db.Model): def create_customer(cls, id, msisdn, country_code, account_id, account_type='savings'): if cls.query.filter_by(id=id).first(): raise ValueError("Customer already exists") + elif Account.query.filter_by(id=account_id).first(): + raise ValueError("Account already exists") # Create the customer customer = cls( diff --git a/app/models/repayment.py b/app/models/repayment.py index 590db56..be77d2c 100644 --- a/app/models/repayment.py +++ b/app/models/repayment.py @@ -23,15 +23,7 @@ class Repayment(db.Model): transaction_id = db.Column(db.String(50), nullable=True) @classmethod - def create_repayment(cls, customer_id, loan_id, product_id, transaction_id): - - - # Check customer exists - if not Customer.is_valid_customer(customer_id): - raise ValueError("Invalid customer") - - # Check loan exists - loan = Loan.get_customer_loan(loan_id = loan_id, customer_id = customer_id) + def create_repayment(cls, customer_id, loan, transaction_id): # Check that the loan is active if loan.status not in [LoanStatus.ACTIVE, LoanStatus.START_REPAY]: @@ -40,8 +32,8 @@ class Repayment(db.Model): repayment = cls( customer_id=customer_id, - loan_id=loan_id, - product_id=product_id, + loan_id=loan.id, + product_id=loan.product_id, transaction_id = transaction_id, created_at=datetime.now(timezone.utc), updated_at=datetime.now(timezone.utc) diff --git a/app/swagger/schemas/RepaymentRequest.json b/app/swagger/schemas/RepaymentRequest.json index 9543f74..1c28b71 100644 --- a/app/swagger/schemas/RepaymentRequest.json +++ b/app/swagger/schemas/RepaymentRequest.json @@ -9,10 +9,6 @@ "type": "string", "example": "10" }, - "productId": { - "type": "string", - "example": "101" - }, "transactionId": { "type": "string", "example": "20171209232115" @@ -21,9 +17,9 @@ "type": "string", "example": "CID0000025585" }, - "channel": { + "loanRef": { "type": "string", - "example": "USSD" + "example": "Trx5847365252USSD3MPC" }, "accountId": { "type": "string",