145 lines
4.5 KiB
Python
145 lines
4.5 KiB
Python
from flask import session, jsonify
|
|
from app.models.loan import Loan
|
|
from app.utils.logger import logger
|
|
from app.api.services.base_service import BaseService
|
|
# from app.api.schemas.eligibility_check import EligibilityCheckSchema
|
|
from marshmallow import ValidationError
|
|
# from app.api.enums import TransactionType
|
|
# from app.api.integrations import SimbrellaIntegration
|
|
from app.extensions import db
|
|
from app.models import Offer, MembersPending, Members
|
|
# from app.api.services.offer_analysis import OfferAnalysis
|
|
from app.api.helpers.response_helper import ResponseHelper
|
|
from werkzeug.security import generate_password_hash, check_password_hash
|
|
from app.api.schemas.register import RegisterSchema
|
|
from flask_mail import Mail, Message
|
|
|
|
|
|
|
|
import datetime
|
|
import jwt
|
|
import random
|
|
from app.config import Config
|
|
|
|
|
|
class RegisterService(BaseService):
|
|
JWT_SECRET_KEY = Config.JWT_SECRET_KEY
|
|
|
|
@staticmethod
|
|
def process_request(data):
|
|
"""
|
|
Process the Login request.
|
|
|
|
Args:
|
|
data (dict): The request data.
|
|
|
|
Returns:
|
|
dict: A standardized response.
|
|
"""
|
|
try:
|
|
with db.session.begin():
|
|
|
|
validated_data = RegisterService.validate_data(data, RegisterSchema())
|
|
# Simulate processing
|
|
firstname = validated_data.get('firstname')
|
|
lastname = validated_data.get('lastname')
|
|
email = validated_data.get('email')
|
|
|
|
regData = MembersPending.add_members_pending( firstname, lastname, email)
|
|
# {
|
|
# "email": "ameye@chiefsoft.com",
|
|
# "firstname": "Olusesan",
|
|
# "lastname": "Ameye",
|
|
# "isChecked": true
|
|
# }
|
|
send_register_mail(regData.email, regData.uid, regData.id, firstname)
|
|
|
|
response_data = {
|
|
"member_id": 0,
|
|
"uid": 0,
|
|
}
|
|
|
|
return ResponseHelper.success(data=response_data)
|
|
|
|
except ValidationError as err:
|
|
|
|
logger.error(f"Validation Error: {getattr(err, 'messages', str(err))}")
|
|
db.session.rollback()
|
|
return ResponseHelper.unprocessable_entity(result_description="Validation exception")
|
|
|
|
except ValueError as err:
|
|
logger.error(f"{getattr(err, 'messages', str(err))}")
|
|
db.session.rollback()
|
|
return ResponseHelper.error(result_description=str(err))
|
|
|
|
except Exception as e:
|
|
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
|
db.session.rollback()
|
|
return ResponseHelper.internal_server_error()
|
|
|
|
@staticmethod
|
|
def check_loan_limits(customer_id):
|
|
"""
|
|
Checks if a customer has exceeded the loan limits for given offer.
|
|
"""
|
|
loan = Loan.get_customer_last_loan(customer_id)
|
|
|
|
if not loan:
|
|
return True
|
|
|
|
offer_id = loan.offer_id[:5]
|
|
|
|
offer = Offer.get_offer_by_id(offer_id)
|
|
if not offer:
|
|
logger.error(f"Offer not found for offer_id: {offer_id} (customer_id: {customer_id})")
|
|
return False
|
|
|
|
daily_count = Loan.get_daily_loan_count(customer_id, offer.product_id)
|
|
|
|
logger.info(f"daily_count: {daily_count}, Max: {offer.max_daily_loans}")
|
|
|
|
if offer.max_daily_loans is not None and daily_count >= offer.max_daily_loans:
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def send_register_mail(signup_email, email_uid,last_row_id,firstname):
|
|
signup_data = {}
|
|
signup_data["id"] = last_row_id
|
|
signup_data["uid"] = email_uid
|
|
#'d8651e10-3279-4858-87da-b52936faa6f0'
|
|
|
|
jwt_part = jwt.encode(
|
|
{"user": signup_data, 'exp' : datetime.datetime.utcnow() + datetime.timedelta(minutes=3330)},
|
|
RegisterService.JWT_SECRET_KEY, algorithm='HS256'
|
|
)
|
|
panel_url = "www.me.com"
|
|
link_url= str(panel_url) + '/csignup/' + jwt_part
|
|
print(link_url)
|
|
#firstname ='Name001'
|
|
msg = Message(
|
|
'verify your MERMS Account',
|
|
sender ='message@chiefsoft.com',
|
|
recipients = [signup_email,'ameye+merscopy@chiefsoft.com']
|
|
)
|
|
msg.body = f"""
|
|
Hello {firstname},
|
|
You received this message for account verification
|
|
|
|
Follow the link:{link_url}
|
|
|
|
For any Support
|
|
Reach Out
|
|
"""
|
|
|
|
mail.send(msg)
|
|
|
|
|
|
# {
|
|
# "email": "ameye@chiefsoft.com",
|
|
# "firstname": "Olusesan",
|
|
# "lastname": "Ameye",
|
|
# "isChecked": true
|
|
# } |