189 lines
6.1 KiB
Python
189 lines
6.1 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 smtplib
|
|
from email.mime.text import MIMEText
|
|
from email.mime.multipart import MIMEMultipart
|
|
|
|
|
|
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):
|
|
|
|
sender_email = "support@wrenchboard.com"
|
|
sender_password = 'May12002!xF7F220A97f' # Use an app-specific password if using Gmail
|
|
receiver_email = signup_email
|
|
subject = "Python Email Test"
|
|
body = "This is a test email sent from Python."
|
|
|
|
|
|
msg = MIMEMultipart()
|
|
msg['Subject'] = subject
|
|
msg['From'] = sender_email
|
|
msg['To'] = receiver_email
|
|
msg.attach(MIMEText(body, 'plain')) # or 'html' for HTML content
|
|
|
|
try:
|
|
# For Gmail, use 'smtp.gmail.com' and port 587 (TLS) or 465 (SSL)
|
|
# For other providers, consult their documentation for SMTP server and port
|
|
server = smtplib.SMTP('smtp.gmail.com', 587)
|
|
# server.starttls() # Enable TLS encryption
|
|
server.login(sender_email, sender_password)
|
|
server.sendmail(sender_email, receiver_email, msg.as_string())
|
|
print("Email sent successfully!")
|
|
except Exception as e:
|
|
print(f"Error sending email: {e}")
|
|
logger.error(f"Error sending email: {e}")
|
|
finally:
|
|
server.quit() # Close the connection
|
|
|
|
#
|
|
# 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)
|
|
|
|
# mail = Mail(app) # instantiate the mail class
|
|
#
|
|
# # configuration of mail
|
|
# app.config['MAIL_SERVER'] = 'smtp.gmail.com'
|
|
# app.config['MAIL_PORT'] = 465
|
|
# # app.config['MAIL_PORT'] = 587
|
|
# app.config['MAIL_USERNAME'] = 'message@chiefsoft.com'
|
|
# app.config['MAIL_PASSWORD'] = 'may12002!'
|
|
# app.config['MAIL_USE_TLS'] = False
|
|
# app.config['MAIL_USE_SSL'] = True
|
|
# mail = Mail(app)
|
|
|
|
|
|
|
|
# {
|
|
# "email": "ameye@chiefsoft.com",
|
|
# "firstname": "Olusesan",
|
|
# "lastname": "Ameye",
|
|
# "isChecked": true
|
|
# } |