325 lines
11 KiB
Python
325 lines
11 KiB
Python
from marshmallow import ValidationError
|
|
import logging
|
|
from app.api.integrations import KafkaIntegration
|
|
from app.config import Config
|
|
from app.models import MembersWebfiles
|
|
|
|
logger = logging.getLogger(__name__)
|
|
from app.api.integrations import StripeIntegration
|
|
import datetime
|
|
import jwt
|
|
import requests
|
|
import redis
|
|
import json
|
|
from app.notifications.mail_factory import send_email_factory
|
|
|
|
|
|
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
|
|
THIS_SITE_URL = Config.THIS_SITE_URL
|
|
|
|
CACHE_SERVER = Config.CACHE_SERVER
|
|
CACHE_PORT = Config.CACHE_PORT
|
|
CACHE_PASSWORD = Config.CACHE_PASSWORD
|
|
CACHE_DEFAULT_EXPIRE = Config.CACHE_DEFAULT_EXPIRE
|
|
MEDIA_SERVER = Config.MEDIA_SERVER
|
|
|
|
@staticmethod
|
|
def get_profile_picture_url(profile_uid):
|
|
file_url = ''
|
|
if profile_uid is None or profile_uid == '':
|
|
return file_url
|
|
|
|
selectedFile = MembersWebfiles.get_member_webfiles_by_file_uid(profile_uid)
|
|
if selectedFile:
|
|
file_url = (
|
|
BaseService.MEDIA_SERVER + "/" + selectedFile.file_group + "/" + str(
|
|
selectedFile.uid) + "/" + selectedFile.filename).lower()
|
|
return file_url
|
|
|
|
@staticmethod
|
|
def addStripeCustomer(customerData):
|
|
customer_data = {
|
|
"email": customerData["email"],
|
|
"name": customerData["name"],
|
|
}
|
|
stripe_customer = StripeIntegration.create_customer(customer_data)
|
|
logger.info(f"Stripe_Customer ===== : {stripe_customer}")
|
|
return stripe_customer
|
|
|
|
@staticmethod
|
|
def send_completepass_mail(signup_email, pending_uid, pending_id, firstname, lastname):
|
|
|
|
# html_body = f"""\
|
|
# <html>
|
|
# <head></head>
|
|
# <body style="font-size:14px;line-height:1.5;">
|
|
# <table width="550px" border="0" cellpadding="3" cellspacing="3" background-color="#F0F8FF" style="font-size:16px">
|
|
# <tr>
|
|
# <td style="text-align:center">
|
|
# <img style="width:150px; height:auto;" src="mermsemr.com/images/logo-pink.png" />
|
|
# </td>
|
|
# </tr>
|
|
# <tr>
|
|
# <td>
|
|
# Hello <b>{firstname}!</b>
|
|
# </td>
|
|
# </tr>
|
|
# <tr>
|
|
# <td>
|
|
# Password Reset Completed<br>
|
|
# </td>
|
|
# </tr>
|
|
#
|
|
# <tr>
|
|
# <td>
|
|
# For any support<br>
|
|
# Reach Out<br>
|
|
# support@mermsemr.com<br>
|
|
# https://www.mermsemr.com/
|
|
# </td>
|
|
# </tr>
|
|
# </table>
|
|
# </body>
|
|
# </html>
|
|
# """
|
|
html_body = f"""
|
|
<table width="550px" border="0" cellpadding="3" cellspacing="3" background-color="#F0F8FF" style="font-size:16px">
|
|
<tr>
|
|
<td>
|
|
Hello <b>{firstname}!</b>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td>
|
|
Password Reset Completed<br>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
"""
|
|
receiver_email = signup_email
|
|
subject = "Reset Password Completed"
|
|
|
|
send_email_factory(
|
|
to_email=receiver_email,
|
|
subject=subject,
|
|
html_content=html_body
|
|
)
|
|
|
|
@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,
|
|
}
|
|
|
|
logger.info(f"Send Email DATA ***** {pending_member}")
|
|
|
|
jwt_part = jwt.encode(
|
|
{"user": pending_member, 'exp': datetime.datetime.utcnow() + datetime.timedelta(minutes=3330)},
|
|
BaseService.JWT_SECRET_KEY, algorithm='HS256'
|
|
)
|
|
panel_url = BaseService.THIS_SITE_URL # "https://qa-panel.mermsemr.com"
|
|
link_url = str(panel_url) + '/accreset/' + jwt_part
|
|
|
|
# html_body = f"""\
|
|
# <html>
|
|
# <head></head>
|
|
# <body style="font-size:14px;line-height:1.5;">
|
|
# <table width="550px" border="0" cellpadding="3" cellspacing="3" background-color="#F0F8FF" style="font-size:16px">
|
|
# <tr>
|
|
# <td style="text-align:center">
|
|
# <img style="width:150px; height:auto;" src="mermsemr.com/images/logo-pink.png" />
|
|
# </td>
|
|
# </tr>
|
|
# <tr>
|
|
# <td>
|
|
# Hello <b>{firstname}!</b>
|
|
# </td>
|
|
# </tr>
|
|
# <tr>
|
|
# <td>
|
|
# You received this message for account reset password<br>
|
|
# </td>
|
|
# </tr>
|
|
# <tr>
|
|
# <td>
|
|
# Follow the link: <a href="{link_url}">link</a> to reset your password.<br>
|
|
# </td>
|
|
# </tr>
|
|
# <tr>
|
|
# <td>
|
|
# For any support<br>
|
|
# Reach Out<br>
|
|
# support@mermsemr.com<br>
|
|
# https://www.mermsemr.com/
|
|
# </td>
|
|
# </tr>
|
|
# </table>
|
|
# </body>
|
|
# </html>
|
|
# """
|
|
|
|
html_body = f"""\
|
|
<table width="550px" border="0" cellpadding="3" cellspacing="3" background-color="#F0F8FF" style="font-size:16px">
|
|
<tr>
|
|
<td>
|
|
Hello <b>{firstname}!</b>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td>
|
|
You received this message for account reset password<br>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td>
|
|
Follow the link: <a href="{link_url}">link</a> to reset your password.<br>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
"""
|
|
receiver_email = signup_email
|
|
subject = "Reset Password Email"
|
|
|
|
send_email_factory(
|
|
to_email=receiver_email,
|
|
subject=subject,
|
|
html_content=html_body
|
|
)
|
|
|
|
def send_verify_signup_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 = BaseService.THIS_SITE_URL # "https://qa-panel.mermsemr.com"
|
|
link_url = str(panel_url) + '/csignup/' + jwt_part
|
|
|
|
|
|
receiver_email = signup_email
|
|
subject = "Verify your MERMS(AI) Account Setup"
|
|
|
|
html_body = f"""\
|
|
<div style="font-weight: 700; font-size: 18px; margin-bottom: 16px;">
|
|
Hello <b>{firstname}!</b>,
|
|
</div>
|
|
|
|
<p style="margin: 0 0 16px 0;">
|
|
You have received this message for your account verification.
|
|
</p>
|
|
|
|
<p style="margin: 0 0 24px 0;">
|
|
Follow the link: <a href="{link_url}">link</a> to complete the verification process.
|
|
</p>
|
|
"""
|
|
|
|
send_email_factory(
|
|
to_email=receiver_email,
|
|
subject=subject,
|
|
html_content=html_body
|
|
)
|
|
|
|
|
|
@classmethod
|
|
def validate_data(cls, data, schema):
|
|
"""
|
|
Validate input data based on the provided schema.
|
|
"""
|
|
logger.info(f"Processing {cls.TRANSACTION_TYPE} request")
|
|
return schema.load(data)
|
|
|
|
|
|
@classmethod
|
|
def async_send_settings_refresh_to_kafka(cls, settings_data, subscription_uid, topic):
|
|
KafkaIntegration.send_setting_refresh_request(settings_data=settings_data, subscription_uid=subscription_uid,
|
|
topic=topic)
|
|
KafkaIntegration.flush()
|
|
|
|
@classmethod
|
|
def async_send_to_kafka(cls, loan_data, request_id, topic):
|
|
KafkaIntegration.send_loan_request(loan_data=loan_data, request_id=request_id, topic=topic)
|
|
KafkaIntegration.flush()
|
|
|
|
@staticmethod
|
|
def get_site_imges_data(provision_uid, primary_server, provision_port, selected_flavor):
|
|
destination_server = "http://" + str(primary_server) + ":" + str(provision_port)
|
|
api_url = destination_server + "/api/props"
|
|
try:
|
|
payload = {'provision_uid': provision_uid, 'flavor': selected_flavor}
|
|
logger.info(f"api_url: {str(api_url)}")
|
|
logger.info(f"selected_flavor: {str(selected_flavor)}")
|
|
|
|
# Make the GET request
|
|
res_data = []
|
|
response = requests.get(api_url, params=payload)
|
|
if response.status_code == 200:
|
|
logger.info(f"Response Site Images: {res_data}")
|
|
# Convert the JSON response to a Python dictionary
|
|
res_data = response.json()
|
|
logger.info(f"Response Site Images: {res_data}")
|
|
else:
|
|
res_data={}
|
|
|
|
response_data = {
|
|
"provision_uid": str(provision_uid),
|
|
"data": res_data,
|
|
# "product_id": product_id,
|
|
}
|
|
|
|
return response_data
|
|
|
|
except Exception as e:
|
|
logger.error(f"An error occurred while get_site_imges_data data: {str(e)}", exc_info=True)
|
|
return None
|
|
|
|
@staticmethod
|
|
def write_cache_data(cacheSection, cacheId, cacheData):
|
|
|
|
try:
|
|
cacheKey = "MERMS-" + cacheSection + '-' + cacheId # note theh use of -
|
|
logger.info(f"write_cache_data () key {cacheKey}", exc_info=True)
|
|
# Define connection parameters and connect
|
|
r = redis.Redis(host=BaseService.CACHE_SERVER, port=BaseService.CACHE_PORT,
|
|
password=BaseService.CACHE_PASSWORD,
|
|
decode_responses=True)
|
|
|
|
if r.exists(cacheKey):
|
|
r.unlink(cacheKey)
|
|
|
|
# Set a key 'foo' with value 'bar'
|
|
json_string = json.dumps(cacheData, indent=4)
|
|
r.set(cacheKey, json_string, ex=BaseService.CACHE_DEFAULT_EXPIRE)
|
|
|
|
# Verify by getting the value
|
|
value = r.get(cacheKey)
|
|
print(f"Value of {cacheKey}: {value}") # Output: Value of 'foo': bar
|
|
|
|
response_data = {
|
|
"session_details": value,
|
|
# "product_id": product_id,
|
|
}
|
|
|
|
return response_data
|
|
|
|
except Exception as e:
|
|
logger.error(f"An error occurred while write_cache_data data: {str(e)}", exc_info=True)
|
|
return None
|