diff --git a/app.py b/app.py index abf0488..23d7bd8 100644 --- a/app.py +++ b/app.py @@ -1,20 +1,8 @@ from app import create_app from flask_mail import Mail, Message -from app.config import Config +# from app.config import Config app = create_app() if __name__ == "__main__": app.run(host="0.0.0.0", port=5000, debug=True) - - 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) diff --git a/app/api/routes/routes.py b/app/api/routes/routes.py index ecd43b6..7a2cec3 100644 --- a/app/api/routes/routes.py +++ b/app/api/routes/routes.py @@ -47,6 +47,15 @@ def serve_paths(filename): swagger_dir = os.path.join("swagger") return send_from_directory(swagger_dir, filename) + +@api.route("/panel/auth/reset", methods=["POST"]) +@jwt_required() +def merms_reset(): + data = request.get_json() + response = LoginService.process_reset(data) + return response + + @api.route("/panel/Login", methods=["POST"]) @jwt_required() def merms_login(): diff --git a/app/api/schemas/reset_pass_start.py b/app/api/schemas/reset_pass_start.py new file mode 100644 index 0000000..9809d00 --- /dev/null +++ b/app/api/schemas/reset_pass_start.py @@ -0,0 +1,4 @@ +from marshmallow import Schema, fields + +class ResetPassStart(Schema): + username = fields.Str(required=True) diff --git a/app/api/services/base_service.py b/app/api/services/base_service.py index 4a6fd86..50bbc08 100644 --- a/app/api/services/base_service.py +++ b/app/api/services/base_service.py @@ -4,11 +4,78 @@ from flask import jsonify from marshmallow import ValidationError import logging from app.api.integrations import KafkaIntegration - +from app.config import Config logger = logging.getLogger(__name__) +from flask_mail import Mail, Message +import smtplib +from email.mime.text import MIMEText +from email.mime.multipart import MIMEMultipart + + class BaseService: TRANSACTION_TYPE = None + JWT_SECRET_KEY = Config.JWT_SECRET_KEY + + SEND_EMAIL_FROM = Config.SEND_EMAIL_FROM + SEND_EMAIL_PASS = Config.SEND_EMAIL_PASS + + + @staticmethod + def send_resetpass_mail(signup_email, pending_uid, pending_id, firstname, lastname): + + pending_member = { + "email": signup_email, + "pending_uid": pending_uid, + "first_name": firstname, + "last_name": lastname, + "pending_id": pending_id, + } + jwt_part = jwt.encode( + {"user": pending_member, 'exp': datetime.datetime.utcnow() + datetime.timedelta(minutes=3330)}, + BaseService.JWT_SECRET_KEY, algorithm='HS256' + ) + panel_url = "https://qa-panel.mermsemr.com" + link_url = str(panel_url) + '/accreset/' + jwt_part + + msg_body = f""" + Hello {firstname}, + + You received this message for account reset password + + Follow the link:{link_url} + + For any Support + Reach Out + support@mermsemr.com + """ + + sender_email = BaseService.SEND_EMAIL_FROM + sender_password = BaseService.SEND_EMAIL_PASS + receiver_email = signup_email + subject = "Reset Password Email" + body = msg_body + + 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 + @classmethod def validate_data(cls, data, schema): diff --git a/app/api/services/login.py b/app/api/services/login.py index 8bfb3fc..d7838f9 100644 --- a/app/api/services/login.py +++ b/app/api/services/login.py @@ -1,17 +1,18 @@ from flask import session, jsonify -from app.models.loan import Loan +#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 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.api.enums import TransactionType +# from app.api.integrations import SimbrellaIntegration from app.extensions import db -from app.models import Offer, RACCheck, Members -from app.api.services.offer_analysis import OfferAnalysis +from app.models import PasswordReset, 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.login import LoginSchema +from app.api.schemas.reset_pass_start import ResetPassStart import datetime import jwt import random @@ -19,6 +20,47 @@ from app.config import Config class LoginService(BaseService): + @staticmethod + def process_reset(data): + try: + with db.session.begin(): + + validated_data = LoginService.validate_data(data, ResetPassStart()) + username = validated_data.get('username') + member = Members.get_member_by_username(username) + if not member: + invalid_data = { + "error_message": "invalid username or password", + "message_key": "invalid_username_or_password", + } + return ResponseHelper.success(data=invalid_data) + PasswordReset.create_reset(username=username) + BaseService.send_resetpass_mail(member.email, member.uid, member.id, "FF","LL") #pending_uid, pending_id, firstname, lastname + + response_data = { + "error_message": "invalid username or password 000", + "message_key": "invalid_username_or_password", + } + + 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 process_request(data): @@ -150,28 +192,28 @@ class LoginService(BaseService): 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 + # @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 diff --git a/app/models/__init__.py b/app/models/__init__.py index 19d41a6..2a954b8 100644 --- a/app/models/__init__.py +++ b/app/models/__init__.py @@ -18,8 +18,10 @@ from .members_actions import MembersActions from .members_pending import MembersPending from .products_details import ProductsDetails from .provision_actions import ProvisionActions +from .password_reset import PasswordReset + __all__ = ['Members','Customer', 'Account', 'Products', - 'MembersProducts', 'MembersActions', 'MembersPending', 'ProductsDetails', 'ProvisionActions', 'Loan', 'Transaction', 'Repayment', + 'MembersProducts', 'MembersActions', 'MembersPending', 'ProductsDetails', 'ProvisionActions', 'PasswordReset','Loan', 'Transaction', 'Repayment', 'LoanCharge', 'Offer', 'Charge', 'RACCheck', 'LoanRepaymentSchedule', 'TransactionOffer', 'RepaymentsData', 'Salary'] \ No newline at end of file diff --git a/app/models/password_reset.py b/app/models/password_reset.py new file mode 100644 index 0000000..c8df651 --- /dev/null +++ b/app/models/password_reset.py @@ -0,0 +1,47 @@ +from datetime import datetime, timezone +from app.extensions import db +from app.models.charge import Charge +from sqlalchemy.orm import relationship +from sqlalchemy.sql import func +from sqlalchemy.exc import IntegrityError +import uuid +from app.utils.logger import logger + +class PasswordReset(db.Model): + __tablename__ = 'password_reset' + + id = db.Column(db.String, primary_key=True) + uid = db.Column(db.String(150), nullable=True) + username = db.Column(db.String, nullable=False) + status = db.Column(db.Integer, nullable=True, default=0) + added = db.Column(db.DateTime(timezone=True), server_default=func.now()) + updated = db.Column(db.DateTime(timezone=True), server_default=func.now(), onupdate=func.now()) + + def to_dict(self): + return { + "id": self.id, + "uid": self.uid, + "username": self.account_id, + "status": self.status, + "added": self.added.isoformat() if self.added else None, + "updated": self.updated.isoformat() if self.updated else None, + } + + def __repr__(self): + return f'' + + +@classmethod +def create_reset(cls, username): + pass_reset = cls( + uid=str(uuid.uuid4()), + username=username, + created_at=datetime.now(timezone.utc), + updated_at=datetime.now(timezone.utc) + ) + + try: + db.session.add(pass_reset) + except IntegrityError as err: + raise ValueError(f"Database integrity error: {err}") + return pass_reset