Untitled
unknown
plain_text
5 months ago
4.2 kB
1
Indexable
from typing import List, Optional from sqlalchemy.orm import Session, joinedload from sqlalchemy import or_ from app.graphql.schema import Info from app.graphql.types.product_type import ProductType, ProductFieldType, StatusType, CountryType, TherapeuticType from app.models import Product, User async def product_data( self, info: Info, content_type: Optional[str] = None, user: str = None, page: int = 1, rows: int = 10 ) -> List[ProductType]: session: Session = info.context.session try: # Fetch user details user_obj = session.query(User).filter(User.login == user).first() if not user_obj: # Return an error message instead of raising an exception return [{"error": f"User '{user}' not found"}] # Get associated countries and therapeutic areas country_ids = [uc.id for uc in user_obj.countries] ta_ids = [ut.id for ut in user_obj.therapeutics] # Build the product query query = ( session.query(Product) .filter( Product.is_deleted != 2, # Check if product is not marked as deleted or_( Product.last_active > 0, # Last active condition Product.current_record == 1, # Current record condition Product.is_deleted == 1 # Allow deleted product if marked with 1 ), Product.country_id.in_(country_ids) if country_ids else True, Product.th_area.in_(ta_ids) if ta_ids else True ) .options( joinedload(Product.status), joinedload(Product.country), joinedload(Product.th_area), joinedload(Product.product_fields) # Load product_fields relationship ) ) # Filter by content type if provided if content_type: query = query.filter_by(type=content_type) # Execute the query products = query.all() # If no products are found, return a message if not products: return [{"message": "No products found matching the criteria"}] # Construct product response response = [] for product in products: product_fields = [ ProductFieldType( record_id=pf.record_id, # Use product_field_id or appropriate identifier field_id=pf.field_id, max_field_value=pf.field_value, # Set field_value as max_field_value field_detail=FieldDetailType( field_id=pf.field_detail.field_id, field_name=pf.field_detail.field_name ) ) for pf in product.product_fields ] response.append(ProductType( record_id=product.record_id, janssen_mstr_prdct_nm=product.janssen_mstr_prdct_nm, product_phase=product.product_phase, jnj_full_compound_id=product.jnj_full_compound_id, generic_name=product.generic_name, jnj_flag=product.jnj_flag, product_status=product.product_status, current_record=product.current_record, last_active=product.last_active, created_date=product.created_date, updated_date=product.updated_date, status=StatusType( status_id=product.status.id, status_desc=product.status.description ), country=CountryType( country_id=product.country.id, country_name=product.country.name ), therapeutic_area=TherapeuticAreaType( ta_id=product.therapeutic_area.id, ta_name=product.therapeutic_area.name ), product_fields=product_fields # Add product_fields to the response )) return response finally: session.close()
Editor is loading...
Leave a Comment