from flask import session, jsonify from app.utils.logger import logger from app.api.services.base_service import BaseService from marshmallow import ValidationError from app.extensions import db from app.models import MembersProducts, Products, Members, ProductsDetails, ProductsDetails, ProvisionActions from app.api.helpers.response_helper import ResponseHelper from app.api.schemas.myproduct import MyProductSchema from app.api.schemas.provision import ProvisionSchema import datetime import jwt import random from app.config import Config class MyProductsService(BaseService): @staticmethod def process_provision_actions(data): pass @staticmethod def process_provision(data): try: with db.session.begin(): logger.info(f"Incoming MyProduct data ==>>>> {data}") validated_data = MyProductsService.validate_data(data, ProvisionSchema()) token = validated_data.get('token') uid = validated_data.get('uid') member_data = Members.get_member_by_uid(uid) member_id = member_data.id product_id = validated_data.get('product_id') product_data = Products.get_product_by_product_id(product_id) if not product_data: return { "message": "Please provide product_id", "data": None, "error": "Bad request" }, 400 logger.info(f"GET HERE ******************************** : {data}", exc_info=True) subscription = MembersProducts.get_member_product_by_product_member_id(member_id, product_id) provision_activities = ProvisionActions.get_provision_action_by_product_uid(subscription.uid, 4) provision_activities_data =[] for t in provision_activities: provision_activities_data.append({ 'id': t.id, 'uid': t.uid, 'action': t.action, 'added': t.added.isoformat() if t.added else None, }) response_data = { "subscription_uid": subscription.uid, "subscription_id": subscription.id, "last_update": datetime.datetime.utcnow(), "percent_completed": random.randint(10, 99), "activities": provision_activities_data } 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_subscription(data): try: with db.session.begin(): logger.info(f"Incoming MyProduct data ==>>>> {data}") validated_data = MyProductsService.validate_data(data, MyProductSchema()) token = validated_data.get('token') uid = validated_data.get('uid') member_data = Members.get_member_by_uid(uid) member_id = member_data.id product_id = validated_data.get('product_id') product_data = Products.get_product_by_product_id(product_id) if not product_data: return { "message": "Please provide product_id", "data": None, "error": "Bad request" }, 400 logger.info(f"GET HERE ******************************** : {data}", exc_info=True) mumberSub = MembersProducts.get_member_product_by_product_member_id(member_id, product_id) if mumberSub: logger.error(f"Ready to add data to tables") response_data = { "subscription": mumberSub, "member_id": member_id, "error": "already_subscribed", "uid": uid } return ResponseHelper.success(data=response_data) internal_url = str(random.randint(100000, 999999)) + ".devprov.mermsemr.com" #INSERT_NEW_PRODUCT = "INSERT INTO members_products (member_id ,product_id,status,internal_url) VALUES (%s, %s, %s, %s)" # val_insert = (member_id, product_id,6,internal_url) status = 6 subscription = MembersProducts.create_subscription(member_id ,product_id,status,internal_url) response_data = { "subscription_uid": subscription.uid, "subscription_id": subscription.id, "internal_url": subscription.internal_url, "member_id": member_id, "uid": uid } 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): try: with db.session.begin(): logger.info(f"Incoming MyProduct data ==>>>> {data}") validated_data = MyProductsService.validate_data(data, MyProductSchema()) token = validated_data.get('token') uid = validated_data.get('uid') member_data = Members.get_member_by_uid(uid) member_id = member_data.id product_id = validated_data.get('product_id') ''' Table "public.products" Column | Type | Collation | Nullable | Default -------------+-----------------------------+-----------+----------+-------------------------------------- id | integer | | not null | nextval('products_id_seq'::regclass) uid | uuid | | | uuid_generate_v4() product_id | character varying(25) | | not null | name | character varying(100) | | not null | description | character varying(250) | | not null | status | integer | | | 0 added | timestamp without time zone | | | now() updated | timestamp without time zone | | | now() banner | character varying(100) | | | Indexes: "products_id_key" UNIQUE CONSTRAINT, btree (id) "products_name_key" UNIQUE CONSTRAINT, btree (name) "products_product_id_key" UNIQUE CONSTRAINT, btree (product_id) Referenced by: TABLE "members_products" CONSTRAINT "members_products_product_id_fkey" FOREIGN KEY (product_id) REFERENCES products(product_id) merms_panel=# "Product Description - Commitment is something that comes from understanding that everything has its price and then having the willingness to pay that price. This is important because nobody wants to put significant effort into something, only to find out after the fact that the price was too high.The price is something not necessarily defined as financial. It could be time, effort, sacrifice, money or perhaps, something else.", 'internal_url': self.internal_url, 'external_url': self.external_url, ''' product_subscription_uid='' product_subscription_external_url = '' product_subscription_internal_url = '' product_data = Products.get_product_by_product_id(product_id) product_description = ProductsDetails.get_product_details_with_product_id('A000002') productDataStatus = product_data.status memberSubscription = MembersProducts.get_member_product_by_product_member_id(member_id, product_id) if memberSubscription is not None: logger.info(f"Incoming MyProduct data ==>>>> {memberSubscription}") productDataStatus = memberSubscription.status product_subscription_uid = memberSubscription.uid product_subscription_external_url = memberSubscription.external_url product_subscription_internal_url = memberSubscription.internal_url # "banner": "banner.jpg", myproduct_data = { "myproudct": { "banner": product_data.banner, "description": product_description.details, "sale_text" : product_description.sale_text, "internal_url": product_subscription_internal_url, "external_url": product_subscription_external_url, "price_text": "90 days free and 3.95/Month", "product_id": product_data.product_id, "product_name": product_data.name, "product_subscription_uid": product_subscription_uid, "product_uid": product_data.uid, "promotion_text": "Start Free Today !", "subscription_uid": product_subscription_uid, "status": productDataStatus, "subscription_text": "Start with your goals in mind and then work possible.ith yand Goals. If the plan doesn\u2019t support the vision then change it!", "title": "Your personal professional web presence" } } # Simulate processing response_data = { "myproduct": myproduct_data, "member_id": member_id, "uid": uid, } 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()